thread_pool.hpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. //
  2. // thread_pool.hpp
  3. // ~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_THREAD_POOL_HPP
  11. #define BOOST_ASIO_THREAD_POOL_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/atomic_count.hpp>
  17. #include <boost/asio/detail/scheduler.hpp>
  18. #include <boost/asio/detail/thread_group.hpp>
  19. #include <boost/asio/execution.hpp>
  20. #include <boost/asio/execution_context.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. struct thread_pool_bits
  26. {
  27. static constexpr unsigned int blocking_never = 1;
  28. static constexpr unsigned int blocking_always = 2;
  29. static constexpr unsigned int blocking_mask = 3;
  30. static constexpr unsigned int relationship_continuation = 4;
  31. static constexpr unsigned int outstanding_work_tracked = 8;
  32. };
  33. } // namespace detail
  34. /// A simple fixed-size thread pool.
  35. /**
  36. * The thread pool class is an execution context where functions are permitted
  37. * to run on one of a fixed number of threads.
  38. *
  39. * @par Submitting tasks to the pool
  40. *
  41. * To submit functions to the thread pool, use the @ref boost::asio::dispatch,
  42. * @ref boost::asio::post or @ref boost::asio::defer free functions.
  43. *
  44. * For example:
  45. *
  46. * @code void my_task()
  47. * {
  48. * ...
  49. * }
  50. *
  51. * ...
  52. *
  53. * // Launch the pool with four threads.
  54. * boost::asio::thread_pool pool(4);
  55. *
  56. * // Submit a function to the pool.
  57. * boost::asio::post(pool, my_task);
  58. *
  59. * // Submit a lambda object to the pool.
  60. * boost::asio::post(pool,
  61. * []()
  62. * {
  63. * ...
  64. * });
  65. *
  66. * // Wait for all tasks in the pool to complete.
  67. * pool.join(); @endcode
  68. */
  69. class thread_pool
  70. : public execution_context
  71. {
  72. public:
  73. template <typename Allocator, unsigned int Bits>
  74. class basic_executor_type;
  75. template <typename Allocator, unsigned int Bits>
  76. friend class basic_executor_type;
  77. /// Executor used to submit functions to a thread pool.
  78. typedef basic_executor_type<std::allocator<void>, 0> executor_type;
  79. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  80. /// Constructs a pool with an automatically determined number of threads.
  81. BOOST_ASIO_DECL thread_pool();
  82. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  83. /// Constructs a pool with a specified number of threads.
  84. BOOST_ASIO_DECL thread_pool(std::size_t num_threads);
  85. /// Destructor.
  86. /**
  87. * Automatically stops and joins the pool, if not explicitly done beforehand.
  88. */
  89. BOOST_ASIO_DECL ~thread_pool();
  90. /// Obtains the executor associated with the pool.
  91. executor_type get_executor() noexcept;
  92. /// Obtains the executor associated with the pool.
  93. executor_type executor() noexcept;
  94. /// Stops the threads.
  95. /**
  96. * This function stops the threads as soon as possible. As a result of calling
  97. * @c stop(), pending function objects may be never be invoked.
  98. */
  99. BOOST_ASIO_DECL void stop();
  100. /// Attaches the current thread to the pool.
  101. /**
  102. * This function attaches the current thread to the pool so that it may be
  103. * used for executing submitted function objects. Blocks the calling thread
  104. * until the pool is stopped or joined and has no outstanding work.
  105. */
  106. BOOST_ASIO_DECL void attach();
  107. /// Joins the threads.
  108. /**
  109. * This function blocks until the threads in the pool have completed. If @c
  110. * stop() is not called prior to @c join(), the @c join() call will wait
  111. * until the pool has no more outstanding work.
  112. */
  113. BOOST_ASIO_DECL void join();
  114. /// Waits for threads to complete.
  115. /**
  116. * This function blocks until the threads in the pool have completed. If @c
  117. * stop() is not called prior to @c wait(), the @c wait() call will wait
  118. * until the pool has no more outstanding work.
  119. */
  120. BOOST_ASIO_DECL void wait();
  121. private:
  122. thread_pool(const thread_pool&) = delete;
  123. thread_pool& operator=(const thread_pool&) = delete;
  124. struct thread_function;
  125. // Helper function to create the underlying scheduler.
  126. BOOST_ASIO_DECL detail::scheduler& add_scheduler(detail::scheduler* s);
  127. // The underlying scheduler.
  128. detail::scheduler& scheduler_;
  129. // The threads in the pool.
  130. detail::thread_group threads_;
  131. // The current number of threads in the pool.
  132. detail::atomic_count num_threads_;
  133. };
  134. /// Executor implementation type used to submit functions to a thread pool.
  135. template <typename Allocator, unsigned int Bits>
  136. class thread_pool::basic_executor_type : detail::thread_pool_bits
  137. {
  138. public:
  139. /// Copy constructor.
  140. basic_executor_type(const basic_executor_type& other) noexcept
  141. : pool_(other.pool_),
  142. allocator_(other.allocator_),
  143. bits_(other.bits_)
  144. {
  145. if (Bits & outstanding_work_tracked)
  146. if (pool_)
  147. pool_->scheduler_.work_started();
  148. }
  149. /// Move constructor.
  150. basic_executor_type(basic_executor_type&& other) noexcept
  151. : pool_(other.pool_),
  152. allocator_(static_cast<Allocator&&>(other.allocator_)),
  153. bits_(other.bits_)
  154. {
  155. if (Bits & outstanding_work_tracked)
  156. other.pool_ = 0;
  157. }
  158. /// Destructor.
  159. ~basic_executor_type() noexcept
  160. {
  161. if (Bits & outstanding_work_tracked)
  162. if (pool_)
  163. pool_->scheduler_.work_finished();
  164. }
  165. /// Assignment operator.
  166. basic_executor_type& operator=(const basic_executor_type& other) noexcept;
  167. /// Move assignment operator.
  168. basic_executor_type& operator=(basic_executor_type&& other) noexcept;
  169. #if !defined(GENERATING_DOCUMENTATION)
  170. private:
  171. friend struct boost_asio_require_fn::impl;
  172. friend struct boost_asio_prefer_fn::impl;
  173. #endif // !defined(GENERATING_DOCUMENTATION)
  174. /// Obtain an executor with the @c blocking.possibly property.
  175. /**
  176. * Do not call this function directly. It is intended for use with the
  177. * boost::asio::require customisation point.
  178. *
  179. * For example:
  180. * @code auto ex1 = my_thread_pool.executor();
  181. * auto ex2 = boost::asio::require(ex1,
  182. * boost::asio::execution::blocking.possibly); @endcode
  183. */
  184. constexpr basic_executor_type<Allocator,
  185. BOOST_ASIO_UNSPECIFIED(Bits & ~blocking_mask)>
  186. require(execution::blocking_t::possibly_t) const
  187. {
  188. return basic_executor_type<Allocator, Bits & ~blocking_mask>(
  189. pool_, allocator_, bits_ & ~blocking_mask);
  190. }
  191. /// Obtain an executor with the @c blocking.always property.
  192. /**
  193. * Do not call this function directly. It is intended for use with the
  194. * boost::asio::require customisation point.
  195. *
  196. * For example:
  197. * @code auto ex1 = my_thread_pool.executor();
  198. * auto ex2 = boost::asio::require(ex1,
  199. * boost::asio::execution::blocking.always); @endcode
  200. */
  201. constexpr basic_executor_type<Allocator,
  202. BOOST_ASIO_UNSPECIFIED((Bits & ~blocking_mask) | blocking_always)>
  203. require(execution::blocking_t::always_t) const
  204. {
  205. return basic_executor_type<Allocator,
  206. BOOST_ASIO_UNSPECIFIED((Bits & ~blocking_mask) | blocking_always)>(
  207. pool_, allocator_, bits_ & ~blocking_mask);
  208. }
  209. /// Obtain an executor with the @c blocking.never property.
  210. /**
  211. * Do not call this function directly. It is intended for use with the
  212. * boost::asio::require customisation point.
  213. *
  214. * For example:
  215. * @code auto ex1 = my_thread_pool.executor();
  216. * auto ex2 = boost::asio::require(ex1,
  217. * boost::asio::execution::blocking.never); @endcode
  218. */
  219. constexpr basic_executor_type<Allocator,
  220. BOOST_ASIO_UNSPECIFIED(Bits & ~blocking_mask)>
  221. require(execution::blocking_t::never_t) const
  222. {
  223. return basic_executor_type<Allocator, Bits & ~blocking_mask>(
  224. pool_, allocator_, (bits_ & ~blocking_mask) | blocking_never);
  225. }
  226. /// Obtain an executor with the @c relationship.fork property.
  227. /**
  228. * Do not call this function directly. It is intended for use with the
  229. * boost::asio::require customisation point.
  230. *
  231. * For example:
  232. * @code auto ex1 = my_thread_pool.executor();
  233. * auto ex2 = boost::asio::require(ex1,
  234. * boost::asio::execution::relationship.fork); @endcode
  235. */
  236. constexpr basic_executor_type require(execution::relationship_t::fork_t) const
  237. {
  238. return basic_executor_type(pool_,
  239. allocator_, bits_ & ~relationship_continuation);
  240. }
  241. /// Obtain an executor with the @c relationship.continuation property.
  242. /**
  243. * Do not call this function directly. It is intended for use with the
  244. * boost::asio::require customisation point.
  245. *
  246. * For example:
  247. * @code auto ex1 = my_thread_pool.executor();
  248. * auto ex2 = boost::asio::require(ex1,
  249. * boost::asio::execution::relationship.continuation); @endcode
  250. */
  251. constexpr basic_executor_type require(
  252. execution::relationship_t::continuation_t) const
  253. {
  254. return basic_executor_type(pool_,
  255. allocator_, bits_ | relationship_continuation);
  256. }
  257. /// Obtain an executor with the @c outstanding_work.tracked property.
  258. /**
  259. * Do not call this function directly. It is intended for use with the
  260. * boost::asio::require customisation point.
  261. *
  262. * For example:
  263. * @code auto ex1 = my_thread_pool.executor();
  264. * auto ex2 = boost::asio::require(ex1,
  265. * boost::asio::execution::outstanding_work.tracked); @endcode
  266. */
  267. constexpr basic_executor_type<Allocator,
  268. BOOST_ASIO_UNSPECIFIED(Bits | outstanding_work_tracked)>
  269. require(execution::outstanding_work_t::tracked_t) const
  270. {
  271. return basic_executor_type<Allocator, Bits | outstanding_work_tracked>(
  272. pool_, allocator_, bits_);
  273. }
  274. /// Obtain an executor with the @c outstanding_work.untracked property.
  275. /**
  276. * Do not call this function directly. It is intended for use with the
  277. * boost::asio::require customisation point.
  278. *
  279. * For example:
  280. * @code auto ex1 = my_thread_pool.executor();
  281. * auto ex2 = boost::asio::require(ex1,
  282. * boost::asio::execution::outstanding_work.untracked); @endcode
  283. */
  284. constexpr basic_executor_type<Allocator,
  285. BOOST_ASIO_UNSPECIFIED(Bits & ~outstanding_work_tracked)>
  286. require(execution::outstanding_work_t::untracked_t) const
  287. {
  288. return basic_executor_type<Allocator, Bits & ~outstanding_work_tracked>(
  289. pool_, allocator_, bits_);
  290. }
  291. /// Obtain an executor with the specified @c allocator property.
  292. /**
  293. * Do not call this function directly. It is intended for use with the
  294. * boost::asio::require customisation point.
  295. *
  296. * For example:
  297. * @code auto ex1 = my_thread_pool.executor();
  298. * auto ex2 = boost::asio::require(ex1,
  299. * boost::asio::execution::allocator(my_allocator)); @endcode
  300. */
  301. template <typename OtherAllocator>
  302. constexpr basic_executor_type<OtherAllocator, Bits>
  303. require(execution::allocator_t<OtherAllocator> a) const
  304. {
  305. return basic_executor_type<OtherAllocator, Bits>(
  306. pool_, a.value(), bits_);
  307. }
  308. /// Obtain an executor with the default @c allocator property.
  309. /**
  310. * Do not call this function directly. It is intended for use with the
  311. * boost::asio::require customisation point.
  312. *
  313. * For example:
  314. * @code auto ex1 = my_thread_pool.executor();
  315. * auto ex2 = boost::asio::require(ex1,
  316. * boost::asio::execution::allocator); @endcode
  317. */
  318. constexpr basic_executor_type<std::allocator<void>, Bits>
  319. require(execution::allocator_t<void>) const
  320. {
  321. return basic_executor_type<std::allocator<void>, Bits>(
  322. pool_, std::allocator<void>(), bits_);
  323. }
  324. #if !defined(GENERATING_DOCUMENTATION)
  325. private:
  326. friend struct boost_asio_query_fn::impl;
  327. friend struct boost::asio::execution::detail::mapping_t<0>;
  328. friend struct boost::asio::execution::detail::outstanding_work_t<0>;
  329. #endif // !defined(GENERATING_DOCUMENTATION)
  330. /// Query the current value of the @c mapping property.
  331. /**
  332. * Do not call this function directly. It is intended for use with the
  333. * boost::asio::query customisation point.
  334. *
  335. * For example:
  336. * @code auto ex = my_thread_pool.executor();
  337. * if (boost::asio::query(ex, boost::asio::execution::mapping)
  338. * == boost::asio::execution::mapping.thread)
  339. * ... @endcode
  340. */
  341. static constexpr execution::mapping_t query(execution::mapping_t) noexcept
  342. {
  343. return execution::mapping.thread;
  344. }
  345. /// Query the current value of the @c context property.
  346. /**
  347. * Do not call this function directly. It is intended for use with the
  348. * boost::asio::query customisation point.
  349. *
  350. * For example:
  351. * @code auto ex = my_thread_pool.executor();
  352. * boost::asio::thread_pool& pool = boost::asio::query(
  353. * ex, boost::asio::execution::context); @endcode
  354. */
  355. thread_pool& query(execution::context_t) const noexcept
  356. {
  357. return *pool_;
  358. }
  359. /// Query the current value of the @c blocking property.
  360. /**
  361. * Do not call this function directly. It is intended for use with the
  362. * boost::asio::query customisation point.
  363. *
  364. * For example:
  365. * @code auto ex = my_thread_pool.executor();
  366. * if (boost::asio::query(ex, boost::asio::execution::blocking)
  367. * == boost::asio::execution::blocking.always)
  368. * ... @endcode
  369. */
  370. constexpr execution::blocking_t query(execution::blocking_t) const noexcept
  371. {
  372. return (bits_ & blocking_never)
  373. ? execution::blocking_t(execution::blocking.never)
  374. : ((Bits & blocking_always)
  375. ? execution::blocking_t(execution::blocking.always)
  376. : execution::blocking_t(execution::blocking.possibly));
  377. }
  378. /// Query the current value of the @c relationship property.
  379. /**
  380. * Do not call this function directly. It is intended for use with the
  381. * boost::asio::query customisation point.
  382. *
  383. * For example:
  384. * @code auto ex = my_thread_pool.executor();
  385. * if (boost::asio::query(ex, boost::asio::execution::relationship)
  386. * == boost::asio::execution::relationship.continuation)
  387. * ... @endcode
  388. */
  389. constexpr execution::relationship_t query(
  390. execution::relationship_t) const noexcept
  391. {
  392. return (bits_ & relationship_continuation)
  393. ? execution::relationship_t(execution::relationship.continuation)
  394. : execution::relationship_t(execution::relationship.fork);
  395. }
  396. /// Query the current value of the @c outstanding_work property.
  397. /**
  398. * Do not call this function directly. It is intended for use with the
  399. * boost::asio::query customisation point.
  400. *
  401. * For example:
  402. * @code auto ex = my_thread_pool.executor();
  403. * if (boost::asio::query(ex, boost::asio::execution::outstanding_work)
  404. * == boost::asio::execution::outstanding_work.tracked)
  405. * ... @endcode
  406. */
  407. static constexpr execution::outstanding_work_t query(
  408. execution::outstanding_work_t) noexcept
  409. {
  410. return (Bits & outstanding_work_tracked)
  411. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  412. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  413. }
  414. /// Query the current value of the @c allocator property.
  415. /**
  416. * Do not call this function directly. It is intended for use with the
  417. * boost::asio::query customisation point.
  418. *
  419. * For example:
  420. * @code auto ex = my_thread_pool.executor();
  421. * auto alloc = boost::asio::query(ex,
  422. * boost::asio::execution::allocator); @endcode
  423. */
  424. template <typename OtherAllocator>
  425. constexpr Allocator query(
  426. execution::allocator_t<OtherAllocator>) const noexcept
  427. {
  428. return allocator_;
  429. }
  430. /// Query the current value of the @c allocator property.
  431. /**
  432. * Do not call this function directly. It is intended for use with the
  433. * boost::asio::query customisation point.
  434. *
  435. * For example:
  436. * @code auto ex = my_thread_pool.executor();
  437. * auto alloc = boost::asio::query(ex,
  438. * boost::asio::execution::allocator); @endcode
  439. */
  440. constexpr Allocator query(execution::allocator_t<void>) const noexcept
  441. {
  442. return allocator_;
  443. }
  444. /// Query the occupancy (recommended number of work items) for the pool.
  445. /**
  446. * Do not call this function directly. It is intended for use with the
  447. * boost::asio::query customisation point.
  448. *
  449. * For example:
  450. * @code auto ex = my_thread_pool.executor();
  451. * std::size_t occupancy = boost::asio::query(
  452. * ex, boost::asio::execution::occupancy); @endcode
  453. */
  454. std::size_t query(execution::occupancy_t) const noexcept
  455. {
  456. return static_cast<std::size_t>(pool_->num_threads_);
  457. }
  458. public:
  459. /// Determine whether the thread pool is running in the current thread.
  460. /**
  461. * @return @c true if the current thread is running the thread pool. Otherwise
  462. * returns @c false.
  463. */
  464. bool running_in_this_thread() const noexcept;
  465. /// Compare two executors for equality.
  466. /**
  467. * Two executors are equal if they refer to the same underlying thread pool.
  468. */
  469. friend bool operator==(const basic_executor_type& a,
  470. const basic_executor_type& b) noexcept
  471. {
  472. return a.pool_ == b.pool_
  473. && a.allocator_ == b.allocator_
  474. && a.bits_ == b.bits_;
  475. }
  476. /// Compare two executors for inequality.
  477. /**
  478. * Two executors are equal if they refer to the same underlying thread pool.
  479. */
  480. friend bool operator!=(const basic_executor_type& a,
  481. const basic_executor_type& b) noexcept
  482. {
  483. return a.pool_ != b.pool_
  484. || a.allocator_ != b.allocator_
  485. || a.bits_ != b.bits_;
  486. }
  487. /// Execution function.
  488. template <typename Function>
  489. void execute(Function&& f) const
  490. {
  491. this->do_execute(static_cast<Function&&>(f),
  492. integral_constant<bool, (Bits & blocking_always) != 0>());
  493. }
  494. public:
  495. #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  496. /// Obtain the underlying execution context.
  497. thread_pool& context() const noexcept;
  498. /// Inform the thread pool that it has some outstanding work to do.
  499. /**
  500. * This function is used to inform the thread pool that some work has begun.
  501. * This ensures that the thread pool's join() function will not return while
  502. * the work is underway.
  503. */
  504. void on_work_started() const noexcept;
  505. /// Inform the thread pool that some work is no longer outstanding.
  506. /**
  507. * This function is used to inform the thread pool that some work has
  508. * finished. Once the count of unfinished work reaches zero, the thread
  509. * pool's join() function is permitted to exit.
  510. */
  511. void on_work_finished() const noexcept;
  512. /// Request the thread pool to invoke the given function object.
  513. /**
  514. * This function is used to ask the thread pool to execute the given function
  515. * object. If the current thread belongs to the pool, @c dispatch() executes
  516. * the function before returning. Otherwise, the function will be scheduled
  517. * to run on the thread pool.
  518. *
  519. * @param f The function object to be called. The executor will make
  520. * a copy of the handler object as required. The function signature of the
  521. * function object must be: @code void function(); @endcode
  522. *
  523. * @param a An allocator that may be used by the executor to allocate the
  524. * internal storage needed for function invocation.
  525. */
  526. template <typename Function, typename OtherAllocator>
  527. void dispatch(Function&& f, const OtherAllocator& a) const;
  528. /// Request the thread pool to invoke the given function object.
  529. /**
  530. * This function is used to ask the thread pool to execute the given function
  531. * object. The function object will never be executed inside @c post().
  532. * Instead, it will be scheduled to run on the thread pool.
  533. *
  534. * @param f The function object to be called. The executor will make
  535. * a copy of the handler object as required. The function signature of the
  536. * function object must be: @code void function(); @endcode
  537. *
  538. * @param a An allocator that may be used by the executor to allocate the
  539. * internal storage needed for function invocation.
  540. */
  541. template <typename Function, typename OtherAllocator>
  542. void post(Function&& f, const OtherAllocator& a) const;
  543. /// Request the thread pool to invoke the given function object.
  544. /**
  545. * This function is used to ask the thread pool to execute the given function
  546. * object. The function object will never be executed inside @c defer().
  547. * Instead, it will be scheduled to run on the thread pool.
  548. *
  549. * If the current thread belongs to the thread pool, @c defer() will delay
  550. * scheduling the function object until the current thread returns control to
  551. * the pool.
  552. *
  553. * @param f The function object to be called. The executor will make
  554. * a copy of the handler object as required. The function signature of the
  555. * function object must be: @code void function(); @endcode
  556. *
  557. * @param a An allocator that may be used by the executor to allocate the
  558. * internal storage needed for function invocation.
  559. */
  560. template <typename Function, typename OtherAllocator>
  561. void defer(Function&& f, const OtherAllocator& a) const;
  562. #endif // !defined(BOOST_ASIO_NO_TS_EXECUTORS)
  563. private:
  564. friend class thread_pool;
  565. template <typename, unsigned int> friend class basic_executor_type;
  566. // Constructor used by thread_pool::get_executor().
  567. explicit basic_executor_type(thread_pool& p) noexcept
  568. : pool_(&p),
  569. allocator_(),
  570. bits_(0)
  571. {
  572. if (Bits & outstanding_work_tracked)
  573. pool_->scheduler_.work_started();
  574. }
  575. // Constructor used by require().
  576. basic_executor_type(thread_pool* p,
  577. const Allocator& a, unsigned int bits) noexcept
  578. : pool_(p),
  579. allocator_(a),
  580. bits_(bits)
  581. {
  582. if (Bits & outstanding_work_tracked)
  583. if (pool_)
  584. pool_->scheduler_.work_started();
  585. }
  586. /// Execution helper implementation for possibly and never blocking.
  587. template <typename Function>
  588. void do_execute(Function&& f, false_type) const;
  589. /// Execution helper implementation for always blocking.
  590. template <typename Function>
  591. void do_execute(Function&& f, true_type) const;
  592. // The underlying thread pool.
  593. thread_pool* pool_;
  594. // The allocator used for execution functions.
  595. Allocator allocator_;
  596. // The runtime-switched properties of the thread pool executor.
  597. unsigned int bits_;
  598. };
  599. #if !defined(GENERATING_DOCUMENTATION)
  600. namespace traits {
  601. #if !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  602. template <typename Allocator, unsigned int Bits>
  603. struct equality_comparable<
  604. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>
  605. >
  606. {
  607. static constexpr bool is_valid = true;
  608. static constexpr bool is_noexcept = true;
  609. };
  610. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EQUALITY_COMPARABLE_TRAIT)
  611. #if !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  612. template <typename Allocator, unsigned int Bits, typename Function>
  613. struct execute_member<
  614. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  615. Function
  616. >
  617. {
  618. static constexpr bool is_valid = true;
  619. static constexpr bool is_noexcept = false;
  620. typedef void result_type;
  621. };
  622. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_EXECUTE_MEMBER_TRAIT)
  623. #if !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  624. template <typename Allocator, unsigned int Bits>
  625. struct require_member<
  626. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  627. boost::asio::execution::blocking_t::possibly_t
  628. > : boost::asio::detail::thread_pool_bits
  629. {
  630. static constexpr bool is_valid = true;
  631. static constexpr bool is_noexcept = true;
  632. typedef boost::asio::thread_pool::basic_executor_type<
  633. Allocator, Bits & ~blocking_mask> result_type;
  634. };
  635. template <typename Allocator, unsigned int Bits>
  636. struct require_member<
  637. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  638. boost::asio::execution::blocking_t::always_t
  639. > : boost::asio::detail::thread_pool_bits
  640. {
  641. static constexpr bool is_valid = true;
  642. static constexpr bool is_noexcept = false;
  643. typedef boost::asio::thread_pool::basic_executor_type<Allocator,
  644. (Bits & ~blocking_mask) | blocking_always> result_type;
  645. };
  646. template <typename Allocator, unsigned int Bits>
  647. struct require_member<
  648. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  649. boost::asio::execution::blocking_t::never_t
  650. > : boost::asio::detail::thread_pool_bits
  651. {
  652. static constexpr bool is_valid = true;
  653. static constexpr bool is_noexcept = false;
  654. typedef boost::asio::thread_pool::basic_executor_type<
  655. Allocator, Bits & ~blocking_mask> result_type;
  656. };
  657. template <typename Allocator, unsigned int Bits>
  658. struct require_member<
  659. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  660. boost::asio::execution::relationship_t::fork_t
  661. >
  662. {
  663. static constexpr bool is_valid = true;
  664. static constexpr bool is_noexcept = false;
  665. typedef boost::asio::thread_pool::basic_executor_type<
  666. Allocator, Bits> result_type;
  667. };
  668. template <typename Allocator, unsigned int Bits>
  669. struct require_member<
  670. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  671. boost::asio::execution::relationship_t::continuation_t
  672. >
  673. {
  674. static constexpr bool is_valid = true;
  675. static constexpr bool is_noexcept = false;
  676. typedef boost::asio::thread_pool::basic_executor_type<
  677. Allocator, Bits> result_type;
  678. };
  679. template <typename Allocator, unsigned int Bits>
  680. struct require_member<
  681. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  682. boost::asio::execution::outstanding_work_t::tracked_t
  683. > : boost::asio::detail::thread_pool_bits
  684. {
  685. static constexpr bool is_valid = true;
  686. static constexpr bool is_noexcept = false;
  687. typedef boost::asio::thread_pool::basic_executor_type<
  688. Allocator, Bits | outstanding_work_tracked> result_type;
  689. };
  690. template <typename Allocator, unsigned int Bits>
  691. struct require_member<
  692. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  693. boost::asio::execution::outstanding_work_t::untracked_t
  694. > : boost::asio::detail::thread_pool_bits
  695. {
  696. static constexpr bool is_valid = true;
  697. static constexpr bool is_noexcept = false;
  698. typedef boost::asio::thread_pool::basic_executor_type<
  699. Allocator, Bits & ~outstanding_work_tracked> result_type;
  700. };
  701. template <typename Allocator, unsigned int Bits>
  702. struct require_member<
  703. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  704. boost::asio::execution::allocator_t<void>
  705. >
  706. {
  707. static constexpr bool is_valid = true;
  708. static constexpr bool is_noexcept = false;
  709. typedef boost::asio::thread_pool::basic_executor_type<
  710. std::allocator<void>, Bits> result_type;
  711. };
  712. template <unsigned int Bits,
  713. typename Allocator, typename OtherAllocator>
  714. struct require_member<
  715. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  716. boost::asio::execution::allocator_t<OtherAllocator>
  717. >
  718. {
  719. static constexpr bool is_valid = true;
  720. static constexpr bool is_noexcept = false;
  721. typedef boost::asio::thread_pool::basic_executor_type<
  722. OtherAllocator, Bits> result_type;
  723. };
  724. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_REQUIRE_MEMBER_TRAIT)
  725. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  726. template <typename Allocator, unsigned int Bits, typename Property>
  727. struct query_static_constexpr_member<
  728. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  729. Property,
  730. typename boost::asio::enable_if<
  731. boost::asio::is_convertible<
  732. Property,
  733. boost::asio::execution::outstanding_work_t
  734. >::value
  735. >::type
  736. > : boost::asio::detail::thread_pool_bits
  737. {
  738. static constexpr bool is_valid = true;
  739. static constexpr bool is_noexcept = true;
  740. typedef boost::asio::execution::outstanding_work_t result_type;
  741. static constexpr result_type value() noexcept
  742. {
  743. return (Bits & outstanding_work_tracked)
  744. ? execution::outstanding_work_t(execution::outstanding_work.tracked)
  745. : execution::outstanding_work_t(execution::outstanding_work.untracked);
  746. }
  747. };
  748. template <typename Allocator, unsigned int Bits, typename Property>
  749. struct query_static_constexpr_member<
  750. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  751. Property,
  752. typename boost::asio::enable_if<
  753. boost::asio::is_convertible<
  754. Property,
  755. boost::asio::execution::mapping_t
  756. >::value
  757. >::type
  758. >
  759. {
  760. static constexpr bool is_valid = true;
  761. static constexpr bool is_noexcept = true;
  762. typedef boost::asio::execution::mapping_t::thread_t result_type;
  763. static constexpr result_type value() noexcept
  764. {
  765. return result_type();
  766. }
  767. };
  768. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_STATIC_CONSTEXPR_MEMBER_TRAIT)
  769. #if !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  770. template <typename Allocator, unsigned int Bits, typename Property>
  771. struct query_member<
  772. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  773. Property,
  774. typename boost::asio::enable_if<
  775. boost::asio::is_convertible<
  776. Property,
  777. boost::asio::execution::blocking_t
  778. >::value
  779. >::type
  780. >
  781. {
  782. static constexpr bool is_valid = true;
  783. static constexpr bool is_noexcept = true;
  784. typedef boost::asio::execution::blocking_t result_type;
  785. };
  786. template <typename Allocator, unsigned int Bits, typename Property>
  787. struct query_member<
  788. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  789. Property,
  790. typename boost::asio::enable_if<
  791. boost::asio::is_convertible<
  792. Property,
  793. boost::asio::execution::relationship_t
  794. >::value
  795. >::type
  796. >
  797. {
  798. static constexpr bool is_valid = true;
  799. static constexpr bool is_noexcept = true;
  800. typedef boost::asio::execution::relationship_t result_type;
  801. };
  802. template <typename Allocator, unsigned int Bits>
  803. struct query_member<
  804. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  805. boost::asio::execution::occupancy_t
  806. >
  807. {
  808. static constexpr bool is_valid = true;
  809. static constexpr bool is_noexcept = true;
  810. typedef std::size_t result_type;
  811. };
  812. template <typename Allocator, unsigned int Bits>
  813. struct query_member<
  814. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  815. boost::asio::execution::context_t
  816. >
  817. {
  818. static constexpr bool is_valid = true;
  819. static constexpr bool is_noexcept = true;
  820. typedef boost::asio::thread_pool& result_type;
  821. };
  822. template <typename Allocator, unsigned int Bits>
  823. struct query_member<
  824. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  825. boost::asio::execution::allocator_t<void>
  826. >
  827. {
  828. static constexpr bool is_valid = true;
  829. static constexpr bool is_noexcept = true;
  830. typedef Allocator result_type;
  831. };
  832. template <typename Allocator, unsigned int Bits, typename OtherAllocator>
  833. struct query_member<
  834. boost::asio::thread_pool::basic_executor_type<Allocator, Bits>,
  835. boost::asio::execution::allocator_t<OtherAllocator>
  836. >
  837. {
  838. static constexpr bool is_valid = true;
  839. static constexpr bool is_noexcept = true;
  840. typedef Allocator result_type;
  841. };
  842. #endif // !defined(BOOST_ASIO_HAS_DEDUCED_QUERY_MEMBER_TRAIT)
  843. } // namespace traits
  844. namespace execution {
  845. template <>
  846. struct is_executor<thread_pool> : false_type
  847. {
  848. };
  849. } // namespace execution
  850. #endif // !defined(GENERATING_DOCUMENTATION)
  851. } // namespace asio
  852. } // namespace boost
  853. #include <boost/asio/detail/pop_options.hpp>
  854. #include <boost/asio/impl/thread_pool.hpp>
  855. #if defined(BOOST_ASIO_HEADER_ONLY)
  856. # include <boost/asio/impl/thread_pool.ipp>
  857. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  858. #endif // BOOST_ASIO_THREAD_POOL_HPP