execution_context.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // execution_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 ASIO_EXECUTION_CONTEXT_HPP
  11. #define ASIO_EXECUTION_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/detail/noncopyable.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. class execution_context;
  23. class io_context;
  24. #if !defined(GENERATING_DOCUMENTATION)
  25. template <typename Service> Service& use_service(execution_context&);
  26. template <typename Service> Service& use_service(io_context&);
  27. template <typename Service> void add_service(execution_context&, Service*);
  28. template <typename Service> bool has_service(execution_context&);
  29. #endif // !defined(GENERATING_DOCUMENTATION)
  30. namespace detail { class service_registry; }
  31. /// A context for function object execution.
  32. /**
  33. * An execution context represents a place where function objects will be
  34. * executed. An @c io_context is an example of an execution context.
  35. *
  36. * @par The execution_context class and services
  37. *
  38. * Class execution_context implements an extensible, type-safe, polymorphic set
  39. * of services, indexed by service type.
  40. *
  41. * Services exist to manage the resources that are shared across an execution
  42. * context. For example, timers may be implemented in terms of a single timer
  43. * queue, and this queue would be stored in a service.
  44. *
  45. * Access to the services of an execution_context is via three function
  46. * templates, use_service(), add_service() and has_service().
  47. *
  48. * In a call to @c use_service<Service>(), the type argument chooses a service,
  49. * making available all members of the named type. If @c Service is not present
  50. * in an execution_context, an object of type @c Service is created and added
  51. * to the execution_context. A C++ program can check if an execution_context
  52. * implements a particular service with the function template @c
  53. * has_service<Service>().
  54. *
  55. * Service objects may be explicitly added to an execution_context using the
  56. * function template @c add_service<Service>(). If the @c Service is already
  57. * present, the service_already_exists exception is thrown. If the owner of the
  58. * service is not the same object as the execution_context parameter, the
  59. * invalid_service_owner exception is thrown.
  60. *
  61. * Once a service reference is obtained from an execution_context object by
  62. * calling use_service(), that reference remains usable as long as the owning
  63. * execution_context object exists.
  64. *
  65. * All service implementations have execution_context::service as a public base
  66. * class. Custom services may be implemented by deriving from this class and
  67. * then added to an execution_context using the facilities described above.
  68. *
  69. * @par The execution_context as a base class
  70. *
  71. * Class execution_context may be used only as a base class for concrete
  72. * execution context types. The @c io_context is an example of such a derived
  73. * type.
  74. *
  75. * On destruction, a class that is derived from execution_context must perform
  76. * <tt>execution_context::shutdown()</tt> followed by
  77. * <tt>execution_context::destroy()</tt>.
  78. *
  79. * This destruction sequence permits programs to simplify their resource
  80. * management by using @c shared_ptr<>. Where an object's lifetime is tied to
  81. * the lifetime of a connection (or some other sequence of asynchronous
  82. * operations), a @c shared_ptr to the object would be bound into the handlers
  83. * for all asynchronous operations associated with it. This works as follows:
  84. *
  85. * @li When a single connection ends, all associated asynchronous operations
  86. * complete. The corresponding handler objects are destroyed, and all @c
  87. * shared_ptr references to the objects are destroyed.
  88. *
  89. * @li To shut down the whole program, the io_context function stop() is called
  90. * to terminate any run() calls as soon as possible. The io_context destructor
  91. * calls @c shutdown() and @c destroy() to destroy all pending handlers,
  92. * causing all @c shared_ptr references to all connection objects to be
  93. * destroyed.
  94. */
  95. class execution_context
  96. : private noncopyable
  97. {
  98. public:
  99. class id;
  100. class service;
  101. public:
  102. /// Constructor.
  103. ASIO_DECL execution_context();
  104. /// Destructor.
  105. ASIO_DECL ~execution_context();
  106. protected:
  107. /// Shuts down all services in the context.
  108. /**
  109. * This function is implemented as follows:
  110. *
  111. * @li For each service object @c svc in the execution_context set, in
  112. * reverse order of the beginning of service object lifetime, performs @c
  113. * svc->shutdown().
  114. */
  115. ASIO_DECL void shutdown();
  116. /// Destroys all services in the context.
  117. /**
  118. * This function is implemented as follows:
  119. *
  120. * @li For each service object @c svc in the execution_context set, in
  121. * reverse order * of the beginning of service object lifetime, performs
  122. * <tt>delete static_cast<execution_context::service*>(svc)</tt>.
  123. */
  124. ASIO_DECL void destroy();
  125. public:
  126. /// Fork-related event notifications.
  127. enum fork_event
  128. {
  129. /// Notify the context that the process is about to fork.
  130. fork_prepare,
  131. /// Notify the context that the process has forked and is the parent.
  132. fork_parent,
  133. /// Notify the context that the process has forked and is the child.
  134. fork_child
  135. };
  136. /// Notify the execution_context of a fork-related event.
  137. /**
  138. * This function is used to inform the execution_context that the process is
  139. * about to fork, or has just forked. This allows the execution_context, and
  140. * the services it contains, to perform any necessary housekeeping to ensure
  141. * correct operation following a fork.
  142. *
  143. * This function must not be called while any other execution_context
  144. * function, or any function associated with the execution_context's derived
  145. * class, is being called in another thread. It is, however, safe to call
  146. * this function from within a completion handler, provided no other thread
  147. * is accessing the execution_context or its derived class.
  148. *
  149. * @param event A fork-related event.
  150. *
  151. * @throws asio::system_error Thrown on failure. If the notification
  152. * fails the execution_context object should no longer be used and should be
  153. * destroyed.
  154. *
  155. * @par Example
  156. * The following code illustrates how to incorporate the notify_fork()
  157. * function:
  158. * @code my_execution_context.notify_fork(execution_context::fork_prepare);
  159. * if (fork() == 0)
  160. * {
  161. * // This is the child process.
  162. * my_execution_context.notify_fork(execution_context::fork_child);
  163. * }
  164. * else
  165. * {
  166. * // This is the parent process.
  167. * my_execution_context.notify_fork(execution_context::fork_parent);
  168. * } @endcode
  169. *
  170. * @note For each service object @c svc in the execution_context set,
  171. * performs <tt>svc->notify_fork();</tt>. When processing the fork_prepare
  172. * event, services are visited in reverse order of the beginning of service
  173. * object lifetime. Otherwise, services are visited in order of the beginning
  174. * of service object lifetime.
  175. */
  176. ASIO_DECL void notify_fork(fork_event event);
  177. /// Obtain the service object corresponding to the given type.
  178. /**
  179. * This function is used to locate a service object that corresponds to the
  180. * given service type. If there is no existing implementation of the service,
  181. * then the execution_context will create a new instance of the service.
  182. *
  183. * @param e The execution_context object that owns the service.
  184. *
  185. * @return The service interface implementing the specified service type.
  186. * Ownership of the service interface is not transferred to the caller.
  187. */
  188. template <typename Service>
  189. friend Service& use_service(execution_context& e);
  190. /// Obtain the service object corresponding to the given type.
  191. /**
  192. * This function is used to locate a service object that corresponds to the
  193. * given service type. If there is no existing implementation of the service,
  194. * then the io_context will create a new instance of the service.
  195. *
  196. * @param ioc The io_context object that owns the service.
  197. *
  198. * @return The service interface implementing the specified service type.
  199. * Ownership of the service interface is not transferred to the caller.
  200. *
  201. * @note This overload is preserved for backwards compatibility with services
  202. * that inherit from io_context::service.
  203. */
  204. template <typename Service>
  205. friend Service& use_service(io_context& ioc);
  206. /// Creates a service object and adds it to the execution_context.
  207. /**
  208. * This function is used to add a service to the execution_context.
  209. *
  210. * @param e The execution_context object that owns the service.
  211. *
  212. * @param args Zero or more arguments to be passed to the service
  213. * constructor.
  214. *
  215. * @throws asio::service_already_exists Thrown if a service of the
  216. * given type is already present in the execution_context.
  217. */
  218. template <typename Service, typename... Args>
  219. friend Service& make_service(execution_context& e, Args&&... args);
  220. /// (Deprecated: Use make_service().) Add a service object to the
  221. /// execution_context.
  222. /**
  223. * This function is used to add a service to the execution_context.
  224. *
  225. * @param e The execution_context object that owns the service.
  226. *
  227. * @param svc The service object. On success, ownership of the service object
  228. * is transferred to the execution_context. When the execution_context object
  229. * is destroyed, it will destroy the service object by performing: @code
  230. * delete static_cast<execution_context::service*>(svc) @endcode
  231. *
  232. * @throws asio::service_already_exists Thrown if a service of the
  233. * given type is already present in the execution_context.
  234. *
  235. * @throws asio::invalid_service_owner Thrown if the service's owning
  236. * execution_context is not the execution_context object specified by the
  237. * @c e parameter.
  238. */
  239. template <typename Service>
  240. friend void add_service(execution_context& e, Service* svc);
  241. /// Determine if an execution_context contains a specified service type.
  242. /**
  243. * This function is used to determine whether the execution_context contains a
  244. * service object corresponding to the given service type.
  245. *
  246. * @param e The execution_context object that owns the service.
  247. *
  248. * @return A boolean indicating whether the execution_context contains the
  249. * service.
  250. */
  251. template <typename Service>
  252. friend bool has_service(execution_context& e);
  253. private:
  254. // The service registry.
  255. asio::detail::service_registry* service_registry_;
  256. };
  257. /// Class used to uniquely identify a service.
  258. class execution_context::id
  259. : private noncopyable
  260. {
  261. public:
  262. /// Constructor.
  263. id() {}
  264. };
  265. /// Base class for all io_context services.
  266. class execution_context::service
  267. : private noncopyable
  268. {
  269. public:
  270. /// Get the context object that owns the service.
  271. execution_context& context();
  272. protected:
  273. /// Constructor.
  274. /**
  275. * @param owner The execution_context object that owns the service.
  276. */
  277. ASIO_DECL service(execution_context& owner);
  278. /// Destructor.
  279. ASIO_DECL virtual ~service();
  280. private:
  281. /// Destroy all user-defined handler objects owned by the service.
  282. virtual void shutdown() = 0;
  283. /// Handle notification of a fork-related event to perform any necessary
  284. /// housekeeping.
  285. /**
  286. * This function is not a pure virtual so that services only have to
  287. * implement it if necessary. The default implementation does nothing.
  288. */
  289. ASIO_DECL virtual void notify_fork(
  290. execution_context::fork_event event);
  291. friend class asio::detail::service_registry;
  292. struct key
  293. {
  294. key() : type_info_(0), id_(0) {}
  295. const std::type_info* type_info_;
  296. const execution_context::id* id_;
  297. } key_;
  298. execution_context& owner_;
  299. service* next_;
  300. };
  301. /// Exception thrown when trying to add a duplicate service to an
  302. /// execution_context.
  303. class service_already_exists
  304. : public std::logic_error
  305. {
  306. public:
  307. ASIO_DECL service_already_exists();
  308. };
  309. /// Exception thrown when trying to add a service object to an
  310. /// execution_context where the service has a different owner.
  311. class invalid_service_owner
  312. : public std::logic_error
  313. {
  314. public:
  315. ASIO_DECL invalid_service_owner();
  316. };
  317. namespace detail {
  318. // Special derived service id type to keep classes header-file only.
  319. template <typename Type>
  320. class service_id
  321. : public execution_context::id
  322. {
  323. };
  324. // Special service base class to keep classes header-file only.
  325. template <typename Type>
  326. class execution_context_service_base
  327. : public execution_context::service
  328. {
  329. public:
  330. static service_id<Type> id;
  331. // Constructor.
  332. execution_context_service_base(execution_context& e)
  333. : execution_context::service(e)
  334. {
  335. }
  336. };
  337. template <typename Type>
  338. service_id<Type> execution_context_service_base<Type>::id;
  339. } // namespace detail
  340. } // namespace asio
  341. #include "asio/detail/pop_options.hpp"
  342. #include "asio/impl/execution_context.hpp"
  343. #if defined(ASIO_HEADER_ONLY)
  344. # include "asio/impl/execution_context.ipp"
  345. #endif // defined(ASIO_HEADER_ONLY)
  346. #endif // ASIO_EXECUTION_CONTEXT_HPP