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.

413 lines
14KB

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