ca8fd6d75725aaad8790f3e01e94325220a17f56
[ghc-hetmet.git] / ghc / rts / OSThreads.c
1 /* ---------------------------------------------------------------------------
2  *
3  * (c) The GHC Team, 2001
4  *
5  * Accessing OS threads functionality in a (mostly) OS-independent
6  * manner. 
7  *
8  * 
9  * --------------------------------------------------------------------------*/
10 #include "Rts.h"
11 #if defined(RTS_SUPPORTS_THREADS)
12 #include "OSThreads.h"
13 #include "RtsUtils.h"
14
15 #if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
16 /*
17  * This (allegedly) OS threads independent layer was initially
18  * abstracted away from code that used Pthreads, so the functions
19  * provided here are mostly just wrappers to the Pthreads API.
20  *
21  */
22
23 void
24 initCondition( Condition* pCond )
25 {
26   pthread_cond_init(pCond, NULL);
27   return;
28 }
29
30 void
31 closeCondition( Condition* pCond )
32 {
33   pthread_cond_destroy(pCond);
34   return;
35 }
36
37 rtsBool
38 broadcastCondition ( Condition* pCond )
39 {
40   return (pthread_cond_broadcast(pCond) == 0);
41 }
42
43 rtsBool
44 signalCondition ( Condition* pCond )
45 {
46   return (pthread_cond_signal(pCond) == 0);
47 }
48
49 rtsBool
50 waitCondition ( Condition* pCond, Mutex* pMut )
51 {
52   return (pthread_cond_wait(pCond,pMut) == 0);
53 }
54
55 void
56 yieldThread()
57 {
58   sched_yield();
59   return;
60 }
61
62 void
63 shutdownThread()
64 {
65   pthread_exit(NULL);
66 }
67
68 /* Don't need the argument nor the result, at least not yet. */
69 static void* startProcWrapper(void* pProc);
70 static void*
71 startProcWrapper(void* pProc)
72 {
73   ((void (*)(void))pProc)();
74   return NULL;
75 }
76
77
78 int
79 createOSThread ( OSThreadId* pId, void (*startProc)(void))
80 {
81   int result = pthread_create(pId, NULL, startProcWrapper, (void*)startProc);
82   if(!result)
83     pthread_detach(*pId);
84   return result;
85 }
86
87 OSThreadId
88 osThreadId()
89 {
90   return pthread_self();
91 }
92
93 void
94 initMutex(Mutex* pMut)
95 {
96   pthread_mutex_init(pMut,NULL);
97   return;
98 }
99
100 static void *
101 forkOS_createThreadWrapper ( void * entry )
102 {
103     rts_lock();
104     rts_evalStableIO((HsStablePtr) entry, NULL);
105     rts_unlock();
106     return NULL;
107 }
108
109 int
110 forkOS_createThread ( HsStablePtr entry )
111 {
112     pthread_t tid;
113     int result = pthread_create(&tid, NULL,
114                                 forkOS_createThreadWrapper, (void*)entry);
115     if(!result)
116         pthread_detach(tid);
117     return result;
118 }
119
120 #elif defined(HAVE_WINDOWS_H)
121 /* For reasons not yet clear, the entire contents of process.h is protected 
122  * by __STRICT_ANSI__ not being defined.
123  */
124 #undef __STRICT_ANSI__
125 #include <process.h>
126
127 /* Win32 threads and synchronisation objects */
128
129 /* A Condition is represented by a Win32 Event object;
130  * a Mutex by a Mutex kernel object.
131  *
132  * ToDo: go through the defn and usage of these to
133  * make sure the semantics match up with that of 
134  * the (assumed) pthreads behaviour. This is really
135  * just a first pass at getting something compilable.
136  */
137
138 void
139 initCondition( Condition* pCond )
140 {
141   HANDLE h =  CreateEvent(NULL, 
142                           FALSE,  /* auto reset */
143                           FALSE,  /* initially not signalled */
144                           NULL); /* unnamed => process-local. */
145   
146   if ( h == NULL ) {
147     belch("initCondition: unable to create");
148   }
149   *pCond = h;
150   return;
151 }
152
153 void
154 closeCondition( Condition* pCond )
155 {
156   if ( CloseHandle(*pCond) == 0 ) {
157     belch("closeCondition: failed to close");
158   }
159   return;
160 }
161
162 rtsBool
163 broadcastCondition ( Condition* pCond )
164 {
165   PulseEvent(*pCond);
166   return rtsTrue;
167 }
168
169 rtsBool
170 signalCondition ( Condition* pCond )
171 {
172     if (SetEvent(*pCond) == 0) {
173         barf("SetEvent: %d", GetLastError());
174     }
175     return rtsTrue;
176 }
177
178 rtsBool
179 waitCondition ( Condition* pCond, Mutex* pMut )
180 {
181   RELEASE_LOCK(pMut);
182   WaitForSingleObject(*pCond, INFINITE);
183   /* Hmm..use WaitForMultipleObjects() ? */
184   ACQUIRE_LOCK(pMut);
185   return rtsTrue;
186 }
187
188 void
189 yieldThread()
190 {
191   Sleep(0);
192   return;
193 }
194
195 void
196 shutdownThread()
197 {
198   _endthreadex(0);
199 }
200
201 static unsigned __stdcall startProcWrapper(void* pReal);
202 static unsigned __stdcall
203 startProcWrapper(void* pReal)
204 {
205   ((void (*)(void))pReal)();
206   return 0;
207 }
208
209 int
210 createOSThread ( OSThreadId* pId, void (*startProc)(void))
211 {
212   
213   return (_beginthreadex ( NULL,  /* default security attributes */
214                            0,
215                            startProcWrapper,
216                            (void*)startProc,
217                            0,
218                            (unsigned*)pId) == 0);
219 }
220
221 OSThreadId
222 osThreadId()
223 {
224   return GetCurrentThreadId();
225 }
226
227 void
228 initMutex (Mutex* pMut)
229 {
230   HANDLE h = CreateMutex ( NULL,  /* default sec. attributes */
231                            FALSE, /* not owned => initially signalled */
232                            NULL
233                            );
234   *pMut = h;
235   return;
236 }
237
238 static unsigned __stdcall
239 forkOS_createThreadWrapper ( void * entry )
240 {
241     rts_lock();
242     rts_evalStableIO((HsStablePtr) entry, NULL);
243     rts_unlock();
244     return 0;
245 }
246
247 int
248 forkOS_createThread ( HsStablePtr entry )
249 {
250     unsigned long pId;
251     return (_beginthreadex ( NULL,  /* default security attributes */
252                            0,
253                            forkOS_createThreadWrapper,
254                            (void*)entry,
255                            0,
256                            (unsigned*)&pId) == 0);
257 }
258
259 #endif /* defined(HAVE_PTHREAD_H) */
260
261 #else /* !defined(RTS_SUPPORTS_THREADS) */
262
263 int
264 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
265 {
266     return -1;
267 }
268
269 #endif /* !defined(RTS_SUPPORTS_THREADS) */
270