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)
38 #if defined(darwin_HOST_OS)
39 #include <mach/mach.h>
43 * This (allegedly) OS threads independent layer was initially
44 * abstracted away from code that used Pthreads, so the functions
45 * provided here are mostly just wrappers to the Pthreads API.
50 initCondition( Condition* pCond )
52 pthread_cond_init(pCond, NULL);
57 closeCondition( Condition* pCond )
59 pthread_cond_destroy(pCond);
64 broadcastCondition ( Condition* pCond )
66 return (pthread_cond_broadcast(pCond) == 0);
70 signalCondition ( Condition* pCond )
72 return (pthread_cond_signal(pCond) == 0);
76 waitCondition ( Condition* pCond, Mutex* pMut )
78 return (pthread_cond_wait(pCond,pMut) == 0);
95 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
97 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
106 return pthread_self();
110 osThreadIsAlive(OSThreadId id STG_UNUSED)
112 // no good way to implement this on POSIX, AFAICT. Returning true
118 initMutex(Mutex* pMut)
121 pthread_mutexattr_t attr;
122 pthread_mutexattr_init(&attr);
123 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
124 pthread_mutex_init(pMut,&attr);
126 pthread_mutex_init(pMut,NULL);
131 closeMutex(Mutex* pMut)
133 pthread_mutex_destroy(pMut);
137 newThreadLocalKey (ThreadLocalKey *key)
140 if ((r = pthread_key_create(key, NULL)) != 0) {
141 barf("newThreadLocalKey: %s", strerror(r));
146 getThreadLocalVar (ThreadLocalKey *key)
148 return pthread_getspecific(*key);
149 // Note: a return value of NULL can indicate that either the key
150 // is not valid, or the key is valid and the data value has not
151 // yet been set. We need to use the latter case, so we cannot
152 // detect errors here.
156 setThreadLocalVar (ThreadLocalKey *key, void *value)
159 if ((r = pthread_setspecific(*key,value)) != 0) {
160 barf("setThreadLocalVar: %s", strerror(r));
165 freeThreadLocalKey (ThreadLocalKey *key)
168 if ((r = pthread_key_delete(*key)) != 0) {
169 barf("freeThreadLocalKey: %s", strerror(r));
174 forkOS_createThreadWrapper ( void * entry )
178 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
179 taskTimeStamp(myTask());
185 forkOS_createThread ( HsStablePtr entry )
188 int result = pthread_create(&tid, NULL,
189 forkOS_createThreadWrapper, (void*)entry);
196 getNumberOfProcessors (void)
198 static nat nproc = 0;
201 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
202 nproc = sysconf(_SC_NPROCESSORS_ONLN);
203 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
204 nproc = sysconf(_SC_NPROCESSORS_CONF);
213 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
214 // Schedules the thread to run on CPU n of m. m may be less than the
215 // number of physical CPUs, in which case, the thread will be allowed
216 // to run on CPU n, n+m, n+2m etc.
218 setThreadAffinity (nat n, nat m)
224 nproc = getNumberOfProcessors();
226 for (i = n; i < nproc; i+=m) {
229 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
232 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
233 // Schedules the current thread in the affinity set identified by tag n.
235 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
237 thread_affinity_policy_data_t policy;
239 policy.affinity_tag = n;
240 thread_policy_set(mach_thread_self(),
241 THREAD_AFFINITY_POLICY,
242 (thread_policy_t) &policy,
243 THREAD_AFFINITY_POLICY_COUNT);
248 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
249 nat m GNUC3_ATTRIBUTE(__unused__))
254 #else /* !defined(THREADED_RTS) */
257 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
262 #endif /* !defined(THREADED_RTS) */