1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #if defined(__linux__) || defined(__GLIBC__)
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(linux_HOST_OS)
28 #include <sys/types.h>
29 #include <sys/syscall.h>
32 #if defined(THREADED_RTS)
40 #if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
41 #include <sys/types.h>
42 #include <sys/sysctl.h>
45 #if !defined(HAVE_PTHREAD_H)
46 #error pthreads.h is required for the threaded RTS on Posix platforms
49 #if defined(HAVE_SCHED_H)
53 #if defined(HAVE_SYS_CPUSET_H)
54 #include <sys/param.h>
55 #include <sys/cpuset.h>
62 #if defined(darwin_HOST_OS)
63 #include <mach/mach.h>
71 * This (allegedly) OS threads independent layer was initially
72 * abstracted away from code that used Pthreads, so the functions
73 * provided here are mostly just wrappers to the Pthreads API.
78 initCondition( Condition
* pCond
)
80 pthread_cond_init(pCond
, NULL
);
85 closeCondition( Condition
* pCond
)
87 pthread_cond_destroy(pCond
);
92 broadcastCondition ( Condition
* pCond
)
94 return (pthread_cond_broadcast(pCond
) == 0);
98 signalCondition ( Condition
* pCond
)
100 return (pthread_cond_signal(pCond
) == 0);
104 waitCondition ( Condition
* pCond
, Mutex
* pMut
)
106 return (pthread_cond_wait(pCond
,pMut
) == 0);
123 createOSThread (OSThreadId
* pId
, OSThreadProc
*startProc
, void *param
)
125 int result
= pthread_create(pId
, NULL
, (void *(*)(void *))startProc
, param
);
127 pthread_detach(*pId
);
134 return pthread_self();
138 osThreadIsAlive(OSThreadId id STG_UNUSED
)
140 // no good way to implement this on POSIX, AFAICT. Returning true
146 initMutex(Mutex
* pMut
)
149 pthread_mutexattr_t attr
;
150 pthread_mutexattr_init(&attr
);
151 pthread_mutexattr_settype(&attr
,PTHREAD_MUTEX_ERRORCHECK
);
152 pthread_mutex_init(pMut
,&attr
);
154 pthread_mutex_init(pMut
,NULL
);
159 closeMutex(Mutex
* pMut
)
161 pthread_mutex_destroy(pMut
);
165 newThreadLocalKey (ThreadLocalKey
*key
)
168 if ((r
= pthread_key_create(key
, NULL
)) != 0) {
169 barf("newThreadLocalKey: %s", strerror(r
));
174 getThreadLocalVar (ThreadLocalKey
*key
)
176 return pthread_getspecific(*key
);
177 // Note: a return value of NULL can indicate that either the key
178 // is not valid, or the key is valid and the data value has not
179 // yet been set. We need to use the latter case, so we cannot
180 // detect errors here.
184 setThreadLocalVar (ThreadLocalKey
*key
, void *value
)
187 if ((r
= pthread_setspecific(*key
,value
)) != 0) {
188 barf("setThreadLocalVar: %s", strerror(r
));
193 freeThreadLocalKey (ThreadLocalKey
*key
)
196 if ((r
= pthread_key_delete(*key
)) != 0) {
197 barf("freeThreadLocalKey: %s", strerror(r
));
202 forkOS_createThreadWrapper ( void * entry
)
206 rts_evalStableIO(&cap
, (HsStablePtr
) entry
, NULL
);
212 forkOS_createThread ( HsStablePtr entry
)
215 int result
= pthread_create(&tid
, NULL
,
216 forkOS_createThreadWrapper
, (void*)entry
);
223 getNumberOfProcessors (void)
225 static nat nproc
= 0;
228 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
229 nproc
= sysconf(_SC_NPROCESSORS_ONLN
);
230 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
231 nproc
= sysconf(_SC_NPROCESSORS_CONF
);
232 #elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
233 size_t size
= sizeof(nat
);
234 if(0 != sysctlbyname("hw.ncpu",&nproc
,&size
,NULL
,0))
244 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
245 // Schedules the thread to run on CPU n of m. m may be less than the
246 // number of physical CPUs, in which case, the thread will be allowed
247 // to run on CPU n, n+m, n+2m etc.
249 setThreadAffinity (nat n
, nat m
)
255 nproc
= getNumberOfProcessors();
257 for (i
= n
; i
< nproc
; i
+=m
) {
260 sched_setaffinity(0, sizeof(cpu_set_t
), &cs
);
263 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
264 // Schedules the current thread in the affinity set identified by tag n.
266 setThreadAffinity (nat n
, nat m
GNUC3_ATTRIBUTE(__unused__
))
268 thread_affinity_policy_data_t policy
;
270 policy
.affinity_tag
= n
;
271 thread_policy_set(mach_thread_self(),
272 THREAD_AFFINITY_POLICY
,
273 (thread_policy_t
) &policy
,
274 THREAD_AFFINITY_POLICY_COUNT
);
277 #elif defined(HAVE_SYS_CPUSET_H) /* FreeBSD 7.1+ */
279 setThreadAffinity(nat n
, nat m
)
285 nproc
= getNumberOfProcessors();
288 for (i
= n
; i
< nproc
; i
+= m
)
291 cpuset_setaffinity(CPU_LEVEL_WHICH
, CPU_WHICH_TID
, -1, sizeof(cpuset_t
), &cs
);
296 setThreadAffinity (nat n
GNUC3_ATTRIBUTE(__unused__
),
297 nat m
GNUC3_ATTRIBUTE(__unused__
))
303 interruptOSThread (OSThreadId id
)
305 pthread_kill(id
, SIGPIPE
);
308 #else /* !defined(THREADED_RTS) */
311 forkOS_createThread ( HsStablePtr entry STG_UNUSED
)
316 nat
getNumberOfProcessors (void)
321 #endif /* defined(THREADED_RTS) */
323 KernelThreadId
kernelThreadId (void)
325 #if defined(linux_HOST_OS)
326 pid_t tid
= syscall(SYS_gettid
); // no really, see man gettid
329 #elif defined(freebsd_HOST_OS)
330 return pthread_getthreadid_np();
332 #elif defined(darwin_HOST_OS)
334 pthread_threadid_np(NULL
, &ktid
);