signal_set_service.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // detail/signal_set_service.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 BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #include <cstddef>
  17. #include <signal.h>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/execution_context.hpp>
  20. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  21. #include <boost/asio/detail/memory.hpp>
  22. #include <boost/asio/detail/op_queue.hpp>
  23. #include <boost/asio/detail/signal_handler.hpp>
  24. #include <boost/asio/detail/signal_op.hpp>
  25. #include <boost/asio/detail/socket_types.hpp>
  26. #if defined(BOOST_ASIO_HAS_IOCP)
  27. # include <boost/asio/detail/win_iocp_io_context.hpp>
  28. #else // defined(BOOST_ASIO_HAS_IOCP)
  29. # include <boost/asio/detail/scheduler.hpp>
  30. #endif // defined(BOOST_ASIO_HAS_IOCP)
  31. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  32. # include <boost/asio/detail/reactor.hpp>
  33. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  34. #include <boost/asio/detail/push_options.hpp>
  35. namespace boost {
  36. namespace asio {
  37. namespace detail {
  38. #if defined(NSIG) && (NSIG > 0)
  39. enum { max_signal_number = NSIG };
  40. #else
  41. enum { max_signal_number = 128 };
  42. #endif
  43. extern BOOST_ASIO_DECL struct signal_state* get_signal_state();
  44. extern "C" BOOST_ASIO_DECL void boost_asio_signal_handler(int signal_number);
  45. class signal_set_service :
  46. public execution_context_service_base<signal_set_service>
  47. {
  48. public:
  49. // Type used for tracking an individual signal registration.
  50. class registration
  51. {
  52. public:
  53. // Default constructor.
  54. registration()
  55. : signal_number_(0),
  56. queue_(0),
  57. undelivered_(0),
  58. next_in_table_(0),
  59. prev_in_table_(0),
  60. next_in_set_(0)
  61. {
  62. }
  63. private:
  64. // Only this service will have access to the internal values.
  65. friend class signal_set_service;
  66. // The signal number that is registered.
  67. int signal_number_;
  68. // The waiting signal handlers.
  69. op_queue<signal_op>* queue_;
  70. // The number of undelivered signals.
  71. std::size_t undelivered_;
  72. // Pointers to adjacent registrations in the registrations_ table.
  73. registration* next_in_table_;
  74. registration* prev_in_table_;
  75. // Link to next registration in the signal set.
  76. registration* next_in_set_;
  77. };
  78. // The implementation type of the signal_set.
  79. class implementation_type
  80. {
  81. public:
  82. // Default constructor.
  83. implementation_type()
  84. : signals_(0)
  85. {
  86. }
  87. private:
  88. // Only this service will have access to the internal values.
  89. friend class signal_set_service;
  90. // The pending signal handlers.
  91. op_queue<signal_op> queue_;
  92. // Linked list of registered signals.
  93. registration* signals_;
  94. };
  95. // Constructor.
  96. BOOST_ASIO_DECL signal_set_service(execution_context& context);
  97. // Destructor.
  98. BOOST_ASIO_DECL ~signal_set_service();
  99. // Destroy all user-defined handler objects owned by the service.
  100. BOOST_ASIO_DECL void shutdown();
  101. // Perform fork-related housekeeping.
  102. BOOST_ASIO_DECL void notify_fork(
  103. boost::asio::execution_context::fork_event fork_ev);
  104. // Construct a new signal_set implementation.
  105. BOOST_ASIO_DECL void construct(implementation_type& impl);
  106. // Destroy a signal_set implementation.
  107. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  108. // Add a signal to a signal_set.
  109. BOOST_ASIO_DECL boost::system::error_code add(implementation_type& impl,
  110. int signal_number, boost::system::error_code& ec);
  111. // Remove a signal to a signal_set.
  112. BOOST_ASIO_DECL boost::system::error_code remove(implementation_type& impl,
  113. int signal_number, boost::system::error_code& ec);
  114. // Remove all signals from a signal_set.
  115. BOOST_ASIO_DECL boost::system::error_code clear(implementation_type& impl,
  116. boost::system::error_code& ec);
  117. // Cancel all operations associated with the signal set.
  118. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  119. boost::system::error_code& ec);
  120. // Start an asynchronous operation to wait for a signal to be delivered.
  121. template <typename Handler, typename IoExecutor>
  122. void async_wait(implementation_type& impl,
  123. Handler& handler, const IoExecutor& io_ex)
  124. {
  125. // Allocate and construct an operation to wrap the handler.
  126. typedef signal_handler<Handler, IoExecutor> op;
  127. typename op::ptr p = { boost::asio::detail::addressof(handler),
  128. op::ptr::allocate(handler), 0 };
  129. p.p = new (p.v) op(handler, io_ex);
  130. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  131. *p.p, "signal_set", &impl, 0, "async_wait"));
  132. start_wait_op(impl, p.p);
  133. p.v = p.p = 0;
  134. }
  135. // Deliver notification that a particular signal occurred.
  136. BOOST_ASIO_DECL static void deliver_signal(int signal_number);
  137. private:
  138. // Helper function to add a service to the global signal state.
  139. BOOST_ASIO_DECL static void add_service(signal_set_service* service);
  140. // Helper function to remove a service from the global signal state.
  141. BOOST_ASIO_DECL static void remove_service(signal_set_service* service);
  142. // Helper function to create the pipe descriptors.
  143. BOOST_ASIO_DECL static void open_descriptors();
  144. // Helper function to close the pipe descriptors.
  145. BOOST_ASIO_DECL static void close_descriptors();
  146. // Helper function to start a wait operation.
  147. BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
  148. // The scheduler used for dispatching handlers.
  149. #if defined(BOOST_ASIO_HAS_IOCP)
  150. typedef class win_iocp_io_context scheduler_impl;
  151. #else
  152. typedef class scheduler scheduler_impl;
  153. #endif
  154. scheduler_impl& scheduler_;
  155. #if !defined(BOOST_ASIO_WINDOWS) \
  156. && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
  157. && !defined(__CYGWIN__)
  158. // The type used for registering for pipe reactor notifications.
  159. class pipe_read_op;
  160. // The reactor used for waiting for pipe readiness.
  161. reactor& reactor_;
  162. // The per-descriptor reactor data used for the pipe.
  163. reactor::per_descriptor_data reactor_data_;
  164. #endif // !defined(BOOST_ASIO_WINDOWS)
  165. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  166. // && !defined(__CYGWIN__)
  167. // A mapping from signal number to the registered signal sets.
  168. registration* registrations_[max_signal_number];
  169. // Pointers to adjacent services in linked list.
  170. signal_set_service* next_;
  171. signal_set_service* prev_;
  172. };
  173. } // namespace detail
  174. } // namespace asio
  175. } // namespace boost
  176. #include <boost/asio/detail/pop_options.hpp>
  177. #if defined(BOOST_ASIO_HEADER_ONLY)
  178. # include <boost/asio/detail/impl/signal_set_service.ipp>
  179. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  180. #endif // BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP