1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #if defined(__linux__)
11 /* We want GNU extensions in DEBUG mode for mutex error checking */
12 /* We also want the affinity API, which requires _GNU_SOURCE */
17 #if defined(THREADED_RTS)
18 #include "OSThreads.h"
26 #if !defined(HAVE_PTHREAD_H)
27 #error pthreads.h is required for the threaded RTS on Posix platforms
30 #if defined(HAVE_SCHED_H)
35 * This (allegedly) OS threads independent layer was initially
36 * abstracted away from code that used Pthreads, so the functions
37 * provided here are mostly just wrappers to the Pthreads API.
42 initCondition( Condition* pCond )
44 pthread_cond_init(pCond, NULL);
49 closeCondition( Condition* pCond )
51 pthread_cond_destroy(pCond);
56 broadcastCondition ( Condition* pCond )
58 return (pthread_cond_broadcast(pCond) == 0);
62 signalCondition ( Condition* pCond )
64 return (pthread_cond_signal(pCond) == 0);
68 waitCondition ( Condition* pCond, Mutex* pMut )
70 return (pthread_cond_wait(pCond,pMut) == 0);
87 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
89 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
98 return pthread_self();
102 osThreadIsAlive(OSThreadId id STG_UNUSED)
104 // no good way to implement this on POSIX, AFAICT. Returning true
110 initMutex(Mutex* pMut)
113 pthread_mutexattr_t attr;
114 pthread_mutexattr_init(&attr);
115 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
116 pthread_mutex_init(pMut,&attr);
118 pthread_mutex_init(pMut,NULL);
123 closeMutex(Mutex* pMut)
125 pthread_mutex_destroy(pMut);
129 newThreadLocalKey (ThreadLocalKey *key)
132 if ((r = pthread_key_create(key, NULL)) != 0) {
133 barf("newThreadLocalKey: %s", strerror(r));
138 getThreadLocalVar (ThreadLocalKey *key)
140 return pthread_getspecific(*key);
141 // Note: a return value of NULL can indicate that either the key
142 // is not valid, or the key is valid and the data value has not
143 // yet been set. We need to use the latter case, so we cannot
144 // detect errors here.
148 setThreadLocalVar (ThreadLocalKey *key, void *value)
151 if ((r = pthread_setspecific(*key,value)) != 0) {
152 barf("setThreadLocalVar: %s", strerror(r));
157 freeThreadLocalKey (ThreadLocalKey *key)
160 if ((r = pthread_key_delete(*key)) != 0) {
161 barf("freeThreadLocalKey: %s", strerror(r));
166 forkOS_createThreadWrapper ( void * entry )
170 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
171 taskTimeStamp(myTask());
177 forkOS_createThread ( HsStablePtr entry )
180 int result = pthread_create(&tid, NULL,
181 forkOS_createThreadWrapper, (void*)entry);
188 getNumberOfProcessors (void)
190 static nat nproc = 0;
193 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
194 nproc = sysconf(_SC_NPROCESSORS_ONLN);
195 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
196 nproc = sysconf(_SC_NPROCESSORS_CONF);
205 // Schedules the thread to run on CPU n of m. m may be less than the
206 // number of physical CPUs, in which case, the thread will be allowed
207 // to run on CPU n, n+m, n+2m etc.
209 setThreadAffinity (nat n, nat m)
211 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
216 nproc = getNumberOfProcessors();
218 for (i = n; i < nproc; i+=m) {
221 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
225 #else /* !defined(THREADED_RTS) */
228 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
233 #endif /* !defined(THREADED_RTS) */