Chris@16: // Chris@16: // detail/posix_signal_blocker.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP Chris@16: #define BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_PTHREADS) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: class posix_signal_blocker Chris@16: : private noncopyable Chris@16: { Chris@16: public: Chris@16: // Constructor blocks all signals for the calling thread. Chris@16: posix_signal_blocker() Chris@16: : blocked_(false) Chris@16: { Chris@16: sigset_t new_mask; Chris@16: sigfillset(&new_mask); Chris@16: blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0); Chris@16: } Chris@16: Chris@16: // Destructor restores the previous signal mask. Chris@16: ~posix_signal_blocker() Chris@16: { Chris@16: if (blocked_) Chris@16: pthread_sigmask(SIG_SETMASK, &old_mask_, 0); Chris@16: } Chris@16: Chris@16: // Block all signals for the calling thread. Chris@16: void block() Chris@16: { Chris@16: if (!blocked_) Chris@16: { Chris@16: sigset_t new_mask; Chris@16: sigfillset(&new_mask); Chris@16: blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0); Chris@16: } Chris@16: } Chris@16: Chris@16: // Restore the previous signal mask. Chris@16: void unblock() Chris@16: { Chris@16: if (blocked_) Chris@16: blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0); Chris@16: } Chris@16: Chris@16: private: Chris@16: // Have signals been blocked. Chris@16: bool blocked_; Chris@16: Chris@16: // The previous signal mask. Chris@16: sigset_t old_mask_; Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // defined(BOOST_ASIO_HAS_PTHREADS) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP