1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
11 #if defined(THREADED_RTS)
12 #include "OSThreads.h"
15 /* For reasons not yet clear, the entire contents of process.h is protected
16 * by __STRICT_ANSI__ not being defined.
18 #undef __STRICT_ANSI__
21 /* Win32 threads and synchronisation objects */
23 /* A Condition is represented by a Win32 Event object;
24 * a Mutex by a Mutex kernel object.
26 * ToDo: go through the defn and usage of these to
27 * make sure the semantics match up with that of
28 * the (assumed) pthreads behaviour. This is really
29 * just a first pass at getting something compilable.
33 initCondition( Condition* pCond )
35 HANDLE h = CreateEvent(NULL,
36 FALSE, /* auto reset */
37 FALSE, /* initially not signalled */
38 NULL); /* unnamed => process-local. */
41 errorBelch("initCondition: unable to create");
48 closeCondition( Condition* pCond )
50 if ( CloseHandle(*pCond) == 0 ) {
51 errorBelch("closeCondition: failed to close");
57 broadcastCondition ( Condition* pCond )
64 signalCondition ( Condition* pCond )
66 if (SetEvent(*pCond) == 0) {
67 barf("SetEvent: %d", GetLastError());
73 waitCondition ( Condition* pCond, Mutex* pMut )
76 WaitForSingleObject(*pCond, INFINITE);
77 /* Hmm..use WaitForMultipleObjects() ? */
96 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
99 return (_beginthreadex ( NULL, /* default security attributes */
101 (unsigned (__stdcall *)(void *)) startProc,
104 (unsigned*)pId) == 0);
110 return GetCurrentThreadId();
113 #ifdef USE_CRITICAL_SECTIONS
115 initMutex (Mutex* pMut)
117 InitializeCriticalSectionAndSpinCount(pMut,4000);
120 closeMutex (Mutex* pMut)
122 DeleteCriticalSection(pMut);
126 initMutex (Mutex* pMut)
128 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
129 FALSE, /* not owned => initially signalled */
136 closeMutex (Mutex* pMut)
143 newThreadLocalKey (ThreadLocalKey *key)
147 if (r == TLS_OUT_OF_INDEXES) {
148 barf("newThreadLocalKey: out of keys");
154 getThreadLocalVar (ThreadLocalKey *key)
157 r = TlsGetValue(*key);
159 // r is allowed to be NULL - it can mean that either there was an
160 // error or the stored value is in fact NULL.
161 if (GetLastError() != NO_ERROR) {
162 barf("getThreadLocalVar: key not found");
169 setThreadLocalVar (ThreadLocalKey *key, void *value)
172 b = TlsSetValue(*key, value);
174 barf("setThreadLocalVar: %d", GetLastError());
179 static unsigned __stdcall
180 forkOS_createThreadWrapper ( void * entry )
184 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
190 forkOS_createThread ( HsStablePtr entry )
193 return (_beginthreadex ( NULL, /* default security attributes */
195 forkOS_createThreadWrapper,
198 (unsigned*)&pId) == 0);
201 #else /* !defined(THREADED_RTS) */
204 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
209 #endif /* !defined(THREADED_RTS) */