strand.hpp 18 KB

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