signal_set_service.hpp 6.4 KB

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