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