Browse Source

[4065] New threads got all signals blocked

Francis Dupont 9 years ago
parent
commit
720abaa70d
1 changed files with 27 additions and 0 deletions
  1. 27 0
      src/lib/util/threads/thread.cc

+ 27 - 0
src/lib/util/threads/thread.cc

@@ -21,7 +21,9 @@
 #include <cerrno>
 #include <cerrno>
 
 
 #include <pthread.h>
 #include <pthread.h>
+#include <signal.h>
 
 
+#include <boost/noncopyable.hpp>
 #include <boost/scoped_ptr.hpp>
 #include <boost/scoped_ptr.hpp>
 
 
 using std::string;
 using std::string;
@@ -33,6 +35,30 @@ namespace isc {
 namespace util {
 namespace util {
 namespace thread {
 namespace thread {
 
 
+namespace {
+
+// Signal blocker class.
+class Blocker : boost::noncopyable {
+public:
+    // Constructor blocks all signals
+    Blocker() {
+	sigset_t new_mask;
+	sigfillset(&new_mask);
+	pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_);
+    }
+
+    // Destructor restores the previous signal mask
+    ~Blocker() {
+	pthread_sigmask(SIG_SETMASK, &old_mask_, 0);
+    }
+
+private:
+    // The previous signal mask
+    sigset_t old_mask_;
+};
+
+}
+
 // The implementation of the Thread class.
 // The implementation of the Thread class.
 //
 //
 // This internal state is not deleted until the thread terminates and is either
 // This internal state is not deleted until the thread terminates and is either
@@ -105,6 +131,7 @@ Thread::Thread(const boost::function<void ()>& main) :
     impl_(NULL)
     impl_(NULL)
 {
 {
     auto_ptr<Impl> impl(new Impl(main));
     auto_ptr<Impl> impl(new Impl(main));
+    Blocker blocker;
     const int result = pthread_create(&impl->tid_, NULL, &Impl::run,
     const int result = pthread_create(&impl->tid_, NULL, &Impl::run,
                                       impl.get());
                                       impl.get());
     // Any error here?
     // Any error here?