basic_signal_set.hpp 19 KB

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