Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:09

0001 //
0002 // detail/posix_signal_blocker.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
0012 #define BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 
0020 #if defined(BOOST_ASIO_HAS_PTHREADS)
0021 
0022 #include <csignal>
0023 #include <pthread.h>
0024 #include <signal.h>
0025 #include <boost/asio/detail/noncopyable.hpp>
0026 
0027 #include <boost/asio/detail/push_options.hpp>
0028 
0029 namespace boost {
0030 namespace asio {
0031 namespace detail {
0032 
0033 class posix_signal_blocker
0034   : private noncopyable
0035 {
0036 public:
0037   // Constructor blocks all signals for the calling thread.
0038   posix_signal_blocker()
0039     : blocked_(false)
0040   {
0041     sigset_t new_mask;
0042     sigfillset(&new_mask);
0043     blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
0044   }
0045 
0046   // Destructor restores the previous signal mask.
0047   ~posix_signal_blocker()
0048   {
0049     if (blocked_)
0050       pthread_sigmask(SIG_SETMASK, &old_mask_, 0);
0051   }
0052 
0053   // Block all signals for the calling thread.
0054   void block()
0055   {
0056     if (!blocked_)
0057     {
0058       sigset_t new_mask;
0059       sigfillset(&new_mask);
0060       blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
0061     }
0062   }
0063 
0064   // Restore the previous signal mask.
0065   void unblock()
0066   {
0067     if (blocked_)
0068       blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0);
0069   }
0070 
0071 private:
0072   // Have signals been blocked.
0073   bool blocked_;
0074 
0075   // The previous signal mask.
0076   sigset_t old_mask_;
0077 };
0078 
0079 } // namespace detail
0080 } // namespace asio
0081 } // namespace boost
0082 
0083 #include <boost/asio/detail/pop_options.hpp>
0084 
0085 #endif // defined(BOOST_ASIO_HAS_PTHREADS)
0086 
0087 #endif // BOOST_ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP