Chris@16: // Chris@16: // basic_signal_set.hpp Chris@16: // ~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_BASIC_SIGNAL_SET_HPP Chris@16: #define BOOST_ASIO_BASIC_SIGNAL_SET_HPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: Chris@16: /// Provides signal functionality. Chris@16: /** Chris@16: * The basic_signal_set class template provides the ability to perform an Chris@16: * asynchronous wait for one or more signals to occur. Chris@16: * Chris@16: * Most applications will use the boost::asio::signal_set typedef. Chris@16: * Chris@16: * @par Thread Safety Chris@16: * @e Distinct @e objects: Safe.@n Chris@16: * @e Shared @e objects: Unsafe. Chris@16: * Chris@16: * @par Example Chris@16: * Performing an asynchronous wait: Chris@16: * @code Chris@16: * void handler( Chris@16: * const boost::system::error_code& error, Chris@16: * int signal_number) Chris@16: * { Chris@16: * if (!error) Chris@16: * { Chris@16: * // A signal occurred. Chris@16: * } Chris@16: * } Chris@16: * Chris@16: * ... Chris@16: * Chris@16: * // Construct a signal set registered for process termination. Chris@16: * boost::asio::signal_set signals(io_service, SIGINT, SIGTERM); Chris@16: * Chris@16: * // Start an asynchronous wait for one of the signals to occur. Chris@16: * signals.async_wait(handler); Chris@16: * @endcode Chris@16: * Chris@16: * @par Queueing of signal notifications Chris@16: * Chris@16: * If a signal is registered with a signal_set, and the signal occurs when Chris@16: * there are no waiting handlers, then the signal notification is queued. The Chris@16: * next async_wait operation on that signal_set will dequeue the notification. Chris@16: * If multiple notifications are queued, subsequent async_wait operations Chris@16: * dequeue them one at a time. Signal notifications are dequeued in order of Chris@16: * ascending signal number. Chris@16: * Chris@16: * If a signal number is removed from a signal_set (using the @c remove or @c Chris@16: * erase member functions) then any queued notifications for that signal are Chris@16: * discarded. Chris@16: * Chris@16: * @par Multiple registration of signals Chris@16: * Chris@16: * The same signal number may be registered with different signal_set objects. Chris@16: * When the signal occurs, one handler is called for each signal_set object. Chris@16: * Chris@16: * Note that multiple registration only works for signals that are registered Chris@16: * using Asio. The application must not also register a signal handler using Chris@16: * functions such as @c signal() or @c sigaction(). Chris@16: * Chris@16: * @par Signal masking on POSIX platforms Chris@16: * Chris@16: * POSIX allows signals to be blocked using functions such as @c sigprocmask() Chris@16: * and @c pthread_sigmask(). For signals to be delivered, programs must ensure Chris@16: * that any signals registered using signal_set objects are unblocked in at Chris@16: * least one thread. Chris@16: */ Chris@16: template Chris@16: class basic_signal_set Chris@16: : public basic_io_object Chris@16: { Chris@16: public: Chris@16: /// Construct a signal set without adding any signals. Chris@16: /** Chris@16: * This constructor creates a signal set without registering for any signals. Chris@16: * Chris@16: * @param io_service The io_service object that the signal set will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the set. Chris@16: */ Chris@16: explicit basic_signal_set(boost::asio::io_service& io_service) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: } Chris@16: Chris@16: /// Construct a signal set and add one signal. Chris@16: /** Chris@16: * This constructor creates a signal set and registers for one signal. Chris@16: * Chris@16: * @param io_service The io_service object that the signal set will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the set. Chris@16: * Chris@16: * @param signal_number_1 The signal number to be added. Chris@16: * Chris@16: * @note This constructor is equivalent to performing: Chris@16: * @code boost::asio::signal_set signals(io_service); Chris@16: * signals.add(signal_number_1); @endcode Chris@16: */ Chris@16: basic_signal_set(boost::asio::io_service& io_service, int signal_number_1) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.add(this->implementation, signal_number_1, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: } Chris@16: Chris@16: /// Construct a signal set and add two signals. Chris@16: /** Chris@16: * This constructor creates a signal set and registers for two signals. Chris@16: * Chris@16: * @param io_service The io_service object that the signal set will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the set. Chris@16: * Chris@16: * @param signal_number_1 The first signal number to be added. Chris@16: * Chris@16: * @param signal_number_2 The second signal number to be added. Chris@16: * Chris@16: * @note This constructor is equivalent to performing: Chris@16: * @code boost::asio::signal_set signals(io_service); Chris@16: * signals.add(signal_number_1); Chris@16: * signals.add(signal_number_2); @endcode Chris@16: */ Chris@16: basic_signal_set(boost::asio::io_service& io_service, int signal_number_1, Chris@16: int signal_number_2) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.add(this->implementation, signal_number_1, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: this->service.add(this->implementation, signal_number_2, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: } Chris@16: Chris@16: /// Construct a signal set and add three signals. Chris@16: /** Chris@16: * This constructor creates a signal set and registers for three signals. Chris@16: * Chris@16: * @param io_service The io_service object that the signal set will use to Chris@16: * dispatch handlers for any asynchronous operations performed on the set. Chris@16: * Chris@16: * @param signal_number_1 The first signal number to be added. Chris@16: * Chris@16: * @param signal_number_2 The second signal number to be added. Chris@16: * Chris@16: * @param signal_number_3 The third signal number to be added. Chris@16: * Chris@16: * @note This constructor is equivalent to performing: Chris@16: * @code boost::asio::signal_set signals(io_service); Chris@16: * signals.add(signal_number_1); Chris@16: * signals.add(signal_number_2); Chris@16: * signals.add(signal_number_3); @endcode Chris@16: */ Chris@16: basic_signal_set(boost::asio::io_service& io_service, int signal_number_1, Chris@16: int signal_number_2, int signal_number_3) Chris@16: : basic_io_object(io_service) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.add(this->implementation, signal_number_1, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: this->service.add(this->implementation, signal_number_2, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: this->service.add(this->implementation, signal_number_3, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: } Chris@16: Chris@16: /// Add a signal to a signal_set. Chris@16: /** Chris@16: * This function adds the specified signal to the set. It has no effect if the Chris@16: * signal is already in the set. Chris@16: * Chris@16: * @param signal_number The signal to be added to the set. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: */ Chris@16: void add(int signal_number) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.add(this->implementation, signal_number, ec); Chris@16: boost::asio::detail::throw_error(ec, "add"); Chris@16: } Chris@16: Chris@16: /// Add a signal to a signal_set. Chris@16: /** Chris@16: * This function adds the specified signal to the set. It has no effect if the Chris@16: * signal is already in the set. Chris@16: * Chris@16: * @param signal_number The signal to be added to the set. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: */ Chris@16: boost::system::error_code add(int signal_number, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->service.add(this->implementation, signal_number, ec); Chris@16: } Chris@16: Chris@16: /// Remove a signal from a signal_set. Chris@16: /** Chris@16: * This function removes the specified signal from the set. It has no effect Chris@16: * if the signal is not in the set. Chris@16: * Chris@16: * @param signal_number The signal to be removed from the set. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note Removes any notifications that have been queued for the specified Chris@16: * signal number. Chris@16: */ Chris@16: void remove(int signal_number) Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.remove(this->implementation, signal_number, ec); Chris@16: boost::asio::detail::throw_error(ec, "remove"); Chris@16: } Chris@16: Chris@16: /// Remove a signal from a signal_set. Chris@16: /** Chris@16: * This function removes the specified signal from the set. It has no effect Chris@16: * if the signal is not in the set. Chris@16: * Chris@16: * @param signal_number The signal to be removed from the set. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @note Removes any notifications that have been queued for the specified Chris@16: * signal number. Chris@16: */ Chris@16: boost::system::error_code remove(int signal_number, Chris@16: boost::system::error_code& ec) Chris@16: { Chris@16: return this->service.remove(this->implementation, signal_number, ec); Chris@16: } Chris@16: Chris@16: /// Remove all signals from a signal_set. Chris@16: /** Chris@16: * This function removes all signals from the set. It has no effect if the set Chris@16: * is already empty. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note Removes all queued notifications. Chris@16: */ Chris@16: void clear() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.clear(this->implementation, ec); Chris@16: boost::asio::detail::throw_error(ec, "clear"); Chris@16: } Chris@16: Chris@16: /// Remove all signals from a signal_set. Chris@16: /** Chris@16: * This function removes all signals from the set. It has no effect if the set Chris@16: * is already empty. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @note Removes all queued notifications. Chris@16: */ Chris@16: boost::system::error_code clear(boost::system::error_code& ec) Chris@16: { Chris@16: return this->service.clear(this->implementation, ec); Chris@16: } Chris@16: Chris@16: /// Cancel all operations associated with the signal set. Chris@16: /** Chris@16: * This function forces the completion of any pending asynchronous wait Chris@16: * operations against the signal set. The handler for each cancelled Chris@16: * operation will be invoked with the boost::asio::error::operation_aborted Chris@16: * error code. Chris@16: * Chris@16: * Cancellation does not alter the set of registered signals. Chris@16: * Chris@16: * @throws boost::system::system_error Thrown on failure. Chris@16: * Chris@16: * @note If a registered signal occurred before cancel() is called, then the Chris@16: * handlers for asynchronous wait operations will: Chris@16: * Chris@16: * @li have already been invoked; or Chris@16: * Chris@16: * @li have been queued for invocation in the near future. Chris@16: * Chris@16: * These handlers can no longer be cancelled, and therefore are passed an Chris@16: * error code that indicates the successful completion of the wait operation. Chris@16: */ Chris@16: void cancel() Chris@16: { Chris@16: boost::system::error_code ec; Chris@16: this->service.cancel(this->implementation, ec); Chris@16: boost::asio::detail::throw_error(ec, "cancel"); Chris@16: } Chris@16: Chris@16: /// Cancel all operations associated with the signal set. Chris@16: /** Chris@16: * This function forces the completion of any pending asynchronous wait Chris@16: * operations against the signal set. The handler for each cancelled Chris@16: * operation will be invoked with the boost::asio::error::operation_aborted Chris@16: * error code. Chris@16: * Chris@16: * Cancellation does not alter the set of registered signals. Chris@16: * Chris@16: * @param ec Set to indicate what error occurred, if any. Chris@16: * Chris@16: * @note If a registered signal occurred before cancel() is called, then the Chris@16: * handlers for asynchronous wait operations will: Chris@16: * Chris@16: * @li have already been invoked; or Chris@16: * Chris@16: * @li have been queued for invocation in the near future. Chris@16: * Chris@16: * These handlers can no longer be cancelled, and therefore are passed an Chris@16: * error code that indicates the successful completion of the wait operation. Chris@16: */ Chris@16: boost::system::error_code cancel(boost::system::error_code& ec) Chris@16: { Chris@16: return this->service.cancel(this->implementation, ec); Chris@16: } Chris@16: Chris@16: /// Start an asynchronous operation to wait for a signal to be delivered. Chris@16: /** Chris@16: * This function may be used to initiate an asynchronous wait against the Chris@16: * signal set. It always returns immediately. Chris@16: * Chris@16: * For each call to async_wait(), the supplied handler will be called exactly Chris@16: * once. The handler will be called when: Chris@16: * Chris@16: * @li One of the registered signals in the signal set occurs; or Chris@16: * Chris@16: * @li The signal set was cancelled, in which case the handler is passed the Chris@16: * error code boost::asio::error::operation_aborted. Chris@16: * Chris@16: * @param handler The handler to be called when the signal occurs. Copies Chris@16: * will be made of the handler as required. The function signature of the Chris@16: * handler must be: Chris@16: * @code void handler( Chris@16: * const boost::system::error_code& error, // Result of operation. Chris@16: * int signal_number // Indicates which signal occurred. Chris@16: * ); @endcode Chris@16: * Regardless of whether the asynchronous operation completes immediately or Chris@16: * not, the handler will not be invoked from within this function. Invocation Chris@16: * of the handler will be performed in a manner equivalent to using Chris@16: * boost::asio::io_service::post(). Chris@16: */ Chris@16: template Chris@16: BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler, Chris@16: void (boost::system::error_code, int)) Chris@16: async_wait(BOOST_ASIO_MOVE_ARG(SignalHandler) handler) Chris@16: { Chris@16: // If you get an error on the following line it means that your handler does Chris@16: // not meet the documented type requirements for a SignalHandler. Chris@16: BOOST_ASIO_SIGNAL_HANDLER_CHECK(SignalHandler, handler) type_check; Chris@16: Chris@16: return this->service.async_wait(this->implementation, Chris@16: BOOST_ASIO_MOVE_CAST(SignalHandler)(handler)); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP