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