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);
101 forkOS_createThreadWrapper ( void * entry )
104 rts_evalStableIO((HsStablePtr) entry, NULL);
110 forkOS_createThread ( HsStablePtr entry )
113 int result = pthread_create(&tid, NULL,
114 forkOS_createThreadWrapper, (void*)entry);
120 #elif defined(HAVE_WINDOWS_H)
121 /* For reasons not yet clear, the entire contents of process.h is protected
122 * by __STRICT_ANSI__ not being defined.
124 #undef __STRICT_ANSI__
127 /* Win32 threads and synchronisation objects */
129 /* A Condition is represented by a Win32 Event object;
130 * a Mutex by a Mutex kernel object.
132 * ToDo: go through the defn and usage of these to
133 * make sure the semantics match up with that of
134 * the (assumed) pthreads behaviour. This is really
135 * just a first pass at getting something compilable.
139 initCondition( Condition* pCond )
141 HANDLE h = CreateEvent(NULL,
142 FALSE, /* auto reset */
143 FALSE, /* initially not signalled */
144 NULL); /* unnamed => process-local. */
147 errorBelch("initCondition: unable to create");
154 closeCondition( Condition* pCond )
156 if ( CloseHandle(*pCond) == 0 ) {
157 errorBelch("closeCondition: failed to close");
163 broadcastCondition ( Condition* pCond )
170 signalCondition ( Condition* pCond )
172 if (SetEvent(*pCond) == 0) {
173 barf("SetEvent: %d", GetLastError());
179 waitCondition ( Condition* pCond, Mutex* pMut )
182 WaitForSingleObject(*pCond, INFINITE);
183 /* Hmm..use WaitForMultipleObjects() ? */
201 static unsigned __stdcall startProcWrapper(void* pReal);
202 static unsigned __stdcall
203 startProcWrapper(void* pReal)
205 ((void (*)(void))pReal)();
210 createOSThread ( OSThreadId* pId, void (*startProc)(void))
213 return (_beginthreadex ( NULL, /* default security attributes */
218 (unsigned*)pId) == 0);
224 return GetCurrentThreadId();
228 initMutex (Mutex* pMut)
230 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
231 FALSE, /* not owned => initially signalled */
238 static unsigned __stdcall
239 forkOS_createThreadWrapper ( void * entry )
242 rts_evalStableIO((HsStablePtr) entry, NULL);
248 forkOS_createThread ( HsStablePtr entry )
251 return (_beginthreadex ( NULL, /* default security attributes */
253 forkOS_createThreadWrapper,
256 (unsigned*)&pId) == 0);
259 #endif /* defined(HAVE_PTHREAD_H) */
261 #else /* !defined(RTS_SUPPORTS_THREADS) */
264 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
269 #endif /* !defined(RTS_SUPPORTS_THREADS) */