fix doc example typo
[boost.git] / boost / asio / detail / posix_signal_blocker.hpp
blobf8234fb411e9d01088e3d16ddb1c76f64307ccbc
1 //
2 // posix_signal_blocker.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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 //
11 #ifndef BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
12 #define BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/config.hpp>
22 #include <boost/asio/detail/pop_options.hpp>
24 #if defined(BOOST_HAS_PTHREADS)
26 #include <boost/asio/detail/push_options.hpp>
27 #include <csignal>
28 #include <pthread.h>
29 #include <signal.h>
30 #include <boost/asio/detail/pop_options.hpp>
32 #include <boost/asio/detail/noncopyable.hpp>
34 namespace boost {
35 namespace asio {
36 namespace detail {
38 class posix_signal_blocker
39 : private noncopyable
41 public:
42 // Constructor blocks all signals for the calling thread.
43 posix_signal_blocker()
44 : blocked_(false)
46 sigset_t new_mask;
47 sigfillset(&new_mask);
48 blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
51 // Destructor restores the previous signal mask.
52 ~posix_signal_blocker()
54 if (blocked_)
55 pthread_sigmask(SIG_SETMASK, &old_mask_, 0);
58 // Block all signals for the calling thread.
59 void block()
61 if (!blocked_)
63 sigset_t new_mask;
64 sigfillset(&new_mask);
65 blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
69 // Restore the previous signal mask.
70 void unblock()
72 if (blocked_)
73 blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0);
76 private:
77 // Have signals been blocked.
78 bool blocked_;
80 // The previous signal mask.
81 sigset_t old_mask_;
84 } // namespace detail
85 } // namespace asio
86 } // namespace boost
88 #endif // defined(BOOST_HAS_PTHREADS)
90 #include <boost/asio/detail/pop_options.hpp>
92 #endif // BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP