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(darwin_HOST_OS)
27 #include <sys/types.h>
28 #include <sys/sysctl.h>
31 #if !defined(HAVE_PTHREAD_H)
32 #error pthreads.h is required for the threaded RTS on Posix platforms
35 #if defined(HAVE_SCHED_H)
43 #if defined(darwin_HOST_OS)
44 #include <mach/mach.h>
48 * This (allegedly) OS threads independent layer was initially
49 * abstracted away from code that used Pthreads, so the functions
50 * provided here are mostly just wrappers to the Pthreads API.
55 initCondition( Condition* pCond )
57 pthread_cond_init(pCond, NULL);
62 closeCondition( Condition* pCond )
64 pthread_cond_destroy(pCond);
69 broadcastCondition ( Condition* pCond )
71 return (pthread_cond_broadcast(pCond) == 0);
75 signalCondition ( Condition* pCond )
77 return (pthread_cond_signal(pCond) == 0);
81 waitCondition ( Condition* pCond, Mutex* pMut )
83 return (pthread_cond_wait(pCond,pMut) == 0);
100 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
102 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
104 pthread_detach(*pId);
111 return pthread_self();
115 osThreadIsAlive(OSThreadId id STG_UNUSED)
117 // no good way to implement this on POSIX, AFAICT. Returning true
123 initMutex(Mutex* pMut)
126 pthread_mutexattr_t attr;
127 pthread_mutexattr_init(&attr);
128 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
129 pthread_mutex_init(pMut,&attr);
131 pthread_mutex_init(pMut,NULL);
136 closeMutex(Mutex* pMut)
138 pthread_mutex_destroy(pMut);
142 newThreadLocalKey (ThreadLocalKey *key)
145 if ((r = pthread_key_create(key, NULL)) != 0) {
146 barf("newThreadLocalKey: %s", strerror(r));
151 getThreadLocalVar (ThreadLocalKey *key)
153 return pthread_getspecific(*key);
154 // Note: a return value of NULL can indicate that either the key
155 // is not valid, or the key is valid and the data value has not
156 // yet been set. We need to use the latter case, so we cannot
157 // detect errors here.
161 setThreadLocalVar (ThreadLocalKey *key, void *value)
164 if ((r = pthread_setspecific(*key,value)) != 0) {
165 barf("setThreadLocalVar: %s", strerror(r));
170 freeThreadLocalKey (ThreadLocalKey *key)
173 if ((r = pthread_key_delete(*key)) != 0) {
174 barf("freeThreadLocalKey: %s", strerror(r));
179 forkOS_createThreadWrapper ( void * entry )
183 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
184 taskTimeStamp(myTask());
190 forkOS_createThread ( HsStablePtr entry )
193 int result = pthread_create(&tid, NULL,
194 forkOS_createThreadWrapper, (void*)entry);
201 getNumberOfProcessors (void)
203 static nat nproc = 0;
206 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
207 nproc = sysconf(_SC_NPROCESSORS_ONLN);
208 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
209 nproc = sysconf(_SC_NPROCESSORS_CONF);
210 #elif defined(darwin_HOST_OS)
211 size_t size = sizeof(nat);
212 if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0))
222 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
223 // Schedules the thread to run on CPU n of m. m may be less than the
224 // number of physical CPUs, in which case, the thread will be allowed
225 // to run on CPU n, n+m, n+2m etc.
227 setThreadAffinity (nat n, nat m)
233 nproc = getNumberOfProcessors();
235 for (i = n; i < nproc; i+=m) {
238 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
241 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
242 // Schedules the current thread in the affinity set identified by tag n.
244 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
246 thread_affinity_policy_data_t policy;
248 policy.affinity_tag = n;
249 thread_policy_set(mach_thread_self(),
250 THREAD_AFFINITY_POLICY,
251 (thread_policy_t) &policy,
252 THREAD_AFFINITY_POLICY_COUNT);
257 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
258 nat m GNUC3_ATTRIBUTE(__unused__))
263 #else /* !defined(THREADED_RTS) */
266 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
271 #endif /* !defined(THREADED_RTS) */