io_context.hpp 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  1. //
  2. // io_context.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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. namespace detail {
  600. } // namespace detail
  601. /// Executor implementation type used to submit functions to an io_context.
  602. template <typename Allocator, uintptr_t Bits>
  603. class io_context::basic_executor_type :
  604. detail::io_context_bits, Allocator
  605. {
  606. public:
  607. /// Copy constructor.
  608. basic_executor_type(const basic_executor_type& other) noexcept
  609. : Allocator(static_cast<const Allocator&>(other)),
  610. target_(other.target_)
  611. {
  612. if (Bits & outstanding_work_tracked)
  613. if (context_ptr())
  614. context_ptr()->impl_.work_started();
  615. }
  616. /// Move constructor.
  617. basic_executor_type(basic_executor_type&& other) noexcept
  618. : Allocator(static_cast<Allocator&&>(other)),
  619. target_(other.target_)
  620. {
  621. if (Bits & outstanding_work_tracked)
  622. other.target_ = 0;
  623. }
  624. /// Destructor.
  625. ~basic_executor_type() noexcept
  626. {
  627. if (Bits & outstanding_work_tracked)
  628. if (context_ptr())
  629. context_ptr()->impl_.work_finished();
  630. }
  631. /// Assignment operator.
  632. basic_executor_type& operator=(const basic_executor_type& other) noexcept;
  633. /// Move assignment operator.
  634. basic_executor_type& operator=(basic_executor_type&& other) noexcept;
  635. #if !defined(GENERATING_DOCUMENTATION)
  636. private:
  637. friend struct boost_asio_require_fn::impl;
  638. friend struct boost_asio_prefer_fn::impl;
  639. #endif // !defined(GENERATING_DOCUMENTATION)
  640. /// Obtain an executor with the @c blocking.possibly property.
  641. /**
  642. * Do not call this function directly. It is intended for use with the
  643. * boost::asio::require customisation point.
  644. *
  645. * For example:
  646. * @code auto ex1 = my_io_context.get_executor();
  647. * auto ex2 = boost::asio::require(ex1,
  648. * boost::asio::execution::blocking.possibly); @endcode
  649. */
  650. constexpr basic_executor_type require(execution::blocking_t::possibly_t) const
  651. {
  652. return basic_executor_type(context_ptr(),
  653. *this, bits() & ~blocking_never);
  654. }
  655. /// Obtain an executor with the @c blocking.never property.
  656. /**
  657. * Do not call this function directly. It is intended for use with the
  658. * boost::asio::require customisation point.
  659. *
  660. * For example:
  661. * @code auto ex1 = my_io_context.get_executor();
  662. * auto ex2 = boost::asio::require(ex1,
  663. * boost::asio::execution::blocking.never); @endcode
  664. */
  665. constexpr basic_executor_type require(execution::blocking_t::never_t) const
  666. {
  667. return basic_executor_type(context_ptr(),
  668. *this, bits() | blocking_never);
  669. }
  670. /// Obtain an executor with the @c relationship.fork property.
  671. /**
  672. * Do not call this function directly. It is intended for use with the
  673. * boost::asio::require customisation point.
  674. *
  675. * For example:
  676. * @code auto ex1 = my_io_context.get_executor();
  677. * auto ex2 = boost::asio::require(ex1,
  678. * boost::asio::execution::relationship.fork); @endcode
  679. */
  680. constexpr basic_executor_type require(execution::relationship_t::fork_t) const
  681. {
  682. return basic_executor_type(context_ptr(),
  683. *this, bits() & ~relationship_continuation);
  684. }
  685. /// Obtain an executor with the @c relationship.continuation property.
  686. /**
  687. * Do not call this function directly. It is intended for use with the
  688. * boost::asio::require customisation point.
  689. *
  690. * For example:
  691. * @code auto ex1 = my_io_context.get_executor();
  692. * auto ex2 = boost::asio::require(ex1,
  693. * boost::asio::execution::relationship.continuation); @endcode
  694. */
  695. constexpr basic_executor_type require(
  696. execution::relationship_t::continuation_t) const
  697. {
  698. return basic_executor_type(context_ptr(),
  699. *this, bits() | relationship_continuation);
  700. }
  701. /// Obtain an executor with the @c outstanding_work.tracked property.
  702. /**
  703. * Do not call this function directly. It is intended for use with the
  704. * boost::asio::require customisation point.
  705. *
  706. * For example:
  707. * @code auto ex1 = my_io_context.get_executor();
  708. * auto ex2 = boost::asio::require(ex1,
  709. * boost::asio::execution::outstanding_work.tracked); @endcode
  710. */
  711. constexpr basic_executor_type<Allocator,
  712. BOOST_ASIO_UNSPECIFIED(Bits | outstanding_work_tracked)>
  713. require(execution::outstanding_work_t::tracked_t) const
  714. {
  715. return basic_executor_type<Allocator, Bits | outstanding_work_tracked>(
  716. context_ptr(), *this, bits());
  717. }
  718. /// Obtain an executor with the @c outstanding_work.untracked property.
  719. /**
  720. * Do not call this function directly. It is intended for use with the
  721. * boost::asio::require customisation point.
  722. *
  723. * For example:
  724. * @code auto ex1 = my_io_context.get_executor();
  725. * auto ex2 = boost::asio::require(ex1,
  726. * boost::asio::execution::outstanding_work.untracked); @endcode
  727. */
  728. constexpr basic_executor_type<Allocator,
  729. BOOST_ASIO_UNSPECIFIED(Bits & ~outstanding_work_tracked)>
  730. require(execution::outstanding_work_t::untracked_t) const
  731. {
  732. return basic_executor_type<Allocator, Bits & ~outstanding_work_tracked>(
  733. context_ptr(), *this, bits());
  734. }
  735. /// Obtain an executor with the specified @c allocator property.
  736. /**
  737. * Do not call this function directly. It is intended for use with the
  738. * boost::asio::require customisation point.
  739. *
  740. * For example:
  741. * @code auto ex1 = my_io_context.get_executor();
  742. * auto ex2 = boost::asio::require(ex1,
  743. * boost::asio::execution::allocator(my_allocator)); @endcode
  744. */
  745. template <typename OtherAllocator>
  746. constexpr basic_executor_type<OtherAllocator, Bits>
  747. require(execution::allocator_t<OtherAllocator> a) const
  748. {
  749. return basic_executor_type<OtherAllocator, Bits>(
  750. context_ptr(), a.value(), bits());
  751. }
  752. /// Obtain an executor with the default @c allocator property.
  753. /**
  754. * Do not call this function directly. It is intended for use with the
  755. * boost::asio::require customisation point.
  756. *
  757. * For example:
  758. * @code auto ex1 = my_io_context.get_executor();
  759. * auto ex2 = boost::asio::require(ex1,
  760. * boost::asio::execution::allocator); @endcode
  761. */
  762. constexpr basic_executor_type<std::allocator<void>, Bits>
  763. require(execution::allocator_t<void>) const
  764. {
  765. return basic_executor_type<std::allocator<void>, Bits>(
  766. context_ptr(), std::allocator<void>(), bits());
  767. }
  768. #if !defined(GENERATING_DOCUMENTATION)
  769. private:
  770. friend struct boost_asio_query_fn::impl;
  771. friend struct boost::asio::execution::detail::mapping_t<0>;
  772. friend struct boost::asio::execution::detail::outstanding_work_t<0>;
  773. #endif // !defined(GENERATING_DOCUMENTATION)
  774. /// Query the current value of the @c mapping property.
  775. /**
  776. * Do not call this function directly. It is intended for use with the
  777. * boost::asio::query customisation point.
  778. *
  779. * For example:
  780. * @code auto ex = my_io_context.get_executor();
  781. * if (boost::asio::query(ex, boost::asio::execution::mapping)
  782. * == boost::asio::execution::mapping.thread)
  783. * ... @endcode
  784. */
  785. static constexpr execution::mapping_t query(execution::mapping_t) noexcept
  786. {
  787. return execution::mapping.thread;
  788. }
  789. /// Query the current value of the @c context property.
  790. /**
  791. * Do not call this function directly. It is intended for use with the
  792. * boost::asio::query customisation point.
  793. *
  794. * For example:
  795. * @code auto ex = my_io_context.get_executor();
  796. * boost::asio::io_context& ctx = boost::asio::query(
  797. * ex, boost::asio::execution::context); @endcode
  798. */
  799. io_context& query(execution::context_t) const noexcept
  800. {
  801. return *context_ptr();
  802. }
  803. /// Query the current value of the @c blocking property.
  804. /**
  805. * Do not call this function directly. It is intended for use with the
  806. * boost::asio::query customisation point.
  807. *
  808. * For example:
  809. * @code auto ex = my_io_context.get_executor();
  810. * if (boost::asio::query(ex, boost::asio::execution::blocking)
  811. * == boost::asio::execution::blocking.always)
  812. * ... @endcode
  813. */
  814. constexpr execution::blocking_t query(execution::blocking_t) const noexcept
  815. {
  816. return (bits() & blocking_never)
  817. ? execution::blocking_t(execution::blocking.never)
  818. : execution::blocking_t(execution::blocking.possibly);
  819. }
  820. /// Query the current value of the @c relationship property.
  821. /**
  822. * Do not call this function directly. It is intended for use with the
  823. * boost::asio::query customisation point.
  824. *
  825. * For example:
  826. * @code auto ex = my_io_context.get_executor();
  827. * if (boost::asio::query(ex, boost::asio::execution::relationship)
  828. * == boost::asio::execution::relationship.continuation)
  829. * ... @endcode
  830. */
  831. constexpr execution::relationship_t query(
  832. execution::relationship_t) const noexcept
  833. {
  834. return (bits() & relationship_continuation)
  835. ? execution::relationship_t(execution::relationship.continuation)
  836. : execution::relationship_t(execution::relationship.fork);
  837. }
  838. /// Query the current value of the @c outstanding_work property.
  839. /**
  840. * Do not call this function directly. It is intended for use with the
  841. * boost::asio::query customisation point.
  842. *
  843. * For example:
  844. * @code auto ex = my_io_context.get_executor();
  845. * if (boost::asio::query(ex, boost::asio::execution::outstanding_work)
  846. * == boost::asio::execution::outstanding_work.tracked)
  847. * ... @endcode
  848. */
  849. static constexpr execution::outstanding_work_t query(
  850. execution::outstanding_work_t) noexcept
  851. {
  852. return (Bits & outstanding_work_tracked)
  853. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  854. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  855. }
  856. /// Query the current value of the @c allocator property.
  857. /**
  858. * Do not call this function directly. It is intended for use with the
  859. * boost::asio::query customisation point.
  860. *
  861. * For example:
  862. * @code auto ex = my_io_context.get_executor();
  863. * auto alloc = boost::asio::query(ex,
  864. * boost::asio::execution::allocator); @endcode
  865. */
  866. template <typename OtherAllocator>
  867. constexpr Allocator query(
  868. execution::allocator_t<OtherAllocator>) const noexcept
  869. {
  870. return static_cast<const Allocator&>(*this);
  871. }
  872. /// Query the current value of the @c allocator property.
  873. /**
  874. * Do not call this function directly. It is intended for use with the
  875. * boost::asio::query customisation point.
  876. *
  877. * For example:
  878. * @code auto ex = my_io_context.get_executor();
  879. * auto alloc = boost::asio::query(ex,
  880. * boost::asio::execution::allocator); @endcode
  881. */
  882. constexpr Allocator query(execution::allocator_t<void>) const noexcept
  883. {
  884. return static_cast<const Allocator&>(*this);
  885. }
  886. public:
  887. /// Determine whether the io_context is running in the current thread.
  888. /**
  889. * @return @c true if the current thread is running the io_context. Otherwise
  890. * returns @c false.
  891. */
  892. bool running_in_this_thread() const noexcept;
  893. /// Compare two executors for equality.
  894. /**
  895. * Two executors are equal if they refer to the same underlying io_context.
  896. */
  897. friend bool operator==(const basic_executor_type& a,
  898. const basic_executor_type& b) noexcept
  899. {
  900. return a.target_ == b.target_
  901. && static_cast<const Allocator&>(a) == static_cast<const Allocator&>(b);
  902. }
  903. /// Compare two executors for inequality.
  904. /**
  905. * Two executors are equal if they refer to the same underlying io_context.
  906. */
  907. friend bool operator!=(const basic_executor_type& a,
  908. const basic_executor_type& b) noexcept
  909. {
  910. return a.target_ != b.target_
  911. || static_cast<const Allocator&>(a) != static_cast<const Allocator&>(b);
  912. }
  913. /// Execution function.
  914. template <typename Function>
  915. void execute(Function&& f) const;
  916. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  917. public:
  918. /// Obtain the underlying execution context.
  919. io_context& context() const noexcept;
  920. /// Inform the io_context that it has some outstanding work to do.
  921. /**
  922. * This function is used to inform the io_context that some work has begun.
  923. * This ensures that the io_context's run() and run_one() functions do not
  924. * exit while the work is underway.
  925. */
  926. void on_work_started() const noexcept;
  927. /// Inform the io_context that some work is no longer outstanding.
  928. /**
  929. * This function is used to inform the io_context that some work has
  930. * finished. Once the count of unfinished work reaches zero, the io_context
  931. * is stopped and the run() and run_one() functions may exit.
  932. */
  933. void on_work_finished() const noexcept;
  934. /// Request the io_context to invoke the given function object.
  935. /**
  936. * This function is used to ask the io_context to execute the given function
  937. * object. If the current thread is running the io_context, @c dispatch()
  938. * executes the function before returning. Otherwise, the function will be
  939. * scheduled to run on the io_context.
  940. *
  941. * @param f The function object to be called. The executor will make a copy
  942. * of the handler object as required. The function signature of the function
  943. * object must be: @code void function(); @endcode
  944. *
  945. * @param a An allocator that may be used by the executor to allocate the
  946. * internal storage needed for function invocation.
  947. */
  948. template <typename Function, typename OtherAllocator>
  949. void dispatch(Function&& f, const OtherAllocator& a) const;
  950. /// Request the io_context to invoke the given function object.
  951. /**
  952. * This function is used to ask the io_context to execute the given function
  953. * object. The function object will never be executed inside @c post().
  954. * Instead, it will be scheduled to run on the io_context.
  955. *
  956. * @param f The function object to be called. The executor will make a copy
  957. * of the handler object as required. The function signature of the function
  958. * object must be: @code void function(); @endcode
  959. *
  960. * @param a An allocator that may be used by the executor to allocate the
  961. * internal storage needed for function invocation.
  962. */
  963. template <typename Function, typename OtherAllocator>
  964. void post(Function&& f, const OtherAllocator& a) const;
  965. /// Request the io_context to invoke the given function object.
  966. /**
  967. * This function is used to ask the io_context to execute the given function
  968. * object. The function object will never be executed inside @c defer().
  969. * Instead, it will be scheduled to run on the io_context.
  970. *
  971. * If the current thread belongs to the io_context, @c defer() will delay
  972. * scheduling the function object until the current thread returns control to
  973. * the pool.
  974. *
  975. * @param f The function object to be called. The executor will make a copy
  976. * of the handler object as required. The function signature of the function
  977. * object must be: @code void function(); @endcode
  978. *
  979. * @param a An allocator that may be used by the executor to allocate the
  980. * internal storage needed for function invocation.
  981. */
  982. template <typename Function, typename OtherAllocator>
  983. void defer(Function&& f, const OtherAllocator& a) const;
  984. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  985. private:
  986. friend class io_context;
  987. template <typename, uintptr_t> friend class basic_executor_type;
  988. // Constructor used by io_context::get_executor().
  989. explicit basic_executor_type(io_context& i) noexcept
  990. : Allocator(),
  991. target_(reinterpret_cast<uintptr_t>(&i))
  992. {
  993. if (Bits & outstanding_work_tracked)
  994. context_ptr()->impl_.work_started();
  995. }
  996. // Constructor used by require().
  997. basic_executor_type(io_context* i,
  998. const Allocator& a, uintptr_t bits) noexcept
  999. : Allocator(a),
  1000. target_(reinterpret_cast<uintptr_t>(i) | bits)
  1001. {
  1002. if (Bits & outstanding_work_tracked)
  1003. if (context_ptr())
  1004. context_ptr()->impl_.work_started();
  1005. }
  1006. io_context* context_ptr() const noexcept
  1007. {
  1008. return reinterpret_cast<io_context*>(target_ & ~runtime_bits);
  1009. }
  1010. uintptr_t bits() const noexcept
  1011. {
  1012. return target_ & runtime_bits;
  1013. }
  1014. // The underlying io_context and runtime bits.
  1015. uintptr_t target_;
  1016. };
  1017. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1018. /// (Deprecated: Use executor_work_guard.) Class to inform the io_context when
  1019. /// it has work to do.
  1020. /**
  1021. * The work class is used to inform the io_context when work starts and
  1022. * finishes. This ensures that the io_context object's run() function will not
  1023. * exit while work is underway, and that it does exit when there is no
  1024. * unfinished work remaining.
  1025. *
  1026. * The work class is copy-constructible so that it may be used as a data member
  1027. * in a handler class. It is not assignable.
  1028. */
  1029. class io_context::work
  1030. {
  1031. public:
  1032. /// Constructor notifies the io_context that work is starting.
  1033. /**
  1034. * The constructor is used to inform the io_context that some work has begun.
  1035. * This ensures that the io_context object's run() function will not exit
  1036. * while the work is underway.
  1037. */
  1038. explicit work(boost::asio::io_context& io_context);
  1039. /// Copy constructor notifies the io_context that work is starting.
  1040. /**
  1041. * The constructor is used to inform the io_context that some work has begun.
  1042. * This ensures that the io_context object's run() function will not exit
  1043. * while the work is underway.
  1044. */
  1045. work(const work& other);
  1046. /// Destructor notifies the io_context that the work is complete.
  1047. /**
  1048. * The destructor is used to inform the io_context that some work has
  1049. * finished. Once the count of unfinished work reaches zero, the io_context
  1050. * object's run() function is permitted to exit.
  1051. */
  1052. ~work();
  1053. /// Get the io_context associated with the work.
  1054. boost::asio::io_context& get_io_context();
  1055. private:
  1056. // Prevent assignment.
  1057. void operator=(const work& other);
  1058. // The io_context implementation.
  1059. detail::io_context_impl& io_context_impl_;
  1060. };
  1061. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1062. /// Base class for all io_context services.
  1063. class io_context::service
  1064. : public execution_context::service
  1065. {
  1066. public:
  1067. /// Get the io_context object that owns the service.
  1068. boost::asio::io_context& get_io_context();
  1069. private:
  1070. /// Destroy all user-defined handler objects owned by the service.
  1071. BOOST_ASIO_DECL virtual void shutdown();
  1072. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1073. /// (Deprecated: Use shutdown().) Destroy all user-defined handler objects
  1074. /// owned by the service.
  1075. BOOST_ASIO_DECL virtual void shutdown_service();
  1076. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1077. /// Handle notification of a fork-related event to perform any necessary
  1078. /// housekeeping.
  1079. /**
  1080. * This function is not a pure virtual so that services only have to
  1081. * implement it if necessary. The default implementation does nothing.
  1082. */
  1083. BOOST_ASIO_DECL virtual void notify_fork(
  1084. execution_context::fork_event event);
  1085. #if !defined(BOOST_ASIO_NO_DEPRECATED)
  1086. /// (Deprecated: Use notify_fork().) Handle notification of a fork-related
  1087. /// event to perform any necessary housekeeping.
  1088. /**
  1089. * This function is not a pure virtual so that services only have to
  1090. * implement it if necessary. The default implementation does nothing.
  1091. */
  1092. BOOST_ASIO_DECL virtual void fork_service(
  1093. execution_context::fork_event event);
  1094. #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
  1095. protected:
  1096. /// Constructor.
  1097. /**
  1098. * @param owner The io_context object that owns the service.
  1099. */
  1100. BOOST_ASIO_DECL service(boost::asio::io_context& owner);
  1101. /// Destructor.
  1102. BOOST_ASIO_DECL virtual ~service();
  1103. };
  1104. namespace detail {
  1105. // Special service base class to keep classes header-file only.
  1106. template <typename Type>
  1107. class service_base
  1108. : public boost::asio::io_context::service
  1109. {
  1110. public:
  1111. static boost::asio::detail::service_id<Type> id;
  1112. // Constructor.
  1113. service_base(boost::asio::io_context& io_context)
  1114. : boost::asio::io_context::service(io_context)
  1115. {
  1116. }
  1117. };
  1118. template <typename Type>
  1119. boost::asio::detail::service_id<Type> service_base<Type>::id;
  1120. } // namespace detail
  1121. #if !defined(GENERATING_DOCUMENTATION)
  1122. namespace traits {
  1123. #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1124. template <typename Allocator, uintptr_t Bits>
  1125. struct equality_comparable<
  1126. boost::asio::io_context::basic_executor_type<Allocator, Bits>
  1127. >
  1128. {
  1129. static constexpr bool is_valid = true;
  1130. static constexpr bool is_noexcept = true;
  1131. };
  1132. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  1133. #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1134. template <typename Allocator, uintptr_t Bits, typename Function>
  1135. struct execute_member<
  1136. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1137. Function
  1138. >
  1139. {
  1140. static constexpr bool is_valid = true;
  1141. static constexpr bool is_noexcept = false;
  1142. typedef void result_type;
  1143. };
  1144. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  1145. #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1146. template <typename Allocator, uintptr_t Bits>
  1147. struct require_member<
  1148. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1149. boost::asio::execution::blocking_t::possibly_t
  1150. >
  1151. {
  1152. static constexpr bool is_valid = true;
  1153. static constexpr bool is_noexcept = false;
  1154. typedef boost::asio::io_context::basic_executor_type<
  1155. Allocator, Bits> result_type;
  1156. };
  1157. template <typename Allocator, uintptr_t Bits>
  1158. struct require_member<
  1159. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1160. boost::asio::execution::blocking_t::never_t
  1161. >
  1162. {
  1163. static constexpr bool is_valid = true;
  1164. static constexpr bool is_noexcept = false;
  1165. typedef boost::asio::io_context::basic_executor_type<
  1166. Allocator, Bits> result_type;
  1167. };
  1168. template <typename Allocator, uintptr_t Bits>
  1169. struct require_member<
  1170. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1171. boost::asio::execution::relationship_t::fork_t
  1172. >
  1173. {
  1174. static constexpr bool is_valid = true;
  1175. static constexpr bool is_noexcept = false;
  1176. typedef boost::asio::io_context::basic_executor_type<
  1177. Allocator, Bits> result_type;
  1178. };
  1179. template <typename Allocator, uintptr_t Bits>
  1180. struct require_member<
  1181. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1182. boost::asio::execution::relationship_t::continuation_t
  1183. >
  1184. {
  1185. static constexpr bool is_valid = true;
  1186. static constexpr bool is_noexcept = false;
  1187. typedef boost::asio::io_context::basic_executor_type<
  1188. Allocator, Bits> result_type;
  1189. };
  1190. template <typename Allocator, uintptr_t Bits>
  1191. struct require_member<
  1192. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1193. boost::asio::execution::outstanding_work_t::tracked_t
  1194. > : boost::asio::detail::io_context_bits
  1195. {
  1196. static constexpr bool is_valid = true;
  1197. static constexpr bool is_noexcept = false;
  1198. typedef boost::asio::io_context::basic_executor_type<
  1199. Allocator, Bits | outstanding_work_tracked> result_type;
  1200. };
  1201. template <typename Allocator, uintptr_t Bits>
  1202. struct require_member<
  1203. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1204. boost::asio::execution::outstanding_work_t::untracked_t
  1205. > : boost::asio::detail::io_context_bits
  1206. {
  1207. static constexpr bool is_valid = true;
  1208. static constexpr bool is_noexcept = false;
  1209. typedef boost::asio::io_context::basic_executor_type<
  1210. Allocator, Bits & ~outstanding_work_tracked> result_type;
  1211. };
  1212. template <typename Allocator, uintptr_t Bits>
  1213. struct require_member<
  1214. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1215. boost::asio::execution::allocator_t<void>
  1216. >
  1217. {
  1218. static constexpr bool is_valid = true;
  1219. static constexpr bool is_noexcept = false;
  1220. typedef boost::asio::io_context::basic_executor_type<
  1221. std::allocator<void>, Bits> result_type;
  1222. };
  1223. template <uintptr_t Bits,
  1224. typename Allocator, typename OtherAllocator>
  1225. struct require_member<
  1226. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1227. boost::asio::execution::allocator_t<OtherAllocator>
  1228. >
  1229. {
  1230. static constexpr bool is_valid = true;
  1231. static constexpr bool is_noexcept = false;
  1232. typedef boost::asio::io_context::basic_executor_type<
  1233. OtherAllocator, Bits> result_type;
  1234. };
  1235. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  1236. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  1237. template <typename Allocator, uintptr_t Bits, typename Property>
  1238. struct query_static_constexpr_member<
  1239. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1240. Property,
  1241. typename boost::asio::enable_if<
  1242. boost::asio::is_convertible<
  1243. Property,
  1244. boost::asio::execution::outstanding_work_t
  1245. >::value
  1246. >::type
  1247. > : boost::asio::detail::io_context_bits
  1248. {
  1249. static constexpr bool is_valid = true;
  1250. static constexpr bool is_noexcept = true;
  1251. typedef boost::asio::execution::outstanding_work_t result_type;
  1252. static constexpr result_type value() noexcept
  1253. {
  1254. return (Bits & outstanding_work_tracked)
  1255. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  1256. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  1257. }
  1258. };
  1259. template <typename Allocator, uintptr_t Bits, typename Property>
  1260. struct query_static_constexpr_member<
  1261. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1262. Property,
  1263. typename boost::asio::enable_if<
  1264. boost::asio::is_convertible<
  1265. Property,
  1266. boost::asio::execution::mapping_t
  1267. >::value
  1268. >::type
  1269. >
  1270. {
  1271. static constexpr bool is_valid = true;
  1272. static constexpr bool is_noexcept = true;
  1273. typedef boost::asio::execution::mapping_t::thread_t result_type;
  1274. static constexpr result_type value() noexcept
  1275. {
  1276. return result_type();
  1277. }
  1278. };
  1279. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  1280. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1281. template <typename Allocator, uintptr_t Bits, typename Property>
  1282. struct query_member<
  1283. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1284. Property,
  1285. typename boost::asio::enable_if<
  1286. boost::asio::is_convertible<
  1287. Property,
  1288. boost::asio::execution::blocking_t
  1289. >::value
  1290. >::type
  1291. >
  1292. {
  1293. static constexpr bool is_valid = true;
  1294. static constexpr bool is_noexcept = true;
  1295. typedef boost::asio::execution::blocking_t result_type;
  1296. };
  1297. template <typename Allocator, uintptr_t Bits, typename Property>
  1298. struct query_member<
  1299. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1300. Property,
  1301. typename boost::asio::enable_if<
  1302. boost::asio::is_convertible<
  1303. Property,
  1304. boost::asio::execution::relationship_t
  1305. >::value
  1306. >::type
  1307. >
  1308. {
  1309. static constexpr bool is_valid = true;
  1310. static constexpr bool is_noexcept = true;
  1311. typedef boost::asio::execution::relationship_t result_type;
  1312. };
  1313. template <typename Allocator, uintptr_t Bits>
  1314. struct query_member<
  1315. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1316. boost::asio::execution::context_t
  1317. >
  1318. {
  1319. static constexpr bool is_valid = true;
  1320. static constexpr bool is_noexcept = true;
  1321. typedef boost::asio::io_context& result_type;
  1322. };
  1323. template <typename Allocator, uintptr_t Bits>
  1324. struct query_member<
  1325. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1326. boost::asio::execution::allocator_t<void>
  1327. >
  1328. {
  1329. static constexpr bool is_valid = true;
  1330. static constexpr bool is_noexcept = true;
  1331. typedef Allocator result_type;
  1332. };
  1333. template <typename Allocator, uintptr_t Bits, typename OtherAllocator>
  1334. struct query_member<
  1335. boost::asio::io_context::basic_executor_type<Allocator, Bits>,
  1336. boost::asio::execution::allocator_t<OtherAllocator>
  1337. >
  1338. {
  1339. static constexpr bool is_valid = true;
  1340. static constexpr bool is_noexcept = true;
  1341. typedef Allocator result_type;
  1342. };
  1343. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  1344. } // namespace traits
  1345. namespace execution {
  1346. template <>
  1347. struct is_executor<io_context> : false_type
  1348. {
  1349. };
  1350. } // namespace execution
  1351. #endif // !defined(GENERATING_DOCUMENTATION)
  1352. } // namespace asio
  1353. } // namespace boost
  1354. #include <boost/asio/detail/pop_options.hpp>
  1355. #include <boost/asio/impl/io_context.hpp>
  1356. #if defined(BOOST_ASIO_HEADER_ONLY)
  1357. # include <boost/asio/impl/io_context.ipp>
  1358. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  1359. // If both io_context.hpp and strand.hpp have been included, automatically
  1360. // include the header file needed for the io_context::strand class.
  1361. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  1362. # if defined(BOOST_ASIO_STRAND_HPP)
  1363. # include <boost/asio/io_context_strand.hpp>
  1364. # endif // defined(BOOST_ASIO_STRAND_HPP)
  1365. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  1366. #endif // BOOST_ASIO_IO_CONTEXT_HPP