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)
39 * This (allegedly) OS threads independent layer was initially
40 * abstracted away from code that used Pthreads, so the functions
41 * provided here are mostly just wrappers to the Pthreads API.
46 initCondition( Condition* pCond )
48 pthread_cond_init(pCond, NULL);
53 closeCondition( Condition* pCond )
55 pthread_cond_destroy(pCond);
60 broadcastCondition ( Condition* pCond )
62 return (pthread_cond_broadcast(pCond) == 0);
66 signalCondition ( Condition* pCond )
68 return (pthread_cond_signal(pCond) == 0);
72 waitCondition ( Condition* pCond, Mutex* pMut )
74 return (pthread_cond_wait(pCond,pMut) == 0);
91 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
93 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
102 return pthread_self();
106 osThreadIsAlive(OSThreadId id STG_UNUSED)
108 // no good way to implement this on POSIX, AFAICT. Returning true
114 initMutex(Mutex* pMut)
117 pthread_mutexattr_t attr;
118 pthread_mutexattr_init(&attr);
119 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
120 pthread_mutex_init(pMut,&attr);
122 pthread_mutex_init(pMut,NULL);
127 closeMutex(Mutex* pMut)
129 pthread_mutex_destroy(pMut);
133 newThreadLocalKey (ThreadLocalKey *key)
136 if ((r = pthread_key_create(key, NULL)) != 0) {
137 barf("newThreadLocalKey: %s", strerror(r));
142 getThreadLocalVar (ThreadLocalKey *key)
144 return pthread_getspecific(*key);
145 // Note: a return value of NULL can indicate that either the key
146 // is not valid, or the key is valid and the data value has not
147 // yet been set. We need to use the latter case, so we cannot
148 // detect errors here.
152 setThreadLocalVar (ThreadLocalKey *key, void *value)
155 if ((r = pthread_setspecific(*key,value)) != 0) {
156 barf("setThreadLocalVar: %s", strerror(r));
161 freeThreadLocalKey (ThreadLocalKey *key)
164 if ((r = pthread_key_delete(*key)) != 0) {
165 barf("freeThreadLocalKey: %s", strerror(r));
170 forkOS_createThreadWrapper ( void * entry )
174 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
175 taskTimeStamp(myTask());
181 forkOS_createThread ( HsStablePtr entry )
184 int result = pthread_create(&tid, NULL,
185 forkOS_createThreadWrapper, (void*)entry);
192 getNumberOfProcessors (void)
194 static nat nproc = 0;
197 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
198 nproc = sysconf(_SC_NPROCESSORS_ONLN);
199 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
200 nproc = sysconf(_SC_NPROCESSORS_CONF);
209 // Schedules the thread to run on CPU n of m. m may be less than the
210 // number of physical CPUs, in which case, the thread will be allowed
211 // to run on CPU n, n+m, n+2m etc.
213 setThreadAffinity (nat n, nat m)
215 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
220 nproc = getNumberOfProcessors();
222 for (i = n; i < nproc; i+=m) {
225 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
229 #else /* !defined(THREADED_RTS) */
232 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
237 #endif /* !defined(THREADED_RTS) */