Chris@16
|
1 //
|
Chris@16
|
2 // basic_signal_set.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_BASIC_SIGNAL_SET_HPP
|
Chris@16
|
12 #define BOOST_ASIO_BASIC_SIGNAL_SET_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 <boost/asio/basic_io_object.hpp>
|
Chris@16
|
21 #include <boost/asio/detail/handler_type_requirements.hpp>
|
Chris@16
|
22 #include <boost/asio/detail/throw_error.hpp>
|
Chris@16
|
23 #include <boost/asio/error.hpp>
|
Chris@16
|
24 #include <boost/asio/signal_set_service.hpp>
|
Chris@16
|
25
|
Chris@16
|
26 #include <boost/asio/detail/push_options.hpp>
|
Chris@16
|
27
|
Chris@16
|
28 namespace boost {
|
Chris@16
|
29 namespace asio {
|
Chris@16
|
30
|
Chris@16
|
31 /// Provides signal functionality.
|
Chris@16
|
32 /**
|
Chris@16
|
33 * The basic_signal_set class template provides the ability to perform an
|
Chris@16
|
34 * asynchronous wait for one or more signals to occur.
|
Chris@16
|
35 *
|
Chris@16
|
36 * Most applications will use the boost::asio::signal_set typedef.
|
Chris@16
|
37 *
|
Chris@16
|
38 * @par Thread Safety
|
Chris@16
|
39 * @e Distinct @e objects: Safe.@n
|
Chris@16
|
40 * @e Shared @e objects: Unsafe.
|
Chris@16
|
41 *
|
Chris@16
|
42 * @par Example
|
Chris@16
|
43 * Performing an asynchronous wait:
|
Chris@16
|
44 * @code
|
Chris@16
|
45 * void handler(
|
Chris@16
|
46 * const boost::system::error_code& error,
|
Chris@16
|
47 * int signal_number)
|
Chris@16
|
48 * {
|
Chris@16
|
49 * if (!error)
|
Chris@16
|
50 * {
|
Chris@16
|
51 * // A signal occurred.
|
Chris@16
|
52 * }
|
Chris@16
|
53 * }
|
Chris@16
|
54 *
|
Chris@16
|
55 * ...
|
Chris@16
|
56 *
|
Chris@16
|
57 * // Construct a signal set registered for process termination.
|
Chris@16
|
58 * boost::asio::signal_set signals(io_service, SIGINT, SIGTERM);
|
Chris@16
|
59 *
|
Chris@16
|
60 * // Start an asynchronous wait for one of the signals to occur.
|
Chris@16
|
61 * signals.async_wait(handler);
|
Chris@16
|
62 * @endcode
|
Chris@16
|
63 *
|
Chris@16
|
64 * @par Queueing of signal notifications
|
Chris@16
|
65 *
|
Chris@16
|
66 * If a signal is registered with a signal_set, and the signal occurs when
|
Chris@16
|
67 * there are no waiting handlers, then the signal notification is queued. The
|
Chris@16
|
68 * next async_wait operation on that signal_set will dequeue the notification.
|
Chris@16
|
69 * If multiple notifications are queued, subsequent async_wait operations
|
Chris@16
|
70 * dequeue them one at a time. Signal notifications are dequeued in order of
|
Chris@16
|
71 * ascending signal number.
|
Chris@16
|
72 *
|
Chris@16
|
73 * If a signal number is removed from a signal_set (using the @c remove or @c
|
Chris@16
|
74 * erase member functions) then any queued notifications for that signal are
|
Chris@16
|
75 * discarded.
|
Chris@16
|
76 *
|
Chris@16
|
77 * @par Multiple registration of signals
|
Chris@16
|
78 *
|
Chris@16
|
79 * The same signal number may be registered with different signal_set objects.
|
Chris@16
|
80 * When the signal occurs, one handler is called for each signal_set object.
|
Chris@16
|
81 *
|
Chris@16
|
82 * Note that multiple registration only works for signals that are registered
|
Chris@16
|
83 * using Asio. The application must not also register a signal handler using
|
Chris@16
|
84 * functions such as @c signal() or @c sigaction().
|
Chris@16
|
85 *
|
Chris@16
|
86 * @par Signal masking on POSIX platforms
|
Chris@16
|
87 *
|
Chris@16
|
88 * POSIX allows signals to be blocked using functions such as @c sigprocmask()
|
Chris@16
|
89 * and @c pthread_sigmask(). For signals to be delivered, programs must ensure
|
Chris@16
|
90 * that any signals registered using signal_set objects are unblocked in at
|
Chris@16
|
91 * least one thread.
|
Chris@16
|
92 */
|
Chris@16
|
93 template <typename SignalSetService = signal_set_service>
|
Chris@16
|
94 class basic_signal_set
|
Chris@16
|
95 : public basic_io_object<SignalSetService>
|
Chris@16
|
96 {
|
Chris@16
|
97 public:
|
Chris@16
|
98 /// Construct a signal set without adding any signals.
|
Chris@16
|
99 /**
|
Chris@16
|
100 * This constructor creates a signal set without registering for any signals.
|
Chris@16
|
101 *
|
Chris@16
|
102 * @param io_service The io_service object that the signal set will use to
|
Chris@16
|
103 * dispatch handlers for any asynchronous operations performed on the set.
|
Chris@16
|
104 */
|
Chris@16
|
105 explicit basic_signal_set(boost::asio::io_service& io_service)
|
Chris@16
|
106 : basic_io_object<SignalSetService>(io_service)
|
Chris@16
|
107 {
|
Chris@16
|
108 }
|
Chris@16
|
109
|
Chris@16
|
110 /// Construct a signal set and add one signal.
|
Chris@16
|
111 /**
|
Chris@16
|
112 * This constructor creates a signal set and registers for one signal.
|
Chris@16
|
113 *
|
Chris@16
|
114 * @param io_service The io_service object that the signal set will use to
|
Chris@16
|
115 * dispatch handlers for any asynchronous operations performed on the set.
|
Chris@16
|
116 *
|
Chris@16
|
117 * @param signal_number_1 The signal number to be added.
|
Chris@16
|
118 *
|
Chris@16
|
119 * @note This constructor is equivalent to performing:
|
Chris@16
|
120 * @code boost::asio::signal_set signals(io_service);
|
Chris@16
|
121 * signals.add(signal_number_1); @endcode
|
Chris@16
|
122 */
|
Chris@16
|
123 basic_signal_set(boost::asio::io_service& io_service, int signal_number_1)
|
Chris@16
|
124 : basic_io_object<SignalSetService>(io_service)
|
Chris@16
|
125 {
|
Chris@16
|
126 boost::system::error_code ec;
|
Chris@16
|
127 this->service.add(this->implementation, signal_number_1, ec);
|
Chris@16
|
128 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
129 }
|
Chris@16
|
130
|
Chris@16
|
131 /// Construct a signal set and add two signals.
|
Chris@16
|
132 /**
|
Chris@16
|
133 * This constructor creates a signal set and registers for two signals.
|
Chris@16
|
134 *
|
Chris@16
|
135 * @param io_service The io_service object that the signal set will use to
|
Chris@16
|
136 * dispatch handlers for any asynchronous operations performed on the set.
|
Chris@16
|
137 *
|
Chris@16
|
138 * @param signal_number_1 The first signal number to be added.
|
Chris@16
|
139 *
|
Chris@16
|
140 * @param signal_number_2 The second signal number to be added.
|
Chris@16
|
141 *
|
Chris@16
|
142 * @note This constructor is equivalent to performing:
|
Chris@16
|
143 * @code boost::asio::signal_set signals(io_service);
|
Chris@16
|
144 * signals.add(signal_number_1);
|
Chris@16
|
145 * signals.add(signal_number_2); @endcode
|
Chris@16
|
146 */
|
Chris@16
|
147 basic_signal_set(boost::asio::io_service& io_service, int signal_number_1,
|
Chris@16
|
148 int signal_number_2)
|
Chris@16
|
149 : basic_io_object<SignalSetService>(io_service)
|
Chris@16
|
150 {
|
Chris@16
|
151 boost::system::error_code ec;
|
Chris@16
|
152 this->service.add(this->implementation, signal_number_1, ec);
|
Chris@16
|
153 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
154 this->service.add(this->implementation, signal_number_2, ec);
|
Chris@16
|
155 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
156 }
|
Chris@16
|
157
|
Chris@16
|
158 /// Construct a signal set and add three signals.
|
Chris@16
|
159 /**
|
Chris@16
|
160 * This constructor creates a signal set and registers for three signals.
|
Chris@16
|
161 *
|
Chris@16
|
162 * @param io_service The io_service object that the signal set will use to
|
Chris@16
|
163 * dispatch handlers for any asynchronous operations performed on the set.
|
Chris@16
|
164 *
|
Chris@16
|
165 * @param signal_number_1 The first signal number to be added.
|
Chris@16
|
166 *
|
Chris@16
|
167 * @param signal_number_2 The second signal number to be added.
|
Chris@16
|
168 *
|
Chris@16
|
169 * @param signal_number_3 The third signal number to be added.
|
Chris@16
|
170 *
|
Chris@16
|
171 * @note This constructor is equivalent to performing:
|
Chris@16
|
172 * @code boost::asio::signal_set signals(io_service);
|
Chris@16
|
173 * signals.add(signal_number_1);
|
Chris@16
|
174 * signals.add(signal_number_2);
|
Chris@16
|
175 * signals.add(signal_number_3); @endcode
|
Chris@16
|
176 */
|
Chris@16
|
177 basic_signal_set(boost::asio::io_service& io_service, int signal_number_1,
|
Chris@16
|
178 int signal_number_2, int signal_number_3)
|
Chris@16
|
179 : basic_io_object<SignalSetService>(io_service)
|
Chris@16
|
180 {
|
Chris@16
|
181 boost::system::error_code ec;
|
Chris@16
|
182 this->service.add(this->implementation, signal_number_1, ec);
|
Chris@16
|
183 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
184 this->service.add(this->implementation, signal_number_2, ec);
|
Chris@16
|
185 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
186 this->service.add(this->implementation, signal_number_3, ec);
|
Chris@16
|
187 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
188 }
|
Chris@16
|
189
|
Chris@16
|
190 /// Add a signal to a signal_set.
|
Chris@16
|
191 /**
|
Chris@16
|
192 * This function adds the specified signal to the set. It has no effect if the
|
Chris@16
|
193 * signal is already in the set.
|
Chris@16
|
194 *
|
Chris@16
|
195 * @param signal_number The signal to be added to the set.
|
Chris@16
|
196 *
|
Chris@16
|
197 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
198 */
|
Chris@16
|
199 void add(int signal_number)
|
Chris@16
|
200 {
|
Chris@16
|
201 boost::system::error_code ec;
|
Chris@16
|
202 this->service.add(this->implementation, signal_number, ec);
|
Chris@16
|
203 boost::asio::detail::throw_error(ec, "add");
|
Chris@16
|
204 }
|
Chris@16
|
205
|
Chris@16
|
206 /// Add a signal to a signal_set.
|
Chris@16
|
207 /**
|
Chris@16
|
208 * This function adds the specified signal to the set. It has no effect if the
|
Chris@16
|
209 * signal is already in the set.
|
Chris@16
|
210 *
|
Chris@16
|
211 * @param signal_number The signal to be added to the set.
|
Chris@16
|
212 *
|
Chris@16
|
213 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
214 */
|
Chris@16
|
215 boost::system::error_code add(int signal_number,
|
Chris@16
|
216 boost::system::error_code& ec)
|
Chris@16
|
217 {
|
Chris@16
|
218 return this->service.add(this->implementation, signal_number, ec);
|
Chris@16
|
219 }
|
Chris@16
|
220
|
Chris@16
|
221 /// Remove a signal from a signal_set.
|
Chris@16
|
222 /**
|
Chris@16
|
223 * This function removes the specified signal from the set. It has no effect
|
Chris@16
|
224 * if the signal is not in the set.
|
Chris@16
|
225 *
|
Chris@16
|
226 * @param signal_number The signal to be removed from the set.
|
Chris@16
|
227 *
|
Chris@16
|
228 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
229 *
|
Chris@16
|
230 * @note Removes any notifications that have been queued for the specified
|
Chris@16
|
231 * signal number.
|
Chris@16
|
232 */
|
Chris@16
|
233 void remove(int signal_number)
|
Chris@16
|
234 {
|
Chris@16
|
235 boost::system::error_code ec;
|
Chris@16
|
236 this->service.remove(this->implementation, signal_number, ec);
|
Chris@16
|
237 boost::asio::detail::throw_error(ec, "remove");
|
Chris@16
|
238 }
|
Chris@16
|
239
|
Chris@16
|
240 /// Remove a signal from a signal_set.
|
Chris@16
|
241 /**
|
Chris@16
|
242 * This function removes the specified signal from the set. It has no effect
|
Chris@16
|
243 * if the signal is not in the set.
|
Chris@16
|
244 *
|
Chris@16
|
245 * @param signal_number The signal to be removed from the set.
|
Chris@16
|
246 *
|
Chris@16
|
247 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
248 *
|
Chris@16
|
249 * @note Removes any notifications that have been queued for the specified
|
Chris@16
|
250 * signal number.
|
Chris@16
|
251 */
|
Chris@16
|
252 boost::system::error_code remove(int signal_number,
|
Chris@16
|
253 boost::system::error_code& ec)
|
Chris@16
|
254 {
|
Chris@16
|
255 return this->service.remove(this->implementation, signal_number, ec);
|
Chris@16
|
256 }
|
Chris@16
|
257
|
Chris@16
|
258 /// Remove all signals from a signal_set.
|
Chris@16
|
259 /**
|
Chris@16
|
260 * This function removes all signals from the set. It has no effect if the set
|
Chris@16
|
261 * is already empty.
|
Chris@16
|
262 *
|
Chris@16
|
263 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
264 *
|
Chris@16
|
265 * @note Removes all queued notifications.
|
Chris@16
|
266 */
|
Chris@16
|
267 void clear()
|
Chris@16
|
268 {
|
Chris@16
|
269 boost::system::error_code ec;
|
Chris@16
|
270 this->service.clear(this->implementation, ec);
|
Chris@16
|
271 boost::asio::detail::throw_error(ec, "clear");
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 /// Remove all signals from a signal_set.
|
Chris@16
|
275 /**
|
Chris@16
|
276 * This function removes all signals from the set. It has no effect if the set
|
Chris@16
|
277 * is already empty.
|
Chris@16
|
278 *
|
Chris@16
|
279 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
280 *
|
Chris@16
|
281 * @note Removes all queued notifications.
|
Chris@16
|
282 */
|
Chris@16
|
283 boost::system::error_code clear(boost::system::error_code& ec)
|
Chris@16
|
284 {
|
Chris@16
|
285 return this->service.clear(this->implementation, ec);
|
Chris@16
|
286 }
|
Chris@16
|
287
|
Chris@16
|
288 /// Cancel all operations associated with the signal set.
|
Chris@16
|
289 /**
|
Chris@16
|
290 * This function forces the completion of any pending asynchronous wait
|
Chris@16
|
291 * operations against the signal set. The handler for each cancelled
|
Chris@16
|
292 * operation will be invoked with the boost::asio::error::operation_aborted
|
Chris@16
|
293 * error code.
|
Chris@16
|
294 *
|
Chris@16
|
295 * Cancellation does not alter the set of registered signals.
|
Chris@16
|
296 *
|
Chris@16
|
297 * @throws boost::system::system_error Thrown on failure.
|
Chris@16
|
298 *
|
Chris@16
|
299 * @note If a registered signal occurred before cancel() is called, then the
|
Chris@16
|
300 * handlers for asynchronous wait operations will:
|
Chris@16
|
301 *
|
Chris@16
|
302 * @li have already been invoked; or
|
Chris@16
|
303 *
|
Chris@16
|
304 * @li have been queued for invocation in the near future.
|
Chris@16
|
305 *
|
Chris@16
|
306 * These handlers can no longer be cancelled, and therefore are passed an
|
Chris@16
|
307 * error code that indicates the successful completion of the wait operation.
|
Chris@16
|
308 */
|
Chris@16
|
309 void cancel()
|
Chris@16
|
310 {
|
Chris@16
|
311 boost::system::error_code ec;
|
Chris@16
|
312 this->service.cancel(this->implementation, ec);
|
Chris@16
|
313 boost::asio::detail::throw_error(ec, "cancel");
|
Chris@16
|
314 }
|
Chris@16
|
315
|
Chris@16
|
316 /// Cancel all operations associated with the signal set.
|
Chris@16
|
317 /**
|
Chris@16
|
318 * This function forces the completion of any pending asynchronous wait
|
Chris@16
|
319 * operations against the signal set. The handler for each cancelled
|
Chris@16
|
320 * operation will be invoked with the boost::asio::error::operation_aborted
|
Chris@16
|
321 * error code.
|
Chris@16
|
322 *
|
Chris@16
|
323 * Cancellation does not alter the set of registered signals.
|
Chris@16
|
324 *
|
Chris@16
|
325 * @param ec Set to indicate what error occurred, if any.
|
Chris@16
|
326 *
|
Chris@16
|
327 * @note If a registered signal occurred before cancel() is called, then the
|
Chris@16
|
328 * handlers for asynchronous wait operations will:
|
Chris@16
|
329 *
|
Chris@16
|
330 * @li have already been invoked; or
|
Chris@16
|
331 *
|
Chris@16
|
332 * @li have been queued for invocation in the near future.
|
Chris@16
|
333 *
|
Chris@16
|
334 * These handlers can no longer be cancelled, and therefore are passed an
|
Chris@16
|
335 * error code that indicates the successful completion of the wait operation.
|
Chris@16
|
336 */
|
Chris@16
|
337 boost::system::error_code cancel(boost::system::error_code& ec)
|
Chris@16
|
338 {
|
Chris@16
|
339 return this->service.cancel(this->implementation, ec);
|
Chris@16
|
340 }
|
Chris@16
|
341
|
Chris@16
|
342 /// Start an asynchronous operation to wait for a signal to be delivered.
|
Chris@16
|
343 /**
|
Chris@16
|
344 * This function may be used to initiate an asynchronous wait against the
|
Chris@16
|
345 * signal set. It always returns immediately.
|
Chris@16
|
346 *
|
Chris@16
|
347 * For each call to async_wait(), the supplied handler will be called exactly
|
Chris@16
|
348 * once. The handler will be called when:
|
Chris@16
|
349 *
|
Chris@16
|
350 * @li One of the registered signals in the signal set occurs; or
|
Chris@16
|
351 *
|
Chris@16
|
352 * @li The signal set was cancelled, in which case the handler is passed the
|
Chris@16
|
353 * error code boost::asio::error::operation_aborted.
|
Chris@16
|
354 *
|
Chris@16
|
355 * @param handler The handler to be called when the signal occurs. Copies
|
Chris@16
|
356 * will be made of the handler as required. The function signature of the
|
Chris@16
|
357 * handler must be:
|
Chris@16
|
358 * @code void handler(
|
Chris@16
|
359 * const boost::system::error_code& error, // Result of operation.
|
Chris@16
|
360 * int signal_number // Indicates which signal occurred.
|
Chris@16
|
361 * ); @endcode
|
Chris@16
|
362 * Regardless of whether the asynchronous operation completes immediately or
|
Chris@16
|
363 * not, the handler will not be invoked from within this function. Invocation
|
Chris@16
|
364 * of the handler will be performed in a manner equivalent to using
|
Chris@16
|
365 * boost::asio::io_service::post().
|
Chris@16
|
366 */
|
Chris@16
|
367 template <typename SignalHandler>
|
Chris@16
|
368 BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler,
|
Chris@16
|
369 void (boost::system::error_code, int))
|
Chris@16
|
370 async_wait(BOOST_ASIO_MOVE_ARG(SignalHandler) handler)
|
Chris@16
|
371 {
|
Chris@16
|
372 // If you get an error on the following line it means that your handler does
|
Chris@16
|
373 // not meet the documented type requirements for a SignalHandler.
|
Chris@16
|
374 BOOST_ASIO_SIGNAL_HANDLER_CHECK(SignalHandler, handler) type_check;
|
Chris@16
|
375
|
Chris@16
|
376 return this->service.async_wait(this->implementation,
|
Chris@16
|
377 BOOST_ASIO_MOVE_CAST(SignalHandler)(handler));
|
Chris@16
|
378 }
|
Chris@16
|
379 };
|
Chris@16
|
380
|
Chris@16
|
381 } // namespace asio
|
Chris@16
|
382 } // namespace boost
|
Chris@16
|
383
|
Chris@16
|
384 #include <boost/asio/detail/pop_options.hpp>
|
Chris@16
|
385
|
Chris@16
|
386 #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP
|