Realistic 3D camera system
3D camera system components
posix_signal_blocker.hpp
Go to the documentation of this file.
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 ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
12 #define 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 "asio/detail/config.hpp"
19 
20 #if defined(ASIO_HAS_PTHREADS)
21 
22 #include <csignal>
23 #include <pthread.h>
24 #include <signal.h>
26 
28 
29 namespace asio {
30 namespace detail {
31 
32 class posix_signal_blocker
33  : private noncopyable
34 {
35 public:
36  // Constructor blocks all signals for the calling thread.
37  posix_signal_blocker()
38  : blocked_(false)
39  {
40  sigset_t new_mask;
41  sigfillset(&new_mask);
42  blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
43  }
44 
45  // Destructor restores the previous signal mask.
46  ~posix_signal_blocker()
47  {
48  if (blocked_)
49  pthread_sigmask(SIG_SETMASK, &old_mask_, 0);
50  }
51 
52  // Block all signals for the calling thread.
53  void block()
54  {
55  if (!blocked_)
56  {
57  sigset_t new_mask;
58  sigfillset(&new_mask);
59  blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
60  }
61  }
62 
63  // Restore the previous signal mask.
64  void unblock()
65  {
66  if (blocked_)
67  blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0);
68  }
69 
70 private:
71  // Have signals been blocked.
72  bool blocked_;
73 
74  // The previous signal mask.
75  sigset_t old_mask_;
76 };
77 
78 } // namespace detail
79 } // namespace asio
80 
82 
83 #endif // defined(ASIO_HAS_PTHREADS)
84 
85 #endif // ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP