signal_set_service.hpp 6.4 KB

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