1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #if defined(DEBUG) && defined(__linux__)
11 /* We want GNU extensions in DEBUG mode for mutex error checking */
16 #if defined(THREADED_RTS)
17 #include "OSThreads.h"
24 #if !defined(HAVE_PTHREAD_H)
25 #error pthreads.h is required for the threaded RTS on Posix platforms
29 * This (allegedly) OS threads independent layer was initially
30 * abstracted away from code that used Pthreads, so the functions
31 * provided here are mostly just wrappers to the Pthreads API.
36 initCondition( Condition* pCond )
38 pthread_cond_init(pCond, NULL);
43 closeCondition( Condition* pCond )
45 pthread_cond_destroy(pCond);
50 broadcastCondition ( Condition* pCond )
52 return (pthread_cond_broadcast(pCond) == 0);
56 signalCondition ( Condition* pCond )
58 return (pthread_cond_signal(pCond) == 0);
62 waitCondition ( Condition* pCond, Mutex* pMut )
64 return (pthread_cond_wait(pCond,pMut) == 0);
81 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
83 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
92 return pthread_self();
96 initMutex(Mutex* pMut)
98 #if defined(DEBUG) && defined(linux_HOST_OS)
99 pthread_mutexattr_t attr;
100 pthread_mutexattr_init(&attr);
101 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
102 pthread_mutex_init(pMut,&attr);
104 pthread_mutex_init(pMut,NULL);
110 newThreadLocalKey (ThreadLocalKey *key)
113 if ((r = pthread_key_create(key, NULL)) != 0) {
114 barf("newThreadLocalKey: %s", strerror(r));
119 getThreadLocalVar (ThreadLocalKey *key)
121 return pthread_getspecific(*key);
122 // Note: a return value of NULL can indicate that either the key
123 // is not valid, or the key is valid and the data value has not
124 // yet been set. We need to use the latter case, so we cannot
125 // detect errors here.
129 setThreadLocalVar (ThreadLocalKey *key, void *value)
132 if ((r = pthread_setspecific(*key,value)) != 0) {
133 barf("setThreadLocalVar: %s", strerror(r));
138 forkOS_createThreadWrapper ( void * entry )
142 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
148 forkOS_createThread ( HsStablePtr entry )
151 int result = pthread_create(&tid, NULL,
152 forkOS_createThreadWrapper, (void*)entry);
158 #else /* !defined(THREADED_RTS) */
161 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
166 #endif /* !defined(THREADED_RTS) */