io_context.hpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  1. //
  2. // io_context.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_IO_CONTEXT_HPP
  11. #define BOOST_ASIO_IO_CONTEXT_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 <cstddef>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include <boost/asio/async_result.hpp>
  20. #include <boost/asio/detail/chrono.hpp>
  21. #include <boost/asio/detail/concurrency_hint.hpp>
  22. #include <boost/asio/detail/cstdint.hpp>
  23. #include <boost/asio/detail/wrapped_handler.hpp>
  24. #include <boost/system/error_code.hpp>
  25. #include <boost/asio/execution.hpp>
  26. #include <boost/asio/execution_context.hpp>
  27. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  28. # include <boost/asio/detail/winsock_init.hpp>
  29. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  30. || defined(__osf__)
  31. # include <boost/asio/detail/signal_init.hpp>
  32. #endif
  33. #if defined(BOOST_ASIO_HAS_IOCP)
  34. # include <boost/asio/detail/win_iocp_io_context.hpp>
  35. #else
  36. # include <boost/asio/detail/scheduler.hpp>
  37. #endif
  38. #include <boost/asio/detail/push_options.hpp>
  39. namespace boost {
  40. namespace asio {
  41. namespace detail {
  42. #if defined(BOOST_ASIO_HAS_IOCP)
  43. typedef win_iocp_io_context io_context_impl;
  44. class win_iocp_overlapped_ptr;
  45. #else
  46. typedef scheduler io_context_impl;
  47. #endif
  48. struct io_context_bits
  49. {
  50. static constexpr uintptr_t blocking_never = 1;
  51. static constexpr uintptr_t relationship_continuation = 2;
  52. static constexpr uintptr_t outstanding_work_tracked = 4;
  53. static constexpr uintptr_t runtime_bits = 3;
  54. };
  55. } // namespace detail
  56. /// Provides core I/O functionality.
  57. /**
  58. * The io_context class provides the core I/O functionality for users of the
  59. * asynchronous I/O objects, including:
  60. *
  61. * @li boost::asio::ip::tcp::socket
  62. * @li boost::asio::ip::tcp::acceptor
  63. * @li boost::asio::ip::udp::socket
  64. * @li boost::asio::deadline_timer.
  65. *
  66. * The io_context class also includes facilities intended for developers of
  67. * custom asynchronous services.
  68. *
  69. * @par Thread Safety
  70. * @e Distinct @e objects: Safe.@n
  71. * @e Shared @e objects: Safe, with the specific exceptions of the restart()
  72. * and notify_fork() functions. Calling restart() while there are unfinished
  73. * run(), run_one(), run_for(), run_until(), poll() or poll_one() calls results
  74. * in undefined behaviour. The notify_fork() function should not be called
  75. * while any io_context function, or any function on an I/O object that is
  76. * associated with the io_context, is being called in another thread.
  77. *
  78. * @par Concepts:
  79. * Dispatcher.
  80. *
  81. * @par Synchronous and asynchronous operations
  82. *
  83. * Synchronous operations on I/O objects implicitly run the io_context object
  84. * for an individual operation. The io_context functions run(), run_one(),
  85. * run_for(), run_until(), poll() or poll_one() must be called for the
  86. * io_context to perform asynchronous operations on behalf of a C++ program.
  87. * Notification that an asynchronous operation has completed is delivered by
  88. * invocation of the associated handler. Handlers are invoked only by a thread
  89. * that is currently calling any overload of run(), run_one(), run_for(),
  90. * run_until(), poll() or poll_one() for the io_context.
  91. *
  92. * @par Effect of exceptions thrown from handlers
  93. *
  94. * If an exception is thrown from a handler, the exception is allowed to
  95. * propagate through the throwing thread's invocation of run(), run_one(),
  96. * run_for(), run_until(), poll() or poll_one(). No other threads that are
  97. * calling any of these functions are affected. It is then the responsibility
  98. * of the application to catch the exception.
  99. *
  100. * After the exception has been caught, the run(), run_one(), run_for(),
  101. * run_until(), poll() or poll_one() call may be restarted @em without the need
  102. * for an intervening call to restart(). This allows the thread to rejoin the
  103. * io_context object's thread pool without impacting any other threads in the
  104. * pool.
  105. *
  106. * For example:
  107. *
  108. * @code
  109. * boost::asio::io_context io_context;
  110. * ...
  111. * for (;;)
  112. * {
  113. * try
  114. * {
  115. * io_context.run();
  116. * break; // run() exited normally
  117. * }
  118. * catch (my_exception& e)
  119. * {
  120. * // Deal with exception as appropriate.
  121. * }
  122. * }
  123. * @endcode
  124. *
  125. * @par Submitting arbitrary tasks to the io_context
  126. *
  127. * To submit functions to the io_context, use the @ref boost::asio::dispatch,
  128. * @ref boost::asio::post or @ref boost::asio::defer free functions.
  129. *
  130. * For example:
  131. *
  132. * @code void my_task()
  133. * {
  134. * ...
  135. * }
  136. *
  137. * ...
  138. *
  139. * boost::asio::io_context io_context;
  140. *
  141. * // Submit a function to the io_context.
  142. * boost::asio::post(io_context, my_task);
  143. *
  144. * // Submit a lambda object to the io_context.
  145. * boost::asio::post(io_context,
  146. * []()
  147. * {
  148. * ...
  149. * });
  150. *
  151. * // Run the io_context until it runs out of work.
  152. * io_context.run(); @endcode
  153. *
  154. * @par Stopping the io_context from running out of work
  155. *
  156. * Some applications may need to prevent an io_context object's run() call from
  157. * returning when there is no more work to do. For example, the io_context may
  158. * be being run in a background thread that is launched prior to the
  159. * application's asynchronous operations. The run() call may be kept running by
  160. * using the @ref make_work_guard function to create an object of type
  161. * boost::asio::executor_work_guard<io_context::executor_type>:
  162. *
  163. * @code boost::asio::io_context io_context;
  164. * boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
  165. * = boost::asio::make_work_guard(io_context);
  166. * ... @endcode
  167. *
  168. * To effect a shutdown, the application will then need to call the io_context
  169. * object's stop() member function. This will cause the io_context run() call
  170. * to return as soon as possible, abandoning unfinished operations and without
  171. * permitting ready handlers to be dispatched.
  172. *
  173. * Alternatively, if the application requires that all operations and handlers
  174. * be allowed to finish normally, the work object may be explicitly reset.
  175. *
  176. * @code boost::asio::io_context io_context;
  177. * boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
  178. * = boost::asio::make_work_guard(io_context);
  179. * ...
  180. * work.reset(); // Allow run() to exit. @endcode
  181. */
  182. class io_context
  183. : public execution_context
  184. {
  185. private:
  186. typedef detail::io_context_impl impl_type;
  187. #if defined(BOOST_ASIO_HAS_IOCP)
  188. friend class detail::win_iocp_overlapped_ptr;
  189. #endif
  190. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  191. struct initiate_dispatch;
  192. struct initiate_post;
  193. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  194. public:
  195. template <typename Allocator, uintptr_t Bits>
  196. class basic_executor_type;
  197. template <typename Allocator, uintptr_t Bits>
  198. friend class basic_executor_type;
  199. /// Executor used to submit functions to an io_context.
  200. typedef basic_executor_type<std::allocator<void>, 0> executor_type;
  201. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  202. class work;
  203. friend class work;
  204. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  205. class service;
  206. #if !defined(BOOST_ASIO_NO_EXTENSIONS) \
  207. && !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  208. class strand;
  209. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  210. // && !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  211. /// The type used to count the number of handlers executed by the context.
  212. typedef std::size_t count_type;
  213. /// Constructor.
  214. BOOST_ASIO_DECL io_context();
  215. /// Constructor.
  216. /**
  217. * Construct with a hint about the required level of concurrency.
  218. *
  219. * @param concurrency_hint A suggestion to the implementation on how many
  220. * threads it should allow to run simultaneously.
  221. */
  222. BOOST_ASIO_DECL explicit io_context(int concurrency_hint);
  223. /// Destructor.
  224. /**
  225. * On destruction, the io_context performs the following sequence of
  226. * operations:
  227. *
  228. * @li For each service object @c svc in the io_context set, in reverse order
  229. * of the beginning of service object lifetime, performs
  230. * @c svc->shutdown().
  231. *
  232. * @li Uninvoked handler objects that were scheduled for deferred invocation
  233. * on the io_context, or any associated strand, are destroyed.
  234. *
  235. * @li For each service object @c svc in the io_context set, in reverse order
  236. * of the beginning of service object lifetime, performs
  237. * <tt>delete static_cast<io_context::service*>(svc)</tt>.
  238. *
  239. * @note The destruction sequence described above permits programs to
  240. * simplify their resource management by using @c shared_ptr<>. Where an
  241. * object's lifetime is tied to the lifetime of a connection (or some other
  242. * sequence of asynchronous operations), a @c shared_ptr to the object would
  243. * be bound into the handlers for all asynchronous operations associated with
  244. * it. This works as follows:
  245. *
  246. * @li When a single connection ends, all associated asynchronous operations
  247. * complete. The corresponding handler objects are destroyed, and all
  248. * @c shared_ptr references to the objects are destroyed.
  249. *
  250. * @li To shut down the whole program, the io_context function stop() is
  251. * called to terminate any run() calls as soon as possible. The io_context
  252. * destructor defined above destroys all handlers, causing all @c shared_ptr
  253. * references to all connection objects to be destroyed.
  254. */
  255. BOOST_ASIO_DECL ~io_context();
  256. /// Obtains the executor associated with the io_context.
  257. executor_type get_executor() noexcept;
  258. /// Run the io_context object's event processing loop.
  259. /**
  260. * The run() function blocks until all work has finished and there are no
  261. * more handlers to be dispatched, or until the io_context has been stopped.
  262. *
  263. * Multiple threads may call the run() function to set up a pool of threads
  264. * from which the io_context may execute handlers. All threads that are
  265. * waiting in the pool are equivalent and the io_context may choose any one
  266. * of them to invoke a handler.
  267. *
  268. * A normal exit from the run() function implies that the io_context object
  269. * is stopped (the stopped() function returns @c true). Subsequent calls to
  270. * run(), run_one(), poll() or poll_one() will return immediately unless there
  271. * is a prior call to restart().
  272. *
  273. * @return The number of handlers that were executed.
  274. *
  275. * @note Calling the run() function from a thread that is currently calling
  276. * one of run(), run_one(), run_for(), run_until(), poll() or poll_one() on
  277. * the same io_context object may introduce the potential for deadlock. It is
  278. * the caller's reponsibility to avoid this.
  279. *
  280. * The poll() function may also be used to dispatch ready handlers, but
  281. * without blocking.
  282. */
  283. BOOST_ASIO_DECL count_type run();
  284. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  285. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  286. /// event processing loop.
  287. /**
  288. * The run() function blocks until all work has finished and there are no
  289. * more handlers to be dispatched, or until the io_context has been stopped.
  290. *
  291. * Multiple threads may call the run() function to set up a pool of threads
  292. * from which the io_context may execute handlers. All threads that are
  293. * waiting in the pool are equivalent and the io_context may choose any one
  294. * of them to invoke a handler.
  295. *
  296. * A normal exit from the run() function implies that the io_context object
  297. * is stopped (the stopped() function returns @c true). Subsequent calls to
  298. * run(), run_one(), poll() or poll_one() will return immediately unless there
  299. * is a prior call to restart().
  300. *
  301. * @param ec Set to indicate what error occurred, if any.
  302. *
  303. * @return The number of handlers that were executed.
  304. *
  305. * @note Calling the run() function from a thread that is currently calling
  306. * one of run(), run_one(), run_for(), run_until(), poll() or poll_one() on
  307. * the same io_context object may introduce the potential for deadlock. It is
  308. * the caller's reponsibility to avoid this.
  309. *
  310. * The poll() function may also be used to dispatch ready handlers, but
  311. * without blocking.
  312. */
  313. BOOST_ASIO_DECL count_type run(boost::system::error_code& ec);
  314. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  315. /// Run the io_context object's event processing loop for a specified
  316. /// duration.
  317. /**
  318. * The run_for() function blocks until all work has finished and there are no
  319. * more handlers to be dispatched, until the io_context has been stopped, or
  320. * until the specified duration has elapsed.
  321. *
  322. * @param rel_time The duration for which the call may block.
  323. *
  324. * @return The number of handlers that were executed.
  325. */
  326. template <typename Rep, typename Period>
  327. std::size_t run_for(const chrono::duration<Rep, Period>& rel_time);
  328. /// Run the io_context object's event processing loop until a specified time.
  329. /**
  330. * The run_until() function blocks until all work has finished and there are
  331. * no more handlers to be dispatched, until the io_context has been stopped,
  332. * or until the specified time has been reached.
  333. *
  334. * @param abs_time The time point until which the call may block.
  335. *
  336. * @return The number of handlers that were executed.
  337. */
  338. template <typename Clock, typename Duration>
  339. std::size_t run_until(const chrono::time_point<Clock, Duration>& abs_time);
  340. /// Run the io_context object's event processing loop to execute at most one
  341. /// handler.
  342. /**
  343. * The run_one() function blocks until one handler has been dispatched, or
  344. * until the io_context has been stopped.
  345. *
  346. * @return The number of handlers that were executed. A zero return value
  347. * implies that the io_context object is stopped (the stopped() function
  348. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  349. * poll_one() will return immediately unless there is a prior call to
  350. * restart().
  351. *
  352. * @note Calling the run_one() function from a thread that is currently
  353. * calling one of run(), run_one(), run_for(), run_until(), poll() or
  354. * poll_one() on the same io_context object may introduce the potential for
  355. * deadlock. It is the caller's reponsibility to avoid this.
  356. */
  357. BOOST_ASIO_DECL count_type run_one();
  358. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  359. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  360. /// event processing loop to execute at most one handler.
  361. /**
  362. * The run_one() function blocks until one handler has been dispatched, or
  363. * until the io_context has been stopped.
  364. *
  365. * @return The number of handlers that were executed. A zero return value
  366. * implies that the io_context object is stopped (the stopped() function
  367. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  368. * poll_one() will return immediately unless there is a prior call to
  369. * restart().
  370. *
  371. * @return The number of handlers that were executed.
  372. *
  373. * @note Calling the run_one() function from a thread that is currently
  374. * calling one of run(), run_one(), run_for(), run_until(), poll() or
  375. * poll_one() on the same io_context object may introduce the potential for
  376. * deadlock. It is the caller's reponsibility to avoid this.
  377. */
  378. BOOST_ASIO_DECL count_type run_one(boost::system::error_code& ec);
  379. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  380. /// Run the io_context object's event processing loop for a specified duration
  381. /// to execute at most one handler.
  382. /**
  383. * The run_one_for() function blocks until one handler has been dispatched,
  384. * until the io_context has been stopped, or until the specified duration has
  385. * elapsed.
  386. *
  387. * @param rel_time The duration for which the call may block.
  388. *
  389. * @return The number of handlers that were executed.
  390. */
  391. template <typename Rep, typename Period>
  392. std::size_t run_one_for(const chrono::duration<Rep, Period>& rel_time);
  393. /// Run the io_context object's event processing loop until a specified time
  394. /// to execute at most one handler.
  395. /**
  396. * The run_one_until() function blocks until one handler has been dispatched,
  397. * until the io_context has been stopped, or until the specified time has
  398. * been reached.
  399. *
  400. * @param abs_time The time point until which the call may block.
  401. *
  402. * @return The number of handlers that were executed.
  403. */
  404. template <typename Clock, typename Duration>
  405. std::size_t run_one_until(
  406. const chrono::time_point<Clock, Duration>& abs_time);
  407. /// Run the io_context object's event processing loop to execute ready
  408. /// handlers.
  409. /**
  410. * The poll() function runs handlers that are ready to run, without blocking,
  411. * until the io_context has been stopped or there are no more ready handlers.
  412. *
  413. * @return The number of handlers that were executed.
  414. */
  415. BOOST_ASIO_DECL count_type poll();
  416. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  417. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  418. /// event processing loop to execute ready handlers.
  419. /**
  420. * The poll() function runs handlers that are ready to run, without blocking,
  421. * until the io_context has been stopped or there are no more ready handlers.
  422. *
  423. * @param ec Set to indicate what error occurred, if any.
  424. *
  425. * @return The number of handlers that were executed.
  426. */
  427. BOOST_ASIO_DECL count_type poll(boost::system::error_code& ec);
  428. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  429. /// Run the io_context object's event processing loop to execute one ready
  430. /// handler.
  431. /**
  432. * The poll_one() function runs at most one handler that is ready to run,
  433. * without blocking.
  434. *
  435. * @return The number of handlers that were executed.
  436. */
  437. BOOST_ASIO_DECL count_type poll_one();
  438. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  439. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  440. /// event processing loop to execute one ready handler.
  441. /**
  442. * The poll_one() function runs at most one handler that is ready to run,
  443. * without blocking.
  444. *
  445. * @param ec Set to indicate what error occurred, if any.
  446. *
  447. * @return The number of handlers that were executed.
  448. */
  449. BOOST_ASIO_DECL count_type poll_one(boost::system::error_code& ec);
  450. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  451. /// Stop the io_context object's event processing loop.
  452. /**
  453. * This function does not block, but instead simply signals the io_context to
  454. * stop. All invocations of its run() or run_one() member functions should
  455. * return as soon as possible. Subsequent calls to run(), run_one(), poll()
  456. * or poll_one() will return immediately until restart() is called.
  457. */
  458. BOOST_ASIO_DECL void stop();
  459. /// Determine whether the io_context object has been stopped.
  460. /**
  461. * This function is used to determine whether an io_context object has been
  462. * stopped, either through an explicit call to stop(), or due to running out
  463. * of work. When an io_context object is stopped, calls to run(), run_one(),
  464. * poll() or poll_one() will return immediately without invoking any
  465. * handlers.
  466. *
  467. * @return @c true if the io_context object is stopped, otherwise @c false.
  468. */
  469. BOOST_ASIO_DECL bool stopped() const;
  470. /// Restart the io_context in preparation for a subsequent run() invocation.
  471. /**
  472. * This function must be called prior to any second or later set of
  473. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  474. * previous invocation of these functions returned due to the io_context
  475. * being stopped or running out of work. After a call to restart(), the
  476. * io_context object's stopped() function will return @c false.
  477. *
  478. * This function must not be called while there are any unfinished calls to
  479. * the run(), run_one(), poll() or poll_one() functions.
  480. */
  481. BOOST_ASIO_DECL void restart();
  482. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  483. /// (Deprecated: Use restart().) Reset the io_context in preparation for a
  484. /// subsequent run() invocation.
  485. /**
  486. * This function must be called prior to any second or later set of
  487. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  488. * previous invocation of these functions returned due to the io_context
  489. * being stopped or running out of work. After a call to restart(), the
  490. * io_context object's stopped() function will return @c false.
  491. *
  492. * This function must not be called while there are any unfinished calls to
  493. * the run(), run_one(), poll() or poll_one() functions.
  494. */
  495. void reset();
  496. /// (Deprecated: Use boost::asio::dispatch().) Request the io_context to
  497. /// invoke the given handler.
  498. /**
  499. * This function is used to ask the io_context to execute the given handler.
  500. *
  501. * The io_context guarantees that the handler will only be called in a thread
  502. * in which the run(), run_one(), poll() or poll_one() member functions is
  503. * currently being invoked. The handler may be executed inside this function
  504. * if the guarantee can be met.
  505. *
  506. * @param handler The handler to be called. The io_context will make
  507. * a copy of the handler object as required. The function signature of the
  508. * handler must be: @code void handler(); @endcode
  509. *
  510. * @note This function throws an exception only if:
  511. *
  512. * @li the handler's associated allocator; or
  513. *
  514. * @li the handler's copy constructor
  515. *
  516. * throws an exception.
  517. */
  518. template <typename LegacyCompletionHandler>
  519. auto dispatch(LegacyCompletionHandler&& handler)
  520. -> decltype(
  521. async_initiate<LegacyCompletionHandler, void ()>(
  522. declval<initiate_dispatch>(), handler, this));
  523. /// (Deprecated: Use boost::asio::post().) Request the io_context to invoke
  524. /// the given handler and return immediately.
  525. /**
  526. * This function is used to ask the io_context to execute the given handler,
  527. * but without allowing the io_context to call the handler from inside this
  528. * function.
  529. *
  530. * The io_context guarantees that the handler will only be called in a thread
  531. * in which the run(), run_one(), poll() or poll_one() member functions is
  532. * currently being invoked.
  533. *
  534. * @param handler The handler to be called. The io_context will make
  535. * a copy of the handler object as required. The function signature of the
  536. * handler must be: @code void handler(); @endcode
  537. *
  538. * @note This function throws an exception only if:
  539. *
  540. * @li the handler's associated allocator; or
  541. *
  542. * @li the handler's copy constructor
  543. *
  544. * throws an exception.
  545. */
  546. template <typename LegacyCompletionHandler>
  547. auto post(LegacyCompletionHandler&& handler)
  548. -> decltype(
  549. async_initiate<LegacyCompletionHandler, void ()>(
  550. declval<initiate_post>(), handler, this));
  551. /// (Deprecated: Use boost::asio::bind_executor().) Create a new handler that
  552. /// automatically dispatches the wrapped handler on the io_context.
  553. /**
  554. * This function is used to create a new handler function object that, when
  555. * invoked, will automatically pass the wrapped handler to the io_context
  556. * object's dispatch function.
  557. *
  558. * @param handler The handler to be wrapped. The io_context will make a copy
  559. * of the handler object as required. The function signature of the handler
  560. * must be: @code void handler(A1 a1, ... An an); @endcode
  561. *
  562. * @return A function object that, when invoked, passes the wrapped handler to
  563. * the io_context object's dispatch function. Given a function object with the
  564. * signature:
  565. * @code R f(A1 a1, ... An an); @endcode
  566. * If this function object is passed to the wrap function like so:
  567. * @code io_context.wrap(f); @endcode
  568. * then the return value is a function object with the signature
  569. * @code void g(A1 a1, ... An an); @endcode
  570. * that, when invoked, executes code equivalent to:
  571. * @code io_context.dispatch(boost::bind(f, a1, ... an)); @endcode
  572. */
  573. template <typename Handler>
  574. #if defined(GENERATING_DOCUMENTATION)
  575. unspecified
  576. #else
  577. detail::wrapped_handler<io_context&, Handler>
  578. #endif
  579. wrap(Handler handler);
  580. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  581. private:
  582. io_context(const io_context&) = delete;
  583. io_context& operator=(const io_context&) = delete;
  584. // Helper function to add the implementation.
  585. BOOST_ASIO_DECL impl_type& add_impl(impl_type* impl);
  586. // Backwards compatible overload for use with services derived from
  587. // io_context::service.
  588. template <typename Service>
  589. friend Service& use_service(io_context& ioc);
  590. #if defined(BOOST_ASIO_WINDOWS) || defined(__CYGWIN__)
  591. detail::winsock_init<> init_;
  592. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  593. || defined(__osf__)
  594. detail::signal_init<> init_;
  595. #endif
  596. // The implementation.
  597. impl_type& impl_;
  598. };
  599. /// Executor implementation type used to submit functions to an io_context.
  600. template <typename Allocator, uintptr_t Bits>
  601. class io_context::basic_executor_type :
  602. detail::io_context_bits, Allocator
  603. {
  604. public:
  605. /// Copy constructor.
  606. basic_executor_type(const basic_executor_type& other) noexcept
  607. : Allocator(static_cast<const Allocator&>(other)),
  608. target_(other.target_)
  609. {
  610. if (Bits & outstanding_work_tracked)
  611. if (context_ptr())
  612. context_ptr()->impl_.work_started();
  613. }
  614. /// Move constructor.
  615. basic_executor_type(basic_executor_type&& other) noexcept
  616. : Allocator(static_cast<Allocator&&>(other)),
  617. target_(other.target_)
  618. {
  619. if (Bits & outstanding_work_tracked)
  620. other.target_ = 0;
  621. }
  622. /// Destructor.
  623. ~basic_executor_type() noexcept
  624. {
  625. if (Bits & outstanding_work_tracked)
  626. if (context_ptr())
  627. context_ptr()->impl_.work_finished();
  628. }
  629. /// Assignment operator.
  630. basic_executor_type& operator=(const basic_executor_type& other) noexcept;
  631. /// Move assignment operator.
  632. basic_executor_type& operator=(basic_executor_type&& other) noexcept;
  633. #if !defined(GENERATING_DOCUMENTATION)
  634. private:
  635. friend struct boost_asio_require_fn::impl;
  636. friend struct boost_asio_prefer_fn::impl;
  637. #endif // !defined(GENERATING_DOCUMENTATION)
  638. /// Obtain an executor with the @c blocking.possibly property.
  639. /**
  640. * Do not call this function directly. It is intended for use with the
  641. * boost::asio::require customisation point.
  642. *
  643. * For example:
  644. * @code auto ex1 = my_io_context.get_executor();
  645. * auto ex2 = boost::asio::require(ex1,
  646. * boost::asio::execution::blocking.possibly); @endcode
  647. */
  648. constexpr basic_executor_type require(execution::blocking_t::possibly_t) const
  649. {
  650. return basic_executor_type(context_ptr(),
  651. *this, bits() & ~blocking_never);
  652. }
  653. /// Obtain an executor with the @c blocking.never property.
  654. /**
  655. * Do not call this function directly. It is intended for use with the
  656. * boost::asio::require customisation point.
  657. *
  658. * For example:
  659. * @code auto ex1 = my_io_context.get_executor();
  660. * auto ex2 = boost::asio::require(ex1,
  661. * boost::asio::execution::blocking.never); @endcode
  662. */
  663. constexpr basic_executor_type require(execution::blocking_t::never_t) const
  664. {
  665. return basic_executor_type(context_ptr(),
  666. *this, bits() | blocking_never);
  667. }
  668. /// Obtain an executor with the @c relationship.fork property.
  669. /**
  670. * Do not call this function directly. It is intended for use with the
  671. * boost::asio::require customisation point.
  672. *
  673. * For example:
  674. * @code auto ex1 = my_io_context.get_executor();
  675. * auto ex2 = boost::asio::require(ex1,
  676. * boost::asio::execution::relationship.fork); @endcode
  677. */
  678. constexpr basic_executor_type require(execution::relationship_t::fork_t) const
  679. {
  680. return basic_executor_type(context_ptr(),
  681. *this, bits() & ~relationship_continuation);
  682. }
  683. /// Obtain an executor with the @c relationship.continuation property.
  684. /**
  685. * Do not call this function directly. It is intended for use with the
  686. * boost::asio::require customisation point.
  687. *
  688. * For example:
  689. * @code auto ex1 = my_io_context.get_executor();
  690. * auto ex2 = boost::asio::require(ex1,
  691. * boost::asio::execution::relationship.continuation); @endcode
  692. */
  693. constexpr basic_executor_type require(
  694. execution::relationship_t::continuation_t) const
  695. {
  696. return basic_executor_type(context_ptr(),
  697. *this, bits() | relationship_continuation);
  698. }
  699. /// Obtain an executor with the @c outstanding_work.tracked property.
  700. /**
  701. * Do not call this function directly. It is intended for use with the
  702. * boost::asio::require customisation point.
  703. *
  704. * For example:
  705. * @code auto ex1 = my_io_context.get_executor();
  706. * auto ex2 = boost::asio::require(ex1,
  707. * boost::asio::execution::outstanding_work.tracked); @endcode
  708. */
  709. constexpr basic_executor_type<Allocator,
  710. BOOST_ASIO_UNSPECIFIED(Bits | outstanding_work_tracked)>
  711. require(execution::outstanding_work_t::tracked_t) const
  712. {
  713. return basic_executor_type<Allocator, Bits | outstanding_work_tracked>(
  714. context_ptr(), *this, bits());
  715. }
  716. /// Obtain an executor with the @c outstanding_work.untracked property.
  717. /**
  718. * Do not call this function directly. It is intended for use with the
  719. * boost::asio::require customisation point.
  720. *
  721. * For example:
  722. * @code auto ex1 = my_io_context.get_executor();
  723. * auto ex2 = boost::asio::require(ex1,
  724. * boost::asio::execution::outstanding_work.untracked); @endcode
  725. */
  726. constexpr basic_executor_type<Allocator,
  727. BOOST_ASIO_UNSPECIFIED(Bits & ~outstanding_work_tracked)>
  728. require(execution::outstanding_work_t::untracked_t) const
  729. {
  730. return basic_executor_type<Allocator, Bits & ~outstanding_work_tracked>(
  731. context_ptr(), *this, bits());
  732. }
  733. /// Obtain an executor with the specified @c allocator property.
  734. /**
  735. * Do not call this function directly. It is intended for use with the
  736. * boost::asio::require customisation point.
  737. *
  738. * For example:
  739. * @code auto ex1 = my_io_context.get_executor();
  740. * auto ex2 = boost::asio::require(ex1,
  741. * boost::asio::execution::allocator(my_allocator)); @endcode
  742. */
  743. template <typename OtherAllocator>
  744. constexpr basic_executor_type<OtherAllocator, Bits>
  745. require(execution::allocator_t<OtherAllocator> a) const
  746. {
  747. return basic_executor_type<OtherAllocator, Bits>(
  748. context_ptr(), a.value(), bits());
  749. }
  750. /// Obtain an executor with the default @c allocator property.
  751. /**
  752. * Do not call this function directly. It is intended for use with the
  753. * boost::asio::require customisation point.
  754. *
  755. * For example:
  756. * @code auto ex1 = my_io_context.get_executor();
  757. * auto ex2 = boost::asio::require(ex1,
  758. * boost::asio::execution::allocator); @endcode
  759. */
  760. constexpr basic_executor_type<std::allocator<void>, Bits>
  761. require(execution::allocator_t<void>) const
  762. {
  763. return basic_executor_type<std::allocator<void>, Bits>(
  764. context_ptr(), std::allocator<void>(), bits());
  765. }
  766. #if !defined(GENERATING_DOCUMENTATION)
  767. private:
  768. friend struct boost_asio_query_fn::impl;
  769. friend struct boost::asio::execution::detail::mapping_t<0>;
  770. friend struct boost::asio::execution::detail::outstanding_work_t<0>;
  771. #endif // !defined(GENERATING_DOCUMENTATION)
  772. /// Query the current value of the @c mapping property.
  773. /**
  774. * Do not call this function directly. It is intended for use with the
  775. * boost::asio::query customisation point.
  776. *
  777. * For example:
  778. * @code auto ex = my_io_context.get_executor();
  779. * if (boost::asio::query(ex, boost::asio::execution::mapping)
  780. * == boost::asio::execution::mapping.thread)
  781. * ... @endcode
  782. */
  783. static constexpr execution::mapping_t query(execution::mapping_t) noexcept
  784. {
  785. return execution::mapping.thread;
  786. }
  787. /// Query the current value of the @c context property.
  788. /**
  789. * Do not call this function directly. It is intended for use with the
  790. * boost::asio::query customisation point.
  791. *
  792. * For example:
  793. * @code auto ex = my_io_context.get_executor();
  794. * boost::asio::io_context& ctx = boost::asio::query(
  795. * ex, boost::asio::execution::context); @endcode
  796. */
  797. io_context& query(execution::context_t) const noexcept
  798. {
  799. return *context_ptr();
  800. }
  801. /// Query the current value of the @c blocking property.
  802. /**
  803. * Do not call this function directly. It is intended for use with the
  804. * boost::asio::query customisation point.
  805. *
  806. * For example:
  807. * @code auto ex = my_io_context.get_executor();
  808. * if (boost::asio::query(ex, boost::asio::execution::blocking)
  809. * == boost::asio::execution::blocking.always)
  810. * ... @endcode
  811. */
  812. constexpr execution::blocking_t query(execution::blocking_t) const noexcept
  813. {
  814. return (bits() & blocking_never)
  815. ? execution::blocking_t(execution::blocking.never)
  816. : execution::blocking_t(execution::blocking.possibly);
  817. }
  818. /// Query the current value of the @c relationship property.
  819. /**
  820. * Do not call this function directly. It is intended for use with the
  821. * boost::asio::query customisation point.
  822. *
  823. * For example:
  824. * @code auto ex = my_io_context.get_executor();
  825. * if (boost::asio::query(ex, boost::asio::execution::relationship)
  826. * == boost::asio::execution::relationship.continuation)
  827. * ... @endcode
  828. */
  829. constexpr execution::relationship_t query(
  830. execution::relationship_t) const noexcept
  831. {
  832. return (bits() & relationship_continuation)
  833. ? execution::relationship_t(execution::relationship.continuation)
  834. : execution::relationship_t(execution::relationship.fork);
  835. }
  836. /// Query the current value of the @c outstanding_work property.
  837. /**
  838. * Do not call this function directly. It is intended for use with the
  839. * boost::asio::query customisation point.
  840. *
  841. * For example:
  842. * @code auto ex = my_io_context.get_executor();
  843. * if (boost::asio::query(ex, boost::asio::execution::outstanding_work)
  844. * == boost::asio::execution::outstanding_work.tracked)
  845. * ... @endcode
  846. */
  847. static constexpr execution::outstanding_work_t query(
  848. execution::outstanding_work_t) noexcept
  849. {
  850. return (Bits & outstanding_work_tracked)
  851. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  852. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  853. }
  854. /// Query the current value of the @c allocator property.
  855. /**
  856. * Do not call this function directly. It is intended for use with the
  857. * boost::asio::query customisation point.
  858. *
  859. * For example:
  860. * @code auto ex = my_io_context.get_executor();
  861. * auto alloc = boost::asio::query(ex,
  862. * boost::asio::execution::allocator); @endcode
  863. */
  864. template <typename OtherAllocator>
  865. constexpr Allocator query(
  866. execution::allocator_t<OtherAllocator>) const noexcept
  867. {
  868. return static_cast<const Allocator&>(*this);
  869. }
  870. /// Query the current value of the @c allocator property.
  871. /**
  872. * Do not call this function directly. It is intended for use with the
  873. * boost::asio::query customisation point.
  874. *
  875. * For example:
  876. * @code auto ex = my_io_context.get_executor();
  877. * auto alloc = boost::asio::query(ex,
  878. * boost::asio::execution::allocator); @endcode
  879. */
  880. constexpr Allocator query(execution::allocator_t<void>) const noexcept
  881. {
  882. return static_cast<const Allocator&>(*this);
  883. }
  884. public:
  885. /// Determine whether the io_context is running in the current thread.
  886. /**
  887. * @return @c true if the current thread is running the io_context. Otherwise
  888. * returns @c false.
  889. */
  890. bool running_in_this_thread() const noexcept;
  891. /// Compare two executors for equality.
  892. /**
  893. * Two executors are equal if they refer to the same underlying io_context.
  894. */
  895. friend bool operator==(const basic_executor_type& a,
  896. const basic_executor_type& b) noexcept
  897. {
  898. return a.target_ == b.target_
  899. && static_cast<const Allocator&>(a) == static_cast<const Allocator&>(b);
  900. }
  901. /// Compare two executors for inequality.
  902. /**
  903. * Two executors are equal if they refer to the same underlying io_context.
  904. */
  905. friend bool operator!=(const basic_executor_type& a,
  906. const basic_executor_type& b) noexcept
  907. {
  908. return a.target_ != b.target_
  909. || static_cast<const Allocator&>(a) != static_cast<const Allocator&>(b);
  910. }
  911. /// Execution function.
  912. template <typename Function>
  913. void execute(Function&& f) const;
  914. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  915. public:
  916. /// Obtain the underlying execution context.
  917. io_context& context() const noexcept;
  918. /// Inform the io_context that it has some outstanding work to do.
  919. /**
  920. * This function is used to inform the io_context that some work has begun.
  921. * This ensures that the io_context's run() and run_one() functions do not
  922. * exit while the work is underway.
  923. */
  924. void on_work_started() const noexcept;
  925. /// Inform the io_context that some work is no longer outstanding.
  926. /**
  927. * This function is used to inform the io_context that some work has
  928. * finished. Once the count of unfinished work reaches zero, the io_context
  929. * is stopped and the run() and run_one() functions may exit.
  930. */
  931. void on_work_finished() const noexcept;
  932. /// Request the io_context to invoke the given function object.
  933. /**
  934. * This function is used to ask the io_context to execute the given function
  935. * object. If the current thread is running the io_context, @c dispatch()
  936. * executes the function before returning. Otherwise, the function will be
  937. * scheduled to run on the io_context.
  938. *
  939. * @param f The function object to be called. The executor will make a copy
  940. * of the handler object as required. The function signature of the function
  941. * object must be: @code void function(); @endcode
  942. *
  943. * @param a An allocator that may be used by the executor to allocate the
  944. * internal storage needed for function invocation.
  945. */
  946. template <typename Function, typename OtherAllocator>
  947. void dispatch(Function&& f, const OtherAllocator& a) const;
  948. /// Request the io_context to invoke the given function object.
  949. /**
  950. * This function is used to ask the io_context to execute the given function
  951. * object. The function object will never be executed inside @c post().
  952. * Instead, it will be scheduled to run on the io_context.
  953. *
  954. * @param f The function object to be called. The executor will make a copy
  955. * of the handler object as required. The function signature of the function
  956. * object must be: @code void function(); @endcode
  957. *
  958. * @param a An allocator that may be used by the executor to allocate the
  959. * internal storage needed for function invocation.
  960. */
  961. template <typename Function, typename OtherAllocator>
  962. void post(Function&& f, const OtherAllocator& a) const;
  963. /// Request the io_context to invoke the given function object.
  964. /**
  965. * This function is used to ask the io_context to execute the given function
  966. * object. The function object will never be executed inside @c defer().
  967. * Instead, it will be scheduled to run on the io_context.
  968. *
  969. * If the current thread belongs to the io_context, @c defer() will delay
  970. * scheduling the function object until the current thread returns control to
  971. * the pool.
  972. *
  973. * @param f The function object to be called. The executor will make a copy
  974. * of the handler object as required. The function signature of the function
  975. * object must be: @code void function(); @endcode
  976. *
  977. * @param a An allocator that may be used by the executor to allocate the
  978. * internal storage needed for function invocation.
  979. */
  980. template <typename Function, typename OtherAllocator>
  981. void defer(Function&& f, const OtherAllocator& a) const;
  982. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  983. private:
  984. friend class io_context;
  985. template <typename, uintptr_t> friend class basic_executor_type;
  986. // Constructor used by io_context::get_executor().
  987. explicit basic_executor_type(io_context& i) noexcept
  988. : Allocator(),
  989. target_(reinterpret_cast<uintptr_t>(&i))
  990. {
  991. if (Bits & outstanding_work_tracked)
  992. context_ptr()->impl_.work_started();
  993. }
  994. // Constructor used by require().
  995. basic_executor_type(io_context* i,
  996. const Allocator& a, uintptr_t bits) noexcept
  997. : Allocator(a),
  998. target_(reinterpret_cast<uintptr_t>(i) | bits)
  999. {
  1000. if (Bits & outstanding_work_tracked)
  1001. if (context_ptr())
  1002. context_ptr()->impl_.work_started();
  1003. }
  1004. io_context* context_ptr() const noexcept
  1005. {
  1006. return reinterpret_cast<io_context*>(target_ & ~runtime_bits);
  1007. }
  1008. uintptr_t bits() const noexcept
  1009. {
  1010. return target_ & runtime_bits;
  1011. }
  1012. // The underlying io_context and runtime bits.
  1013. uintptr_t target_;
  1014. };
  1015. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1016. /// (Deprecated: Use executor_work_guard.) Class to inform the io_context when
  1017. /// it has work to do.
  1018. /**
  1019. * The work class is used to inform the io_context when work starts and
  1020. * finishes. This ensures that the io_context object's run() function will not
  1021. * exit while work is underway, and that it does exit when there is no
  1022. * unfinished work remaining.
  1023. *
  1024. * The work class is copy-constructible so that it may be used as a data member
  1025. * in a handler class. It is not assignable.
  1026. */
  1027. class io_context::work
  1028. {
  1029. public:
  1030. /// Constructor notifies the io_context that work is starting.
  1031. /**
  1032. * The constructor is used to inform the io_context that some work has begun.
  1033. * This ensures that the io_context object's run() function will not exit
  1034. * while the work is underway.
  1035. */
  1036. explicit work(boost::asio::io_context& io_context);
  1037. /// Copy constructor notifies the io_context that work is starting.
  1038. /**
  1039. * The constructor is used to inform the io_context that some work has begun.
  1040. * This ensures that the io_context object's run() function will not exit
  1041. * while the work is underway.
  1042. */
  1043. work(const work& other);
  1044. /// Destructor notifies the io_context that the work is complete.
  1045. /**
  1046. * The destructor is used to inform the io_context that some work has
  1047. * finished. Once the count of unfinished work reaches zero, the io_context
  1048. * object's run() function is permitted to exit.
  1049. */
  1050. ~work();
  1051. /// Get the io_context associated with the work.
  1052. boost::asio::io_context& get_io_context();
  1053. private:
  1054. // Prevent assignment.
  1055. void operator=(const work& other);
  1056. // The io_context implementation.
  1057. detail::io_context_impl& io_context_impl_;
  1058. };
  1059. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1060. /// Base class for all io_context services.
  1061. class io_context::service
  1062. : public execution_context::service
  1063. {
  1064. public:
  1065. /// Get the io_context object that owns the service.
  1066. boost::asio::io_context& get_io_context();
  1067. private:
  1068. /// Destroy all user-defined handler objects owned by the service.
  1069. BOOST_ASIO_DECL virtual void shutdown();
  1070. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1071. /// (Deprecated: Use shutdown().) Destroy all user-defined handler objects
  1072. /// owned by the service.
  1073. BOOST_ASIO_DECL virtual void shutdown_service();
  1074. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1075. /// Handle notification of a fork-related event to perform any necessary
  1076. /// housekeeping.
  1077. /**
  1078. * This function is not a pure virtual so that services only have to
  1079. * implement it if necessary. The default implementation does nothing.
  1080. */
  1081. BOOST_ASIO_DECL virtual void notify_fork(
  1082. execution_context::fork_event event);
  1083. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1084. /// (Deprecated: Use notify_fork().) Handle notification of a fork-related
  1085. /// event to perform any necessary housekeeping.
  1086. /**
  1087. * This function is not a pure virtual so that services only have to
  1088. * implement it if necessary. The default implementation does nothing.
  1089. */
  1090. BOOST_ASIO_DECL virtual void fork_service(
  1091. execution_context::fork_event event);
  1092. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1093. protected:
  1094. /// Constructor.
  1095. /**
  1096. * @param owner The io_context object that owns the service.
  1097. */
  1098. BOOST_ASIO_DECL service(boost::asio::io_context& owner);
  1099. /// Destructor.
  1100. BOOST_ASIO_DECL virtual ~service();
  1101. };
  1102. namespace detail {
  1103. // Special service base class to keep classes header-file only.
  1104. template <typename Type>
  1105. class service_base
  1106. : public boost::asio::io_context::service
  1107. {
  1108. public:
  1109. static boost::asio::detail::service_id<Type> id;
  1110. // Constructor.
  1111. service_base(boost::asio::io_context& io_context)
  1112. : boost::asio::io_context::service(io_context)
  1113. {
  1114. }
  1115. };
  1116. template <typename Type>
  1117. boost::asio::detail::service_id<Type> service_base<Type>::id;
  1118. } // namespace detail
  1119. #if !defined(GENERATING_DOCUMENTATION)
  1120. namespace traits {
  1121. #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1122. template <typename Allocator, uintptr_t Bits>
  1123. struct equality_comparable<
  1124. boost::asio::io_context::basic_executor_type<Allocator, Bits>
  1125. >
  1126. {
  1127. static constexpr bool is_valid = true;
  1128. static constexpr bool is_noexcept = true;
  1129. };
  1130. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1131. #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1132. template <typename Allocator, uintptr_t Bits, typename Function>
  1133. struct execute_member<
  1134. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1135. Function
  1136. >
  1137. {
  1138. static constexpr bool is_valid = true;
  1139. static constexpr bool is_noexcept = false;
  1140. typedef void result_type;
  1141. };
  1142. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1143. #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1144. template <typename Allocator, uintptr_t Bits>
  1145. struct require_member<
  1146. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1147. boost::asio::execution::blocking_t::possibly_t
  1148. >
  1149. {
  1150. static constexpr bool is_valid = true;
  1151. static constexpr bool is_noexcept = false;
  1152. typedef boost::asio::io_context::basic_executor_type<
  1153. Allocator, Bits> result_type;
  1154. };
  1155. template <typename Allocator, uintptr_t Bits>
  1156. struct require_member<
  1157. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1158. boost::asio::execution::blocking_t::never_t
  1159. >
  1160. {
  1161. static constexpr bool is_valid = true;
  1162. static constexpr bool is_noexcept = false;
  1163. typedef boost::asio::io_context::basic_executor_type<
  1164. Allocator, Bits> result_type;
  1165. };
  1166. template <typename Allocator, uintptr_t Bits>
  1167. struct require_member<
  1168. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1169. boost::asio::execution::relationship_t::fork_t
  1170. >
  1171. {
  1172. static constexpr bool is_valid = true;
  1173. static constexpr bool is_noexcept = false;
  1174. typedef boost::asio::io_context::basic_executor_type<
  1175. Allocator, Bits> result_type;
  1176. };
  1177. template <typename Allocator, uintptr_t Bits>
  1178. struct require_member<
  1179. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1180. boost::asio::execution::relationship_t::continuation_t
  1181. >
  1182. {
  1183. static constexpr bool is_valid = true;
  1184. static constexpr bool is_noexcept = false;
  1185. typedef boost::asio::io_context::basic_executor_type<
  1186. Allocator, Bits> result_type;
  1187. };
  1188. template <typename Allocator, uintptr_t Bits>
  1189. struct require_member<
  1190. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1191. boost::asio::execution::outstanding_work_t::tracked_t
  1192. > : boost::asio::detail::io_context_bits
  1193. {
  1194. static constexpr bool is_valid = true;
  1195. static constexpr bool is_noexcept = false;
  1196. typedef boost::asio::io_context::basic_executor_type<
  1197. Allocator, Bits | outstanding_work_tracked> result_type;
  1198. };
  1199. template <typename Allocator, uintptr_t Bits>
  1200. struct require_member<
  1201. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1202. boost::asio::execution::outstanding_work_t::untracked_t
  1203. > : boost::asio::detail::io_context_bits
  1204. {
  1205. static constexpr bool is_valid = true;
  1206. static constexpr bool is_noexcept = false;
  1207. typedef boost::asio::io_context::basic_executor_type<
  1208. Allocator, Bits & ~outstanding_work_tracked> result_type;
  1209. };
  1210. template <typename Allocator, uintptr_t Bits>
  1211. struct require_member<
  1212. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1213. boost::asio::execution::allocator_t<void>
  1214. >
  1215. {
  1216. static constexpr bool is_valid = true;
  1217. static constexpr bool is_noexcept = false;
  1218. typedef boost::asio::io_context::basic_executor_type<
  1219. std::allocator<void>, Bits> result_type;
  1220. };
  1221. template <uintptr_t Bits,
  1222. typename Allocator, typename OtherAllocator>
  1223. struct require_member<
  1224. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1225. boost::asio::execution::allocator_t<OtherAllocator>
  1226. >
  1227. {
  1228. static constexpr bool is_valid = true;
  1229. static constexpr bool is_noexcept = false;
  1230. typedef boost::asio::io_context::basic_executor_type<
  1231. OtherAllocator, Bits> result_type;
  1232. };
  1233. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1234. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  1235. template <typename Allocator, uintptr_t Bits, typename Property>
  1236. struct query_static_constexpr_member<
  1237. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1238. Property,
  1239. typename boost::asio::enable_if<
  1240. boost::asio::is_convertible<
  1241. Property,
  1242. boost::asio::execution::outstanding_work_t
  1243. >::value
  1244. >::type
  1245. > : boost::asio::detail::io_context_bits
  1246. {
  1247. static constexpr bool is_valid = true;
  1248. static constexpr bool is_noexcept = true;
  1249. typedef boost::asio::execution::outstanding_work_t result_type;
  1250. static constexpr result_type value() noexcept
  1251. {
  1252. return (Bits & outstanding_work_tracked)
  1253. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  1254. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  1255. }
  1256. };
  1257. template <typename Allocator, uintptr_t Bits, typename Property>
  1258. struct query_static_constexpr_member<
  1259. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1260. Property,
  1261. typename boost::asio::enable_if<
  1262. boost::asio::is_convertible<
  1263. Property,
  1264. boost::asio::execution::mapping_t
  1265. >::value
  1266. >::type
  1267. >
  1268. {
  1269. static constexpr bool is_valid = true;
  1270. static constexpr bool is_noexcept = true;
  1271. typedef boost::asio::execution::mapping_t::thread_t result_type;
  1272. static constexpr result_type value() noexcept
  1273. {
  1274. return result_type();
  1275. }
  1276. };
  1277. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  1278. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1279. template <typename Allocator, uintptr_t Bits, typename Property>
  1280. struct query_member<
  1281. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1282. Property,
  1283. typename boost::asio::enable_if<
  1284. boost::asio::is_convertible<
  1285. Property,
  1286. boost::asio::execution::blocking_t
  1287. >::value
  1288. >::type
  1289. >
  1290. {
  1291. static constexpr bool is_valid = true;
  1292. static constexpr bool is_noexcept = true;
  1293. typedef boost::asio::execution::blocking_t result_type;
  1294. };
  1295. template <typename Allocator, uintptr_t Bits, typename Property>
  1296. struct query_member<
  1297. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1298. Property,
  1299. typename boost::asio::enable_if<
  1300. boost::asio::is_convertible<
  1301. Property,
  1302. boost::asio::execution::relationship_t
  1303. >::value
  1304. >::type
  1305. >
  1306. {
  1307. static constexpr bool is_valid = true;
  1308. static constexpr bool is_noexcept = true;
  1309. typedef boost::asio::execution::relationship_t result_type;
  1310. };
  1311. template <typename Allocator, uintptr_t Bits>
  1312. struct query_member<
  1313. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1314. boost::asio::execution::context_t
  1315. >
  1316. {
  1317. static constexpr bool is_valid = true;
  1318. static constexpr bool is_noexcept = true;
  1319. typedef boost::asio::io_context& result_type;
  1320. };
  1321. template <typename Allocator, uintptr_t Bits>
  1322. struct query_member<
  1323. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1324. boost::asio::execution::allocator_t<void>
  1325. >
  1326. {
  1327. static constexpr bool is_valid = true;
  1328. static constexpr bool is_noexcept = true;
  1329. typedef Allocator result_type;
  1330. };
  1331. template <typename Allocator, uintptr_t Bits, typename OtherAllocator>
  1332. struct query_member<
  1333. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1334. boost::asio::execution::allocator_t<OtherAllocator>
  1335. >
  1336. {
  1337. static constexpr bool is_valid = true;
  1338. static constexpr bool is_noexcept = true;
  1339. typedef Allocator result_type;
  1340. };
  1341. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1342. } // namespace traits
  1343. namespace execution {
  1344. template <>
  1345. struct is_executor<io_context> : false_type
  1346. {
  1347. };
  1348. } // namespace execution
  1349. #endif // !defined(GENERATING_DOCUMENTATION)
  1350. } // namespace asio
  1351. } // namespace boost
  1352. #include <boost/asio/detail/pop_options.hpp>
  1353. #include <boost/asio/impl/io_context.hpp>
  1354. #if defined(BOOST_ASIO_HEADER_ONLY)
  1355. # include <boost/asio/impl/io_context.ipp>
  1356. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  1357. // If both io_context.hpp and strand.hpp have been included, automatically
  1358. // include the header file needed for the io_context::strand class.
  1359. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1360. # if defined(BOOST_ASIO_STRAND_HPP)
  1361. # include <boost/asio/io_context_strand.hpp>
  1362. # endif // defined(BOOST_ASIO_STRAND_HPP)
  1363. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1364. #endif // BOOST_ASIO_IO_CONTEXT_HPP