Repository : ssh://darcs.haskell.org//srv/darcs/ghc

On branch  : ghc-7.4

http://hackage.haskell.org/trac/ghc/changeset/c6b8bcbf0de5c50ea7d95bb811543799fbd44434

>---------------------------------------------------------------

commit c6b8bcbf0de5c50ea7d95bb811543799fbd44434
Author: Ian Lynagh <[email protected]>
Date:   Sun Dec 18 13:10:24 2011 +0000

    MERGED: Add a mutex around stg_sig_install
    
    commit 8792391edd7e274670627aadd689dd23cf061f99
    Author: Simon Marlow <[email protected]>
    Date:   Mon Dec 12 09:29:56 2011 +0000
    
        Add a mutex around stg_sig_install
    
        Protects against a race when two threads call installHandler
        simultaneously.  This was causing occasional failure of the test
        libraries/process/tests/3231(threaded2).

>---------------------------------------------------------------

 rts/posix/Signals.c |   65 +++++++++++++++++++++++++++++++++------------------
 1 files changed, 42 insertions(+), 23 deletions(-)

diff --git a/rts/posix/Signals.c b/rts/posix/Signals.c
index 38c9792..ae6e278 100644
--- a/rts/posix/Signals.c
+++ b/rts/posix/Signals.c
@@ -65,6 +65,39 @@ static StgInt nHandlers = 0;    /* Size of handlers array */
 
 static nat n_haskell_handlers = 0;
 
+static sigset_t userSignals;
+static sigset_t savedSignals;
+
+#ifdef THREADED_RTS
+static Mutex sig_mutex; // protects signal_handlers, nHandlers
+#endif
+
+/* 
-----------------------------------------------------------------------------
+ * Initialisation / deinitialisation
+ * -------------------------------------------------------------------------- 
*/
+
+void
+initUserSignals(void)
+{
+    sigemptyset(&userSignals);
+#ifdef THREADED_RTS
+    initMutex(&sig_mutex);
+#endif
+}
+
+void
+freeSignalHandlers(void) {
+    if (signal_handlers != NULL) {
+        stgFree(signal_handlers);
+        signal_handlers = NULL;
+        nHandlers = 0;
+        n_haskell_handlers = 0;
+    }
+#ifdef THREADED_RTS
+    closeMutex(&sig_mutex);
+#endif
+}
+
 /* 
-----------------------------------------------------------------------------
  * Allocate/resize the table of signal handlers.
  * -------------------------------------------------------------------------- 
*/
@@ -260,19 +293,6 @@ generic_handler(int sig USED_IF_THREADS,
  * Blocking/Unblocking of the user signals
  * -------------------------------------------------------------------------- 
*/
 
-static sigset_t userSignals;
-static sigset_t savedSignals;
-
-void
-initUserSignals(void)
-{
-    sigemptyset(&userSignals);
-#ifndef THREADED_RTS
-    getStablePtr((StgPtr)&base_GHCziConcziSignal_runHandlers_closure);
-    // needed to keep runHandler alive
-#endif
-}
-
 void
 blockUserSignals(void)
 {
@@ -316,11 +336,14 @@ stg_sig_install(int sig, int spi, void *mask)
     struct sigaction action;
     StgInt previous_spi;
 
+    ACQUIRE_LOCK(&sig_mutex);
+
     // Block the signal until we figure out what to do
     // Count on this to fail if the signal number is invalid
     if (sig < 0 || sigemptyset(&signals) ||
        sigaddset(&signals, sig) || sigprocmask(SIG_BLOCK, &signals, 
&osignals)) {
-       return STG_SIG_ERR;
+        RELEASE_LOCK(&sig_mutex);
+        return STG_SIG_ERR;
     }
     
     more_handlers(sig);
@@ -360,7 +383,8 @@ stg_sig_install(int sig, int spi, void *mask)
     if (sigaction(sig, &action, NULL))
     {
         errorBelch("sigaction");
-       return STG_SIG_ERR;
+        RELEASE_LOCK(&sig_mutex);
+        return STG_SIG_ERR;
     }
 
     signal_handlers[sig] = spi;
@@ -385,9 +409,11 @@ stg_sig_install(int sig, int spi, void *mask)
     if (sigprocmask(SIG_SETMASK, &osignals, NULL))
     {
         errorBelch("sigprocmask");
-       return STG_SIG_ERR;
+        RELEASE_LOCK(&sig_mutex);
+        return STG_SIG_ERR;
     }
 
+    RELEASE_LOCK(&sig_mutex);
     return previous_spi;
 }
 
@@ -640,11 +666,4 @@ resetDefaultHandlers(void)
     set_sigtstp_action(rtsFalse);
 }
 
-void
-freeSignalHandlers(void) {
-    if (signal_handlers != NULL) {
-        stgFree(signal_handlers);
-    }
-}
-
 #endif /* RTS_USER_SIGNALS */



_______________________________________________
Cvs-ghc mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/cvs-ghc

Reply via email to