Merge branch 'ticket-7442' of git://github.com/phonohawk/ghc
[ghc.git] / rts / posix / OSThreads.c
1 /* ---------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2001-2005
4 *
5 * Accessing OS threads functionality in a (mostly) OS-independent
6 * manner.
7 *
8 * --------------------------------------------------------------------------*/
9
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 */
13 #define _GNU_SOURCE
14 #endif
15
16 #include "PosixSource.h"
17
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
22 #endif
23
24 #include "Rts.h"
25
26 #if defined(linux_HOST_OS)
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/syscall.h>
30 #endif
31
32 #if defined(HAVE_PTHREAD_H)
33 #include <pthread.h>
34 #if defined(freebsd_HOST_OS)
35 #include <pthread_np.h>
36 #endif
37 #endif
38
39 #if defined(THREADED_RTS)
40 #include "RtsUtils.h"
41 #include "Task.h"
42
43 #if HAVE_STRING_H
44 #include <string.h>
45 #endif
46
47 #if defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
48 #include <sys/types.h>
49 #include <sys/sysctl.h>
50 #endif
51
52 #if !defined(HAVE_PTHREAD_H)
53 #error pthreads.h is required for the threaded RTS on Posix platforms
54 #endif
55
56 #if defined(HAVE_SCHED_H)
57 #include <sched.h>
58 #endif
59
60 #if defined(HAVE_SYS_CPUSET_H)
61 #include <sys/param.h>
62 #include <sys/cpuset.h>
63 #endif
64
65 #ifdef HAVE_UNISTD_H
66 #include <unistd.h>
67 #endif
68
69 #if defined(darwin_HOST_OS)
70 #include <mach/mach.h>
71 #endif
72
73 #ifdef HAVE_SIGNAL_H
74 # include <signal.h>
75 #endif
76
77 /*
78 * This (allegedly) OS threads independent layer was initially
79 * abstracted away from code that used Pthreads, so the functions
80 * provided here are mostly just wrappers to the Pthreads API.
81 *
82 */
83
84 void
85 initCondition( Condition* pCond )
86 {
87 pthread_cond_init(pCond, NULL);
88 return;
89 }
90
91 void
92 closeCondition( Condition* pCond )
93 {
94 pthread_cond_destroy(pCond);
95 return;
96 }
97
98 rtsBool
99 broadcastCondition ( Condition* pCond )
100 {
101 return (pthread_cond_broadcast(pCond) == 0);
102 }
103
104 rtsBool
105 signalCondition ( Condition* pCond )
106 {
107 return (pthread_cond_signal(pCond) == 0);
108 }
109
110 rtsBool
111 waitCondition ( Condition* pCond, Mutex* pMut )
112 {
113 return (pthread_cond_wait(pCond,pMut) == 0);
114 }
115
116 void
117 yieldThread(void)
118 {
119 sched_yield();
120 return;
121 }
122
123 void
124 shutdownThread(void)
125 {
126 pthread_exit(NULL);
127 }
128
129 int
130 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
131 {
132 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
133 if(!result)
134 pthread_detach(*pId);
135 return result;
136 }
137
138 OSThreadId
139 osThreadId(void)
140 {
141 return pthread_self();
142 }
143
144 rtsBool
145 osThreadIsAlive(OSThreadId id STG_UNUSED)
146 {
147 // no good way to implement this on POSIX, AFAICT. Returning true
148 // is safe.
149 return rtsTrue;
150 }
151
152 void
153 initMutex(Mutex* pMut)
154 {
155 #if defined(DEBUG)
156 pthread_mutexattr_t attr;
157 pthread_mutexattr_init(&attr);
158 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
159 pthread_mutex_init(pMut,&attr);
160 #else
161 pthread_mutex_init(pMut,NULL);
162 #endif
163 return;
164 }
165 void
166 closeMutex(Mutex* pMut)
167 {
168 pthread_mutex_destroy(pMut);
169 }
170
171 void
172 newThreadLocalKey (ThreadLocalKey *key)
173 {
174 int r;
175 if ((r = pthread_key_create(key, NULL)) != 0) {
176 barf("newThreadLocalKey: %s", strerror(r));
177 }
178 }
179
180 void *
181 getThreadLocalVar (ThreadLocalKey *key)
182 {
183 return pthread_getspecific(*key);
184 // Note: a return value of NULL can indicate that either the key
185 // is not valid, or the key is valid and the data value has not
186 // yet been set. We need to use the latter case, so we cannot
187 // detect errors here.
188 }
189
190 void
191 setThreadLocalVar (ThreadLocalKey *key, void *value)
192 {
193 int r;
194 if ((r = pthread_setspecific(*key,value)) != 0) {
195 barf("setThreadLocalVar: %s", strerror(r));
196 }
197 }
198
199 void
200 freeThreadLocalKey (ThreadLocalKey *key)
201 {
202 int r;
203 if ((r = pthread_key_delete(*key)) != 0) {
204 barf("freeThreadLocalKey: %s", strerror(r));
205 }
206 }
207
208 static void *
209 forkOS_createThreadWrapper ( void * entry )
210 {
211 Capability *cap;
212 cap = rts_lock();
213 rts_evalStableIO(&cap, (HsStablePtr) entry, NULL);
214 rts_unlock(cap);
215 return NULL;
216 }
217
218 int
219 forkOS_createThread ( HsStablePtr entry )
220 {
221 pthread_t tid;
222 int result = pthread_create(&tid, NULL,
223 forkOS_createThreadWrapper, (void*)entry);
224 if(!result)
225 pthread_detach(tid);
226 return result;
227 }
228
229 nat
230 getNumberOfProcessors (void)
231 {
232 static nat nproc = 0;
233
234 if (nproc == 0) {
235 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
236 nproc = sysconf(_SC_NPROCESSORS_ONLN);
237 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
238 nproc = sysconf(_SC_NPROCESSORS_CONF);
239 #elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
240 size_t size = sizeof(nat);
241 if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0))
242 nproc = 1;
243 #else
244 nproc = 1;
245 #endif
246 }
247
248 return nproc;
249 }
250
251 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
252 // Schedules the thread to run on CPU n of m. m may be less than the
253 // number of physical CPUs, in which case, the thread will be allowed
254 // to run on CPU n, n+m, n+2m etc.
255 void
256 setThreadAffinity (nat n, nat m)
257 {
258 nat nproc;
259 cpu_set_t cs;
260 nat i;
261
262 nproc = getNumberOfProcessors();
263 CPU_ZERO(&cs);
264 for (i = n; i < nproc; i+=m) {
265 CPU_SET(i, &cs);
266 }
267 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
268 }
269
270 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
271 // Schedules the current thread in the affinity set identified by tag n.
272 void
273 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
274 {
275 thread_affinity_policy_data_t policy;
276
277 policy.affinity_tag = n;
278 thread_policy_set(mach_thread_self(),
279 THREAD_AFFINITY_POLICY,
280 (thread_policy_t) &policy,
281 THREAD_AFFINITY_POLICY_COUNT);
282 }
283
284 #elif defined(HAVE_SYS_CPUSET_H) /* FreeBSD 7.1+ */
285 void
286 setThreadAffinity(nat n, nat m)
287 {
288 nat nproc;
289 cpuset_t cs;
290 nat i;
291
292 nproc = getNumberOfProcessors();
293 CPU_ZERO(&cs);
294
295 for (i = n; i < nproc; i += m)
296 CPU_SET(i, &cs);
297
298 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(cpuset_t), &cs);
299 }
300
301 #else
302 void
303 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
304 nat m GNUC3_ATTRIBUTE(__unused__))
305 {
306 }
307 #endif
308
309 void
310 interruptOSThread (OSThreadId id)
311 {
312 pthread_kill(id, SIGPIPE);
313 }
314
315 #else /* !defined(THREADED_RTS) */
316
317 int
318 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
319 {
320 return -1;
321 }
322
323 nat getNumberOfProcessors (void)
324 {
325 return 1;
326 }
327
328 #endif /* defined(THREADED_RTS) */
329
330 KernelThreadId kernelThreadId (void)
331 {
332 #if defined(linux_HOST_OS)
333 pid_t tid = syscall(SYS_gettid); // no really, see man gettid
334 return (KernelThreadId) tid;
335
336 /* FreeBSD 9.0+ */
337 #elif defined(freebsd_HOST_OS) && (__FreeBSD_version >= 900031)
338 return pthread_getthreadid_np();
339
340 // Check for OS X >= 10.6 (see #7356)
341 #elif defined(darwin_HOST_OS) && !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
342 uint64_t ktid;
343 pthread_threadid_np(NULL, &ktid);
344 return ktid;
345
346 #else
347 // unsupported
348 return 0;
349 #endif
350 }