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 sysErrorBelch("initCondition: unable to create");
42 stg_exit(EXIT_FAILURE);
49 closeCondition( Condition* pCond )
51 if ( CloseHandle(*pCond) == 0 ) {
52 sysErrorBelch("closeCondition: failed to close");
58 broadcastCondition ( Condition* pCond )
65 signalCondition ( Condition* pCond )
67 if (SetEvent(*pCond) == 0) {
68 sysErrorBelch("SetEvent");
69 stg_exit(EXIT_FAILURE);
75 waitCondition ( Condition* pCond, Mutex* pMut )
78 WaitForSingleObject(*pCond, INFINITE);
79 /* Hmm..use WaitForMultipleObjects() ? */
98 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
101 return (_beginthreadex ( NULL, /* default security attributes */
103 (unsigned (__stdcall *)(void *)) startProc,
106 (unsigned*)pId) == 0);
112 return GetCurrentThreadId();
115 #ifdef USE_CRITICAL_SECTIONS
117 initMutex (Mutex* pMut)
119 InitializeCriticalSectionAndSpinCount(pMut,4000);
122 closeMutex (Mutex* pMut)
124 DeleteCriticalSection(pMut);
128 initMutex (Mutex* pMut)
130 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
131 FALSE, /* not owned => initially signalled */
138 closeMutex (Mutex* pMut)
145 newThreadLocalKey (ThreadLocalKey *key)
149 if (r == TLS_OUT_OF_INDEXES) {
150 barf("newThreadLocalKey: out of keys");
156 getThreadLocalVar (ThreadLocalKey *key)
159 r = TlsGetValue(*key);
161 // r is allowed to be NULL - it can mean that either there was an
162 // error or the stored value is in fact NULL.
163 if (GetLastError() != NO_ERROR) {
164 barf("getThreadLocalVar: key not found");
171 setThreadLocalVar (ThreadLocalKey *key, void *value)
174 b = TlsSetValue(*key, value);
176 barf("setThreadLocalVar: %d", GetLastError());
181 static unsigned __stdcall
182 forkOS_createThreadWrapper ( void * entry )
186 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
192 forkOS_createThread ( HsStablePtr entry )
195 return (_beginthreadex ( NULL, /* default security attributes */
197 forkOS_createThreadWrapper,
200 (unsigned*)&pId) == 0);
203 #else /* !defined(THREADED_RTS) */
206 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
211 #endif /* !defined(THREADED_RTS) */