Fix bug in setThreadAffinity() (Linux)
[ghc-hetmet.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__)
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 "Rts.h"
17 #if defined(THREADED_RTS)
18 #include "OSThreads.h"
19 #include "RtsUtils.h"
20 #include "Task.h"
21
22 #if HAVE_STRING_H
23 #include <string.h>
24 #endif
25
26 #if !defined(HAVE_PTHREAD_H)
27 #error pthreads.h is required for the threaded RTS on Posix platforms
28 #endif
29
30 #if defined(HAVE_SCHED_H)
31 #include <sched.h>
32 #endif
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 #if defined(darwin_HOST_OS)
39 #include <mach/mach.h>
40 #endif
41
42 /*
43  * This (allegedly) OS threads independent layer was initially
44  * abstracted away from code that used Pthreads, so the functions
45  * provided here are mostly just wrappers to the Pthreads API.
46  *
47  */
48
49 void
50 initCondition( Condition* pCond )
51 {
52   pthread_cond_init(pCond, NULL);
53   return;
54 }
55
56 void
57 closeCondition( Condition* pCond )
58 {
59   pthread_cond_destroy(pCond);
60   return;
61 }
62
63 rtsBool
64 broadcastCondition ( Condition* pCond )
65 {
66   return (pthread_cond_broadcast(pCond) == 0);
67 }
68
69 rtsBool
70 signalCondition ( Condition* pCond )
71 {
72   return (pthread_cond_signal(pCond) == 0);
73 }
74
75 rtsBool
76 waitCondition ( Condition* pCond, Mutex* pMut )
77 {
78   return (pthread_cond_wait(pCond,pMut) == 0);
79 }
80
81 void
82 yieldThread()
83 {
84   sched_yield();
85   return;
86 }
87
88 void
89 shutdownThread()
90 {
91   pthread_exit(NULL);
92 }
93
94 int
95 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
96 {
97   int result = pthread_create(pId, NULL, (void *(*)(void *))startProc, param);
98   if(!result)
99     pthread_detach(*pId);
100   return result;
101 }
102
103 OSThreadId
104 osThreadId()
105 {
106   return pthread_self();
107 }
108
109 rtsBool
110 osThreadIsAlive(OSThreadId id STG_UNUSED)
111 {
112     // no good way to implement this on POSIX, AFAICT.  Returning true
113     // is safe.
114     return rtsTrue;
115 }
116
117 void
118 initMutex(Mutex* pMut)
119 {
120 #if defined(DEBUG)
121     pthread_mutexattr_t attr;
122     pthread_mutexattr_init(&attr);
123     pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_ERRORCHECK);
124     pthread_mutex_init(pMut,&attr);
125 #else
126     pthread_mutex_init(pMut,NULL);
127 #endif
128     return;
129 }
130 void
131 closeMutex(Mutex* pMut)
132 {
133     pthread_mutex_destroy(pMut);
134 }
135
136 void
137 newThreadLocalKey (ThreadLocalKey *key)
138 {
139     int r;
140     if ((r = pthread_key_create(key, NULL)) != 0) {
141         barf("newThreadLocalKey: %s", strerror(r));
142     }
143 }
144
145 void *
146 getThreadLocalVar (ThreadLocalKey *key)
147 {
148     return pthread_getspecific(*key);
149     // Note: a return value of NULL can indicate that either the key
150     // is not valid, or the key is valid and the data value has not
151     // yet been set.  We need to use the latter case, so we cannot
152     // detect errors here.
153 }
154
155 void
156 setThreadLocalVar (ThreadLocalKey *key, void *value)
157 {
158     int r;
159     if ((r = pthread_setspecific(*key,value)) != 0) {
160         barf("setThreadLocalVar: %s", strerror(r));
161     }
162 }
163
164 void
165 freeThreadLocalKey (ThreadLocalKey *key)
166 {
167     int r;
168     if ((r = pthread_key_delete(*key)) != 0) {
169         barf("freeThreadLocalKey: %s", strerror(r));
170     }
171 }
172
173 static void *
174 forkOS_createThreadWrapper ( void * entry )
175 {
176     Capability *cap;
177     cap = rts_lock();
178     cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
179     taskTimeStamp(myTask());
180     rts_unlock(cap);
181     return NULL;
182 }
183
184 int
185 forkOS_createThread ( HsStablePtr entry )
186 {
187     pthread_t tid;
188     int result = pthread_create(&tid, NULL,
189                                 forkOS_createThreadWrapper, (void*)entry);
190     if(!result)
191         pthread_detach(tid);
192     return result;
193 }
194
195 nat
196 getNumberOfProcessors (void)
197 {
198     static nat nproc = 0;
199
200     if (nproc == 0) {
201 #if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
202         nproc = sysconf(_SC_NPROCESSORS_ONLN);
203 #elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_CONF)
204         nproc = sysconf(_SC_NPROCESSORS_CONF);
205 #else
206         nproc = 1;
207 #endif
208     }
209
210     return nproc;
211 }
212
213 #if defined(HAVE_SCHED_H) && defined(HAVE_SCHED_SETAFFINITY)
214 // Schedules the thread to run on CPU n of m.  m may be less than the
215 // number of physical CPUs, in which case, the thread will be allowed
216 // to run on CPU n, n+m, n+2m etc.
217 void
218 setThreadAffinity (nat n, nat m)
219 {
220     nat nproc;
221     cpu_set_t cs;
222     nat i;
223
224     nproc = getNumberOfProcessors();
225     CPU_ZERO(&cs);
226     for (i = n; i < nproc; i+=m) {
227         CPU_SET(i, &cs);
228     }
229     sched_setaffinity(0, sizeof(cpu_set_t), &cs);
230 }
231
232 #elif defined(darwin_HOST_OS) && defined(THREAD_AFFINITY_POLICY)
233 // Schedules the current thread in the affinity set identified by tag n.
234 void
235 setThreadAffinity (nat n, nat m GNUC3_ATTRIBUTE(__unused__))
236 {
237     thread_affinity_policy_data_t policy;
238
239     policy.affinity_tag = n;
240     thread_policy_set(mach_thread_self(), 
241                       THREAD_AFFINITY_POLICY,
242                       (thread_policy_t) &policy,
243                       THREAD_AFFINITY_POLICY_COUNT);
244 }
245
246 #else
247 void
248 setThreadAffinity (nat n GNUC3_ATTRIBUTE(__unused__), 
249                    nat m GNUC3_ATTRIBUTE(__unused__))
250 {
251 }
252 #endif
253
254 #else /* !defined(THREADED_RTS) */
255
256 int
257 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
258 {
259     return -1;
260 }
261
262 #endif /* !defined(THREADED_RTS) */