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 */
104 (unsigned*)pId) == 0);
110 return GetCurrentThreadId();
114 initMutex (Mutex* pMut)
116 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
117 FALSE, /* not owned => initially signalled */
125 newThreadLocalKey (ThreadLocalKey *key)
129 if (r == TLS_OUT_OF_INDEXES) {
130 barf("newThreadLocalKey: out of keys");
136 getThreadLocalVar (ThreadLocalKey *key)
139 r = TlsGetValue(*key);
141 barf("getThreadLocalVar: key not found");
147 setThreadLocalVar (ThreadLocalKey *key, void *value)
150 b = TlsSetValue(*key, value);
152 barf("setThreadLocalVar: %d", GetLastError());
157 static unsigned __stdcall
158 forkOS_createThreadWrapper ( void * entry )
162 rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
168 forkOS_createThread ( HsStablePtr entry )
171 return (_beginthreadex ( NULL, /* default security attributes */
173 forkOS_createThreadWrapper,
176 (unsigned*)&pId) == 0);
179 #endif /* defined(HAVE_PTHREAD_H) */
181 #else /* !defined(THREADED_RTS) */
184 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
189 #endif /* !defined(THREADED_RTS) */