Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

posix_signal_blocker.hpp 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //
  2. // detail/posix_signal_blocker.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
  11. #define ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_PTHREADS)
  17. #include <csignal>
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. class posix_signal_blocker
  25. : private noncopyable
  26. {
  27. public:
  28. // Constructor blocks all signals for the calling thread.
  29. posix_signal_blocker()
  30. : blocked_(false)
  31. {
  32. sigset_t new_mask;
  33. sigfillset(&new_mask);
  34. blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
  35. }
  36. // Destructor restores the previous signal mask.
  37. ~posix_signal_blocker()
  38. {
  39. if (blocked_)
  40. pthread_sigmask(SIG_SETMASK, &old_mask_, 0);
  41. }
  42. // Block all signals for the calling thread.
  43. void block()
  44. {
  45. if (!blocked_)
  46. {
  47. sigset_t new_mask;
  48. sigfillset(&new_mask);
  49. blocked_ = (pthread_sigmask(SIG_BLOCK, &new_mask, &old_mask_) == 0);
  50. }
  51. }
  52. // Restore the previous signal mask.
  53. void unblock()
  54. {
  55. if (blocked_)
  56. blocked_ = (pthread_sigmask(SIG_SETMASK, &old_mask_, 0) != 0);
  57. }
  58. private:
  59. // Have signals been blocked.
  60. bool blocked_;
  61. // The previous signal mask.
  62. sigset_t old_mask_;
  63. };
  64. } // namespace detail
  65. } // namespace asio
  66. #include "asio/detail/pop_options.hpp"
  67. #endif // defined(ASIO_HAS_PTHREADS)
  68. #endif // ASIO_DETAIL_POSIX_SIGNAL_BLOCKER_HPP