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 return pthread_create(pId, NULL, startProcWrapper, (void*)startProc);
87 return pthread_self();
91 initMutex(Mutex* pMut)
93 pthread_mutex_init(pMut,NULL);
97 #elif defined(HAVE_WINDOWS_H)
100 /* Win32 threads and synchronisation objects */
102 /* A Condition is represented by a Win32 Event object;
103 * a Mutex by a Mutex kernel object.
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.
112 initCondition( Condition* pCond )
114 HANDLE h = CreateEvent(NULL,
115 FALSE, /* auto reset */
116 FALSE, /* initially not signalled */
117 NULL); /* unnamed => process-local. */
120 belch("initCondition: unable to create");
127 closeCondition( Condition* pCond )
129 if ( CloseHandle(*pCond) == 0 ) {
130 belch("closeCondition: failed to close");
136 broadcastCondition ( Condition* pCond )
143 signalCondition ( Condition* pCond )
150 waitCondition ( Condition* pCond, Mutex* pMut )
153 WaitForSingleObject(*pCond, INFINITE);
154 /* Hmm..use WaitForMultipleObjects() ? */
155 WaitForSingleObject(*pMut, INFINITE);
172 static unsigned __stdcall startProcWrapper(void* pReal);
173 static unsigned __stdcall
174 startProcWrapper(void* pReal)
176 ((void (*)(void))pReal)();
181 createOSThread ( OSThreadId* pId, void (*startProc)(void))
184 return (_beginthreadex ( NULL, /* default security attributes */
189 (unsigned*)pId) == 0);
195 return GetCurrentThreadId();
199 initMutex (Mutex* pMut)
201 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
202 FALSE, /* not owned => initially signalled */
209 #endif /* defined(HAVE_PTHREAD_H) */
211 #endif /* defined(RTS_SUPPORTS_THREADS) */