Signals: Ensure libdw session is freed
[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, char *name STG_UNUSED,
133 OSThreadProc *startProc, void *param)
134 {
135 int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
136 if (!result) {
137 pthread_detach(*pId);
138 #if HAVE_PTHREAD_SETNAME_NP
139 pthread_setname_np(*pId, name);
140 #endif
141 }
142 return result;
143 }
144
145 OSThreadId
146 osThreadId(void)
147 {
148 return pthread_self();
149 }
150
151 rtsBool
152 osThreadIsAlive(OSThreadId id STG_UNUSED)
153 {
154 // no good way to implement this on POSIX, AFAICT. Returning true
155 // is safe.
156 return rtsTrue;
157 }
158
159 void
160 initMutex(Mutex* pMut)
161 {
162 #if defined(DEBUG)
163 pthread_mutexattr_t attr;
164 pthread_mutexattr_init(&attr);
165 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
166 pthread_mutex_init(pMut,&attr);
167 #else
168 pthread_mutex_init(pMut,NULL);
169 #endif
170 return;
171 }
172 void
173 closeMutex(Mutex* pMut)
174 {
175 pthread_mutex_destroy(pMut);
176 }
177
178 void
179 newThreadLocalKey (ThreadLocalKey *key)
180 {
181 int r;
182 if ((r = pthread_key_create(key, NULL)) != 0) {
183 barf("newThreadLocalKey: %s", strerror(r));
184 }
185 }
186
187 void *
188 getThreadLocalVar (ThreadLocalKey *key)
189 {
190 return pthread_getspecific(*key);
191 // Note: a return value of NULL can indicate that either the key
192 // is not valid, or the key is valid and the data value has not
193 // yet been set. We need to use the latter case, so we cannot
194 // detect errors here.
195 }
196
197 void
198 setThreadLocalVar (ThreadLocalKey *key, void *value)
199 {
200 int r;
201 if ((r = pthread_setspecific(*key,value)) != 0) {
202 barf("setThreadLocalVar: %s", strerror(r));
203 }
204 }
205
206 void
207 freeThreadLocalKey (ThreadLocalKey *key)
208 {
209 int r;
210 if ((r = pthread_key_delete(*key)) != 0) {
211 barf("freeThreadLocalKey: %s", strerror(r));
212 }
213 }
214
215 static void *
216 forkOS_createThreadWrapper ( void * entry )
217 {
218 Capability *cap;
219 cap = rts_lock();
220 rts_evalStableIO(&cap, (HsStablePtr) entry, NULL);
221 rts_unlock(cap);
222 return NULL;
223 }
224
225 int
226 forkOS_createThread ( HsStablePtr entry )
227 {
228 pthread_t tid;
229 int result = pthread_create(&tid, NULL,
230 forkOS_createThreadWrapper, (void*)entry);
231 if(!result)
232 pthread_detach(tid);
233 return result;
234 }
235
236 nat
237 getNumberOfProcessors (void)
238 {
239 static nat nproc = 0;
240
241 if (nproc == 0) {
242 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
243 nproc = sysconf(_SC_NPROCESSORS_ONLN);
244 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
245 nproc = sysconf(_SC_NPROCESSORS_CONF);
246 #elif defined(darwin_HOST_OS) || defined(freebsd_HOST_OS)
247 size_t size = sizeof(nat);
248 if(0 != sysctlbyname("hw.ncpu",&nproc,&size,NULL,0))
249 nproc = 1;
250 #else
251 nproc = 1;
252 #endif
253 }
254
255 return nproc;
256 }
257
258 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
259 // Schedules the thread to run on CPU n of m. m may be less than the
260 // number of physical CPUs, in which case, the thread will be allowed
261 // to run on CPU n, n+m, n+2m etc.
262 void
263 setThreadAffinity (nat n, nat m)
264 {
265 nat nproc;
266 cpu_set_t cs;
267 nat i;
268
269 nproc = getNumberOfProcessors();
270 CPU_ZERO(&cs);
271 for (i = n; i < nproc; i+=m) {
272 CPU_SET(i, &cs);
273 }
274 sched_setaffinity(0, sizeof(cpu_set_t), &cs);
275 }
276
277 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
278 // Schedules the current thread in the affinity set identified by tag n.
279 void
280 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
281 {
282 thread_affinity_policy_data_t policy;
283
284 policy.affinity_tag = n;
285 thread_policy_set(mach_thread_self(),
286 THREAD_AFFINITY_POLICY,
287 (thread_policy_t) &policy,
288 THREAD_AFFINITY_POLICY_COUNT);
289 }
290
291 #elif defined(HAVE_SYS_CPUSET_H) /* FreeBSD 7.1+ */
292 void
293 setThreadAffinity(nat n, nat m)
294 {
295 nat nproc;
296 cpuset_t cs;
297 nat i;
298
299 nproc = getNumberOfProcessors();
300 CPU_ZERO(&cs);
301
302 for (i = n; i < nproc; i += m)
303 CPU_SET(i, &cs);
304
305 cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID,
306 -1, sizeof(cpuset_t), &cs);
307 }
308
309 #else
310 void
311 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__),
312 nat m GNUC3_ATTRIBUTE(__unused__))
313 {
314 }
315 #endif
316
317 void
318 interruptOSThread (OSThreadId id)
319 {
320 pthread_kill(id, SIGPIPE);
321 }
322
323 #else /* !defined(THREADED_RTS) */
324
325 int
326 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
327 {
328 return -1;
329 }
330
331 nat getNumberOfProcessors (void)
332 {
333 return 1;
334 }
335
336 #endif /* defined(THREADED_RTS) */
337
338 KernelThreadId kernelThreadId (void)
339 {
340 #if defined(linux_HOST_OS)
341 pid_t tid = syscall(SYS_gettid); // no really, see man gettid
342 return (KernelThreadId) tid;
343
344 /* FreeBSD 9.0+ */
345 #elif defined(freebsd_HOST_OS) && (__FreeBSD_version >= 900031)
346 return pthread_getthreadid_np();
347
348 // Check for OS X >= 10.6 (see #7356)
349 #elif defined(darwin_HOST_OS) && \
350 !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
351 __MAC_OS_X_VERSION_MIN_REQUIRED < 1060)
352 uint64_t ktid;
353 pthread_threadid_np(NULL, &ktid);
354 return ktid;
355
356 #else
357 // unsupported
358 return 0;
359 #endif
360 }