Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

873 lines
31KB

  1. //
  2. // io_context.hpp
  3. // ~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_IO_CONTEXT_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #include <cstddef>
  17. #include <stdexcept>
  18. #include <typeinfo>
  19. #include "asio/async_result.hpp"
  20. #include "asio/detail/noncopyable.hpp"
  21. #include "asio/detail/wrapped_handler.hpp"
  22. #include "asio/error_code.hpp"
  23. #include "asio/execution_context.hpp"
  24. #if defined(ASIO_HAS_CHRONO)
  25. # include "asio/detail/chrono.hpp"
  26. #endif // defined(ASIO_HAS_CHRONO)
  27. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  28. # include "asio/detail/winsock_init.hpp"
  29. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  30. || defined(__osf__)
  31. # include "asio/detail/signal_init.hpp"
  32. #endif
  33. #include "asio/detail/push_options.hpp"
  34. namespace asio {
  35. namespace detail {
  36. #if defined(ASIO_HAS_IOCP)
  37. typedef class win_iocp_io_context io_context_impl;
  38. class win_iocp_overlapped_ptr;
  39. #else
  40. typedef class scheduler io_context_impl;
  41. #endif
  42. } // namespace detail
  43. /// Provides core I/O functionality.
  44. /**
  45. * The io_context class provides the core I/O functionality for users of the
  46. * asynchronous I/O objects, including:
  47. *
  48. * @li asio::ip::tcp::socket
  49. * @li asio::ip::tcp::acceptor
  50. * @li asio::ip::udp::socket
  51. * @li asio::deadline_timer.
  52. *
  53. * The io_context class also includes facilities intended for developers of
  54. * custom asynchronous services.
  55. *
  56. * @par Thread Safety
  57. * @e Distinct @e objects: Safe.@n
  58. * @e Shared @e objects: Safe, with the specific exceptions of the restart()
  59. * and notify_fork() functions. Calling restart() while there are unfinished
  60. * run(), run_one(), run_for(), run_until(), poll() or poll_one() calls results
  61. * in undefined behaviour. The notify_fork() function should not be called
  62. * while any io_context function, or any function on an I/O object that is
  63. * associated with the io_context, is being called in another thread.
  64. *
  65. * @par Concepts:
  66. * Dispatcher.
  67. *
  68. * @par Synchronous and asynchronous operations
  69. *
  70. * Synchronous operations on I/O objects implicitly run the io_context object
  71. * for an individual operation. The io_context functions run(), run_one(),
  72. * run_for(), run_until(), poll() or poll_one() must be called for the
  73. * io_context to perform asynchronous operations on behalf of a C++ program.
  74. * Notification that an asynchronous operation has completed is delivered by
  75. * invocation of the associated handler. Handlers are invoked only by a thread
  76. * that is currently calling any overload of run(), run_one(), run_for(),
  77. * run_until(), poll() or poll_one() for the io_context.
  78. *
  79. * @par Effect of exceptions thrown from handlers
  80. *
  81. * If an exception is thrown from a handler, the exception is allowed to
  82. * propagate through the throwing thread's invocation of run(), run_one(),
  83. * run_for(), run_until(), poll() or poll_one(). No other threads that are
  84. * calling any of these functions are affected. It is then the responsibility
  85. * of the application to catch the exception.
  86. *
  87. * After the exception has been caught, the run(), run_one(), run_for(),
  88. * run_until(), poll() or poll_one() call may be restarted @em without the need
  89. * for an intervening call to restart(). This allows the thread to rejoin the
  90. * io_context object's thread pool without impacting any other threads in the
  91. * pool.
  92. *
  93. * For example:
  94. *
  95. * @code
  96. * asio::io_context io_context;
  97. * ...
  98. * for (;;)
  99. * {
  100. * try
  101. * {
  102. * io_context.run();
  103. * break; // run() exited normally
  104. * }
  105. * catch (my_exception& e)
  106. * {
  107. * // Deal with exception as appropriate.
  108. * }
  109. * }
  110. * @endcode
  111. *
  112. * @par Submitting arbitrary tasks to the io_context
  113. *
  114. * To submit functions to the io_context, use the @ref asio::dispatch,
  115. * @ref asio::post or @ref asio::defer free functions.
  116. *
  117. * For example:
  118. *
  119. * @code void my_task()
  120. * {
  121. * ...
  122. * }
  123. *
  124. * ...
  125. *
  126. * asio::io_context io_context;
  127. *
  128. * // Submit a function to the io_context.
  129. * asio::post(io_context, my_task);
  130. *
  131. * // Submit a lambda object to the io_context.
  132. * asio::post(io_context,
  133. * []()
  134. * {
  135. * ...
  136. * });
  137. *
  138. * // Run the io_context until it runs out of work.
  139. * io_context.run(); @endcode
  140. *
  141. * @par Stopping the io_context from running out of work
  142. *
  143. * Some applications may need to prevent an io_context object's run() call from
  144. * returning when there is no more work to do. For example, the io_context may
  145. * be being run in a background thread that is launched prior to the
  146. * application's asynchronous operations. The run() call may be kept running by
  147. * creating an object of type
  148. * asio::executor_work_guard<io_context::executor_type>:
  149. *
  150. * @code asio::io_context io_context;
  151. * asio::executor_work_guard<asio::io_context::executor_type>
  152. * = asio::make_work_guard(io_context);
  153. * ... @endcode
  154. *
  155. * To effect a shutdown, the application will then need to call the io_context
  156. * object's stop() member function. This will cause the io_context run() call
  157. * to return as soon as possible, abandoning unfinished operations and without
  158. * permitting ready handlers to be dispatched.
  159. *
  160. * Alternatively, if the application requires that all operations and handlers
  161. * be allowed to finish normally, the work object may be explicitly reset.
  162. *
  163. * @code asio::io_context io_context;
  164. * asio::executor_work_guard<asio::io_context::executor_type>
  165. * = asio::make_work_guard(io_context);
  166. * ...
  167. * work.reset(); // Allow run() to exit. @endcode
  168. */
  169. class io_context
  170. : public execution_context
  171. {
  172. private:
  173. typedef detail::io_context_impl impl_type;
  174. #if defined(ASIO_HAS_IOCP)
  175. friend class detail::win_iocp_overlapped_ptr;
  176. #endif
  177. public:
  178. class executor_type;
  179. friend class executor_type;
  180. #if !defined(ASIO_NO_DEPRECATED)
  181. class work;
  182. friend class work;
  183. #endif // !defined(ASIO_NO_DEPRECATED)
  184. class service;
  185. #if !defined(ASIO_NO_EXTENSIONS)
  186. class strand;
  187. #endif // !defined(ASIO_NO_EXTENSIONS)
  188. /// The type used to count the number of handlers executed by the context.
  189. typedef std::size_t count_type;
  190. /// Constructor.
  191. ASIO_DECL io_context();
  192. /// Constructor.
  193. /**
  194. * Construct with a hint about the required level of concurrency.
  195. *
  196. * @param concurrency_hint A suggestion to the implementation on how many
  197. * threads it should allow to run simultaneously.
  198. */
  199. ASIO_DECL explicit io_context(int concurrency_hint);
  200. /// Destructor.
  201. /**
  202. * On destruction, the io_context performs the following sequence of
  203. * operations:
  204. *
  205. * @li For each service object @c svc in the io_context set, in reverse order
  206. * of the beginning of service object lifetime, performs
  207. * @c svc->shutdown().
  208. *
  209. * @li Uninvoked handler objects that were scheduled for deferred invocation
  210. * on the io_context, or any associated strand, are destroyed.
  211. *
  212. * @li For each service object @c svc in the io_context set, in reverse order
  213. * of the beginning of service object lifetime, performs
  214. * <tt>delete static_cast<io_context::service*>(svc)</tt>.
  215. *
  216. * @note The destruction sequence described above permits programs to
  217. * simplify their resource management by using @c shared_ptr<>. Where an
  218. * object's lifetime is tied to the lifetime of a connection (or some other
  219. * sequence of asynchronous operations), a @c shared_ptr to the object would
  220. * be bound into the handlers for all asynchronous operations associated with
  221. * it. This works as follows:
  222. *
  223. * @li When a single connection ends, all associated asynchronous operations
  224. * complete. The corresponding handler objects are destroyed, and all
  225. * @c shared_ptr references to the objects are destroyed.
  226. *
  227. * @li To shut down the whole program, the io_context function stop() is
  228. * called to terminate any run() calls as soon as possible. The io_context
  229. * destructor defined above destroys all handlers, causing all @c shared_ptr
  230. * references to all connection objects to be destroyed.
  231. */
  232. ASIO_DECL ~io_context();
  233. /// Obtains the executor associated with the io_context.
  234. executor_type get_executor() ASIO_NOEXCEPT;
  235. /// Run the io_context object's event processing loop.
  236. /**
  237. * The run() function blocks until all work has finished and there are no
  238. * more handlers to be dispatched, or until the io_context has been stopped.
  239. *
  240. * Multiple threads may call the run() function to set up a pool of threads
  241. * from which the io_context may execute handlers. All threads that are
  242. * waiting in the pool are equivalent and the io_context may choose any one
  243. * of them to invoke a handler.
  244. *
  245. * A normal exit from the run() function implies that the io_context object
  246. * is stopped (the stopped() function returns @c true). Subsequent calls to
  247. * run(), run_one(), poll() or poll_one() will return immediately unless there
  248. * is a prior call to restart().
  249. *
  250. * @return The number of handlers that were executed.
  251. *
  252. * @note Calling the run() function from a thread that is currently calling
  253. * one of run(), run_one(), run_for(), run_until(), poll() or poll_one() on
  254. * the same io_context object may introduce the potential for deadlock. It is
  255. * the caller's reponsibility to avoid this.
  256. *
  257. * The poll() function may also be used to dispatch ready handlers, but
  258. * without blocking.
  259. */
  260. ASIO_DECL count_type run();
  261. #if !defined(ASIO_NO_DEPRECATED)
  262. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  263. /// event processing loop.
  264. /**
  265. * The run() function blocks until all work has finished and there are no
  266. * more handlers to be dispatched, or until the io_context has been stopped.
  267. *
  268. * Multiple threads may call the run() function to set up a pool of threads
  269. * from which the io_context may execute handlers. All threads that are
  270. * waiting in the pool are equivalent and the io_context may choose any one
  271. * of them to invoke a handler.
  272. *
  273. * A normal exit from the run() function implies that the io_context object
  274. * is stopped (the stopped() function returns @c true). Subsequent calls to
  275. * run(), run_one(), poll() or poll_one() will return immediately unless there
  276. * is a prior call to restart().
  277. *
  278. * @param ec Set to indicate what error occurred, if any.
  279. *
  280. * @return The number of handlers that were executed.
  281. *
  282. * @note Calling the run() function from a thread that is currently calling
  283. * one of run(), run_one(), run_for(), run_until(), poll() or poll_one() on
  284. * the same io_context object may introduce the potential for deadlock. It is
  285. * the caller's reponsibility to avoid this.
  286. *
  287. * The poll() function may also be used to dispatch ready handlers, but
  288. * without blocking.
  289. */
  290. ASIO_DECL count_type run(asio::error_code& ec);
  291. #endif // !defined(ASIO_NO_DEPRECATED)
  292. #if defined(ASIO_HAS_CHRONO) || defined(GENERATING_DOCUMENTATION)
  293. /// Run the io_context object's event processing loop for a specified
  294. /// duration.
  295. /**
  296. * The run_for() function blocks until all work has finished and there are no
  297. * more handlers to be dispatched, until the io_context has been stopped, or
  298. * until the specified duration has elapsed.
  299. *
  300. * @param rel_time The duration for which the call may block.
  301. *
  302. * @return The number of handlers that were executed.
  303. */
  304. template <typename Rep, typename Period>
  305. std::size_t run_for(const chrono::duration<Rep, Period>& rel_time);
  306. /// Run the io_context object's event processing loop until a specified time.
  307. /**
  308. * The run_until() function blocks until all work has finished and there are
  309. * no more handlers to be dispatched, until the io_context has been stopped,
  310. * or until the specified time has been reached.
  311. *
  312. * @param abs_time The time point until which the call may block.
  313. *
  314. * @return The number of handlers that were executed.
  315. */
  316. template <typename Clock, typename Duration>
  317. std::size_t run_until(const chrono::time_point<Clock, Duration>& abs_time);
  318. #endif // defined(ASIO_HAS_CHRONO) || defined(GENERATING_DOCUMENTATION)
  319. /// Run the io_context object's event processing loop to execute at most one
  320. /// handler.
  321. /**
  322. * The run_one() function blocks until one handler has been dispatched, or
  323. * until the io_context has been stopped.
  324. *
  325. * @return The number of handlers that were executed. A zero return value
  326. * implies that the io_context object is stopped (the stopped() function
  327. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  328. * poll_one() will return immediately unless there is a prior call to
  329. * restart().
  330. *
  331. * @note Calling the run_one() function from a thread that is currently
  332. * calling one of run(), run_one(), run_for(), run_until(), poll() or
  333. * poll_one() on the same io_context object may introduce the potential for
  334. * deadlock. It is the caller's reponsibility to avoid this.
  335. */
  336. ASIO_DECL count_type run_one();
  337. #if !defined(ASIO_NO_DEPRECATED)
  338. /// (Deprecated: Use non-error_code overlaod.) Run the io_context object's
  339. /// event processing loop to execute at most one handler.
  340. /**
  341. * The run_one() function blocks until one handler has been dispatched, or
  342. * until the io_context has been stopped.
  343. *
  344. * @return The number of handlers that were executed. A zero return value
  345. * implies that the io_context object is stopped (the stopped() function
  346. * returns @c true). Subsequent calls to run(), run_one(), poll() or
  347. * poll_one() will return immediately unless there is a prior call to
  348. * restart().
  349. *
  350. * @return The number of handlers that were executed.
  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. ASIO_DECL count_type run_one(asio::error_code& ec);
  358. #endif // !defined(ASIO_NO_DEPRECATED)
  359. #if defined(ASIO_HAS_CHRONO) || defined(GENERATING_DOCUMENTATION)
  360. /// Run the io_context object's event processing loop for a specified duration
  361. /// to execute at most one handler.
  362. /**
  363. * The run_one_for() function blocks until one handler has been dispatched,
  364. * until the io_context has been stopped, or until the specified duration has
  365. * elapsed.
  366. *
  367. * @param rel_time The duration for which the call may block.
  368. *
  369. * @return The number of handlers that were executed.
  370. */
  371. template <typename Rep, typename Period>
  372. std::size_t run_one_for(const chrono::duration<Rep, Period>& rel_time);
  373. /// Run the io_context object's event processing loop until a specified time
  374. /// to execute at most one handler.
  375. /**
  376. * The run_one_until() function blocks until one handler has been dispatched,
  377. * until the io_context has been stopped, or until the specified time has
  378. * been reached.
  379. *
  380. * @param abs_time The time point until which the call may block.
  381. *
  382. * @return The number of handlers that were executed.
  383. */
  384. template <typename Clock, typename Duration>
  385. std::size_t run_one_until(
  386. const chrono::time_point<Clock, Duration>& abs_time);
  387. #endif // defined(ASIO_HAS_CHRONO) || defined(GENERATING_DOCUMENTATION)
  388. /// Run the io_context object's event processing loop to execute ready
  389. /// handlers.
  390. /**
  391. * The poll() function runs handlers that are ready to run, without blocking,
  392. * until the io_context has been stopped or there are no more ready handlers.
  393. *
  394. * @return The number of handlers that were executed.
  395. */
  396. ASIO_DECL count_type poll();
  397. #if !defined(ASIO_NO_DEPRECATED)
  398. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  399. /// event processing loop to execute ready handlers.
  400. /**
  401. * The poll() function runs handlers that are ready to run, without blocking,
  402. * until the io_context has been stopped or there are no more ready handlers.
  403. *
  404. * @param ec Set to indicate what error occurred, if any.
  405. *
  406. * @return The number of handlers that were executed.
  407. */
  408. ASIO_DECL count_type poll(asio::error_code& ec);
  409. #endif // !defined(ASIO_NO_DEPRECATED)
  410. /// Run the io_context object's event processing loop to execute one ready
  411. /// handler.
  412. /**
  413. * The poll_one() function runs at most one handler that is ready to run,
  414. * without blocking.
  415. *
  416. * @return The number of handlers that were executed.
  417. */
  418. ASIO_DECL count_type poll_one();
  419. #if !defined(ASIO_NO_DEPRECATED)
  420. /// (Deprecated: Use non-error_code overload.) Run the io_context object's
  421. /// event processing loop to execute one ready handler.
  422. /**
  423. * The poll_one() function runs at most one handler that is ready to run,
  424. * without blocking.
  425. *
  426. * @param ec Set to indicate what error occurred, if any.
  427. *
  428. * @return The number of handlers that were executed.
  429. */
  430. ASIO_DECL count_type poll_one(asio::error_code& ec);
  431. #endif // !defined(ASIO_NO_DEPRECATED)
  432. /// Stop the io_context object's event processing loop.
  433. /**
  434. * This function does not block, but instead simply signals the io_context to
  435. * stop. All invocations of its run() or run_one() member functions should
  436. * return as soon as possible. Subsequent calls to run(), run_one(), poll()
  437. * or poll_one() will return immediately until restart() is called.
  438. */
  439. ASIO_DECL void stop();
  440. /// Determine whether the io_context object has been stopped.
  441. /**
  442. * This function is used to determine whether an io_context object has been
  443. * stopped, either through an explicit call to stop(), or due to running out
  444. * of work. When an io_context object is stopped, calls to run(), run_one(),
  445. * poll() or poll_one() will return immediately without invoking any
  446. * handlers.
  447. *
  448. * @return @c true if the io_context object is stopped, otherwise @c false.
  449. */
  450. ASIO_DECL bool stopped() const;
  451. /// Restart the io_context in preparation for a subsequent run() invocation.
  452. /**
  453. * This function must be called prior to any second or later set of
  454. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  455. * previous invocation of these functions returned due to the io_context
  456. * being stopped or running out of work. After a call to restart(), the
  457. * io_context object's stopped() function will return @c false.
  458. *
  459. * This function must not be called while there are any unfinished calls to
  460. * the run(), run_one(), poll() or poll_one() functions.
  461. */
  462. ASIO_DECL void restart();
  463. #if !defined(ASIO_NO_DEPRECATED)
  464. /// (Deprecated: Use restart().) Reset the io_context in preparation for a
  465. /// subsequent run() invocation.
  466. /**
  467. * This function must be called prior to any second or later set of
  468. * invocations of the run(), run_one(), poll() or poll_one() functions when a
  469. * previous invocation of these functions returned due to the io_context
  470. * being stopped or running out of work. After a call to restart(), the
  471. * io_context object's stopped() function will return @c false.
  472. *
  473. * This function must not be called while there are any unfinished calls to
  474. * the run(), run_one(), poll() or poll_one() functions.
  475. */
  476. void reset();
  477. /// (Deprecated: Use asio::dispatch().) Request the io_context to
  478. /// invoke the given handler.
  479. /**
  480. * This function is used to ask the io_context to execute the given handler.
  481. *
  482. * The io_context guarantees that the handler will only be called in a thread
  483. * in which the run(), run_one(), poll() or poll_one() member functions is
  484. * currently being invoked. The handler may be executed inside this function
  485. * if the guarantee can be met.
  486. *
  487. * @param handler The handler to be called. The io_context will make
  488. * a copy of the handler object as required. The function signature of the
  489. * handler must be: @code void handler(); @endcode
  490. *
  491. * @note This function throws an exception only if:
  492. *
  493. * @li the handler's @c asio_handler_allocate function; or
  494. *
  495. * @li the handler's copy constructor
  496. *
  497. * throws an exception.
  498. */
  499. template <typename LegacyCompletionHandler>
  500. ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
  501. dispatch(ASIO_MOVE_ARG(LegacyCompletionHandler) handler);
  502. /// (Deprecated: Use asio::post().) Request the io_context to invoke
  503. /// the given handler and return immediately.
  504. /**
  505. * This function is used to ask the io_context to execute the given handler,
  506. * but without allowing the io_context to call the handler from inside this
  507. * function.
  508. *
  509. * The io_context guarantees that the handler will only be called in a thread
  510. * in which the run(), run_one(), poll() or poll_one() member functions is
  511. * currently being invoked.
  512. *
  513. * @param handler The handler to be called. The io_context will make
  514. * a copy of the handler object as required. The function signature of the
  515. * handler must be: @code void handler(); @endcode
  516. *
  517. * @note This function throws an exception only if:
  518. *
  519. * @li the handler's @c asio_handler_allocate function; or
  520. *
  521. * @li the handler's copy constructor
  522. *
  523. * throws an exception.
  524. */
  525. template <typename LegacyCompletionHandler>
  526. ASIO_INITFN_RESULT_TYPE(LegacyCompletionHandler, void ())
  527. post(ASIO_MOVE_ARG(LegacyCompletionHandler) handler);
  528. /// (Deprecated: Use asio::bind_executor().) Create a new handler that
  529. /// automatically dispatches the wrapped handler on the io_context.
  530. /**
  531. * This function is used to create a new handler function object that, when
  532. * invoked, will automatically pass the wrapped handler to the io_context
  533. * object's dispatch function.
  534. *
  535. * @param handler The handler to be wrapped. The io_context will make a copy
  536. * of the handler object as required. The function signature of the handler
  537. * must be: @code void handler(A1 a1, ... An an); @endcode
  538. *
  539. * @return A function object that, when invoked, passes the wrapped handler to
  540. * the io_context object's dispatch function. Given a function object with the
  541. * signature:
  542. * @code R f(A1 a1, ... An an); @endcode
  543. * If this function object is passed to the wrap function like so:
  544. * @code io_context.wrap(f); @endcode
  545. * then the return value is a function object with the signature
  546. * @code void g(A1 a1, ... An an); @endcode
  547. * that, when invoked, executes code equivalent to:
  548. * @code io_context.dispatch(boost::bind(f, a1, ... an)); @endcode
  549. */
  550. template <typename Handler>
  551. #if defined(GENERATING_DOCUMENTATION)
  552. unspecified
  553. #else
  554. detail::wrapped_handler<io_context&, Handler>
  555. #endif
  556. wrap(Handler handler);
  557. #endif // !defined(ASIO_NO_DEPRECATED)
  558. private:
  559. #if !defined(ASIO_NO_DEPRECATED)
  560. struct initiate_dispatch;
  561. struct initiate_post;
  562. #endif // !defined(ASIO_NO_DEPRECATED)
  563. // Helper function to add the implementation.
  564. ASIO_DECL impl_type& add_impl(impl_type* impl);
  565. // Backwards compatible overload for use with services derived from
  566. // io_context::service.
  567. template <typename Service>
  568. friend Service& use_service(io_context& ioc);
  569. #if defined(ASIO_WINDOWS) || defined(__CYGWIN__)
  570. detail::winsock_init<> init_;
  571. #elif defined(__sun) || defined(__QNX__) || defined(__hpux) || defined(_AIX) \
  572. || defined(__osf__)
  573. detail::signal_init<> init_;
  574. #endif
  575. // The implementation.
  576. impl_type& impl_;
  577. };
  578. /// Executor used to submit functions to an io_context.
  579. class io_context::executor_type
  580. {
  581. public:
  582. /// Obtain the underlying execution context.
  583. io_context& context() const ASIO_NOEXCEPT;
  584. /// Inform the io_context that it has some outstanding work to do.
  585. /**
  586. * This function is used to inform the io_context that some work has begun.
  587. * This ensures that the io_context's run() and run_one() functions do not
  588. * exit while the work is underway.
  589. */
  590. void on_work_started() const ASIO_NOEXCEPT;
  591. /// Inform the io_context that some work is no longer outstanding.
  592. /**
  593. * This function is used to inform the io_context that some work has
  594. * finished. Once the count of unfinished work reaches zero, the io_context
  595. * is stopped and the run() and run_one() functions may exit.
  596. */
  597. void on_work_finished() const ASIO_NOEXCEPT;
  598. /// Request the io_context to invoke the given function object.
  599. /**
  600. * This function is used to ask the io_context to execute the given function
  601. * object. If the current thread is running the io_context, @c dispatch()
  602. * executes the function before returning. Otherwise, the function will be
  603. * scheduled to run on the io_context.
  604. *
  605. * @param f The function object to be called. The executor will make a copy
  606. * of the handler object as required. The function signature of the function
  607. * object must be: @code void function(); @endcode
  608. *
  609. * @param a An allocator that may be used by the executor to allocate the
  610. * internal storage needed for function invocation.
  611. */
  612. template <typename Function, typename Allocator>
  613. void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  614. /// Request the io_context to invoke the given function object.
  615. /**
  616. * This function is used to ask the io_context to execute the given function
  617. * object. The function object will never be executed inside @c post().
  618. * Instead, it will be scheduled to run on the io_context.
  619. *
  620. * @param f The function object to be called. The executor will make a copy
  621. * of the handler object as required. The function signature of the function
  622. * object must be: @code void function(); @endcode
  623. *
  624. * @param a An allocator that may be used by the executor to allocate the
  625. * internal storage needed for function invocation.
  626. */
  627. template <typename Function, typename Allocator>
  628. void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  629. /// Request the io_context to invoke the given function object.
  630. /**
  631. * This function is used to ask the io_context to execute the given function
  632. * object. The function object will never be executed inside @c defer().
  633. * Instead, it will be scheduled to run on the io_context.
  634. *
  635. * If the current thread belongs to the io_context, @c defer() will delay
  636. * scheduling the function object until the current thread returns control to
  637. * the pool.
  638. *
  639. * @param f The function object to be called. The executor will make a copy
  640. * of the handler object as required. The function signature of the function
  641. * object must be: @code void function(); @endcode
  642. *
  643. * @param a An allocator that may be used by the executor to allocate the
  644. * internal storage needed for function invocation.
  645. */
  646. template <typename Function, typename Allocator>
  647. void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  648. /// Determine whether the io_context is running in the current thread.
  649. /**
  650. * @return @c true if the current thread is running the io_context. Otherwise
  651. * returns @c false.
  652. */
  653. bool running_in_this_thread() const ASIO_NOEXCEPT;
  654. /// Compare two executors for equality.
  655. /**
  656. * Two executors are equal if they refer to the same underlying io_context.
  657. */
  658. friend bool operator==(const executor_type& a,
  659. const executor_type& b) ASIO_NOEXCEPT
  660. {
  661. return &a.io_context_ == &b.io_context_;
  662. }
  663. /// Compare two executors for inequality.
  664. /**
  665. * Two executors are equal if they refer to the same underlying io_context.
  666. */
  667. friend bool operator!=(const executor_type& a,
  668. const executor_type& b) ASIO_NOEXCEPT
  669. {
  670. return &a.io_context_ != &b.io_context_;
  671. }
  672. private:
  673. friend class io_context;
  674. // Constructor.
  675. explicit executor_type(io_context& i) : io_context_(i) {}
  676. // The underlying io_context.
  677. io_context& io_context_;
  678. };
  679. #if !defined(ASIO_NO_DEPRECATED)
  680. /// (Deprecated: Use executor_work_guard.) Class to inform the io_context when
  681. /// it has work to do.
  682. /**
  683. * The work class is used to inform the io_context when work starts and
  684. * finishes. This ensures that the io_context object's run() function will not
  685. * exit while work is underway, and that it does exit when there is no
  686. * unfinished work remaining.
  687. *
  688. * The work class is copy-constructible so that it may be used as a data member
  689. * in a handler class. It is not assignable.
  690. */
  691. class io_context::work
  692. {
  693. public:
  694. /// Constructor notifies the io_context that work is starting.
  695. /**
  696. * The constructor is used to inform the io_context that some work has begun.
  697. * This ensures that the io_context object's run() function will not exit
  698. * while the work is underway.
  699. */
  700. explicit work(asio::io_context& io_context);
  701. /// Copy constructor notifies the io_context that work is starting.
  702. /**
  703. * The constructor is used to inform the io_context that some work has begun.
  704. * This ensures that the io_context object's run() function will not exit
  705. * while the work is underway.
  706. */
  707. work(const work& other);
  708. /// Destructor notifies the io_context that the work is complete.
  709. /**
  710. * The destructor is used to inform the io_context that some work has
  711. * finished. Once the count of unfinished work reaches zero, the io_context
  712. * object's run() function is permitted to exit.
  713. */
  714. ~work();
  715. /// Get the io_context associated with the work.
  716. asio::io_context& get_io_context();
  717. private:
  718. // Prevent assignment.
  719. void operator=(const work& other);
  720. // The io_context implementation.
  721. detail::io_context_impl& io_context_impl_;
  722. };
  723. #endif // !defined(ASIO_NO_DEPRECATED)
  724. /// Base class for all io_context services.
  725. class io_context::service
  726. : public execution_context::service
  727. {
  728. public:
  729. /// Get the io_context object that owns the service.
  730. asio::io_context& get_io_context();
  731. private:
  732. /// Destroy all user-defined handler objects owned by the service.
  733. ASIO_DECL virtual void shutdown();
  734. #if !defined(ASIO_NO_DEPRECATED)
  735. /// (Deprecated: Use shutdown().) Destroy all user-defined handler objects
  736. /// owned by the service.
  737. ASIO_DECL virtual void shutdown_service();
  738. #endif // !defined(ASIO_NO_DEPRECATED)
  739. /// Handle notification of a fork-related event to perform any necessary
  740. /// housekeeping.
  741. /**
  742. * This function is not a pure virtual so that services only have to
  743. * implement it if necessary. The default implementation does nothing.
  744. */
  745. ASIO_DECL virtual void notify_fork(
  746. execution_context::fork_event event);
  747. #if !defined(ASIO_NO_DEPRECATED)
  748. /// (Deprecated: Use notify_fork().) Handle notification of a fork-related
  749. /// event to perform any necessary housekeeping.
  750. /**
  751. * This function is not a pure virtual so that services only have to
  752. * implement it if necessary. The default implementation does nothing.
  753. */
  754. ASIO_DECL virtual void fork_service(
  755. execution_context::fork_event event);
  756. #endif // !defined(ASIO_NO_DEPRECATED)
  757. protected:
  758. /// Constructor.
  759. /**
  760. * @param owner The io_context object that owns the service.
  761. */
  762. ASIO_DECL service(asio::io_context& owner);
  763. /// Destructor.
  764. ASIO_DECL virtual ~service();
  765. };
  766. namespace detail {
  767. // Special service base class to keep classes header-file only.
  768. template <typename Type>
  769. class service_base
  770. : public asio::io_context::service
  771. {
  772. public:
  773. static asio::detail::service_id<Type> id;
  774. // Constructor.
  775. service_base(asio::io_context& io_context)
  776. : asio::io_context::service(io_context)
  777. {
  778. }
  779. };
  780. template <typename Type>
  781. asio::detail::service_id<Type> service_base<Type>::id;
  782. } // namespace detail
  783. } // namespace asio
  784. #include "asio/detail/pop_options.hpp"
  785. #include "asio/impl/io_context.hpp"
  786. #if defined(ASIO_HEADER_ONLY)
  787. # include "asio/impl/io_context.ipp"
  788. #endif // defined(ASIO_HEADER_ONLY)
  789. // If both io_context.hpp and strand.hpp have been included, automatically
  790. // include the header file needed for the io_context::strand class.
  791. #if !defined(ASIO_NO_EXTENSIONS)
  792. # if defined(ASIO_STRAND_HPP)
  793. # include "asio/io_context_strand.hpp"
  794. # endif // defined(ASIO_STRAND_HPP)
  795. #endif // !defined(ASIO_NO_EXTENSIONS)
  796. #endif // ASIO_IO_CONTEXT_HPP