$search
00001 // 00002 // posix_signal_blocker.hpp 00003 // ~~~~~~~~~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP 00012 #define ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <boost/config.hpp> 00022 #include "asio/detail/pop_options.hpp" 00023 00024 #if defined(BOOST_HAS_PTHREADS) 00025 00026 #include "asio/detail/push_options.hpp" 00027 #include <csignal> 00028 #include <pthread.h> 00029 #include <signal.h> 00030 #include "asio/detail/pop_options.hpp" 00031 00032 #include "asio/detail/noncopyable.hpp" 00033 00034 namespace asio { 00035 namespace detail { 00036 00037 class posix_signal_blocker 00038 : private noncopyable 00039 { 00040 public: 00041 // Constructor blocks all signals for the calling thread. 00042 posix_signal_blocker() 00043 : blocked_(false) 00044 { 00045 sigset_t new_mask; 00046 sigfillset(&new_mask); 00047 blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0); 00048 } 00049 00050 // Destructor restores the previous signal mask. 00051 ~posix_signal_blocker() 00052 { 00053 if (blocked_) 00054 pthread_sigmask(SIG_SETMASK, &old_mask_, 0); 00055 } 00056 00057 // Block all signals for the calling thread. 00058 void block() 00059 { 00060 if (!blocked_) 00061 { 00062 sigset_t new_mask; 00063 sigfillset(&new_mask); 00064 blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0); 00065 } 00066 } 00067 00068 // Restore the previous signal mask. 00069 void unblock() 00070 { 00071 if (blocked_) 00072 blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0); 00073 } 00074 00075 private: 00076 // Have signals been blocked. 00077 bool blocked_; 00078 00079 // The previous signal mask. 00080 sigset_t old_mask_; 00081 }; 00082 00083 } // namespace detail 00084 } // namespace asio 00085 00086 #endif // defined(BOOST_HAS_PTHREADS) 00087 00088 #include "asio/detail/pop_options.hpp" 00089 00090 #endif // ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP