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.

765 lines
27KB

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