2 // basic_signal_set.hpp
3 // ~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_BASIC_SIGNAL_SET_HPP
12 #define BOOST_ASIO_BASIC_SIGNAL_SET_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
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>
28 #include <boost/asio/detail/push_options.hpp>
33 /// Provides signal functionality.
35 * The basic_signal_set class template provides the ability to perform an
36 * asynchronous wait for one or more signals to occur.
38 * Most applications will use the boost::asio::signal_set typedef.
41 * @e Distinct @e objects: Safe.@n
42 * @e Shared @e objects: Unsafe.
45 * Performing an asynchronous wait:
48 * const boost::system::error_code& error,
53 * // A signal occurred.
59 * // Construct a signal set registered for process termination.
60 * boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
62 * // Start an asynchronous wait for one of the signals to occur.
63 * signals.async_wait(handler);
66 * @par Queueing of signal notifications
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.
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
79 * @par Multiple registration of signals
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.
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().
88 * @par Signal masking on POSIX platforms
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
95 template <typename SignalSetService = signal_set_service>
96 class basic_signal_set
97 : public basic_io_object<SignalSetService>
100 /// Construct a signal set without adding any signals.
102 * This constructor creates a signal set without registering for any signals.
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.
107 explicit basic_signal_set(boost::asio::io_context& io_context)
108 : basic_io_object<SignalSetService>(io_context)
112 /// Construct a signal set and add one signal.
114 * This constructor creates a signal set and registers for one signal.
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.
119 * @param signal_number_1 The signal number to be added.
121 * @note This constructor is equivalent to performing:
122 * @code boost::asio::signal_set signals(io_context);
123 * signals.add(signal_number_1); @endcode
125 basic_signal_set(boost::asio::io_context& io_context, int signal_number_1)
126 : basic_io_object<SignalSetService>(io_context)
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");
133 /// Construct a signal set and add two signals.
135 * This constructor creates a signal set and registers for two signals.
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.
140 * @param signal_number_1 The first signal number to be added.
142 * @param signal_number_2 The second signal number to be added.
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
149 basic_signal_set(boost::asio::io_context& io_context, int signal_number_1,
151 : basic_io_object<SignalSetService>(io_context)
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");
160 /// Construct a signal set and add three signals.
162 * This constructor creates a signal set and registers for three signals.
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.
167 * @param signal_number_1 The first signal number to be added.
169 * @param signal_number_2 The second signal number to be added.
171 * @param signal_number_3 The third signal number to be added.
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
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)
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");
192 /// Add a signal to a signal_set.
194 * This function adds the specified signal to the set. It has no effect if the
195 * signal is already in the set.
197 * @param signal_number The signal to be added to the set.
199 * @throws boost::system::system_error Thrown on failure.
201 void add(int signal_number)
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");
208 /// Add a signal to a signal_set.
210 * This function adds the specified signal to the set. It has no effect if the
211 * signal is already in the set.
213 * @param signal_number The signal to be added to the set.
215 * @param ec Set to indicate what error occurred, if any.
217 BOOST_ASIO_SYNC_OP_VOID add(int signal_number, boost::system::error_code& ec)
219 this->get_service().add(this->get_implementation(), signal_number, ec);
220 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
223 /// Remove a signal from a signal_set.
225 * This function removes the specified signal from the set. It has no effect
226 * if the signal is not in the set.
228 * @param signal_number The signal to be removed from the set.
230 * @throws boost::system::system_error Thrown on failure.
232 * @note Removes any notifications that have been queued for the specified
235 void remove(int signal_number)
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");
242 /// Remove a signal from a signal_set.
244 * This function removes the specified signal from the set. It has no effect
245 * if the signal is not in the set.
247 * @param signal_number The signal to be removed from the set.
249 * @param ec Set to indicate what error occurred, if any.
251 * @note Removes any notifications that have been queued for the specified
254 BOOST_ASIO_SYNC_OP_VOID remove(int signal_number,
255 boost::system::error_code& ec)
257 this->get_service().remove(this->get_implementation(), signal_number, ec);
258 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
261 /// Remove all signals from a signal_set.
263 * This function removes all signals from the set. It has no effect if the set
266 * @throws boost::system::system_error Thrown on failure.
268 * @note Removes all queued notifications.
272 boost::system::error_code ec;
273 this->get_service().clear(this->get_implementation(), ec);
274 boost::asio::detail::throw_error(ec, "clear");
277 /// Remove all signals from a signal_set.
279 * This function removes all signals from the set. It has no effect if the set
282 * @param ec Set to indicate what error occurred, if any.
284 * @note Removes all queued notifications.
286 BOOST_ASIO_SYNC_OP_VOID clear(boost::system::error_code& ec)
288 this->get_service().clear(this->get_implementation(), ec);
289 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
292 /// Cancel all operations associated with the signal set.
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
299 * Cancellation does not alter the set of registered signals.
301 * @throws boost::system::system_error Thrown on failure.
303 * @note If a registered signal occurred before cancel() is called, then the
304 * handlers for asynchronous wait operations will:
306 * @li have already been invoked; or
308 * @li have been queued for invocation in the near future.
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.
315 boost::system::error_code ec;
316 this->get_service().cancel(this->get_implementation(), ec);
317 boost::asio::detail::throw_error(ec, "cancel");
320 /// Cancel all operations associated with the signal set.
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
327 * Cancellation does not alter the set of registered signals.
329 * @param ec Set to indicate what error occurred, if any.
331 * @note If a registered signal occurred before cancel() is called, then the
332 * handlers for asynchronous wait operations will:
334 * @li have already been invoked; or
336 * @li have been queued for invocation in the near future.
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.
341 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
343 this->get_service().cancel(this->get_implementation(), ec);
344 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
347 /// Start an asynchronous operation to wait for a signal to be delivered.
349 * This function may be used to initiate an asynchronous wait against the
350 * signal set. It always returns immediately.
352 * For each call to async_wait(), the supplied handler will be called exactly
353 * once. The handler will be called when:
355 * @li One of the registered signals in the signal set occurs; or
357 * @li The signal set was cancelled, in which case the handler is passed the
358 * error code boost::asio::error::operation_aborted.
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
363 * @code void handler(
364 * const boost::system::error_code& error, // Result of operation.
365 * int signal_number // Indicates which signal occurred.
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().
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)
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;
381 return this->get_service().async_wait(this->get_implementation(),
382 BOOST_ASIO_MOVE_CAST(SignalHandler)(handler));
389 #include <boost/asio/detail/pop_options.hpp>
391 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
393 #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP