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"
25 #if !defined(HAVE_PTHREAD_H)
26 #error pthreads.h is required for the threaded RTS on Posix platforms
30 * This (allegedly) OS threads independent layer was initially
31 * abstracted away from code that used Pthreads, so the functions
32 * provided here are mostly just wrappers to the Pthreads API.
37 initCondition( Condition* pCond )
39 pthread_cond_init(pCond, NULL);
44 closeCondition( Condition* pCond )
46 pthread_cond_destroy(pCond);
51 broadcastCondition ( Condition* pCond )
53 return (pthread_cond_broadcast(pCond) == 0);
57 signalCondition ( Condition* pCond )
59 return (pthread_cond_signal(pCond) == 0);
63 waitCondition ( Condition* pCond, Mutex* pMut )
65 return (pthread_cond_wait(pCond,pMut) == 0);
82 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
84 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
93 return pthread_self();
97 initMutex(Mutex* pMut)
99 #if defined(DEBUG) && defined(linux_HOST_OS)
100 pthread_mutexattr_t attr;
101 pthread_mutexattr_init(&attr);
102 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK_NP);
103 pthread_mutex_init(pMut,&attr);
105 pthread_mutex_init(pMut,NULL);
111 newThreadLocalKey (ThreadLocalKey *key)
114 if ((r = pthread_key_create(key, NULL)) != 0) {
115 barf("newThreadLocalKey: %s", strerror(r));
120 getThreadLocalVar (ThreadLocalKey *key)
122 return pthread_getspecific(*key);
123 // Note: a return value of NULL can indicate that either the key
124 // is not valid, or the key is valid and the data value has not
125 // yet been set. We need to use the latter case, so we cannot
126 // detect errors here.
130 setThreadLocalVar (ThreadLocalKey *key, void *value)
133 if ((r = pthread_setspecific(*key,value)) != 0) {
134 barf("setThreadLocalVar: %s", strerror(r));
139 forkOS_createThreadWrapper ( void * entry )
143 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
144 taskTimeStamp(myTask());
150 forkOS_createThread ( HsStablePtr entry )
153 int result = pthread_create(&tid, NULL,
154 forkOS_createThreadWrapper, (void*)entry);
160 #else /* !defined(THREADED_RTS) */
163 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
168 #endif /* !defined(THREADED_RTS) */