signal_set_service.hpp 8.4 KB

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