1//
2// detail/posix_signal_blocker.hpp
3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4//
5// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6//
7// Distributed under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9//
10
11#ifndef BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
12#define BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
13
14#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15# pragma once
16#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18#include <boost/asio/detail/config.hpp>
19
20#if defined(BOOST_ASIO_HAS_PTHREADS)
21
22#include <csignal>
23#include <pthread.h>
24#include <signal.h>
25#include <boost/asio/detail/noncopyable.hpp>
26
27#include <boost/asio/detail/push_options.hpp>
28
29namespace boost {
30namespace asio {
31namespace detail {
32
33class posix_signal_blocker
34 : private noncopyable
35{
36public:
37 // Constructor blocks all signals for the calling thread.
38 posix_signal_blocker()
39 : blocked_(false)
40 {
41 sigset_t new_mask;
42 sigfillset(set: &new_mask);
43 blocked_ = (pthread_sigmask(SIG_BLOCK, newmask: &new_mask, oldmask: &old_mask_) == 0);
44 }
45
46 // Destructor restores the previous signal mask.
47 ~posix_signal_blocker()
48 {
49 if (blocked_)
50 pthread_sigmask(SIG_SETMASK, newmask: &old_mask_, oldmask: 0);
51 }
52
53 // Block all signals for the calling thread.
54 void block()
55 {
56 if (!blocked_)
57 {
58 sigset_t new_mask;
59 sigfillset(set: &new_mask);
60 blocked_ = (pthread_sigmask(SIG_BLOCK, newmask: &new_mask, oldmask: &old_mask_) == 0);
61 }
62 }
63
64 // Restore the previous signal mask.
65 void unblock()
66 {
67 if (blocked_)
68 blocked_ = (pthread_sigmask(SIG_SETMASK, newmask: &old_mask_, oldmask: 0) != 0);
69 }
70
71private:
72 // Have signals been blocked.
73 bool blocked_;
74
75 // The previous signal mask.
76 sigset_t old_mask_;
77};
78
79} // namespace detail
80} // namespace asio
81} // namespace boost
82
83#include <boost/asio/detail/pop_options.hpp>
84
85#endif // defined(BOOST_ASIO_HAS_PTHREADS)
86
87#endif // BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
88

source code of boost/boost/asio/detail/posix_signal_blocker.hpp