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.
23 void initCondVar( CondVar* pCond )
25 pthread_cond_init(pCond, NULL);
29 void closeCondVar( CondVar* pCond )
31 pthread_cond_destroy(pCond);
36 broadcastCondVar ( CondVar* pCond )
38 return (pthread_cond_broadcast(pCond) == 0);
42 signalCondVar ( CondVar* pCond )
44 return (pthread_cond_signal(pCond) == 0);
48 waitCondVar ( CondVar* pCond, MutexVar* pMut )
50 return (pthread_cond_wait(pCond,pMut) == 0);
58 int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
60 return pthread_create(pId, NULL, startProc, NULL);
63 OSThreadId osThreadId()
65 return pthread_self();
68 void initMutexVar (MutexVar* pMut)
70 pthread_mutex_init(pMut,NULL);
74 #elif defined(HAVE_WINDOWS_H)
77 /* Win32 threads and synchronisation objects */
79 /* A CondVar is represented by a Win32 Event object,
80 * a MutexVar by a Mutex kernel object.
82 * ToDo: go through the defn and usage of these to
83 * make sure the semantics match up with that of
84 * the (assumed) pthreads behaviour. This is really
85 * just a first pass at getting something compilable.
88 void initCondVar( CondVar* pCond )
90 HANDLE h = CreateEvent(NULL,
91 TRUE, /* manual reset */
92 TRUE, /* initially signalled */
93 NULL); /* unnamed => process-local. */
96 belch("initCondVar: unable to create");
102 void closeCondVar( CondVar* pCond )
104 if ( CloseHandle(*pCond) == 0 ) {
105 belch("closeCondVar: failed to close");
111 broadcastCondVar ( CondVar* pCond )
118 signalCondVar ( CondVar* pCond )
125 waitCondVar ( CondVar* pCond, MutexVar* pMut )
128 WaitForSingleObject(*pCond, INFINITE);
129 /* Hmm..use WaitForMultipleObjects() ? */
130 WaitForSingleObject(*pMut, INFINITE);
134 void shutdownThread()
139 static unsigned __stdcall startProcWrapper(void* pReal);
140 static unsigned __stdcall startProcWrapper(void* pReal)
142 ((void (*)(void*))pReal)(NULL);
146 int createOSThread ( OSThreadId* pId, void *(*startProc)(void*))
149 return _beginthreadex ( NULL, /* default security attributes */
157 OSThreadId osThreadId()
159 return GetCurrentThreadId();
162 void initMutexVar (MutexVar* pMut)
164 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
165 FALSE, /* not owned => initially signalled */
172 #endif /* defined(HAVE_PTHREAD_H) */
174 #endif /* defined(RTS_SUPPORTS_THREADS) */