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