Windows build fixes
[ghc-hetmet.git] / rts / win32 / ConsoleHandler.c
index cc0365b..19057a3 100644 (file)
@@ -5,10 +5,8 @@
 #include "Rts.h"
 #include <windows.h>
 #include "ConsoleHandler.h"
-#include "SchedAPI.h"
 #include "Schedule.h"
 #include "RtsUtils.h"
-#include "RtsFlags.h"
 #include "AsyncIO.h"
 #include "RtsSignals.h"
 
@@ -18,7 +16,9 @@ static BOOL WINAPI shutdown_handler(DWORD dwCtrlType);
 static BOOL WINAPI generic_handler(DWORD dwCtrlType);
 
 static rtsBool deliver_event = rtsTrue;
-static StgInt console_handler = STG_SIG_DFL;
+StgInt console_handler = STG_SIG_DFL;
+
+#if !defined(THREADED_RTS)
 
 static HANDLE hConsoleEvent = INVALID_HANDLE_VALUE;
 
@@ -26,6 +26,8 @@ static HANDLE hConsoleEvent = INVALID_HANDLE_VALUE;
 StgInt stg_pending_events = 0;           /* number of undelivered events */
 DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */
 
+#endif
+
 /*
  * Function: initUserSignals()
  *
@@ -34,8 +36,9 @@ DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */
 void
 initUserSignals(void)
 {
-    stg_pending_events = 0;
     console_handler = STG_SIG_DFL;
+#if !defined (THREADED_RTS)
+    stg_pending_events = 0;
     if (hConsoleEvent == INVALID_HANDLE_VALUE) {
        hConsoleEvent = 
            CreateEvent ( NULL,  /* default security attributes */
@@ -43,9 +46,26 @@ initUserSignals(void)
                          FALSE, /* initially non-signalled */
                          NULL); /* no name */
     }
+#endif
     return;
 }
 
+void
+freeSignalHandlers(void) {
+    /* Do nothing */
+}
+
+/* Seems to be a bit of an orphan...where used? */
+void
+finiUserSignals(void)
+{
+#if !defined (THREADED_RTS)
+    if (hConsoleEvent != INVALID_HANDLE_VALUE) {
+        CloseHandle(hConsoleEvent);
+    }
+#endif
+}
+
 /*
  * Function: shutdown_handler()
  *
@@ -74,9 +94,6 @@ static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
            stg_exit(EXIT_INTERRUPTED);
        } else {
            interruptStgRts();
-           /* Cheesy pulsing of an event to wake up a waiting RTS thread, if any */
-           abandonRequestWait();
-           resetAbandonRequestWait();
        }
        return TRUE;
 
@@ -100,6 +117,12 @@ void initDefaultHandlers(void)
     }
 }
 
+void resetDefaultHandlers(void)
+{
+    if ( !SetConsoleCtrlHandler(shutdown_handler, FALSE) ) {
+       errorBelch("warning: failed to uninstall default console handler");
+    }
+}
 
 /*
  * Function: blockUserSignals()
@@ -139,6 +162,7 @@ void awaitUserSignals(void)
 }
 
 
+#if !defined (THREADED_RTS)
 /*
  * Function: startSignalHandlers()
  *
@@ -171,6 +195,7 @@ void startSignalHandlers(Capability *cap)
     RELEASE_LOCK(&sched_mutex);
     unblockUserSignals();
 }
+#endif /* !THREADED_RTS */
 
 /*
  * Function: markSignalHandlers()
@@ -178,7 +203,7 @@ void startSignalHandlers(Capability *cap)
  * Evacuate the handler stack. _Assumes_ that console event delivery
  * has already been blocked.
  */
-void markSignalHandlers (evac_fn evac)
+void markSignalHandlers (evac_fn evac STG_UNUSED, void *user STG_UNUSED)
 {
     // nothing to mark; the console handler is a StablePtr which is
     // already treated as a root by the GC.
@@ -193,8 +218,6 @@ void markSignalHandlers (evac_fn evac)
  */
 static BOOL WINAPI generic_handler(DWORD dwCtrlType)
 {
-    ACQUIRE_LOCK(&sched_mutex);
-
     /* Ultra-simple -- up the counter + signal a switch. */
     switch(dwCtrlType) {
     case CTRL_CLOSE_EVENT:
@@ -208,17 +231,19 @@ static BOOL WINAPI generic_handler(DWORD dwCtrlType)
     default:
        if (!deliver_event) return TRUE;
 
+#if defined(THREADED_RTS)
+        sendIOManagerEvent((StgWord8) ((dwCtrlType<<1) | 1));
+#else
        if ( stg_pending_events < N_PENDING_EVENTS ) {
            stg_pending_buf[stg_pending_events] = dwCtrlType;
            stg_pending_events++;
        }
-       /* Cheesy pulsing of an event to wake up a waiting RTS thread, if any */
-       abandonRequestWait();
-       resetAbandonRequestWait();
+
+        // we need to wake up awaitEvent()
+        abandonRequestWait();
+#endif
        return TRUE;
     }
-
-    RELEASE_LOCK(&sched_mutex);
 }
 
 
@@ -246,8 +271,13 @@ rts_InstallConsoleEvent(int action, StgStablePtr *handler)
        }
        break;
     case STG_SIG_HAN:
+#ifdef THREADED_RTS
+        // handler is stored in an MVar in the threaded RTS
+       console_handler = STG_SIG_HAN;
+#else
        console_handler = (StgInt)*handler;
-       if ( previous_hdlr < 0 ) {
+#endif
+       if (previous_hdlr < 0 || previous_hdlr == STG_SIG_HAN) {
          /* Only install generic_handler() once */
          if ( !SetConsoleCtrlHandler(generic_handler, TRUE) ) {
            errorBelch("warning: unable to install console event handler");
@@ -257,10 +287,13 @@ rts_InstallConsoleEvent(int action, StgStablePtr *handler)
     }
     
     if (previous_hdlr == STG_SIG_DFL || 
-       previous_hdlr == STG_SIG_IGN) {
+       previous_hdlr == STG_SIG_IGN ||
+        previous_hdlr == STG_SIG_HAN) {
        return previous_hdlr;
     } else {
-       *handler = (StgStablePtr)previous_hdlr;
+       if (handler != NULL) {
+            *handler = (StgStablePtr)previous_hdlr;
+        }
        return STG_SIG_HAN;
     }
 }
@@ -284,17 +317,22 @@ rts_InstallConsoleEvent(int action, StgStablePtr *handler)
  *
  */
 void
-rts_ConsoleHandlerDone(int ev)
+rts_ConsoleHandlerDone (int ev USED_IF_NOT_THREADS)
 {
+#if !defined(THREADED_RTS)
     if ( (DWORD)ev == CTRL_BREAK_EVENT ||
         (DWORD)ev == CTRL_C_EVENT ) {
        /* only these two cause stdin system calls to abort.. */
        SetEvent(hConsoleEvent); /* event is manual-reset */
        Sleep(0); /* yield */
        ResetEvent(hConsoleEvent); /* turn it back off again */
+        // SDM: yeuch, this can't possibly work reliably.
+        // I'm not having it in THREADED_RTS.
     }
+#endif
 }
 
+#if !defined(THREADED_RTS)
 /*
  * Function: rts_waitConsoleHandlerCompletion()
  *
@@ -309,3 +347,4 @@ rts_waitConsoleHandlerCompletion()
      */
     return (WaitForSingleObject(hConsoleEvent, INFINITE) == WAIT_OBJECT_0);
 }
+#endif