basic_signal_set.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. //
  2. // basic_signal_set.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  17. #include <boost/asio/basic_io_object.hpp>
  18. #include <boost/asio/detail/handler_type_requirements.hpp>
  19. #include <boost/asio/detail/throw_error.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/signal_set_service.hpp>
  22. #include <boost/asio/detail/push_options.hpp>
  23. namespace boost {
  24. namespace asio {
  25. /// Provides signal functionality.
  26. /**
  27. * The basic_signal_set class template provides the ability to perform an
  28. * asynchronous wait for one or more signals to occur.
  29. *
  30. * Most applications will use the boost::asio::signal_set typedef.
  31. *
  32. * @par Thread Safety
  33. * @e Distinct @e objects: Safe.@n
  34. * @e Shared @e objects: Unsafe.
  35. *
  36. * @par Example
  37. * Performing an asynchronous wait:
  38. * @code
  39. * void handler(
  40. * const boost::system::error_code& error,
  41. * int signal_number)
  42. * {
  43. * if (!error)
  44. * {
  45. * // A signal occurred.
  46. * }
  47. * }
  48. *
  49. * ...
  50. *
  51. * // Construct a signal set registered for process termination.
  52. * boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
  53. *
  54. * // Start an asynchronous wait for one of the signals to occur.
  55. * signals.async_wait(handler);
  56. * @endcode
  57. *
  58. * @par Queueing of signal notifications
  59. *
  60. * If a signal is registered with a signal_set, and the signal occurs when
  61. * there are no waiting handlers, then the signal notification is queued. The
  62. * next async_wait operation on that signal_set will dequeue the notification.
  63. * If multiple notifications are queued, subsequent async_wait operations
  64. * dequeue them one at a time. Signal notifications are dequeued in order of
  65. * ascending signal number.
  66. *
  67. * If a signal number is removed from a signal_set (using the @c remove or @c
  68. * erase member functions) then any queued notifications for that signal are
  69. * discarded.
  70. *
  71. * @par Multiple registration of signals
  72. *
  73. * The same signal number may be registered with different signal_set objects.
  74. * When the signal occurs, one handler is called for each signal_set object.
  75. *
  76. * Note that multiple registration only works for signals that are registered
  77. * using Asio. The application must not also register a signal handler using
  78. * functions such as @c signal() or @c sigaction().
  79. *
  80. * @par Signal masking on POSIX platforms
  81. *
  82. * POSIX allows signals to be blocked using functions such as @c sigprocmask()
  83. * and @c pthread_sigmask(). For signals to be delivered, programs must ensure
  84. * that any signals registered using signal_set objects are unblocked in at
  85. * least one thread.
  86. */
  87. template <typename SignalSetService = signal_set_service>
  88. class basic_signal_set
  89. : public basic_io_object<SignalSetService>
  90. {
  91. public:
  92. /// Construct a signal set without adding any signals.
  93. /**
  94. * This constructor creates a signal set without registering for any signals.
  95. *
  96. * @param io_context The io_context object that the signal set will use to
  97. * dispatch handlers for any asynchronous operations performed on the set.
  98. */
  99. explicit basic_signal_set(boost::asio::io_context& io_context)
  100. : basic_io_object<SignalSetService>(io_context)
  101. {
  102. }
  103. /// Construct a signal set and add one signal.
  104. /**
  105. * This constructor creates a signal set and registers for one signal.
  106. *
  107. * @param io_context The io_context object that the signal set will use to
  108. * dispatch handlers for any asynchronous operations performed on the set.
  109. *
  110. * @param signal_number_1 The signal number to be added.
  111. *
  112. * @note This constructor is equivalent to performing:
  113. * @code boost::asio::signal_set signals(io_context);
  114. * signals.add(signal_number_1); @endcode
  115. */
  116. basic_signal_set(boost::asio::io_context& io_context, int signal_number_1)
  117. : basic_io_object<SignalSetService>(io_context)
  118. {
  119. boost::system::error_code ec;
  120. this->get_service().add(this->get_implementation(), signal_number_1, ec);
  121. boost::asio::detail::throw_error(ec, "add");
  122. }
  123. /// Construct a signal set and add two signals.
  124. /**
  125. * This constructor creates a signal set and registers for two signals.
  126. *
  127. * @param io_context The io_context object that the signal set will use to
  128. * dispatch handlers for any asynchronous operations performed on the set.
  129. *
  130. * @param signal_number_1 The first signal number to be added.
  131. *
  132. * @param signal_number_2 The second signal number to be added.
  133. *
  134. * @note This constructor is equivalent to performing:
  135. * @code boost::asio::signal_set signals(io_context);
  136. * signals.add(signal_number_1);
  137. * signals.add(signal_number_2); @endcode
  138. */
  139. basic_signal_set(boost::asio::io_context& io_context, int signal_number_1,
  140. int signal_number_2)
  141. : basic_io_object<SignalSetService>(io_context)
  142. {
  143. boost::system::error_code ec;
  144. this->get_service().add(this->get_implementation(), signal_number_1, ec);
  145. boost::asio::detail::throw_error(ec, "add");
  146. this->get_service().add(this->get_implementation(), signal_number_2, ec);
  147. boost::asio::detail::throw_error(ec, "add");
  148. }
  149. /// Construct a signal set and add three signals.
  150. /**
  151. * This constructor creates a signal set and registers for three signals.
  152. *
  153. * @param io_context The io_context object that the signal set will use to
  154. * dispatch handlers for any asynchronous operations performed on the set.
  155. *
  156. * @param signal_number_1 The first signal number to be added.
  157. *
  158. * @param signal_number_2 The second signal number to be added.
  159. *
  160. * @param signal_number_3 The third signal number to be added.
  161. *
  162. * @note This constructor is equivalent to performing:
  163. * @code boost::asio::signal_set signals(io_context);
  164. * signals.add(signal_number_1);
  165. * signals.add(signal_number_2);
  166. * signals.add(signal_number_3); @endcode
  167. */
  168. basic_signal_set(boost::asio::io_context& io_context, int signal_number_1,
  169. int signal_number_2, int signal_number_3)
  170. : basic_io_object<SignalSetService>(io_context)
  171. {
  172. boost::system::error_code ec;
  173. this->get_service().add(this->get_implementation(), signal_number_1, ec);
  174. boost::asio::detail::throw_error(ec, "add");
  175. this->get_service().add(this->get_implementation(), signal_number_2, ec);
  176. boost::asio::detail::throw_error(ec, "add");
  177. this->get_service().add(this->get_implementation(), signal_number_3, ec);
  178. boost::asio::detail::throw_error(ec, "add");
  179. }
  180. /// Add a signal to a signal_set.
  181. /**
  182. * This function adds the specified signal to the set. It has no effect if the
  183. * signal is already in the set.
  184. *
  185. * @param signal_number The signal to be added to the set.
  186. *
  187. * @throws boost::system::system_error Thrown on failure.
  188. */
  189. void add(int signal_number)
  190. {
  191. boost::system::error_code ec;
  192. this->get_service().add(this->get_implementation(), signal_number, ec);
  193. boost::asio::detail::throw_error(ec, "add");
  194. }
  195. /// Add a signal to a signal_set.
  196. /**
  197. * This function adds the specified signal to the set. It has no effect if the
  198. * signal is already in the set.
  199. *
  200. * @param signal_number The signal to be added to the set.
  201. *
  202. * @param ec Set to indicate what error occurred, if any.
  203. */
  204. BOOST_ASIO_SYNC_OP_VOID add(int signal_number, boost::system::error_code& ec)
  205. {
  206. this->get_service().add(this->get_implementation(), signal_number, ec);
  207. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  208. }
  209. /// Remove a signal from a signal_set.
  210. /**
  211. * This function removes the specified signal from the set. It has no effect
  212. * if the signal is not in the set.
  213. *
  214. * @param signal_number The signal to be removed from the set.
  215. *
  216. * @throws boost::system::system_error Thrown on failure.
  217. *
  218. * @note Removes any notifications that have been queued for the specified
  219. * signal number.
  220. */
  221. void remove(int signal_number)
  222. {
  223. boost::system::error_code ec;
  224. this->get_service().remove(this->get_implementation(), signal_number, ec);
  225. boost::asio::detail::throw_error(ec, "remove");
  226. }
  227. /// Remove a signal from a signal_set.
  228. /**
  229. * This function removes the specified signal from the set. It has no effect
  230. * if the signal is not in the set.
  231. *
  232. * @param signal_number The signal to be removed from the set.
  233. *
  234. * @param ec Set to indicate what error occurred, if any.
  235. *
  236. * @note Removes any notifications that have been queued for the specified
  237. * signal number.
  238. */
  239. BOOST_ASIO_SYNC_OP_VOID remove(int signal_number,
  240. boost::system::error_code& ec)
  241. {
  242. this->get_service().remove(this->get_implementation(), signal_number, ec);
  243. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  244. }
  245. /// Remove all signals from a signal_set.
  246. /**
  247. * This function removes all signals from the set. It has no effect if the set
  248. * is already empty.
  249. *
  250. * @throws boost::system::system_error Thrown on failure.
  251. *
  252. * @note Removes all queued notifications.
  253. */
  254. void clear()
  255. {
  256. boost::system::error_code ec;
  257. this->get_service().clear(this->get_implementation(), ec);
  258. boost::asio::detail::throw_error(ec, "clear");
  259. }
  260. /// Remove all signals from a signal_set.
  261. /**
  262. * This function removes all signals from the set. It has no effect if the set
  263. * is already empty.
  264. *
  265. * @param ec Set to indicate what error occurred, if any.
  266. *
  267. * @note Removes all queued notifications.
  268. */
  269. BOOST_ASIO_SYNC_OP_VOID clear(boost::system::error_code& ec)
  270. {
  271. this->get_service().clear(this->get_implementation(), ec);
  272. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  273. }
  274. /// Cancel all operations associated with the signal set.
  275. /**
  276. * This function forces the completion of any pending asynchronous wait
  277. * operations against the signal set. The handler for each cancelled
  278. * operation will be invoked with the boost::asio::error::operation_aborted
  279. * error code.
  280. *
  281. * Cancellation does not alter the set of registered signals.
  282. *
  283. * @throws boost::system::system_error Thrown on failure.
  284. *
  285. * @note If a registered signal occurred before cancel() is called, then the
  286. * handlers for asynchronous wait operations will:
  287. *
  288. * @li have already been invoked; or
  289. *
  290. * @li have been queued for invocation in the near future.
  291. *
  292. * These handlers can no longer be cancelled, and therefore are passed an
  293. * error code that indicates the successful completion of the wait operation.
  294. */
  295. void cancel()
  296. {
  297. boost::system::error_code ec;
  298. this->get_service().cancel(this->get_implementation(), ec);
  299. boost::asio::detail::throw_error(ec, "cancel");
  300. }
  301. /// Cancel all operations associated with the signal set.
  302. /**
  303. * This function forces the completion of any pending asynchronous wait
  304. * operations against the signal set. The handler for each cancelled
  305. * operation will be invoked with the boost::asio::error::operation_aborted
  306. * error code.
  307. *
  308. * Cancellation does not alter the set of registered signals.
  309. *
  310. * @param ec Set to indicate what error occurred, if any.
  311. *
  312. * @note If a registered signal occurred before cancel() is called, then the
  313. * handlers for asynchronous wait operations will:
  314. *
  315. * @li have already been invoked; or
  316. *
  317. * @li have been queued for invocation in the near future.
  318. *
  319. * These handlers can no longer be cancelled, and therefore are passed an
  320. * error code that indicates the successful completion of the wait operation.
  321. */
  322. BOOST_ASIO_SYNC_OP_VOID cancel(boost::system::error_code& ec)
  323. {
  324. this->get_service().cancel(this->get_implementation(), ec);
  325. BOOST_ASIO_SYNC_OP_VOID_RETURN(ec);
  326. }
  327. /// Start an asynchronous operation to wait for a signal to be delivered.
  328. /**
  329. * This function may be used to initiate an asynchronous wait against the
  330. * signal set. It always returns immediately.
  331. *
  332. * For each call to async_wait(), the supplied handler will be called exactly
  333. * once. The handler will be called when:
  334. *
  335. * @li One of the registered signals in the signal set occurs; or
  336. *
  337. * @li The signal set was cancelled, in which case the handler is passed the
  338. * error code boost::asio::error::operation_aborted.
  339. *
  340. * @param handler The handler to be called when the signal occurs. Copies
  341. * will be made of the handler as required. The function signature of the
  342. * handler must be:
  343. * @code void handler(
  344. * const boost::system::error_code& error, // Result of operation.
  345. * int signal_number // Indicates which signal occurred.
  346. * ); @endcode
  347. * Regardless of whether the asynchronous operation completes immediately or
  348. * not, the handler will not be invoked from within this function. Invocation
  349. * of the handler will be performed in a manner equivalent to using
  350. * boost::asio::io_context::post().
  351. */
  352. template <typename SignalHandler>
  353. BOOST_ASIO_INITFN_RESULT_TYPE(SignalHandler,
  354. void (boost::system::error_code, int))
  355. async_wait(BOOST_ASIO_MOVE_ARG(SignalHandler) handler)
  356. {
  357. // If you get an error on the following line it means that your handler does
  358. // not meet the documented type requirements for a SignalHandler.
  359. BOOST_ASIO_SIGNAL_HANDLER_CHECK(SignalHandler, handler) type_check;
  360. return this->get_service().async_wait(this->get_implementation(),
  361. BOOST_ASIO_MOVE_CAST(SignalHandler)(handler));
  362. }
  363. };
  364. } // namespace asio
  365. } // namespace boost
  366. #include <boost/asio/detail/pop_options.hpp>
  367. #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
  368. #endif // BOOST_ASIO_BASIC_SIGNAL_SET_HPP