PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/asio/detail/posix_signal_blocker.hpp

http://hadesmem.googlecode.com/
C++ Header | 87 lines | 55 code | 17 blank | 15 comment | 8 complexity | 44a7522a8332744510eab1f0cef8dc82 MD5 | raw file
 1//
 2// detail/posix_signal_blocker.hpp
 3// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 4//
 5// Copyright (c) 2003-2011 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_HAS_PTHREADS) && !defined(BOOST_ASIO_DISABLE_THREADS)
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(&new_mask);
43    blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &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, &old_mask_, 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(&new_mask);
60      blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &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, &old_mask_, 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_HAS_PTHREADS) && !defined(BOOST_ASIO_DISABLE_THREADS)
86
87#endif // BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP