notice ^C exceptions when waiting for I/O
[ghc-hetmet.git] / rts / win32 / ConsoleHandler.c
index afaa424..13011cb 100644 (file)
@@ -18,7 +18,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 +28,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 +38,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,16 +48,24 @@ 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
 }
 
 /*
@@ -83,9 +96,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;
 
@@ -109,6 +119,12 @@ void initDefaultHandlers(void)
     }
 }
 
+void resetDefaultHandlers(void)
+{
+    if ( !SetConsoleCtrlHandler(shutdown_handler, FALSE) ) {
+       errorBelch("warning: failed to uninstall default console handler");
+    }
+}
 
 /*
  * Function: blockUserSignals()
@@ -148,6 +164,7 @@ void awaitUserSignals(void)
 }
 
 
+#if !defined (THREADED_RTS)
 /*
  * Function: startSignalHandlers()
  *
@@ -180,6 +197,7 @@ void startSignalHandlers(Capability *cap)
     RELEASE_LOCK(&sched_mutex);
     unblockUserSignals();
 }
+#endif /* !THREADED_RTS */
 
 /*
  * Function: markSignalHandlers()
@@ -187,7 +205,7 @@ void startSignalHandlers(Capability *cap)
  * Evacuate the handler stack. _Assumes_ that console event delivery
  * has already been blocked.
  */
-void markSignalHandlers (evac_fn evac STG_UNUSED)
+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.
@@ -202,8 +220,6 @@ void markSignalHandlers (evac_fn evac STG_UNUSED)
  */
 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:
@@ -217,17 +233,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);
 }
 
 
@@ -255,8 +273,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");
@@ -266,10 +289,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;
     }
 }
@@ -293,17 +319,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()
  *
@@ -318,3 +349,4 @@ rts_waitConsoleHandlerCompletion()
      */
     return (WaitForSingleObject(hConsoleEvent, INFINITE) == WAIT_OBJECT_0);
 }
+#endif