1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001
5 * Accessing OS threads functionality in a (mostly) OS-independent
9 * --------------------------------------------------------------------------*/
11 #if defined(RTS_SUPPORTS_THREADS)
12 #include "OSThreads.h"
15 #if defined(HAVE_PTHREAD_H) && !defined(WANT_NATIVE_WIN32_THREADS)
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.
24 initCondition( Condition* pCond )
26 pthread_cond_init(pCond, NULL);
31 closeCondition( Condition* pCond )
33 pthread_cond_destroy(pCond);
38 broadcastCondition ( Condition* pCond )
40 return (pthread_cond_broadcast(pCond) == 0);
44 signalCondition ( Condition* pCond )
46 return (pthread_cond_signal(pCond) == 0);
50 waitCondition ( Condition* pCond, Mutex* pMut )
52 return (pthread_cond_wait(pCond,pMut) == 0);
68 /* Don't need the argument nor the result, at least not yet. */
69 static void* startProcWrapper(void* pProc);
71 startProcWrapper(void* pProc)
73 ((void (*)(void))pProc)();
79 createOSThread ( OSThreadId* pId, void (*startProc)(void))
81 int result = pthread_create(pId, NULL, startProcWrapper, (void*)startProc);
90 return pthread_self();
94 initMutex(Mutex* pMut)
96 pthread_mutex_init(pMut,NULL);
100 #elif defined(HAVE_WINDOWS_H)
103 /* Win32 threads and synchronisation objects */
105 /* A Condition is represented by a Win32 Event object;
106 * a Mutex by a Mutex kernel object.
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.
115 initCondition( Condition* pCond )
117 HANDLE h = CreateEvent(NULL,
118 FALSE, /* auto reset */
119 FALSE, /* initially not signalled */
120 NULL); /* unnamed => process-local. */
123 belch("initCondition: unable to create");
130 closeCondition( Condition* pCond )
132 if ( CloseHandle(*pCond) == 0 ) {
133 belch("closeCondition: failed to close");
139 broadcastCondition ( Condition* pCond )
146 signalCondition ( Condition* pCond )
153 waitCondition ( Condition* pCond, Mutex* pMut )
156 WaitForSingleObject(*pCond, INFINITE);
157 /* Hmm..use WaitForMultipleObjects() ? */
158 WaitForSingleObject(*pMut, INFINITE);
175 static unsigned __stdcall startProcWrapper(void* pReal);
176 static unsigned __stdcall
177 startProcWrapper(void* pReal)
179 ((void (*)(void))pReal)();
184 createOSThread ( OSThreadId* pId, void (*startProc)(void))
187 return (_beginthreadex ( NULL, /* default security attributes */
192 (unsigned*)pId) == 0);
198 return GetCurrentThreadId();
202 initMutex (Mutex* pMut)
204 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
205 FALSE, /* not owned => initially signalled */
212 #endif /* defined(HAVE_PTHREAD_H) */
214 #endif /* defined(RTS_SUPPORTS_THREADS) */