[project @ 2004-02-12 02:04:59 by mthomas]
[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   SetEvent(*pCond);
173   return rtsTrue;
174 }
175
176 rtsBool
177 waitCondition ( Condition* pCond, Mutex* pMut )
178 {
179   ReleaseMutex(*pMut);
180   WaitForSingleObject(*pCond, INFINITE);
181   /* Hmm..use WaitForMultipleObjects() ? */
182   WaitForSingleObject(*pMut, INFINITE);
183   return rtsTrue;
184 }
185
186 void
187 yieldThread()
188 {
189   Sleep(0);
190   return;
191 }
192
193 void
194 shutdownThread()
195 {
196   _endthreadex(0);
197 }
198
199 static unsigned __stdcall startProcWrapper(void* pReal);
200 static unsigned __stdcall
201 startProcWrapper(void* pReal)
202 {
203   ((void (*)(void))pReal)();
204   return 0;
205 }
206
207 int
208 createOSThread ( OSThreadId* pId, void (*startProc)(void))
209 {
210   
211   return (_beginthreadex ( NULL,  /* default security attributes */
212                            0,
213                            startProcWrapper,
214                            (void*)startProc,
215                            0,
216                            (unsigned*)pId) == 0);
217 }
218
219 OSThreadId
220 osThreadId()
221 {
222   return GetCurrentThreadId();
223 }
224
225 void
226 initMutex (Mutex* pMut)
227 {
228   HANDLE h = CreateMutex ( NULL,  /* default sec. attributes */
229                            FALSE, /* not owned => initially signalled */
230                            NULL
231                            );
232   *pMut = h;
233   return;
234 }
235
236 static unsigned __stdcall
237 forkOS_createThreadWrapper ( void * entry )
238 {
239     rts_lock();
240     rts_evalStableIO((HsStablePtr) entry, NULL);
241     rts_unlock();
242     return 0;
243 }
244
245 int
246 forkOS_createThread ( HsStablePtr entry )
247 {
248     unsigned long pId;
249     return (_beginthreadex ( NULL,  /* default security attributes */
250                            0,
251                            forkOS_createThreadWrapper,
252                            (void*)entry,
253                            0,
254                            (unsigned*)&pId) == 0);
255 }
256
257 #endif /* defined(HAVE_PTHREAD_H) */
258
259 #else /* !defined(RTS_SUPPORTS_THREADS) */
260
261 int
262 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
263 {
264     return -1;
265 }
266
267 #endif /* !defined(RTS_SUPPORTS_THREADS) */
268