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"
18 #if defined(freebsd_HOST_OS)
19 /* Inclusion of system headers usually requires __BSD_VISIBLE on FreeBSD,
20 * because of some specific types, like u_char, u_int, etc. */
21 #define __BSD_VISIBLE 1
26 #if defined(THREADED_RTS)
34 #if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
39 #if !defined(HAVE_PTHREAD_H)
40 #error pthreads.h is required for the threaded RTS on Posix platforms
43 #if defined(HAVE_SCHED_H)
47 #if defined(HAVE_SYS_CPUSET_H)
48 #include <sys/param.h>
49 #include <sys/cpuset.h>
56 #if defined(darwin_HOST_OS)
57 #include <mach/mach.h>
65 * This (allegedly) OS threads independent layer was initially
66 * abstracted away from code that used Pthreads, so the functions
67 * provided here are mostly just wrappers to the Pthreads API.
72 initCondition( Condition* pCond )
74 pthread_cond_init(pCond, NULL);
79 closeCondition( Condition* pCond )
81 pthread_cond_destroy(pCond);
86 broadcastCondition ( Condition* pCond )
88 return (pthread_cond_broadcast(pCond) == 0);
92 signalCondition ( Condition* pCond )
94 return (pthread_cond_signal(pCond) == 0);
98 waitCondition ( Condition* pCond, Mutex* pMut )
100 return (pthread_cond_wait(pCond,pMut) == 0);
117 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
119 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
121 pthread_detach(*pId);
128 return pthread_self();
132 osThreadIsAlive(OSThreadId id STG_UNUSED)
134 // no good way to implement this on POSIX, AFAICT. Returning true
140 initMutex(Mutex* pMut)
143 pthread_mutexattr_t attr;
144 pthread_mutexattr_init(&attr);
145 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
146 pthread_mutex_init(pMut,&attr);
148 pthread_mutex_init(pMut,NULL);
153 closeMutex(Mutex* pMut)
155 pthread_mutex_destroy(pMut);
159 newThreadLocalKey (ThreadLocalKey *key)
162 if ((r = pthread_key_create(key, NULL)) != 0) {
163 barf("newThreadLocalKey: %s", strerror(r));
168 getThreadLocalVar (ThreadLocalKey *key)
170 return pthread_getspecific(*key);
171 // Note: a return value of NULL can indicate that either the key
172 // is not valid, or the key is valid and the data value has not
173 // yet been set. We need to use the latter case, so we cannot
174 // detect errors here.
178 setThreadLocalVar (ThreadLocalKey *key, void *value)
181 if ((r = pthread_setspecific(*key,value)) != 0) {
182 barf("setThreadLocalVar: %s", strerror(r));
187 freeThreadLocalKey (ThreadLocalKey *key)
190 if ((r = pthread_key_delete(*key)) != 0) {
191 barf("freeThreadLocalKey: %s", strerror(r));
196 forkOS_createThreadWrapper ( void * entry )
200 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
201 taskTimeStamp(myTask());
207 forkOS_createThread ( HsStablePtr entry )
210 int result = pthread_create(&tid, NULL,
211 forkOS_createThreadWrapper, (void*)entry);
218 getNumberOfProcessors (void)
220 static nat nproc = 0;
223 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
224 nproc = sysconf(_SC_NPROCESSORS_ONLN);
225 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
226 nproc = sysconf(_SC_NPROCESSORS_CONF);
227 #elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
228 size_t size = sizeof(nat);
229 if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0))
239 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
240 // Schedules the thread to run on CPU n of m. m may be less than the
241 // number of physical CPUs, in which case, the thread will be allowed
242 // to run on CPU n, n+m, n+2m etc.
244 setThreadAffinity (nat n, nat m)
250 nproc = getNumberOfProcessors();
252 for (i = n; i < nproc; i+=m) {
255 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
258 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
259 // Schedules the current thread in the affinity set identified by tag n.
261 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
263 thread_affinity_policy_data_t policy;
265 policy.affinity_tag = n;
266 thread_policy_set(mach_thread_self(),
267 THREAD_AFFINITY_POLICY,
268 (thread_policy_t) &policy,
269 THREAD_AFFINITY_POLICY_COUNT);
272 #elif defined(HAVE_SYS_CPUSET_H) /* FreeBSD 7.1+ */
274 setThreadAffinity(nat n, nat m)
280 nproc = getNumberOfProcessors();
283 for (i = n; i < nproc; i += m)
286 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &cs);
291 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
292 nat m GNUC3_ATTRIBUTE(__unused__))
298 interruptOSThread (OSThreadId id)
300 pthread_kill(id, SIGPIPE);
303 #else /* !defined(THREADED_RTS) */
306 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
311 #endif /* !defined(THREADED_RTS) */