1 /* ---------------------------------------------------------------------------
3 * (c) The GHC Team, 2001-2005
5 * Accessing OS threads functionality in a (mostly) OS-independent
8 * --------------------------------------------------------------------------*/
10 #define _WIN32_WINNT 0x0500
13 #if defined(THREADED_RTS)
17 /* For reasons not yet clear, the entire contents of process.h is protected
18 * by __STRICT_ANSI__ not being defined.
20 #undef __STRICT_ANSI__
23 /* Win32 threads and synchronisation objects */
25 /* A Condition is represented by a Win32 Event object;
26 * a Mutex by a Mutex kernel object.
28 * ToDo: go through the defn and usage of these to
29 * make sure the semantics match up with that of
30 * the (assumed) pthreads behaviour. This is really
31 * just a first pass at getting something compilable.
35 initCondition( Condition* pCond )
37 HANDLE h = CreateEvent(NULL,
38 FALSE, /* auto reset */
39 FALSE, /* initially not signalled */
40 NULL); /* unnamed => process-local. */
43 sysErrorBelch("initCondition: unable to create");
44 stg_exit(EXIT_FAILURE);
51 closeCondition( Condition* pCond )
53 if ( CloseHandle(*pCond) == 0 ) {
54 sysErrorBelch("closeCondition: failed to close");
60 broadcastCondition ( Condition* pCond )
67 signalCondition ( Condition* pCond )
69 if (SetEvent(*pCond) == 0) {
70 sysErrorBelch("SetEvent");
71 stg_exit(EXIT_FAILURE);
77 waitCondition ( Condition* pCond, Mutex* pMut )
80 WaitForSingleObject(*pCond, INFINITE);
81 /* Hmm..use WaitForMultipleObjects() ? */
97 barf("_endthreadex returned"); // avoid gcc warning
101 createOSThread (OSThreadId* pId, OSThreadProc *startProc, void *param)
104 return (_beginthreadex ( NULL, /* default security attributes */
106 (unsigned (__stdcall *)(void *)) startProc,
109 (unsigned*)pId) == 0);
115 return GetCurrentThreadId();
119 osThreadIsAlive(OSThreadId id)
123 if (!(hdl = OpenThread(THREAD_QUERY_INFORMATION,FALSE,id))) {
124 sysErrorBelch("osThreadIsAlive: OpenThread");
125 stg_exit(EXIT_FAILURE);
127 if (!GetExitCodeThread(hdl, &exit_code)) {
128 sysErrorBelch("osThreadIsAlive: GetExitCodeThread");
129 stg_exit(EXIT_FAILURE);
131 return (exit_code == STILL_ACTIVE);
134 #ifdef USE_CRITICAL_SECTIONS
136 initMutex (Mutex* pMut)
138 InitializeCriticalSectionAndSpinCount(pMut,4000);
141 closeMutex (Mutex* pMut)
143 DeleteCriticalSection(pMut);
147 initMutex (Mutex* pMut)
149 HANDLE h = CreateMutex ( NULL, /* default sec. attributes */
150 FALSE, /* not owned => initially signalled */
157 closeMutex (Mutex* pMut)
164 newThreadLocalKey (ThreadLocalKey *key)
168 if (r == TLS_OUT_OF_INDEXES) {
169 barf("newThreadLocalKey: out of keys");
175 getThreadLocalVar (ThreadLocalKey *key)
178 r = TlsGetValue(*key);
180 // r is allowed to be NULL - it can mean that either there was an
181 // error or the stored value is in fact NULL.
182 if (GetLastError() != NO_ERROR) {
183 sysErrorBelch("getThreadLocalVar");
184 stg_exit(EXIT_FAILURE);
191 setThreadLocalVar (ThreadLocalKey *key, void *value)
194 b = TlsSetValue(*key, value);
196 sysErrorBelch("setThreadLocalVar");
197 stg_exit(EXIT_FAILURE);
202 freeThreadLocalKey (ThreadLocalKey *key)
207 DWORD dw = GetLastError();
208 barf("freeThreadLocalKey failed: %lu", dw);
213 static unsigned __stdcall
214 forkOS_createThreadWrapper ( void * entry )
218 cap = rts_evalStableIO(cap, (HsStablePtr) entry, NULL);
224 forkOS_createThread ( HsStablePtr entry )
227 return (_beginthreadex ( NULL, /* default security attributes */
229 forkOS_createThreadWrapper,
232 (unsigned*)&pId) == 0);
236 getNumberOfProcessors (void)
238 static nat nproc = 0;
243 nproc = si.dwNumberOfProcessors;
250 setThreadAffinity (nat n, nat m) // cap N of M
253 DWORD_PTR mask, r; // 64-bit win is required to handle more than 32 procs
256 hThread = GetCurrentThread();
258 nproc = getNumberOfProcessors();
261 for (i = n; i < nproc; i+=m) {
265 r = SetThreadAffinityMask(hThread, mask);
267 sysErrorBelch("SetThreadAffinity");
268 stg_exit(EXIT_FAILURE);
272 typedef BOOL (WINAPI *PCSIO)(HANDLE);
275 interruptOSThread (OSThreadId id)
279 if (!(hdl = OpenThread(THREAD_TERMINATE,FALSE,id))) {
280 sysErrorBelch("interruptOSThread: OpenThread");
281 stg_exit(EXIT_FAILURE);
283 pCSIO = (PCSIO) GetProcAddress(GetModuleHandle(TEXT("Kernel32.dll")), "CancelSynchronousIo");
284 if ( NULL != pCSIO ) {
287 // Nothing to do, unfortunately
291 #else /* !defined(THREADED_RTS) */
294 forkOS_createThread ( HsStablePtr entry STG_UNUSED )
299 #endif /* !defined(THREADED_RTS) */