strand.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. //
  2. // strand.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_STRAND_HPP
  11. #define ASIO_STRAND_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 "asio/detail/strand_executor_service.hpp"
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/execution/blocking.hpp"
  19. #include "asio/execution/executor.hpp"
  20. #include "asio/is_executor.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. /// Provides serialised function invocation for any executor type.
  24. template <typename Executor>
  25. class strand
  26. {
  27. public:
  28. /// The type of the underlying executor.
  29. typedef Executor inner_executor_type;
  30. /// Default constructor.
  31. /**
  32. * This constructor is only valid if the underlying executor type is default
  33. * constructible.
  34. */
  35. strand()
  36. : executor_(),
  37. impl_(strand::create_implementation(executor_))
  38. {
  39. }
  40. /// Construct a strand for the specified executor.
  41. template <typename Executor1>
  42. explicit strand(const Executor1& e,
  43. typename constraint<
  44. conditional<
  45. !is_same<Executor1, strand>::value,
  46. is_convertible<Executor1, Executor>,
  47. false_type
  48. >::type::value
  49. >::type = 0)
  50. : executor_(e),
  51. impl_(strand::create_implementation(executor_))
  52. {
  53. }
  54. /// Copy constructor.
  55. strand(const strand& other) ASIO_NOEXCEPT
  56. : executor_(other.executor_),
  57. impl_(other.impl_)
  58. {
  59. }
  60. /// Converting constructor.
  61. /**
  62. * This constructor is only valid if the @c OtherExecutor type is convertible
  63. * to @c Executor.
  64. */
  65. template <class OtherExecutor>
  66. strand(
  67. const strand<OtherExecutor>& other) ASIO_NOEXCEPT
  68. : executor_(other.executor_),
  69. impl_(other.impl_)
  70. {
  71. }
  72. /// Assignment operator.
  73. strand& operator=(const strand& other) ASIO_NOEXCEPT
  74. {
  75. executor_ = other.executor_;
  76. impl_ = other.impl_;
  77. return *this;
  78. }
  79. /// Converting assignment operator.
  80. /**
  81. * This assignment operator is only valid if the @c OtherExecutor type is
  82. * convertible to @c Executor.
  83. */
  84. template <class OtherExecutor>
  85. strand& operator=(
  86. const strand<OtherExecutor>& other) ASIO_NOEXCEPT
  87. {
  88. executor_ = other.executor_;
  89. impl_ = other.impl_;
  90. return *this;
  91. }
  92. #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  93. /// Move constructor.
  94. strand(strand&& other) ASIO_NOEXCEPT
  95. : executor_(ASIO_MOVE_CAST(Executor)(other.executor_)),
  96. impl_(ASIO_MOVE_CAST(implementation_type)(other.impl_))
  97. {
  98. }
  99. /// Converting move constructor.
  100. /**
  101. * This constructor is only valid if the @c OtherExecutor type is convertible
  102. * to @c Executor.
  103. */
  104. template <class OtherExecutor>
  105. strand(strand<OtherExecutor>&& other) ASIO_NOEXCEPT
  106. : executor_(ASIO_MOVE_CAST(OtherExecutor)(other.executor_)),
  107. impl_(ASIO_MOVE_CAST(implementation_type)(other.impl_))
  108. {
  109. }
  110. /// Move assignment operator.
  111. strand& operator=(strand&& other) ASIO_NOEXCEPT
  112. {
  113. executor_ = ASIO_MOVE_CAST(Executor)(other.executor_);
  114. impl_ = ASIO_MOVE_CAST(implementation_type)(other.impl_);
  115. return *this;
  116. }
  117. /// Converting move assignment operator.
  118. /**
  119. * This assignment operator is only valid if the @c OtherExecutor type is
  120. * convertible to @c Executor.
  121. */
  122. template <class OtherExecutor>
  123. strand& operator=(strand<OtherExecutor>&& other) ASIO_NOEXCEPT
  124. {
  125. executor_ = ASIO_MOVE_CAST(OtherExecutor)(other.executor_);
  126. impl_ = ASIO_MOVE_CAST(implementation_type)(other.impl_);
  127. return *this;
  128. }
  129. #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
  130. /// Destructor.
  131. ~strand() ASIO_NOEXCEPT
  132. {
  133. }
  134. /// Obtain the underlying executor.
  135. inner_executor_type get_inner_executor() const ASIO_NOEXCEPT
  136. {
  137. return executor_;
  138. }
  139. /// Forward a query to the underlying executor.
  140. /**
  141. * Do not call this function directly. It is intended for use with the
  142. * asio::query customisation point.
  143. *
  144. * For example:
  145. * @code asio::strand<my_executor_type> ex = ...;
  146. * if (asio::query(ex, asio::execution::blocking)
  147. * == asio::execution::blocking.never)
  148. * ... @endcode
  149. */
  150. template <typename Property>
  151. typename constraint<
  152. can_query<const Executor&, Property>::value,
  153. typename conditional<
  154. is_convertible<Property, execution::blocking_t>::value,
  155. execution::blocking_t,
  156. typename query_result<const Executor&, Property>::type
  157. >::type
  158. >::type query(const Property& p) const
  159. ASIO_NOEXCEPT_IF((
  160. is_nothrow_query<const Executor&, Property>::value))
  161. {
  162. return this->query_helper(
  163. is_convertible<Property, execution::blocking_t>(), p);
  164. }
  165. /// Forward a requirement to the underlying executor.
  166. /**
  167. * Do not call this function directly. It is intended for use with the
  168. * asio::require customisation point.
  169. *
  170. * For example:
  171. * @code asio::strand<my_executor_type> ex1 = ...;
  172. * auto ex2 = asio::require(ex1,
  173. * asio::execution::blocking.never); @endcode
  174. */
  175. template <typename Property>
  176. typename constraint<
  177. can_require<const Executor&, Property>::value
  178. && !is_convertible<Property, execution::blocking_t::always_t>::value,
  179. strand<typename decay<
  180. typename require_result<const Executor&, Property>::type
  181. >::type>
  182. >::type require(const Property& p) const
  183. ASIO_NOEXCEPT_IF((
  184. is_nothrow_require<const Executor&, Property>::value))
  185. {
  186. return strand<typename decay<
  187. typename require_result<const Executor&, Property>::type
  188. >::type>(asio::require(executor_, p), impl_);
  189. }
  190. /// Forward a preference to the underlying executor.
  191. /**
  192. * Do not call this function directly. It is intended for use with the
  193. * asio::prefer customisation point.
  194. *
  195. * For example:
  196. * @code asio::strand<my_executor_type> ex1 = ...;
  197. * auto ex2 = asio::prefer(ex1,
  198. * asio::execution::blocking.never); @endcode
  199. */
  200. template <typename Property>
  201. typename constraint<
  202. can_prefer<const Executor&, Property>::value
  203. && !is_convertible<Property, execution::blocking_t::always_t>::value,
  204. strand<typename decay<
  205. typename prefer_result<const Executor&, Property>::type
  206. >::type>
  207. >::type prefer(const Property& p) const
  208. ASIO_NOEXCEPT_IF((
  209. is_nothrow_prefer<const Executor&, Property>::value))
  210. {
  211. return strand<typename decay<
  212. typename prefer_result<const Executor&, Property>::type
  213. >::type>(asio::prefer(executor_, p), impl_);
  214. }
  215. #if !defined(ASIO_NO_TS_EXECUTORS)
  216. /// Obtain the underlying execution context.
  217. execution_context& context() const ASIO_NOEXCEPT
  218. {
  219. return executor_.context();
  220. }
  221. /// Inform the strand that it has some outstanding work to do.
  222. /**
  223. * The strand delegates this call to its underlying executor.
  224. */
  225. void on_work_started() const ASIO_NOEXCEPT
  226. {
  227. executor_.on_work_started();
  228. }
  229. /// Inform the strand that some work is no longer outstanding.
  230. /**
  231. * The strand delegates this call to its underlying executor.
  232. */
  233. void on_work_finished() const ASIO_NOEXCEPT
  234. {
  235. executor_.on_work_finished();
  236. }
  237. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  238. /// Request the strand to invoke the given function object.
  239. /**
  240. * Do not call this function directly. It is intended for use with the
  241. * execution::execute customisation point.
  242. *
  243. * For example:
  244. * @code asio::strand<my_executor_type> ex = ...;
  245. * execution::execute(ex, my_function_object); @endcode
  246. *
  247. * This function is used to ask the strand to execute the given function
  248. * object on its underlying executor. The function object will be executed
  249. * according to the properties of the underlying executor.
  250. *
  251. * @param f The function object to be called. The executor will make
  252. * a copy of the handler object as required. The function signature of the
  253. * function object must be: @code void function(); @endcode
  254. */
  255. template <typename Function>
  256. typename constraint<
  257. execution::can_execute<const Executor&, Function>::value,
  258. void
  259. >::type execute(ASIO_MOVE_ARG(Function) f) const
  260. {
  261. detail::strand_executor_service::execute(impl_,
  262. executor_, ASIO_MOVE_CAST(Function)(f));
  263. }
  264. #if !defined(ASIO_NO_TS_EXECUTORS)
  265. /// Request the strand to invoke the given function object.
  266. /**
  267. * This function is used to ask the strand to execute the given function
  268. * object on its underlying executor. The function object will be executed
  269. * inside this function if the strand is not otherwise busy and if the
  270. * underlying executor's @c dispatch() function is also able to execute the
  271. * function before returning.
  272. *
  273. * @param f The function object to be called. The executor will make
  274. * a copy of the handler object as required. The function signature of the
  275. * function object must be: @code void function(); @endcode
  276. *
  277. * @param a An allocator that may be used by the executor to allocate the
  278. * internal storage needed for function invocation.
  279. */
  280. template <typename Function, typename Allocator>
  281. void dispatch(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  282. {
  283. detail::strand_executor_service::dispatch(impl_,
  284. executor_, ASIO_MOVE_CAST(Function)(f), a);
  285. }
  286. /// Request the strand to invoke the given function object.
  287. /**
  288. * This function is used to ask the executor to execute the given function
  289. * object. The function object will never be executed inside this function.
  290. * Instead, it will be scheduled by the underlying executor's defer function.
  291. *
  292. * @param f The function object to be called. The executor will make
  293. * a copy of the handler object as required. The function signature of the
  294. * function object must be: @code void function(); @endcode
  295. *
  296. * @param a An allocator that may be used by the executor to allocate the
  297. * internal storage needed for function invocation.
  298. */
  299. template <typename Function, typename Allocator>
  300. void post(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  301. {
  302. detail::strand_executor_service::post(impl_,
  303. executor_, ASIO_MOVE_CAST(Function)(f), a);
  304. }
  305. /// Request the strand to invoke the given function object.
  306. /**
  307. * This function is used to ask the executor to execute the given function
  308. * object. The function object will never be executed inside this function.
  309. * Instead, it will be scheduled by the underlying executor's defer function.
  310. *
  311. * @param f The function object to be called. The executor will make
  312. * a copy of the handler object as required. The function signature of the
  313. * function object must be: @code void function(); @endcode
  314. *
  315. * @param a An allocator that may be used by the executor to allocate the
  316. * internal storage needed for function invocation.
  317. */
  318. template <typename Function, typename Allocator>
  319. void defer(ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  320. {
  321. detail::strand_executor_service::defer(impl_,
  322. executor_, ASIO_MOVE_CAST(Function)(f), a);
  323. }
  324. #endif // !defined(ASIO_NO_TS_EXECUTORS)
  325. /// Determine whether the strand is running in the current thread.
  326. /**
  327. * @return @c true if the current thread is executing a function that was
  328. * submitted to the strand using post(), dispatch() or defer(). Otherwise
  329. * returns @c false.
  330. */
  331. bool running_in_this_thread() const ASIO_NOEXCEPT
  332. {
  333. return detail::strand_executor_service::running_in_this_thread(impl_);
  334. }
  335. /// Compare two strands for equality.
  336. /**
  337. * Two strands are equal if they refer to the same ordered, non-concurrent
  338. * state.
  339. */
  340. friend bool operator==(const strand& a, const strand& b) ASIO_NOEXCEPT
  341. {
  342. return a.impl_ == b.impl_;
  343. }
  344. /// Compare two strands for inequality.
  345. /**
  346. * Two strands are equal if they refer to the same ordered, non-concurrent
  347. * state.
  348. */
  349. friend bool operator!=(const strand& a, const strand& b) ASIO_NOEXCEPT
  350. {
  351. return a.impl_ != b.impl_;
  352. }
  353. #if defined(GENERATING_DOCUMENTATION)
  354. private:
  355. #endif // defined(GENERATING_DOCUMENTATION)
  356. typedef detail::strand_executor_service::implementation_type
  357. implementation_type;
  358. template <typename InnerExecutor>
  359. static implementation_type create_implementation(const InnerExecutor& ex,
  360. typename constraint<
  361. can_query<InnerExecutor, execution::context_t>::value
  362. >::type = 0)
  363. {
  364. return use_service<detail::strand_executor_service>(
  365. asio::query(ex, execution::context)).create_implementation();
  366. }
  367. template <typename InnerExecutor>
  368. static implementation_type create_implementation(const InnerExecutor& ex,
  369. typename constraint<
  370. !can_query<InnerExecutor, execution::context_t>::value
  371. >::type = 0)
  372. {
  373. return use_service<detail::strand_executor_service>(
  374. ex.context()).create_implementation();
  375. }
  376. strand(const Executor& ex, const implementation_type& impl)
  377. : executor_(ex),
  378. impl_(impl)
  379. {
  380. }
  381. template <typename Property>
  382. typename query_result<const Executor&, Property>::type query_helper(
  383. false_type, const Property& property) const
  384. {
  385. return asio::query(executor_, property);
  386. }
  387. template <typename Property>
  388. execution::blocking_t query_helper(true_type, const Property& property) const
  389. {
  390. execution::blocking_t result = asio::query(executor_, property);
  391. return result == execution::blocking.always
  392. ? execution::blocking.possibly : result;
  393. }
  394. Executor executor_;
  395. implementation_type impl_;
  396. };
  397. /** @defgroup make_strand asio::make_strand
  398. *
  399. * @brief The asio::make_strand function creates a @ref strand object for
  400. * an executor or execution context.
  401. */
  402. /*@{*/
  403. /// Create a @ref strand object for an executor.
  404. template <typename Executor>
  405. inline strand<Executor> make_strand(const Executor& ex,
  406. typename constraint<
  407. is_executor<Executor>::value || execution::is_executor<Executor>::value
  408. >::type = 0)
  409. {
  410. return strand<Executor>(ex);
  411. }
  412. /// Create a @ref strand object for an execution context.
  413. template <typename ExecutionContext>
  414. inline strand<typename ExecutionContext::executor_type>
  415. make_strand(ExecutionContext& ctx,
  416. typename constraint<
  417. is_convertible<ExecutionContext&, execution_context&>::value
  418. >::type = 0)
  419. {
  420. return strand<typename ExecutionContext::executor_type>(ctx.get_executor());
  421. }
  422. /*@}*/
  423. #if !defined(GENERATING_DOCUMENTATION)
  424. namespace traits {
  425. #if !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  426. template <typename Executor>
  427. struct equality_comparable<strand<Executor> >
  428. {
  429. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  430. ASIO_STATIC_CONSTEXPR(bool, is_noexcept = true);
  431. };
  432. #endif // !defined(ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  433. #if !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  434. template <typename Executor, typename Function>
  435. struct execute_member<strand<Executor>, Function,
  436. typename enable_if<
  437. execution::can_execute<const Executor&, Function>::value
  438. >::type>
  439. {
  440. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  441. ASIO_STATIC_CONSTEXPR(bool, is_noexcept = false);
  442. typedef void result_type;
  443. };
  444. #endif // !defined(ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  445. #if !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  446. template <typename Executor, typename Property>
  447. struct query_member<strand<Executor>, Property,
  448. typename enable_if<
  449. can_query<const Executor&, Property>::value
  450. >::type>
  451. {
  452. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  453. ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  454. (is_nothrow_query<Executor, Property>::value));
  455. typedef typename conditional<
  456. is_convertible<Property, execution::blocking_t>::value,
  457. execution::blocking_t, typename query_result<Executor, Property>::type
  458. >::type result_type;
  459. };
  460. #endif // !defined(ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  461. #if !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  462. template <typename Executor, typename Property>
  463. struct require_member<strand<Executor>, Property,
  464. typename enable_if<
  465. can_require<const Executor&, Property>::value
  466. && !is_convertible<Property, execution::blocking_t::always_t>::value
  467. >::type>
  468. {
  469. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  470. ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  471. (is_nothrow_require<Executor, Property>::value));
  472. typedef strand<typename decay<
  473. typename require_result<Executor, Property>::type
  474. >::type> result_type;
  475. };
  476. #endif // !defined(ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  477. #if !defined(ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT)
  478. template <typename Executor, typename Property>
  479. struct prefer_member<strand<Executor>, Property,
  480. typename enable_if<
  481. can_prefer<const Executor&, Property>::value
  482. && !is_convertible<Property, execution::blocking_t::always_t>::value
  483. >::type>
  484. {
  485. ASIO_STATIC_CONSTEXPR(bool, is_valid = true);
  486. ASIO_STATIC_CONSTEXPR(bool, is_noexcept =
  487. (is_nothrow_prefer<Executor, Property>::value));
  488. typedef strand<typename decay<
  489. typename prefer_result<Executor, Property>::type
  490. >::type> result_type;
  491. };
  492. #endif // !defined(ASIO_HAS_DEDUCED_PREFER_MEMBER_TRAIT)
  493. } // namespace traits
  494. #endif // !defined(GENERATING_DOCUMENTATION)
  495. } // namespace asio
  496. #include "asio/detail/pop_options.hpp"
  497. // If both io_context.hpp and strand.hpp have been included, automatically
  498. // include the header file needed for the io_context::strand class.
  499. #if !defined(ASIO_NO_EXTENSIONS)
  500. # if defined(ASIO_IO_CONTEXT_HPP)
  501. # include "asio/io_context_strand.hpp"
  502. # endif // defined(ASIO_IO_CONTEXT_HPP)
  503. #endif // !defined(ASIO_NO_EXTENSIONS)
  504. #endif // ASIO_STRAND_HPP