Windows build fixes
[ghc-hetmet.git] / rts / win32 / AsyncIO.c
index 7bcf571..8662e2a 100644 (file)
@@ -4,12 +4,14 @@
  *
  * (c) sof, 2002-2003.
  */
+
+#if !defined(THREADED_RTS)
+
 #include "Rts.h"
 #include "RtsUtils.h"
 #include <windows.h>
 #include <stdio.h>
 #include "Schedule.h"
-#include "RtsFlags.h"
 #include "Capability.h"
 #include "win32/AsyncIO.h"
 #include "win32/IOManager.h"
@@ -45,8 +47,8 @@ typedef struct CompletedReq {
 #define MAX_REQUESTS 200
 
 static CRITICAL_SECTION queue_lock;
-static HANDLE           completed_req_event;
-static HANDLE           abandon_req_wait;
+static HANDLE           completed_req_event = INVALID_HANDLE_VALUE;
+static HANDLE           abandon_req_wait = INVALID_HANDLE_VALUE;
 static HANDLE           wait_handles[2];
 static CompletedReq     completedTable[MAX_REQUESTS];
 static int              completed_hw;
@@ -161,7 +163,7 @@ startupAsyncIO()
     completed_hw = 0;
     if ( !(completed_table_sema = CreateSemaphore (NULL, MAX_REQUESTS, MAX_REQUESTS, NULL)) ) {
        DWORD rc = GetLastError();
-       fprintf(stderr, "startupAsyncIO: CreateSemaphore failed 0x%x\n", rc);
+       fprintf(stderr, "startupAsyncIO: CreateSemaphore failed 0x%x\n", (int)rc);
        fflush(stderr);
     }
 
@@ -171,10 +173,22 @@ startupAsyncIO()
 }
 
 void
-shutdownAsyncIO()
+shutdownAsyncIO(rtsBool wait_threads)
 {
-    CloseHandle(completed_req_event);
-    ShutdownIOManager();
+    ShutdownIOManager(wait_threads);
+    if (completed_req_event != INVALID_HANDLE_VALUE) {
+        CloseHandle(completed_req_event);
+       completed_req_event = INVALID_HANDLE_VALUE;
+    }
+    if (abandon_req_wait != INVALID_HANDLE_VALUE) {
+        CloseHandle(abandon_req_wait);
+       abandon_req_wait = INVALID_HANDLE_VALUE;
+    }
+    if (completed_table_sema != NULL) {
+        CloseHandle(completed_table_sema);
+       completed_table_sema = NULL;
+    }
+    DeleteCriticalSection(&queue_lock);
 }
 
 /*
@@ -260,8 +274,21 @@ start:
            unsigned int rID = completedTable[i].reqID;
            
            prev = NULL;
-           for(tso = blocked_queue_hd ; tso != END_TSO_QUEUE; prev = tso, tso = tso->link) {
+           for(tso = blocked_queue_hd ; tso != END_TSO_QUEUE; tso = tso->_link) {
        
+                if (tso->what_next == ThreadRelocated) {
+                    /* Drop the TSO from blocked_queue */
+                    if (prev) {
+                        setTSOLink(&MainCapability, prev, tso->_link);
+                    } else {
+                        blocked_queue_hd = tso->_link;
+                    }
+                    if (blocked_queue_tl == tso) {
+                        blocked_queue_tl = prev ? prev : END_TSO_QUEUE;
+                    }
+                    continue;
+                }
+
                switch(tso->why_blocked) {
                case BlockedOnRead:
                case BlockedOnWrite:
@@ -275,16 +302,16 @@ start:
                        
                        /* Drop the matched TSO from blocked_queue */
                        if (prev) {
-                           prev->link = tso->link;
+                           setTSOLink(&MainCapability, prev, tso->_link);
                        } else {
-                           blocked_queue_hd = tso->link;
+                           blocked_queue_hd = tso->_link;
                        }
                        if (blocked_queue_tl == tso) {
                            blocked_queue_tl = prev ? prev : END_TSO_QUEUE;
                        }
                    
                        /* Terminates the run queue + this inner for-loop. */
-                       tso->link = END_TSO_QUEUE;
+                       tso->_link = END_TSO_QUEUE;
                        tso->why_blocked = NotBlocked;
                        pushOnRunQueue(&MainCapability, tso);
                        break;
@@ -296,11 +323,13 @@ start:
                    }
                    break;
                }
+
+                prev = tso;
            }
            /* Signal that there's completed table slots available */
            if ( !ReleaseSemaphore(completed_table_sema, 1, NULL) ) {
                DWORD dw = GetLastError();
-               fprintf(stderr, "awaitRequests: failed to signal semaphore (error code=0x%x)\n", dw);
+               fprintf(stderr, "awaitRequests: failed to signal semaphore (error code=0x%x)\n", (int)dw);
                fflush(stderr);
            }
        }
@@ -343,3 +372,4 @@ resetAbandonRequestWait( void )
     ResetEvent(abandon_req_wait);
 }
 
+#endif /* !defined(THREADED_RTS) */