[project @ 2003-09-12 16:16:43 by sof]
[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   return pthread_create(pId, NULL, startProcWrapper, (void*)startProc);
82 }
83
84 OSThreadId
85 osThreadId()
86 {
87   return pthread_self();
88 }
89
90 void
91 initMutex(Mutex* pMut)
92 {
93   pthread_mutex_init(pMut,NULL);
94   return;
95 }
96
97 #elif defined(HAVE_WINDOWS_H)
98 #include <process.h>
99
100 /* Win32 threads and synchronisation objects */
101
102 /* A Condition is represented by a Win32 Event object;
103  * a Mutex by a Mutex kernel object.
104  *
105  * ToDo: go through the defn and usage of these to
106  * make sure the semantics match up with that of 
107  * the (assumed) pthreads behaviour. This is really
108  * just a first pass at getting something compilable.
109  */
110
111 void
112 initCondition( Condition* pCond )
113 {
114   HANDLE h =  CreateEvent(NULL, 
115                           FALSE,  /* auto reset */
116                           FALSE,  /* initially not signalled */
117                           NULL); /* unnamed => process-local. */
118   
119   if ( h == NULL ) {
120     belch("initCondition: unable to create");
121   }
122   *pCond = h;
123   return;
124 }
125
126 void
127 closeCondition( Condition* pCond )
128 {
129   if ( CloseHandle(*pCond) == 0 ) {
130     belch("closeCondition: failed to close");
131   }
132   return;
133 }
134
135 rtsBool
136 broadcastCondition ( Condition* pCond )
137 {
138   PulseEvent(*pCond);
139   return rtsTrue;
140 }
141
142 rtsBool
143 signalCondition ( Condition* pCond )
144 {
145   SetEvent(*pCond);
146   return rtsTrue;
147 }
148
149 rtsBool
150 waitCondition ( Condition* pCond, Mutex* pMut )
151 {
152   ReleaseMutex(*pMut);
153   WaitForSingleObject(*pCond, INFINITE);
154   /* Hmm..use WaitForMultipleObjects() ? */
155   WaitForSingleObject(*pMut, INFINITE);
156   return rtsTrue;
157 }
158
159 void
160 yieldThread()
161 {
162   Sleep(0);
163   return;
164 }
165
166 void
167 shutdownThread()
168 {
169   _endthreadex(0);
170 }
171
172 static unsigned __stdcall startProcWrapper(void* pReal);
173 static unsigned __stdcall
174 startProcWrapper(void* pReal)
175 {
176   ((void (*)(void))pReal)();
177   return 0;
178 }
179
180 int
181 createOSThread ( OSThreadId* pId, void (*startProc)(void))
182 {
183   
184   return (_beginthreadex ( NULL,  /* default security attributes */
185                            0,
186                            startProcWrapper,
187                            (void*)startProc,
188                            0,
189                            (unsigned*)pId) == 0);
190 }
191
192 OSThreadId
193 osThreadId()
194 {
195   return GetCurrentThreadId();
196 }
197
198 void
199 initMutex (Mutex* pMut)
200 {
201   HANDLE h = CreateMutex ( NULL,  /* default sec. attributes */
202                            FALSE, /* not owned => initially signalled */
203                            NULL
204                            );
205   *pMut = h;
206   return;
207 }
208
209 #endif /* defined(HAVE_PTHREAD_H) */
210
211 #endif /* defined(RTS_SUPPORTS_THREADS) */