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);
121 initMutex (Mutex* pMut)
123 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
124 FALSE, /* not owned => initially signalled */
133 newThreadLocalKey (ThreadLocalKey *key)
137 if (r == TLS_OUT_OF_INDEXES) {
138 barf("newThreadLocalKey: out of keys");
144 getThreadLocalVar (ThreadLocalKey *key)
147 r = TlsGetValue(*key);
149 // r is allowed to be NULL - it can mean that either there was an
150 // error or the stored value is in fact NULL.
151 if (GetLastError() != NO_ERROR) {
152 barf("getThreadLocalVar: key not found");
159 setThreadLocalVar (ThreadLocalKey *key, void *value)
162 b = TlsSetValue(*key, value);
164 barf("setThreadLocalVar: %d", GetLastError());
169 static unsigned __stdcall
170 forkOS_createThreadWrapper ( void * entry )
174 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
180 forkOS_createThread ( HsStablePtr entry )
183 return (_beginthreadex ( NULL, /* default security attributes */
185 forkOS_createThreadWrapper,
188 (unsigned*)&pId) == 0);
191 #else /* !defined(THREADED_RTS) */
194 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
199 #endif /* !defined(THREADED_RTS) */