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