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