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 */
16 #include "PosixSource.h"
19 #if defined(THREADED_RTS)
27 #if defined(darwin_HOST_OS)
28 #include <sys/types.h>
29 #include <sys/sysctl.h>
32 #if !defined(HAVE_PTHREAD_H)
33 #error pthreads.h is required for the threaded RTS on Posix platforms
36 #if defined(HAVE_SCHED_H)
44 #if defined(darwin_HOST_OS)
45 #include <mach/mach.h>
49 * This (allegedly) OS threads independent layer was initially
50 * abstracted away from code that used Pthreads, so the functions
51 * provided here are mostly just wrappers to the Pthreads API.
56 initCondition( Condition* pCond )
58 pthread_cond_init(pCond, NULL);
63 closeCondition( Condition* pCond )
65 pthread_cond_destroy(pCond);
70 broadcastCondition ( Condition* pCond )
72 return (pthread_cond_broadcast(pCond) == 0);
76 signalCondition ( Condition* pCond )
78 return (pthread_cond_signal(pCond) == 0);
82 waitCondition ( Condition* pCond, Mutex* pMut )
84 return (pthread_cond_wait(pCond,pMut) == 0);
101 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
103 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
105 pthread_detach(*pId);
112 return pthread_self();
116 osThreadIsAlive(OSThreadId id STG_UNUSED)
118 // no good way to implement this on POSIX, AFAICT. Returning true
124 initMutex(Mutex* pMut)
127 pthread_mutexattr_t attr;
128 pthread_mutexattr_init(&attr);
129 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
130 pthread_mutex_init(pMut,&attr);
132 pthread_mutex_init(pMut,NULL);
137 closeMutex(Mutex* pMut)
139 pthread_mutex_destroy(pMut);
143 newThreadLocalKey (ThreadLocalKey *key)
146 if ((r = pthread_key_create(key, NULL)) != 0) {
147 barf("newThreadLocalKey: %s", strerror(r));
152 getThreadLocalVar (ThreadLocalKey *key)
154 return pthread_getspecific(*key);
155 // Note: a return value of NULL can indicate that either the key
156 // is not valid, or the key is valid and the data value has not
157 // yet been set. We need to use the latter case, so we cannot
158 // detect errors here.
162 setThreadLocalVar (ThreadLocalKey *key, void *value)
165 if ((r = pthread_setspecific(*key,value)) != 0) {
166 barf("setThreadLocalVar: %s", strerror(r));
171 freeThreadLocalKey (ThreadLocalKey *key)
174 if ((r = pthread_key_delete(*key)) != 0) {
175 barf("freeThreadLocalKey: %s", strerror(r));
180 forkOS_createThreadWrapper ( void * entry )
184 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
185 taskTimeStamp(myTask());
191 forkOS_createThread ( HsStablePtr entry )
194 int result = pthread_create(&tid, NULL,
195 forkOS_createThreadWrapper, (void*)entry);
202 getNumberOfProcessors (void)
204 static nat nproc = 0;
207 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
208 nproc = sysconf(_SC_NPROCESSORS_ONLN);
209 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
210 nproc = sysconf(_SC_NPROCESSORS_CONF);
211 #elif defined(darwin_HOST_OS)
212 size_t size = sizeof(nat);
213 if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0))
223 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
224 // Schedules the thread to run on CPU n of m. m may be less than the
225 // number of physical CPUs, in which case, the thread will be allowed
226 // to run on CPU n, n+m, n+2m etc.
228 setThreadAffinity (nat n, nat m)
234 nproc = getNumberOfProcessors();
236 for (i = n; i < nproc; i+=m) {
239 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
242 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
243 // Schedules the current thread in the affinity set identified by tag n.
245 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
247 thread_affinity_policy_data_t policy;
249 policy.affinity_tag = n;
250 thread_policy_set(mach_thread_self(),
251 THREAD_AFFINITY_POLICY,
252 (thread_policy_t) &policy,
253 THREAD_AFFINITY_POLICY_COUNT);
258 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
259 nat m GNUC3_ATTRIBUTE(__unused__))
264 #else /* !defined(THREADED_RTS) */
267 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
272 #endif /* !defined(THREADED_RTS) */