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