basic_signal_set.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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. /// Construct a signal set without adding any signals.
  95. /**
  96. * This constructor creates a signal set without registering for any signals.
  97. *
  98. * @param ex The I/O executor that the signal set will use, by default, to
  99. * dispatch handlers for any asynchronous operations performed on the
  100. * signal set.
  101. */
  102. explicit basic_signal_set(const executor_type& ex)
  103. : impl_(ex)
  104. {
  105. }
  106. /// Construct a signal set without adding any signals.
  107. /**
  108. * This constructor creates a signal set without registering for any signals.
  109. *
  110. * @param context An execution context which provides the I/O executor that
  111. * the signal set will use, by default, to dispatch handlers for any
  112. * asynchronous operations performed on the signal set.
  113. */
  114. template <typename ExecutionContext>
  115. explicit basic_signal_set(ExecutionContext& context,
  116. typename enable_if<
  117. is_convertible<ExecutionContext&, execution_context&>::value
  118. >::type* = 0)
  119. : impl_(context)
  120. {
  121. }
  122. /// Construct a signal set and add one signal.
  123. /**
  124. * This constructor creates a signal set and registers for one signal.
  125. *
  126. * @param ex The I/O executor that the signal set will use, by default, to
  127. * dispatch handlers for any asynchronous operations performed on the
  128. * signal set.
  129. *
  130. * @param signal_number_1 The signal number to be added.
  131. *
  132. * @note This constructor is equivalent to performing:
  133. * @code boost::asio::signal_set signals(ex);
  134. * signals.add(signal_number_1); @endcode
  135. */
  136. basic_signal_set(const executor_type& ex, int signal_number_1)
  137. : impl_(ex)
  138. {
  139. boost::system::error_code ec;
  140. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  141. boost::asio::detail::throw_error(ec, "add");
  142. }
  143. /// Construct a signal set and add one signal.
  144. /**
  145. * This constructor creates a signal set and registers for one signal.
  146. *
  147. * @param context An execution context which provides the I/O executor that
  148. * the signal set will use, by default, to dispatch handlers for any
  149. * asynchronous operations performed on the signal set.
  150. *
  151. * @param signal_number_1 The signal number to be added.
  152. *
  153. * @note This constructor is equivalent to performing:
  154. * @code boost::asio::signal_set signals(context);
  155. * signals.add(signal_number_1); @endcode
  156. */
  157. template <typename ExecutionContext>
  158. basic_signal_set(ExecutionContext& context, int signal_number_1,
  159. typename enable_if<
  160. is_convertible<ExecutionContext&, execution_context&>::value
  161. >::type* = 0)
  162. : impl_(context)
  163. {
  164. boost::system::error_code ec;
  165. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  166. boost::asio::detail::throw_error(ec, "add");
  167. }
  168. /// Construct a signal set and add two signals.
  169. /**
  170. * This constructor creates a signal set and registers for two signals.
  171. *
  172. * @param ex The I/O executor that the signal set will use, by default, to
  173. * dispatch handlers for any asynchronous operations performed on the
  174. * signal set.
  175. *
  176. * @param signal_number_1 The first signal number to be added.
  177. *
  178. * @param signal_number_2 The second signal number to be added.
  179. *
  180. * @note This constructor is equivalent to performing:
  181. * @code boost::asio::signal_set signals(ex);
  182. * signals.add(signal_number_1);
  183. * signals.add(signal_number_2); @endcode
  184. */
  185. basic_signal_set(const executor_type& ex, int signal_number_1,
  186. int signal_number_2)
  187. : impl_(ex)
  188. {
  189. boost::system::error_code ec;
  190. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  191. boost::asio::detail::throw_error(ec, "add");
  192. impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec);
  193. boost::asio::detail::throw_error(ec, "add");
  194. }
  195. /// Construct a signal set and add two signals.
  196. /**
  197. * This constructor creates a signal set and registers for two signals.
  198. *
  199. * @param context An execution context which provides the I/O executor that
  200. * the signal set will use, by default, to dispatch handlers for any
  201. * asynchronous operations performed on the signal set.
  202. *
  203. * @param signal_number_1 The first signal number to be added.
  204. *
  205. * @param signal_number_2 The second signal number to be added.
  206. *
  207. * @note This constructor is equivalent to performing:
  208. * @code boost::asio::signal_set signals(context);
  209. * signals.add(signal_number_1);
  210. * signals.add(signal_number_2); @endcode
  211. */
  212. template <typename ExecutionContext>
  213. basic_signal_set(ExecutionContext& context, int signal_number_1,
  214. int signal_number_2,
  215. typename enable_if<
  216. is_convertible<ExecutionContext&, execution_context&>::value
  217. >::type* = 0)
  218. : impl_(context)
  219. {
  220. boost::system::error_code ec;
  221. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  222. boost::asio::detail::throw_error(ec, "add");
  223. impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec);
  224. boost::asio::detail::throw_error(ec, "add");
  225. }
  226. /// Construct a signal set and add three signals.
  227. /**
  228. * This constructor creates a signal set and registers for three signals.
  229. *
  230. * @param ex The I/O executor that the signal set will use, by default, to
  231. * dispatch handlers for any asynchronous operations performed on the
  232. * signal set.
  233. *
  234. * @param signal_number_1 The first signal number to be added.
  235. *
  236. * @param signal_number_2 The second signal number to be added.
  237. *
  238. * @param signal_number_3 The third signal number to be added.
  239. *
  240. * @note This constructor is equivalent to performing:
  241. * @code boost::asio::signal_set signals(ex);
  242. * signals.add(signal_number_1);
  243. * signals.add(signal_number_2);
  244. * signals.add(signal_number_3); @endcode
  245. */
  246. basic_signal_set(const executor_type& ex, int signal_number_1,
  247. int signal_number_2, int signal_number_3)
  248. : impl_(ex)
  249. {
  250. boost::system::error_code ec;
  251. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  252. boost::asio::detail::throw_error(ec, "add");
  253. impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec);
  254. boost::asio::detail::throw_error(ec, "add");
  255. impl_.get_service().add(impl_.get_implementation(), signal_number_3, ec);
  256. boost::asio::detail::throw_error(ec, "add");
  257. }
  258. /// Construct a signal set and add three signals.
  259. /**
  260. * This constructor creates a signal set and registers for three signals.
  261. *
  262. * @param context An execution context which provides the I/O executor that
  263. * the signal set will use, by default, to dispatch handlers for any
  264. * asynchronous operations performed on the signal set.
  265. *
  266. * @param signal_number_1 The first signal number to be added.
  267. *
  268. * @param signal_number_2 The second signal number to be added.
  269. *
  270. * @param signal_number_3 The third signal number to be added.
  271. *
  272. * @note This constructor is equivalent to performing:
  273. * @code boost::asio::signal_set signals(context);
  274. * signals.add(signal_number_1);
  275. * signals.add(signal_number_2);
  276. * signals.add(signal_number_3); @endcode
  277. */
  278. template <typename ExecutionContext>
  279. basic_signal_set(ExecutionContext& context, int signal_number_1,
  280. int signal_number_2, int signal_number_3,
  281. typename enable_if<
  282. is_convertible<ExecutionContext&, execution_context&>::value
  283. >::type* = 0)
  284. : impl_(context)
  285. {
  286. boost::system::error_code ec;
  287. impl_.get_service().add(impl_.get_implementation(), signal_number_1, ec);
  288. boost::asio::detail::throw_error(ec, "add");
  289. impl_.get_service().add(impl_.get_implementation(), signal_number_2, ec);
  290. boost::asio::detail::throw_error(ec, "add");
  291. impl_.get_service().add(impl_.get_implementation(), signal_number_3, ec);
  292. boost::asio::detail::throw_error(ec, "add");
  293. }
  294. /// Destroys the signal set.
  295. /**
  296. * This function destroys the signal set, cancelling any outstanding
  297. * asynchronous wait operations associated with the signal set as if by
  298. * calling @c cancel.
  299. */
  300. ~basic_signal_set()
  301. {
  302. }
  303. /// Get the executor associated with the object.
  304. executor_type get_executor() BOOST_ASIO_NOEXCEPT
  305. {
  306. return impl_.get_executor();
  307. }
  308. /// Add a signal to a signal_set.
  309. /**
  310. * This function adds the specified signal to the set. It has no effect if the
  311. * signal is already in the set.
  312. *
  313. * @param signal_number The signal to be added to the set.
  314. *
  315. * @throws boost::system::system_error Thrown on failure.
  316. */
  317. void add(int signal_number)
  318. {
  319. boost::system::error_code ec;
  320. impl_.get_service().add(impl_.get_implementation(), signal_number, ec);
  321. boost::asio::detail::throw_error(ec, "add");
  322. }
  323. /// Add a signal to a signal_set.
  324. /**
  325. * This function adds the specified signal to the set. It has no effect if the
  326. * signal is already in the set.
  327. *
  328. * @param signal_number The signal to be added to the set.
  329. *
  330. * @param ec Set to indicate what error occurred, if any.
  331. */
  332. BOOST_ASIO_SYNC_OP_VOID add(int signal_number,
  333. boost::system::error_code& ec)
  334. {
  335. impl_.get_service().add(impl_.get_implementation(), signal_number, ec);
  336. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  337. }
  338. /// Remove a signal from a signal_set.
  339. /**
  340. * This function removes the specified signal from the set. It has no effect
  341. * if the signal is not in the set.
  342. *
  343. * @param signal_number The signal to be removed from the set.
  344. *
  345. * @throws boost::system::system_error Thrown on failure.
  346. *
  347. * @note Removes any notifications that have been queued for the specified
  348. * signal number.
  349. */
  350. void remove(int signal_number)
  351. {
  352. boost::system::error_code ec;
  353. impl_.get_service().remove(impl_.get_implementation(), signal_number, ec);
  354. boost::asio::detail::throw_error(ec, "remove");
  355. }
  356. /// Remove a signal from a signal_set.
  357. /**
  358. * This function removes the specified signal from the set. It has no effect
  359. * if the signal is not in the set.
  360. *
  361. * @param signal_number The signal to be removed from the set.
  362. *
  363. * @param ec Set to indicate what error occurred, if any.
  364. *
  365. * @note Removes any notifications that have been queued for the specified
  366. * signal number.
  367. */
  368. BOOST_ASIO_SYNC_OP_VOID remove(int signal_number,
  369. boost::system::error_code& ec)
  370. {
  371. impl_.get_service().remove(impl_.get_implementation(), signal_number, ec);
  372. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  373. }
  374. /// Remove all signals from a signal_set.
  375. /**
  376. * This function removes all signals from the set. It has no effect if the set
  377. * is already empty.
  378. *
  379. * @throws boost::system::system_error Thrown on failure.
  380. *
  381. * @note Removes all queued notifications.
  382. */
  383. void clear()
  384. {
  385. boost::system::error_code ec;
  386. impl_.get_service().clear(impl_.get_implementation(), ec);
  387. boost::asio::detail::throw_error(ec, "clear");
  388. }
  389. /// Remove all signals from a signal_set.
  390. /**
  391. * This function removes all signals from the set. It has no effect if the set
  392. * is already empty.
  393. *
  394. * @param ec Set to indicate what error occurred, if any.
  395. *
  396. * @note Removes all queued notifications.
  397. */
  398. BOOST_ASIO_SYNC_OP_VOID clear(boost::system::error_code& ec)
  399. {
  400. impl_.get_service().clear(impl_.get_implementation(), ec);
  401. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  402. }
  403. /// Cancel all operations associated with the signal set.
  404. /**
  405. * This function forces the completion of any pending asynchronous wait
  406. * operations against the signal set. The handler for each cancelled
  407. * operation will be invoked with the boost::asio::error::operation_aborted
  408. * error code.
  409. *
  410. * Cancellation does not alter the set of registered signals.
  411. *
  412. * @throws boost::system::system_error Thrown on failure.
  413. *
  414. * @note If a registered signal occurred before cancel() is called, then the
  415. * handlers for asynchronous wait operations will:
  416. *
  417. * @li have already been invoked; or
  418. *
  419. * @li have been queued for invocation in the near future.
  420. *
  421. * These handlers can no longer be cancelled, and therefore are passed an
  422. * error code that indicates the successful completion of the wait operation.
  423. */
  424. void cancel()
  425. {
  426. boost::system::error_code ec;
  427. impl_.get_service().cancel(impl_.get_implementation(), ec);
  428. boost::asio::detail::throw_error(ec, "cancel");
  429. }
  430. /// Cancel all operations associated with the signal set.
  431. /**
  432. * This function forces the completion of any pending asynchronous wait
  433. * operations against the signal set. The handler for each cancelled
  434. * operation will be invoked with the boost::asio::error::operation_aborted
  435. * error code.
  436. *
  437. * Cancellation does not alter the set of registered signals.
  438. *
  439. * @param ec Set to indicate what error occurred, if any.
  440. *
  441. * @note If a registered signal occurred before cancel() is called, then the
  442. * handlers for asynchronous wait operations will:
  443. *
  444. * @li have already been invoked; or
  445. *
  446. * @li have been queued for invocation in the near future.
  447. *
  448. * These handlers can no longer be cancelled, and therefore are passed an
  449. * error code that indicates the successful completion of the wait operation.
  450. */
  451. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  452. {
  453. impl_.get_service().cancel(impl_.get_implementation(), ec);
  454. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  455. }
  456. /// Start an asynchronous operation to wait for a signal to be delivered.
  457. /**
  458. * This function may be used to initiate an asynchronous wait against the
  459. * signal set. It always returns immediately.
  460. *
  461. * For each call to async_wait(), the supplied handler will be called exactly
  462. * once. The handler will be called when:
  463. *
  464. * @li One of the registered signals in the signal set occurs; or
  465. *
  466. * @li The signal set was cancelled, in which case the handler is passed the
  467. * error code boost::asio::error::operation_aborted.
  468. *
  469. * @param handler The handler to be called when the signal occurs. Copies
  470. * will be made of the handler as required. The function signature of the
  471. * handler must be:
  472. * @code void handler(
  473. * const boost::system::error_code& error, // Result of operation.
  474. * int signal_number // Indicates which signal occurred.
  475. * ); @endcode
  476. * Regardless of whether the asynchronous operation completes immediately or
  477. * not, the handler will not be invoked from within this function. On
  478. * immediate completion, invocation of the handler will be performed in a
  479. * manner equivalent to using boost::asio::post().
  480. */
  481. template <typename SignalHandler>
  482. BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler,
  483. void (boost::system::error_code, int))
  484. async_wait(BOOST_ASIO_MOVE_ARG(SignalHandler) handler)
  485. {
  486. return async_initiate<SignalHandler, void (boost::system::error_code, int)>(
  487. initiate_async_wait(), handler, this);
  488. }
  489. private:
  490. // Disallow copying and assignment.
  491. basic_signal_set(const basic_signal_set&) BOOST_ASIO_DELETED;
  492. basic_signal_set& operator=(const basic_signal_set&) BOOST_ASIO_DELETED;
  493. struct initiate_async_wait
  494. {
  495. template <typename SignalHandler>
  496. void operator()(BOOST_ASIO_MOVE_ARG(SignalHandler) handler,
  497. basic_signal_set* self) const
  498. {
  499. // If you get an error on the following line it means that your handler
  500. // does not meet the documented type requirements for a SignalHandler.
  501. BOOST_ASIO_SIGNAL_HANDLER_CHECK(SignalHandler, handler) type_check;
  502. detail::non_const_lvalue<SignalHandler> handler2(handler);
  503. self->impl_.get_service().async_wait(
  504. self->impl_.get_implementation(), handler2.value,
  505. self->impl_.get_implementation_executor());
  506. }
  507. };
  508. detail::io_object_impl<detail::signal_set_service, Executor> impl_;
  509. };
  510. } // namespace asio
  511. } // namespace boost
  512. #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP