basic_signal_set.hpp 20 KB

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