Remove a redundant options pragma
[ghc-hetmet.git] / rts / RtsStartup.c
index c55fdfb..fd84000 100644 (file)
@@ -71,6 +71,9 @@
 #ifdef HAVE_SIGNAL_H
 #include <signal.h>
 #endif
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 #if USE_PAPI
 #include "Papi.h"
@@ -222,6 +225,12 @@ hs_init(int *argc, char **argv[])
     /* initialise the stable pointer table */
     initStablePtrTable();
 
+    /* Add some GC roots (using stable pointers): these are needed by
+     * all foreign export stubs, so they have to be treated as 
+     */
+    getStablePtr((StgPtr)base_GHCziTopHandler_runIO_closure);
+    getStablePtr((StgPtr)base_GHCziTopHandler_runNonIO_closure);
+
     /* initialise the shared Typeable store */
     initTypeableStore();
 
@@ -383,6 +392,8 @@ hs_exit_(rtsBool wait_foreign)
     /* start timing the shutdown */
     stat_startExit();
     
+    OnExitHook();
+
 #if defined(RTS_USER_SIGNALS)
     if (RtsFlags.MiscFlags.install_signal_handlers) {
         freeSignalHandlers();
@@ -440,6 +451,9 @@ hs_exit_(rtsBool wait_foreign)
     PAR_TICKY_PAR_END();
 #endif
 
+    // uninstall signal handlers
+    resetDefaultHandlers();
+
     /* stop timing the shutdown, we're about to print stats */
     stat_endExit();
     
@@ -528,10 +542,10 @@ shutdownHaskell(void)
 void
 shutdownHaskellAndExit(int n)
 {
-    if (hs_init_count == 1) {
-       OnExitHook();
-       hs_exit_(rtsFalse);
-        // we're about to exit(), no need to wait for foreign calls to return.
+    // we're about to exit(), no need to wait for foreign calls to return.
+    hs_exit_(rtsFalse);
+
+    if (hs_init_count == 0) {
 #if defined(PAR)
        /* really exit (stg_exit() would call shutdownParallelSystem() again) */
        exit(n);
@@ -541,6 +555,15 @@ shutdownHaskellAndExit(int n)
     }
 }
 
+#ifndef mingw32_HOST_OS
+void
+shutdownHaskellAndSignal(int sig)
+{
+    hs_exit_(rtsFalse);
+    kill(getpid(),sig);
+}
+#endif
+
 /* 
  * called from STG-land to exit the program
  */