executor.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // executor.hpp
  3. // ~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_EXECUTOR_HPP
  11. #define ASIO_EXECUTOR_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. #if !defined(ASIO_NO_TS_EXECUTORS)
  17. #include <typeinfo>
  18. #include "asio/detail/cstddef.hpp"
  19. #include "asio/detail/executor_function.hpp"
  20. #include "asio/detail/memory.hpp"
  21. #include "asio/detail/throw_exception.hpp"
  22. #include "asio/execution_context.hpp"
  23. #include "asio/detail/push_options.hpp"
  24. namespace asio {
  25. /// Exception thrown when trying to access an empty polymorphic executor.
  26. class bad_executor
  27. : public std::exception
  28. {
  29. public:
  30. /// Constructor.
  31. ASIO_DECL bad_executor() ASIO_NOEXCEPT;
  32. /// Obtain message associated with exception.
  33. ASIO_DECL virtual const char* what() const
  34. ASIO_NOEXCEPT_OR_NOTHROW;
  35. };
  36. /// Polymorphic wrapper for executors.
  37. class executor
  38. {
  39. public:
  40. /// Default constructor.
  41. executor() ASIO_NOEXCEPT
  42. : impl_(0)
  43. {
  44. }
  45. /// Construct from nullptr.
  46. executor(nullptr_t) ASIO_NOEXCEPT
  47. : impl_(0)
  48. {
  49. }
  50. /// Copy constructor.
  51. executor(const executor& other) ASIO_NOEXCEPT
  52. : impl_(other.clone())
  53. {
  54. }
  55. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  56. /// Move constructor.
  57. executor(executor&& other) ASIO_NOEXCEPT
  58. : impl_(other.impl_)
  59. {
  60. other.impl_ = 0;
  61. }
  62. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  63. /// Construct a polymorphic wrapper for the specified executor.
  64. template <typename Executor>
  65. executor(Executor e);
  66. /// Allocator-aware constructor to create a polymorphic wrapper for the
  67. /// specified executor.
  68. template <typename Executor, typename Allocator>
  69. executor(allocator_arg_t, const Allocator& a, Executor e);
  70. /// Destructor.
  71. ~executor()
  72. {
  73. destroy();
  74. }
  75. /// Assignment operator.
  76. executor& operator=(const executor& other) ASIO_NOEXCEPT
  77. {
  78. destroy();
  79. impl_ = other.clone();
  80. return *this;
  81. }
  82. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  83. // Move assignment operator.
  84. executor& operator=(executor&& other) ASIO_NOEXCEPT
  85. {
  86. destroy();
  87. impl_ = other.impl_;
  88. other.impl_ = 0;
  89. return *this;
  90. }
  91. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  92. /// Assignment operator for nullptr_t.
  93. executor& operator=(nullptr_t) ASIO_NOEXCEPT
  94. {
  95. destroy();
  96. impl_ = 0;
  97. return *this;
  98. }
  99. /// Assignment operator to create a polymorphic wrapper for the specified
  100. /// executor.
  101. template <typename Executor>
  102. executor& operator=(ASIO_MOVE_ARG(Executor) e) ASIO_NOEXCEPT
  103. {
  104. executor tmp(ASIO_MOVE_CAST(Executor)(e));
  105. destroy();
  106. impl_ = tmp.impl_;
  107. tmp.impl_ = 0;
  108. return *this;
  109. }
  110. /// Obtain the underlying execution context.
  111. execution_context& context() const ASIO_NOEXCEPT
  112. {
  113. return get_impl()->context();
  114. }
  115. /// Inform the executor that it has some outstanding work to do.
  116. void on_work_started() const ASIO_NOEXCEPT
  117. {
  118. get_impl()->on_work_started();
  119. }
  120. /// Inform the executor that some work is no longer outstanding.
  121. void on_work_finished() const ASIO_NOEXCEPT
  122. {
  123. get_impl()->on_work_finished();
  124. }
  125. /// Request the executor to invoke the given function object.
  126. /**
  127. * This function is used to ask the executor to execute the given function
  128. * object. The function object is executed according to the rules of the
  129. * target executor object.
  130. *
  131. * @param f The function object to be called. The executor will make a copy
  132. * of the handler object as required. The function signature of the function
  133. * object must be: @code void function(); @endcode
  134. *
  135. * @param a An allocator that may be used by the executor to allocate the
  136. * internal storage needed for function invocation.
  137. */
  138. template <typename Function, typename Allocator>
  139. void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  140. /// Request the executor to invoke the given function object.
  141. /**
  142. * This function is used to ask the executor to execute the given function
  143. * object. The function object is executed according to the rules of the
  144. * target executor object.
  145. *
  146. * @param f The function object to be called. The executor will make
  147. * a copy of the handler object as required. The function signature of the
  148. * function object must be: @code void function(); @endcode
  149. *
  150. * @param a An allocator that may be used by the executor to allocate the
  151. * internal storage needed for function invocation.
  152. */
  153. template <typename Function, typename Allocator>
  154. void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  155. /// Request the executor to invoke the given function object.
  156. /**
  157. * This function is used to ask the executor to execute the given function
  158. * object. The function object is executed according to the rules of the
  159. * target executor object.
  160. *
  161. * @param f The function object to be called. The executor will make
  162. * a copy of the handler object as required. The function signature of the
  163. * function object must be: @code void function(); @endcode
  164. *
  165. * @param a An allocator that may be used by the executor to allocate the
  166. * internal storage needed for function invocation.
  167. */
  168. template <typename Function, typename Allocator>
  169. void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) const;
  170. struct unspecified_bool_type_t {};
  171. typedef void (*unspecified_bool_type)(unspecified_bool_type_t);
  172. static void unspecified_bool_true(unspecified_bool_type_t) {}
  173. /// Operator to test if the executor contains a valid target.
  174. operator unspecified_bool_type() const ASIO_NOEXCEPT
  175. {
  176. return impl_ ? &executor::unspecified_bool_true : 0;
  177. }
  178. /// Obtain type information for the target executor object.
  179. /**
  180. * @returns If @c *this has a target type of type @c T, <tt>typeid(T)</tt>;
  181. * otherwise, <tt>typeid(void)</tt>.
  182. */
  183. #if !defined(ASIO_NO_TYPEID) || defined(GENERATING_DOCUMENTATION)
  184. const std::type_info& target_type() const ASIO_NOEXCEPT
  185. {
  186. return impl_ ? impl_->target_type() : typeid(void);
  187. }
  188. #else // !defined(ASIO_NO_TYPEID) || defined(GENERATING_DOCUMENTATION)
  189. const void* target_type() const ASIO_NOEXCEPT
  190. {
  191. return impl_ ? impl_->target_type() : 0;
  192. }
  193. #endif // !defined(ASIO_NO_TYPEID) || defined(GENERATING_DOCUMENTATION)
  194. /// Obtain a pointer to the target executor object.
  195. /**
  196. * @returns If <tt>target_type() == typeid(T)</tt>, a pointer to the stored
  197. * executor target; otherwise, a null pointer.
  198. */
  199. template <typename Executor>
  200. Executor* target() ASIO_NOEXCEPT;
  201. /// Obtain a pointer to the target executor object.
  202. /**
  203. * @returns If <tt>target_type() == typeid(T)</tt>, a pointer to the stored
  204. * executor target; otherwise, a null pointer.
  205. */
  206. template <typename Executor>
  207. const Executor* target() const ASIO_NOEXCEPT;
  208. /// Compare two executors for equality.
  209. friend bool operator==(const executor& a,
  210. const executor& b) ASIO_NOEXCEPT
  211. {
  212. if (a.impl_ == b.impl_)
  213. return true;
  214. if (!a.impl_ || !b.impl_)
  215. return false;
  216. return a.impl_->equals(b.impl_);
  217. }
  218. /// Compare two executors for inequality.
  219. friend bool operator!=(const executor& a,
  220. const executor& b) ASIO_NOEXCEPT
  221. {
  222. return !(a == b);
  223. }
  224. private:
  225. #if !defined(GENERATING_DOCUMENTATION)
  226. typedef detail::executor_function function;
  227. template <typename, typename> class impl;
  228. #if !defined(ASIO_NO_TYPEID)
  229. typedef const std::type_info& type_id_result_type;
  230. #else // !defined(ASIO_NO_TYPEID)
  231. typedef const void* type_id_result_type;
  232. #endif // !defined(ASIO_NO_TYPEID)
  233. template <typename T>
  234. static type_id_result_type type_id()
  235. {
  236. #if !defined(ASIO_NO_TYPEID)
  237. return typeid(T);
  238. #else // !defined(ASIO_NO_TYPEID)
  239. static int unique_id;
  240. return &unique_id;
  241. #endif // !defined(ASIO_NO_TYPEID)
  242. }
  243. // Base class for all polymorphic executor implementations.
  244. class impl_base
  245. {
  246. public:
  247. virtual impl_base* clone() const ASIO_NOEXCEPT = 0;
  248. virtual void destroy() ASIO_NOEXCEPT = 0;
  249. virtual execution_context& context() ASIO_NOEXCEPT = 0;
  250. virtual void on_work_started() ASIO_NOEXCEPT = 0;
  251. virtual void on_work_finished() ASIO_NOEXCEPT = 0;
  252. virtual void dispatch(ASIO_MOVE_ARG(function)) = 0;
  253. virtual void post(ASIO_MOVE_ARG(function)) = 0;
  254. virtual void defer(ASIO_MOVE_ARG(function)) = 0;
  255. virtual type_id_result_type target_type() const ASIO_NOEXCEPT = 0;
  256. virtual void* target() ASIO_NOEXCEPT = 0;
  257. virtual const void* target() const ASIO_NOEXCEPT = 0;
  258. virtual bool equals(const impl_base* e) const ASIO_NOEXCEPT = 0;
  259. protected:
  260. impl_base(bool fast_dispatch) : fast_dispatch_(fast_dispatch) {}
  261. virtual ~impl_base() {}
  262. private:
  263. friend class executor;
  264. const bool fast_dispatch_;
  265. };
  266. // Helper function to check and return the implementation pointer.
  267. impl_base* get_impl() const
  268. {
  269. if (!impl_)
  270. {
  271. bad_executor ex;
  272. asio::detail::throw_exception(ex);
  273. }
  274. return impl_;
  275. }
  276. // Helper function to clone another implementation.
  277. impl_base* clone() const ASIO_NOEXCEPT
  278. {
  279. return impl_ ? impl_->clone() : 0;
  280. }
  281. // Helper function to destroy an implementation.
  282. void destroy() ASIO_NOEXCEPT
  283. {
  284. if (impl_)
  285. impl_->destroy();
  286. }
  287. impl_base* impl_;
  288. #endif // !defined(GENERATING_DOCUMENTATION)
  289. };
  290. } // namespace asio
  291. ASIO_USES_ALLOCATOR(asio::executor)
  292. #include "asio/detail/pop_options.hpp"
  293. #include "asio/impl/executor.hpp"
  294. #if defined(ASIO_HEADER_ONLY)
  295. # include "asio/impl/executor.ipp"
  296. #endif // defined(ASIO_HEADER_ONLY)
  297. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  298. #endif // ASIO_EXECUTOR_HPP