Interruptible FFI calls with pthread_kill and CancelSynchronousIO. v4
[ghc-hetmet.git] / rts / win32 / OSThreads.c
index 6f3629b..44db42f 100644 (file)
@@ -11,8 +11,8 @@
 
 #include "Rts.h"
 #if defined(THREADED_RTS)
-#include "OSThreads.h"
 #include "RtsUtils.h"
+#include <windows.h>
 
 /* For reasons not yet clear, the entire contents of process.h is protected 
  * by __STRICT_ANSI__ not being defined.
@@ -86,7 +86,7 @@ waitCondition ( Condition* pCond, Mutex* pMut )
 void
 yieldThread()
 {
-  Sleep(0);
+  SwitchToThread();
   return;
 }
 
@@ -94,6 +94,7 @@ void
 shutdownThread()
 {
   _endthreadex(0);
+  barf("_endthreadex returned"); // avoid gcc warning
 }
 
 int
@@ -197,6 +198,17 @@ setThreadLocalVar (ThreadLocalKey *key, void *value)
     }
 }
 
+void
+freeThreadLocalKey (ThreadLocalKey *key)
+{
+    BOOL r;
+    r = TlsFree(*key);
+    if (r == 0) {
+        DWORD dw = GetLastError();
+       barf("freeThreadLocalKey failed: %lu", dw);
+    }
+}
+
 
 static unsigned __stdcall
 forkOS_createThreadWrapper ( void * entry )
@@ -220,6 +232,62 @@ forkOS_createThread ( HsStablePtr entry )
                           (unsigned*)&pId) == 0);
 }
 
+nat
+getNumberOfProcessors (void)
+{
+    static nat nproc = 0;
+
+    if (nproc == 0) {
+        SYSTEM_INFO si;
+        GetSystemInfo(&si);
+        nproc = si.dwNumberOfProcessors;
+    }
+
+    return nproc;
+}
+
+void
+setThreadAffinity (nat n, nat m) // cap N of M
+{
+    HANDLE hThread;
+    DWORD_PTR mask, r;  // 64-bit win is required to handle more than 32 procs
+    nat nproc, i;
+
+    hThread = GetCurrentThread();
+
+    nproc = getNumberOfProcessors();
+
+    mask = 0;
+    for (i = n; i < nproc; i+=m) {
+        mask |= 1 << i;
+    }
+
+    r = SetThreadAffinityMask(hThread, mask);
+    if (r == 0) {
+       sysErrorBelch("SetThreadAffinity");
+        stg_exit(EXIT_FAILURE);
+    }
+}
+
+typedef BOOL (WINAPI *PCSIO)(HANDLE);
+
+void
+interruptOSThread (OSThreadId id)
+{
+    HANDLE hdl;
+    PCSIO pCSIO;
+    if (!(hdl = OpenThread(THREAD_TERMINATE,FALSE,id))) {
+        sysErrorBelch("interruptOSThread: OpenThread");
+        stg_exit(EXIT_FAILURE);
+    }
+    pCSIO = (PCSIO) GetProcAddress(GetModuleHandle(TEXT("Kernel32.dll")), "CancelSynchronousIo");
+    if ( NULL != pCSIO ) {
+        pCSIO(hdl);
+    } else {
+        // Nothing to do, unfortunately
+    }
+}
+
 #else /* !defined(THREADED_RTS) */
 
 int