2 // detail/signal_set_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
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>
31 #if !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
32 # include <boost/asio/detail/reactor.hpp>
33 #endif // !defined(BOOST_ASIO_WINDOWS) && !defined(__CYGWIN__)
35 #include <boost/asio/detail/push_options.hpp>
41 #if defined(NSIG) && (NSIG > 0)
42 enum { max_signal_number = NSIG };
44 enum { max_signal_number = 128 };
47 extern BOOST_ASIO_DECL struct signal_state* get_signal_state();
49 extern "C" BOOST_ASIO_DECL void boost_asio_signal_handler(int signal_number);
51 class signal_set_service
54 // Type used for tracking an individual signal registration.
58 // Default constructor.
70 // Only this service will have access to the internal values.
71 friend class signal_set_service;
73 // The signal number that is registered.
76 // The waiting signal handlers.
77 op_queue<signal_op>* queue_;
79 // The number of undelivered signals.
80 std::size_t undelivered_;
82 // Pointers to adjacent registrations in the registrations_ table.
83 registration* next_in_table_;
84 registration* prev_in_table_;
86 // Link to next registration in the signal set.
87 registration* next_in_set_;
90 // The implementation type of the signal_set.
91 class implementation_type
94 // Default constructor.
101 // Only this service will have access to the internal values.
102 friend class signal_set_service;
104 // The pending signal handlers.
105 op_queue<signal_op> queue_;
107 // Linked list of registered signals.
108 registration* signals_;
112 BOOST_ASIO_DECL signal_set_service(boost::asio::io_service& io_service);
115 BOOST_ASIO_DECL ~signal_set_service();
117 // Destroy all user-defined handler objects owned by the service.
118 BOOST_ASIO_DECL void shutdown_service();
120 // Perform fork-related housekeeping.
121 BOOST_ASIO_DECL void fork_service(
122 boost::asio::io_service::fork_event fork_ev);
124 // Construct a new signal_set implementation.
125 BOOST_ASIO_DECL void construct(implementation_type& impl);
127 // Destroy a signal_set implementation.
128 BOOST_ASIO_DECL void destroy(implementation_type& impl);
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);
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);
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);
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);
146 // Start an asynchronous operation to wait for a signal to be delivered.
147 template <typename Handler>
148 void async_wait(implementation_type& impl, Handler& handler)
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);
157 BOOST_ASIO_HANDLER_CREATION((p.p, "signal_set", &impl, "async_wait"));
159 start_wait_op(impl, p.p);
163 // Deliver notification that a particular signal occurred.
164 BOOST_ASIO_DECL static void deliver_signal(int signal_number);
167 // Helper function to add a service to the global signal state.
168 BOOST_ASIO_DECL static void add_service(signal_set_service* service);
170 // Helper function to remove a service from the global signal state.
171 BOOST_ASIO_DECL static void remove_service(signal_set_service* service);
173 // Helper function to create the pipe descriptors.
174 BOOST_ASIO_DECL static void open_descriptors();
176 // Helper function to close the pipe descriptors.
177 BOOST_ASIO_DECL static void close_descriptors();
179 // Helper function to start a wait operation.
180 BOOST_ASIO_DECL void start_wait_op(implementation_type& impl, signal_op* op);
182 // The io_service instance used for dispatching handlers.
183 io_service_impl& io_service_;
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.
191 // The reactor used for waiting for pipe readiness.
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__)
200 // A mapping from signal number to the registered signal sets.
201 registration* registrations_[max_signal_number];
203 // Pointers to adjacent services in linked list.
204 signal_set_service* next_;
205 signal_set_service* prev_;
208 } // namespace detail
212 #include <boost/asio/detail/pop_options.hpp>
214 #if defined(BOOST_ASIO_HEADER_ONLY)
215 # include <boost/asio/detail/impl/signal_set_service.ipp>
216 #endif // defined(BOOST_ASIO_HEADER_ONLY)
218 #endif // BOOST_ASIO_DETAIL_SIGNAL_SET_SERVICE_HPP