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