signal_set_service.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. //
  2. // detail/signal_set_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/associated_cancellation_slot.hpp>
  19. #include <boost/asio/cancellation_type.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/execution_context.hpp>
  22. #include <boost/asio/signal_set_base.hpp>
  23. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  24. #include <boost/asio/detail/memory.hpp>
  25. #include <boost/asio/detail/op_queue.hpp>
  26. #include <boost/asio/detail/signal_handler.hpp>
  27. #include <boost/asio/detail/signal_op.hpp>
  28. #include <boost/asio/detail/socket_types.hpp>
  29. #if defined(BOOST_ASIO_HAS_IOCP)
  30. # include <boost/asio/detail/win_iocp_io_context.hpp>
  31. #else // defined(BOOST_ASIO_HAS_IOCP)
  32. # include <boost/asio/detail/scheduler.hpp>
  33. #endif // defined(BOOST_ASIO_HAS_IOCP)
  34. #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  35. # if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  36. # include <boost/asio/detail/io_uring_service.hpp>
  37. # else // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  38. # include <boost/asio/detail/reactor.hpp>
  39. # endif // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  40. #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
  41. #include <boost/asio/detail/push_options.hpp>
  42. namespace boost {
  43. namespace asio {
  44. namespace detail {
  45. #if defined(NSIG) && (NSIG > 0)
  46. enum { max_signal_number = NSIG };
  47. #else
  48. enum { max_signal_number = 128 };
  49. #endif
  50. extern BOOST_ASIO_DECL struct signal_state* get_signal_state();
  51. extern "C" BOOST_ASIO_DECL void boost_asio_signal_handler(int signal_number);
  52. class signal_set_service :
  53. public execution_context_service_base<signal_set_service>
  54. {
  55. public:
  56. // Type used for tracking an individual signal registration.
  57. class registration
  58. {
  59. public:
  60. // Default constructor.
  61. registration()
  62. : signal_number_(0),
  63. queue_(0),
  64. undelivered_(0),
  65. next_in_table_(0),
  66. prev_in_table_(0),
  67. next_in_set_(0)
  68. {
  69. }
  70. private:
  71. // Only this service will have access to the internal values.
  72. friend class signal_set_service;
  73. // The signal number that is registered.
  74. int signal_number_;
  75. // The waiting signal handlers.
  76. op_queue<signal_op>* queue_;
  77. // The number of undelivered signals.
  78. std::size_t undelivered_;
  79. // Pointers to adjacent registrations in the registrations_ table.
  80. registration* next_in_table_;
  81. registration* prev_in_table_;
  82. // Link to next registration in the signal set.
  83. registration* next_in_set_;
  84. };
  85. // The implementation type of the signal_set.
  86. class implementation_type
  87. {
  88. public:
  89. // Default constructor.
  90. implementation_type()
  91. : signals_(0)
  92. {
  93. }
  94. private:
  95. // Only this service will have access to the internal values.
  96. friend class signal_set_service;
  97. // The pending signal handlers.
  98. op_queue<signal_op> queue_;
  99. // Linked list of registered signals.
  100. registration* signals_;
  101. };
  102. // Constructor.
  103. BOOST_ASIO_DECL signal_set_service(execution_context& context);
  104. // Destructor.
  105. BOOST_ASIO_DECL ~signal_set_service();
  106. // Destroy all user-defined handler objects owned by the service.
  107. BOOST_ASIO_DECL void shutdown();
  108. // Perform fork-related housekeeping.
  109. BOOST_ASIO_DECL void notify_fork(
  110. boost::asio::execution_context::fork_event fork_ev);
  111. // Construct a new signal_set implementation.
  112. BOOST_ASIO_DECL void construct(implementation_type& impl);
  113. // Destroy a signal_set implementation.
  114. BOOST_ASIO_DECL void destroy(implementation_type& impl);
  115. // Add a signal to a signal_set.
  116. boost::system::error_code add(implementation_type& impl,
  117. int signal_number, boost::system::error_code& ec)
  118. {
  119. return add(impl, signal_number, signal_set_base::flags::dont_care, ec);
  120. }
  121. // Add a signal to a signal_set with the specified flags.
  122. BOOST_ASIO_DECL boost::system::error_code add(implementation_type& impl,
  123. int signal_number, signal_set_base::flags_t f,
  124. boost::system::error_code& ec);
  125. // Remove a signal to a signal_set.
  126. BOOST_ASIO_DECL boost::system::error_code remove(implementation_type& impl,
  127. int signal_number, boost::system::error_code& ec);
  128. // Remove all signals from a signal_set.
  129. BOOST_ASIO_DECL boost::system::error_code clear(implementation_type& impl,
  130. boost::system::error_code& ec);
  131. // Cancel all operations associated with the signal set.
  132. BOOST_ASIO_DECL boost::system::error_code cancel(implementation_type& impl,
  133. boost::system::error_code& ec);
  134. // Cancel a specific operation associated with the signal set.
  135. BOOST_ASIO_DECL void cancel_ops_by_key(implementation_type& impl,
  136. void* cancellation_key);
  137. // Start an asynchronous operation to wait for a signal to be delivered.
  138. template <typename Handler, typename IoExecutor>
  139. void async_wait(implementation_type& impl,
  140. Handler& handler, const IoExecutor& io_ex)
  141. {
  142. associated_cancellation_slot_t<Handler> slot
  143. = boost::asio::get_associated_cancellation_slot(handler);
  144. // Allocate and construct an operation to wrap the handler.
  145. typedef signal_handler<Handler, IoExecutor> op;
  146. typename op::ptr p = { boost::asio::detail::addressof(handler),
  147. op::ptr::allocate(handler), 0 };
  148. p.p = new (p.v) op(handler, io_ex);
  149. // Optionally register for per-operation cancellation.
  150. if (slot.is_connected())
  151. {
  152. p.p->cancellation_key_ =
  153. &slot.template emplace<signal_op_cancellation>(this, &impl);
  154. }
  155. BOOST_ASIO_HANDLER_CREATION((scheduler_.context(),
  156. *p.p, "signal_set", &impl, 0, "async_wait"));
  157. start_wait_op(impl, p.p);
  158. p.v = p.p = 0;
  159. }
  160. // Deliver notification that a particular signal occurred.
  161. BOOST_ASIO_DECL static void deliver_signal(int signal_number);
  162. private:
  163. // Helper function to add a service to the global signal state.
  164. BOOST_ASIO_DECL static void add_service(signal_set_service* service);
  165. // Helper function to remove a service from the global signal state.
  166. BOOST_ASIO_DECL static void remove_service(signal_set_service* service);
  167. // Helper function to create the pipe descriptors.
  168. BOOST_ASIO_DECL static void open_descriptors();
  169. // Helper function to close the pipe descriptors.
  170. BOOST_ASIO_DECL static void close_descriptors();
  171. // Helper function to start a wait operation.
  172. BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
  173. // Helper class used to implement per-operation cancellation
  174. class signal_op_cancellation
  175. {
  176. public:
  177. signal_op_cancellation(signal_set_service* s, implementation_type* i)
  178. : service_(s),
  179. implementation_(i)
  180. {
  181. }
  182. void operator()(cancellation_type_t type)
  183. {
  184. if (!!(type &
  185. (cancellation_type::terminal
  186. | cancellation_type::partial
  187. | cancellation_type::total)))
  188. {
  189. service_->cancel_ops_by_key(*implementation_, this);
  190. }
  191. }
  192. private:
  193. signal_set_service* service_;
  194. implementation_type* implementation_;
  195. };
  196. // The scheduler used for dispatching handlers.
  197. #if defined(BOOST_ASIO_HAS_IOCP)
  198. typedef class win_iocp_io_context scheduler_impl;
  199. #else
  200. typedef class scheduler scheduler_impl;
  201. #endif
  202. scheduler_impl& scheduler_;
  203. #if !defined(BOOST_ASIO_WINDOWS) \
  204. && !defined(BOOST_ASIO_WINDOWS_RUNTIME) \
  205. && !defined(__CYGWIN__)
  206. // The type used for processing pipe readiness notifications.
  207. class pipe_read_op;
  208. # if defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  209. // The io_uring service used for waiting for pipe readiness.
  210. io_uring_service& io_uring_service_;
  211. // The per I/O object data used for the pipe.
  212. io_uring_service::per_io_object_data io_object_data_;
  213. # else // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  214. // The reactor used for waiting for pipe readiness.
  215. reactor& reactor_;
  216. // The per-descriptor reactor data used for the pipe.
  217. reactor::per_descriptor_data reactor_data_;
  218. # endif // defined(BOOST_ASIO_HAS_IO_URING_AS_DEFAULT)
  219. #endif // !defined(BOOST_ASIO_WINDOWS)
  220. // && !defined(BOOST_ASIO_WINDOWS_RUNTIME)
  221. // && !defined(__CYGWIN__)
  222. // A mapping from signal number to the registered signal sets.
  223. registration* registrations_[max_signal_number];
  224. // Pointers to adjacent services in linked list.
  225. signal_set_service* next_;
  226. signal_set_service* prev_;
  227. };
  228. } // namespace detail
  229. } // namespace asio
  230. } // namespace boost
  231. #include <boost/asio/detail/pop_options.hpp>
  232. #if defined(BOOST_ASIO_HEADER_ONLY)
  233. # include <boost/asio/detail/impl/signal_set_service.ipp>
  234. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  235. #endif // BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP