1 // 2 // basic_signal_set.hpp 3 // ~~~~~~~~~~~~~~~~~~~~ 4 // 5 // Copyright (c) 2003-2021 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 #include <boost/asio/any_io_executor.hpp> 21 #include <boost/asio/async_result.hpp> 22 #include <boost/asio/detail/handler_type_requirements.hpp> 23 #include <boost/asio/detail/io_object_impl.hpp> 24 #include <boost/asio/detail/non_const_lvalue.hpp> 25 #include <boost/asio/detail/signal_set_service.hpp> 26 #include <boost/asio/detail/throw_error.hpp> 27 #include <boost/asio/detail/type_traits.hpp> 28 #include <boost/asio/error.hpp> 29 #include <boost/asio/execution_context.hpp> 30 31 #include <boost/asio/detail/push_options.hpp> 32 33 namespace boost { 34 namespace asio { 35 36 /// Provides signal functionality. 37 /** 38 * The basic_signal_set class provides the ability to perform an asynchronous 39 * wait for one or more signals to occur. 40 * 41 * @par Thread Safety 42 * @e Distinct @e objects: Safe.@n 43 * @e Shared @e objects: Unsafe. 44 * 45 * @par Example 46 * Performing an asynchronous wait: 47 * @code 48 * void handler( 49 * const boost::system::error_code& error, 50 * int signal_number) 51 * { 52 * if (!error) 53 * { 54 * // A signal occurred. 55 * } 56 * } 57 * 58 * ... 59 * 60 * // Construct a signal set registered for process termination. 61 * boost::asio::signal_set signals(my_context, SIGINT, SIGTERM); 62 * 63 * // Start an asynchronous wait for one of the signals to occur. 64 * signals.async_wait(handler); 65 * @endcode 66 * 67 * @par Queueing of signal notifications 68 * 69 * If a signal is registered with a signal_set, and the signal occurs when 70 * there are no waiting handlers, then the signal notification is queued. The 71 * next async_wait operation on that signal_set will dequeue the notification. 72 * If multiple notifications are queued, subsequent async_wait operations 73 * dequeue them one at a time. Signal notifications are dequeued in order of 74 * ascending signal number. 75 * 76 * If a signal number is removed from a signal_set (using the @c remove or @c 77 * erase member functions) then any queued notifications for that signal are 78 * discarded. 79 * 80 * @par Multiple registration of signals 81 * 82 * The same signal number may be registered with different signal_set objects. 83 * When the signal occurs, one handler is called for each signal_set object. 84 * 85 * Note that multiple registration only works for signals that are registered 86 * using Asio. The application must not also register a signal handler using 87 * functions such as @c signal() or @c sigaction(). 88 * 89 * @par Signal masking on POSIX platforms 90 * 91 * POSIX allows signals to be blocked using functions such as @c sigprocmask() 92 * and @c pthread_sigmask(). For signals to be delivered, programs must ensure 93 * that any signals registered using signal_set objects are unblocked in at 94 * least one thread. 95 */ 96 template <typename Executor = any_io_executor> 97 class basic_signal_set 98 { 99 public: 100 /// The type of the executor associated with the object. 101 typedef Executor executor_type; 102 103 /// Rebinds the signal set type to another executor. 104 template <typename Executor1> 105 struct rebind_executor 106 { 107 /// The signal set type when rebound to the specified executor. 108 typedef basic_signal_set<Executor1> other; 109 }; 110 111 /// Construct a signal set without adding any signals. 112 /** 113 * This constructor creates a signal set without registering for any signals. 114 * 115 * @param ex The I/O executor that the signal set will use, by default, to 116 * dispatch handlers for any asynchronous operations performed on the 117 * signal set. 118 */ basic_signal_set(const executor_type & ex)119 explicit basic_signal_set(const executor_type& ex) 120 : impl_(0, ex) 121 { 122 } 123 124 /// Construct a signal set without adding any signals. 125 /** 126 * This constructor creates a signal set without registering for any signals. 127 * 128 * @param context An execution context which provides the I/O executor that 129 * the signal set will use, by default, to dispatch handlers for any 130 * asynchronous operations performed on the signal set. 131 */ 132 template <typename ExecutionContext> basic_signal_set(ExecutionContext & context,typename constraint<is_convertible<ExecutionContext &,execution_context &>::value,defaulted_constraint>::type=defaulted_constraint ())133 explicit basic_signal_set(ExecutionContext& context, 134 typename constraint< 135 is_convertible<ExecutionContext&, execution_context&>::value, 136 defaulted_constraint 137 >::type = defaulted_constraint()) 138 : impl_(0, 0, context) 139 { 140 } 141 142 /// Construct a signal set and add one signal. 143 /** 144 * This constructor creates a signal set and registers for one signal. 145 * 146 * @param ex The I/O executor that the signal set will use, by default, to 147 * dispatch handlers for any asynchronous operations performed on the 148 * signal set. 149 * 150 * @param signal_number_1 The signal number to be added. 151 * 152 * @note This constructor is equivalent to performing: 153 * @code boost::asio::signal_set signals(ex); 154 * signals.add(signal_number_1); @endcode 155 */ basic_signal_set(const executor_type & ex,int signal_number_1)156 basic_signal_set(const executor_type& ex, int signal_number_1) 157 : impl_(0, ex) 158 { 159 boost::system::error_code ec; 160 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 161 boost::asio::detail::throw_error(ec, "add"); 162 } 163 164 /// Construct a signal set and add one signal. 165 /** 166 * This constructor creates a signal set and registers for one signal. 167 * 168 * @param context An execution context which provides the I/O executor that 169 * the signal set will use, by default, to dispatch handlers for any 170 * asynchronous operations performed on the signal set. 171 * 172 * @param signal_number_1 The signal number to be added. 173 * 174 * @note This constructor is equivalent to performing: 175 * @code boost::asio::signal_set signals(context); 176 * signals.add(signal_number_1); @endcode 177 */ 178 template <typename ExecutionContext> basic_signal_set(ExecutionContext & context,int signal_number_1,typename constraint<is_convertible<ExecutionContext &,execution_context &>::value,defaulted_constraint>::type=defaulted_constraint ())179 basic_signal_set(ExecutionContext& context, int signal_number_1, 180 typename constraint< 181 is_convertible<ExecutionContext&, execution_context&>::value, 182 defaulted_constraint 183 >::type = defaulted_constraint()) 184 : impl_(0, 0, context) 185 { 186 boost::system::error_code ec; 187 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 188 boost::asio::detail::throw_error(ec, "add"); 189 } 190 191 /// Construct a signal set and add two signals. 192 /** 193 * This constructor creates a signal set and registers for two signals. 194 * 195 * @param ex The I/O executor that the signal set will use, by default, to 196 * dispatch handlers for any asynchronous operations performed on the 197 * signal set. 198 * 199 * @param signal_number_1 The first signal number to be added. 200 * 201 * @param signal_number_2 The second signal number to be added. 202 * 203 * @note This constructor is equivalent to performing: 204 * @code boost::asio::signal_set signals(ex); 205 * signals.add(signal_number_1); 206 * signals.add(signal_number_2); @endcode 207 */ basic_signal_set(const executor_type & ex,int signal_number_1,int signal_number_2)208 basic_signal_set(const executor_type& ex, int signal_number_1, 209 int signal_number_2) 210 : impl_(0, ex) 211 { 212 boost::system::error_code ec; 213 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 214 boost::asio::detail::throw_error(ec, "add"); 215 impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec); 216 boost::asio::detail::throw_error(ec, "add"); 217 } 218 219 /// Construct a signal set and add two signals. 220 /** 221 * This constructor creates a signal set and registers for two signals. 222 * 223 * @param context An execution context which provides the I/O executor that 224 * the signal set will use, by default, to dispatch handlers for any 225 * asynchronous operations performed on the signal set. 226 * 227 * @param signal_number_1 The first signal number to be added. 228 * 229 * @param signal_number_2 The second signal number to be added. 230 * 231 * @note This constructor is equivalent to performing: 232 * @code boost::asio::signal_set signals(context); 233 * signals.add(signal_number_1); 234 * signals.add(signal_number_2); @endcode 235 */ 236 template <typename ExecutionContext> basic_signal_set(ExecutionContext & context,int signal_number_1,int signal_number_2,typename constraint<is_convertible<ExecutionContext &,execution_context &>::value,defaulted_constraint>::type=defaulted_constraint ())237 basic_signal_set(ExecutionContext& context, int signal_number_1, 238 int signal_number_2, 239 typename constraint< 240 is_convertible<ExecutionContext&, execution_context&>::value, 241 defaulted_constraint 242 >::type = defaulted_constraint()) 243 : impl_(0, 0, context) 244 { 245 boost::system::error_code ec; 246 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 247 boost::asio::detail::throw_error(ec, "add"); 248 impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec); 249 boost::asio::detail::throw_error(ec, "add"); 250 } 251 252 /// Construct a signal set and add three signals. 253 /** 254 * This constructor creates a signal set and registers for three signals. 255 * 256 * @param ex The I/O executor that the signal set will use, by default, to 257 * dispatch handlers for any asynchronous operations performed on the 258 * signal set. 259 * 260 * @param signal_number_1 The first signal number to be added. 261 * 262 * @param signal_number_2 The second signal number to be added. 263 * 264 * @param signal_number_3 The third signal number to be added. 265 * 266 * @note This constructor is equivalent to performing: 267 * @code boost::asio::signal_set signals(ex); 268 * signals.add(signal_number_1); 269 * signals.add(signal_number_2); 270 * signals.add(signal_number_3); @endcode 271 */ basic_signal_set(const executor_type & ex,int signal_number_1,int signal_number_2,int signal_number_3)272 basic_signal_set(const executor_type& ex, int signal_number_1, 273 int signal_number_2, int signal_number_3) 274 : impl_(0, ex) 275 { 276 boost::system::error_code ec; 277 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 278 boost::asio::detail::throw_error(ec, "add"); 279 impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec); 280 boost::asio::detail::throw_error(ec, "add"); 281 impl_.get_service().add(impl_.get_implementation(), signal_number_3, ec); 282 boost::asio::detail::throw_error(ec, "add"); 283 } 284 285 /// Construct a signal set and add three signals. 286 /** 287 * This constructor creates a signal set and registers for three signals. 288 * 289 * @param context An execution context which provides the I/O executor that 290 * the signal set will use, by default, to dispatch handlers for any 291 * asynchronous operations performed on the signal set. 292 * 293 * @param signal_number_1 The first signal number to be added. 294 * 295 * @param signal_number_2 The second signal number to be added. 296 * 297 * @param signal_number_3 The third signal number to be added. 298 * 299 * @note This constructor is equivalent to performing: 300 * @code boost::asio::signal_set signals(context); 301 * signals.add(signal_number_1); 302 * signals.add(signal_number_2); 303 * signals.add(signal_number_3); @endcode 304 */ 305 template <typename ExecutionContext> basic_signal_set(ExecutionContext & context,int signal_number_1,int signal_number_2,int signal_number_3,typename constraint<is_convertible<ExecutionContext &,execution_context &>::value,defaulted_constraint>::type=defaulted_constraint ())306 basic_signal_set(ExecutionContext& context, int signal_number_1, 307 int signal_number_2, int signal_number_3, 308 typename constraint< 309 is_convertible<ExecutionContext&, execution_context&>::value, 310 defaulted_constraint 311 >::type = defaulted_constraint()) 312 : impl_(0, 0, context) 313 { 314 boost::system::error_code ec; 315 impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec); 316 boost::asio::detail::throw_error(ec, "add"); 317 impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec); 318 boost::asio::detail::throw_error(ec, "add"); 319 impl_.get_service().add(impl_.get_implementation(), signal_number_3, ec); 320 boost::asio::detail::throw_error(ec, "add"); 321 } 322 323 /// Destroys the signal set. 324 /** 325 * This function destroys the signal set, cancelling any outstanding 326 * asynchronous wait operations associated with the signal set as if by 327 * calling @c cancel. 328 */ ~basic_signal_set()329 ~basic_signal_set() 330 { 331 } 332 333 /// Get the executor associated with the object. get_executor()334 executor_type get_executor() BOOST_ASIO_NOEXCEPT 335 { 336 return impl_.get_executor(); 337 } 338 339 /// Add a signal to a signal_set. 340 /** 341 * This function adds the specified signal to the set. It has no effect if the 342 * signal is already in the set. 343 * 344 * @param signal_number The signal to be added to the set. 345 * 346 * @throws boost::system::system_error Thrown on failure. 347 */ add(int signal_number)348 void add(int signal_number) 349 { 350 boost::system::error_code ec; 351 impl_.get_service().add(impl_.get_implementation(), signal_number, ec); 352 boost::asio::detail::throw_error(ec, "add"); 353 } 354 355 /// Add a signal to a signal_set. 356 /** 357 * This function adds the specified signal to the set. It has no effect if the 358 * signal is already in the set. 359 * 360 * @param signal_number The signal to be added to the set. 361 * 362 * @param ec Set to indicate what error occurred, if any. 363 */ add(int signal_number,boost::system::error_code & ec)364 BOOST_ASIO_SYNC_OP_VOID add(int signal_number, 365 boost::system::error_code& ec) 366 { 367 impl_.get_service().add(impl_.get_implementation(), signal_number, ec); 368 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); 369 } 370 371 /// Remove a signal from a signal_set. 372 /** 373 * This function removes the specified signal from the set. It has no effect 374 * if the signal is not in the set. 375 * 376 * @param signal_number The signal to be removed from the set. 377 * 378 * @throws boost::system::system_error Thrown on failure. 379 * 380 * @note Removes any notifications that have been queued for the specified 381 * signal number. 382 */ remove(int signal_number)383 void remove(int signal_number) 384 { 385 boost::system::error_code ec; 386 impl_.get_service().remove(impl_.get_implementation(), signal_number, ec); 387 boost::asio::detail::throw_error(ec, "remove"); 388 } 389 390 /// Remove a signal from a signal_set. 391 /** 392 * This function removes the specified signal from the set. It has no effect 393 * if the signal is not in the set. 394 * 395 * @param signal_number The signal to be removed from the set. 396 * 397 * @param ec Set to indicate what error occurred, if any. 398 * 399 * @note Removes any notifications that have been queued for the specified 400 * signal number. 401 */ remove(int signal_number,boost::system::error_code & ec)402 BOOST_ASIO_SYNC_OP_VOID remove(int signal_number, 403 boost::system::error_code& ec) 404 { 405 impl_.get_service().remove(impl_.get_implementation(), signal_number, ec); 406 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); 407 } 408 409 /// Remove all signals from a signal_set. 410 /** 411 * This function removes all signals from the set. It has no effect if the set 412 * is already empty. 413 * 414 * @throws boost::system::system_error Thrown on failure. 415 * 416 * @note Removes all queued notifications. 417 */ clear()418 void clear() 419 { 420 boost::system::error_code ec; 421 impl_.get_service().clear(impl_.get_implementation(), ec); 422 boost::asio::detail::throw_error(ec, "clear"); 423 } 424 425 /// Remove all signals from a signal_set. 426 /** 427 * This function removes all signals from the set. It has no effect if the set 428 * is already empty. 429 * 430 * @param ec Set to indicate what error occurred, if any. 431 * 432 * @note Removes all queued notifications. 433 */ clear(boost::system::error_code & ec)434 BOOST_ASIO_SYNC_OP_VOID clear(boost::system::error_code& ec) 435 { 436 impl_.get_service().clear(impl_.get_implementation(), ec); 437 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); 438 } 439 440 /// Cancel all operations associated with the signal set. 441 /** 442 * This function forces the completion of any pending asynchronous wait 443 * operations against the signal set. The handler for each cancelled 444 * operation will be invoked with the boost::asio::error::operation_aborted 445 * error code. 446 * 447 * Cancellation does not alter the set of registered signals. 448 * 449 * @throws boost::system::system_error Thrown on failure. 450 * 451 * @note If a registered signal occurred before cancel() is called, then the 452 * handlers for asynchronous wait operations will: 453 * 454 * @li have already been invoked; or 455 * 456 * @li have been queued for invocation in the near future. 457 * 458 * These handlers can no longer be cancelled, and therefore are passed an 459 * error code that indicates the successful completion of the wait operation. 460 */ cancel()461 void cancel() 462 { 463 boost::system::error_code ec; 464 impl_.get_service().cancel(impl_.get_implementation(), ec); 465 boost::asio::detail::throw_error(ec, "cancel"); 466 } 467 468 /// Cancel all operations associated with the signal set. 469 /** 470 * This function forces the completion of any pending asynchronous wait 471 * operations against the signal set. The handler for each cancelled 472 * operation will be invoked with the boost::asio::error::operation_aborted 473 * error code. 474 * 475 * Cancellation does not alter the set of registered signals. 476 * 477 * @param ec Set to indicate what error occurred, if any. 478 * 479 * @note If a registered signal occurred before cancel() is called, then the 480 * handlers for asynchronous wait operations will: 481 * 482 * @li have already been invoked; or 483 * 484 * @li have been queued for invocation in the near future. 485 * 486 * These handlers can no longer be cancelled, and therefore are passed an 487 * error code that indicates the successful completion of the wait operation. 488 */ cancel(boost::system::error_code & ec)489 BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec) 490 { 491 impl_.get_service().cancel(impl_.get_implementation(), ec); 492 BOOST_ASIO_SYNC_OP_VOID_RETURN(ec); 493 } 494 495 /// Start an asynchronous operation to wait for a signal to be delivered. 496 /** 497 * This function may be used to initiate an asynchronous wait against the 498 * signal set. It always returns immediately. 499 * 500 * For each call to async_wait(), the supplied handler will be called exactly 501 * once. The handler will be called when: 502 * 503 * @li One of the registered signals in the signal set occurs; or 504 * 505 * @li The signal set was cancelled, in which case the handler is passed the 506 * error code boost::asio::error::operation_aborted. 507 * 508 * @param handler The handler to be called when the signal occurs. Copies 509 * will be made of the handler as required. The function signature of the 510 * handler must be: 511 * @code void handler( 512 * const boost::system::error_code& error, // Result of operation. 513 * int signal_number // Indicates which signal occurred. 514 * ); @endcode 515 * Regardless of whether the asynchronous operation completes immediately or 516 * not, the handler will not be invoked from within this function. On 517 * immediate completion, invocation of the handler will be performed in a 518 * manner equivalent to using boost::asio::post(). 519 */ 520 template < 521 BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code, int)) 522 SignalHandler BOOST_ASIO_DEFAULT_COMPLETION_TOKEN_TYPE(executor_type)> BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(SignalHandler,void (boost::system::error_code,int))523 BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(SignalHandler, 524 void (boost::system::error_code, int)) 525 async_wait( 526 BOOST_ASIO_MOVE_ARG(SignalHandler) handler 527 BOOST_ASIO_DEFAULT_COMPLETION_TOKEN(executor_type)) 528 { 529 return async_initiate<SignalHandler, void (boost::system::error_code, int)>( 530 initiate_async_wait(this), handler); 531 } 532 533 private: 534 // Disallow copying and assignment. 535 basic_signal_set(const basic_signal_set&) BOOST_ASIO_DELETED; 536 basic_signal_set& operator=(const basic_signal_set&) BOOST_ASIO_DELETED; 537 538 class initiate_async_wait 539 { 540 public: 541 typedef Executor executor_type; 542 initiate_async_wait(basic_signal_set * self)543 explicit initiate_async_wait(basic_signal_set* self) 544 : self_(self) 545 { 546 } 547 get_executor() const548 executor_type get_executor() const BOOST_ASIO_NOEXCEPT 549 { 550 return self_->get_executor(); 551 } 552 553 template <typename SignalHandler> operator ()(BOOST_ASIO_MOVE_ARG (SignalHandler)handler) const554 void operator()(BOOST_ASIO_MOVE_ARG(SignalHandler) handler) const 555 { 556 // If you get an error on the following line it means that your handler 557 // does not meet the documented type requirements for a SignalHandler. 558 BOOST_ASIO_SIGNAL_HANDLER_CHECK(SignalHandler, handler) type_check; 559 560 detail::non_const_lvalue<SignalHandler> handler2(handler); 561 self_->impl_.get_service().async_wait( 562 self_->impl_.get_implementation(), 563 handler2.value, self_->impl_.get_executor()); 564 } 565 566 private: 567 basic_signal_set* self_; 568 }; 569 570 detail::io_object_impl<detail::signal_set_service, Executor> impl_; 571 }; 572 573 } // namespace asio 574 } // namespace boost 575 576 #include <boost/asio/detail/pop_options.hpp> 577 578 #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP 579