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