100.00% Lines (7/7) 100.00% Functions (3/3)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // Copyright (c) 2026 Michael Vandeberg 3   // Copyright (c) 2026 Michael Vandeberg
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 5   // Distributed under the Boost Software License, Version 1.0. (See accompanying
6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/cppalliance/capy 8   // Official repository: https://github.com/cppalliance/capy
9   // 9   //
10   10  
11   #ifndef BOOST_CAPY_EX_THREAD_POOL_HPP 11   #ifndef BOOST_CAPY_EX_THREAD_POOL_HPP
12   #define BOOST_CAPY_EX_THREAD_POOL_HPP 12   #define BOOST_CAPY_EX_THREAD_POOL_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/continuation.hpp> 15   #include <boost/capy/continuation.hpp>
16   #include <coroutine> 16   #include <coroutine>
17   #include <boost/capy/ex/execution_context.hpp> 17   #include <boost/capy/ex/execution_context.hpp>
18   #include <cstddef> 18   #include <cstddef>
19   #include <string_view> 19   #include <string_view>
20   20  
21   namespace boost { 21   namespace boost {
22   namespace capy { 22   namespace capy {
23   23  
24 - /** A pool of threads for executing work concurrently. 24 + /** Distributes posted work across a fixed group of worker threads via a shared queue.
25   25  
26   Use this when you need to run coroutines on multiple threads 26   Use this when you need to run coroutines on multiple threads
27   without the overhead of creating and destroying threads for 27   without the overhead of creating and destroying threads for
28   each task. Work items are distributed across the pool using 28   each task. Work items are distributed across the pool using
29   a shared queue. 29   a shared queue.
30   30  
31   @par Thread Safety 31   @par Thread Safety
32   Distinct objects: Safe. 32   Distinct objects: Safe.
33 - Shared objects: Unsafe. 33 + Shared objects: Safe for @ref get_executor, @ref join, and
  34 + @ref stop. Unsafe for construction and destruction.
34   35  
35   @par Example 36   @par Example
36   @code 37   @code
37   thread_pool pool(4); // 4 worker threads 38   thread_pool pool(4); // 4 worker threads
38   auto ex = pool.get_executor(); 39   auto ex = pool.get_executor();
39 - run_async(ex)(some_task()); // launch work; tracked so join() waits for it 40 + run_async(ex)(some_task()); // start work; tracked so join() waits for it
40   pool.join(); // wait for outstanding work to complete 41   pool.join(); // wait for outstanding work to complete
41   // pool destructor stops the pool, discarding any pending work 42   // pool destructor stops the pool, discarding any pending work
42   @endcode 43   @endcode
43   44  
44   @note `join()` waits only for work that holds outstanding-work 45   @note `join()` waits only for work that holds outstanding-work
45   counting, which `run_async` (and `make_work_guard`) provide. A bare 46   counting, which `run_async` (and `make_work_guard`) provide. A bare
46   `executor_type::post()` does not register outstanding work, so 47   `executor_type::post()` does not register outstanding work, so
47 - `join()` will not wait for it. 48 + `join()` does not wait for it.
48   */ 49   */
49   class BOOST_CAPY_DECL 50   class BOOST_CAPY_DECL
50   thread_pool 51   thread_pool
51   : public execution_context 52   : public execution_context
52   { 53   {
53   class impl; 54   class impl;
54   impl* impl_; 55   impl* impl_;
55   56  
56   public: 57   public:
57   class executor_type; 58   class executor_type;
58   59  
59   /** Destroy the thread pool. 60   /** Destroy the thread pool.
60   61  
61   Signals all worker threads to stop, waits for them to 62   Signals all worker threads to stop, waits for them to
62   finish, and destroys any pending work items. 63   finish, and destroys any pending work items.
63   64  
64 - @par Preconditions 65 + @pre No thread outside this pool may post or dispatch work to it
65 - No thread outside this pool may post or dispatch work to it  
66   (or to a strand built on it) concurrently with, or after, 66   (or to a strand built on it) concurrently with, or after,
67 - destruction; doing so is undefined behavior. Submit such work 67 + destruction. Doing so is undefined behavior. Submit such work
68   through @ref run_async or @ref run and call @ref join before 68   through @ref run_async or @ref run and call @ref join before
69   the pool is destroyed, so it has completed first. 69   the pool is destroyed, so it has completed first.
70   */ 70   */
71   ~thread_pool(); 71   ~thread_pool();
72   72  
73   /** Construct a thread pool. 73   /** Construct a thread pool.
74   74  
75 - Creates a pool with the specified number of worker threads. 75 + Records the requested worker count; no threads are created
  76 + yet. Threads start lazily on the executor's first `post()`.
76   If `num_threads` is zero, the number of threads is set to 77   If `num_threads` is zero, the number of threads is set to
77   the hardware concurrency, or one if that cannot be determined. 78   the hardware concurrency, or one if that cannot be determined.
78   79  
79   @param num_threads The number of worker threads, or zero 80   @param num_threads The number of worker threads, or zero
80   for automatic selection. 81   for automatic selection.
81   82  
82   @param thread_name_prefix The prefix for worker thread names. 83   @param thread_name_prefix The prefix for worker thread names.
83   Thread names appear as "{prefix}0", "{prefix}1", etc. 84   Thread names appear as "{prefix}0", "{prefix}1", etc.
84   The prefix is truncated to 12 characters. Defaults to 85   The prefix is truncated to 12 characters. Defaults to
85   "capy-pool-". 86   "capy-pool-".
86   */ 87   */
87   explicit 88   explicit
88   thread_pool( 89   thread_pool(
89   std::size_t num_threads = 0, 90   std::size_t num_threads = 0,
90   std::string_view thread_name_prefix = "capy-pool-"); 91   std::string_view thread_name_prefix = "capy-pool-");
91   92  
92 - thread_pool(thread_pool const&) = delete; 93 + /** Copy construction is disabled; a pool owns its worker threads.
93 - thread_pool& operator=(thread_pool const&) = delete; 94 +
  95 + @param other The pool that would be copied.
  96 + */
  97 + thread_pool(thread_pool const& other) = delete;
  98 +
  99 + /** Copy assignment is disabled; a pool owns its worker threads.
  100 +
  101 + @param other The pool that would be assigned from.
  102 +
  103 + @return A reference to `*this`.
  104 + */
  105 + thread_pool& operator=(thread_pool const& other) = delete;
94   106  
95   /** Wait for all outstanding work to complete. 107   /** Wait for all outstanding work to complete.
96   108  
97   Releases the internal work guard, then blocks the calling 109   Releases the internal work guard, then blocks the calling
98   thread until all outstanding work tracked by 110   thread until all outstanding work tracked by
99   @ref executor_type::on_work_started and 111   @ref executor_type::on_work_started and
100   @ref executor_type::on_work_finished completes. After all 112   @ref executor_type::on_work_finished completes. After all
101   work finishes, joins the worker threads. 113   work finishes, joins the worker threads.
102   114  
103   If @ref stop is called while `join()` is blocking, the 115   If @ref stop is called while `join()` is blocking, the
104   pool stops without waiting for remaining work to 116   pool stops without waiting for remaining work to
105   complete. Worker threads finish their current item and 117   complete. Worker threads finish their current item and
106   exit; `join()` still waits for all threads to be joined 118   exit; `join()` still waits for all threads to be joined
107   before returning. 119   before returning.
108   120  
109   This function is idempotent. The first call performs the 121   This function is idempotent. The first call performs the
110   join; subsequent calls return immediately. 122   join; subsequent calls return immediately.
111   123  
112 - @par Preconditions 124 + @pre Must not be called from a thread in this pool (undefined
113 - Must not be called from a thread in this pool (undefined  
114   behavior). 125   behavior).
115   126  
116   @par Postconditions 127   @par Postconditions
117   All worker threads have been joined. The pool cannot be 128   All worker threads have been joined. The pool cannot be
118   reused. 129   reused.
119   130  
120   @par Thread Safety 131   @par Thread Safety
121   May be called from any thread not in this pool. 132   May be called from any thread not in this pool.
122   */ 133   */
123   void 134   void
124   join() noexcept; 135   join() noexcept;
125   136  
126   /** Request all worker threads to stop. 137   /** Request all worker threads to stop.
127   138  
128   Signals all threads to exit after finishing their current 139   Signals all threads to exit after finishing their current
129   work item. Queued work that has not started is abandoned. 140   work item. Queued work that has not started is abandoned.
130   Does not wait for threads to exit. 141   Does not wait for threads to exit.
131   142  
132   If @ref join is blocking on another thread, calling 143   If @ref join is blocking on another thread, calling
133   `stop()` causes it to stop waiting for outstanding 144   `stop()` causes it to stop waiting for outstanding
134   work. The `join()` call still waits for worker threads 145   work. The `join()` call still waits for worker threads
135   to finish their current item and exit before returning. 146   to finish their current item and exit before returning.
  147 +
  148 + @par Thread Safety
  149 + May be called concurrently from any thread, including a
  150 + thread in this pool.
136   */ 151   */
137   void 152   void
138   stop() noexcept; 153   stop() noexcept;
139   154  
140   /** Return an executor for this thread pool. 155   /** Return an executor for this thread pool.
141   156  
142   @return An executor associated with this thread pool. 157   @return An executor associated with this thread pool.
143   */ 158   */
144   executor_type 159   executor_type
145   get_executor() const noexcept; 160   get_executor() const noexcept;
146   }; 161   };
147   162  
148   /** An executor that submits work to a thread_pool. 163   /** An executor that submits work to a thread_pool.
149   164  
150   Executors are lightweight handles that can be copied and stored. 165   Executors are lightweight handles that can be copied and stored.
151   All copies refer to the same underlying thread pool. 166   All copies refer to the same underlying thread pool.
152   167  
153   @par Thread Safety 168   @par Thread Safety
154   Distinct objects: Safe. 169   Distinct objects: Safe.
155   Shared objects: Safe. 170   Shared objects: Safe.
156   */ 171   */
157   class thread_pool::executor_type 172   class thread_pool::executor_type
158   { 173   {
159   friend class thread_pool; 174   friend class thread_pool;
160   175  
161   thread_pool* pool_ = nullptr; 176   thread_pool* pool_ = nullptr;
162   177  
163   explicit 178   explicit
HITCBC 164   11797 executor_type(thread_pool& pool) noexcept 179   11800 executor_type(thread_pool& pool) noexcept
HITCBC 165   11797 : pool_(&pool) 180   11800 : pool_(&pool)
166   { 181   {
HITCBC 167   11797 } 182   11800 }
168   183  
169   public: 184   public:
170   /** Construct a default null executor. 185   /** Construct a default null executor.
171   186  
172   The resulting executor is not associated with any pool. 187   The resulting executor is not associated with any pool.
173   `context()`, `dispatch()`, and `post()` require the 188   `context()`, `dispatch()`, and `post()` require the
174   executor to be associated with a pool before use. 189   executor to be associated with a pool before use.
175   */ 190   */
176   executor_type() = default; 191   executor_type() = default;
177   192  
178 - /// Return the underlying thread pool. 193 + /** Return the underlying thread pool.
  194 +
  195 + @return A reference to the associated pool. The behavior is
  196 + undefined if the executor is not associated with a pool.
  197 + */
179   thread_pool& 198   thread_pool&
HITCBC 180   12109 context() const noexcept 199   12104 context() const noexcept
181   { 200   {
HITCBC 182   12109 return *pool_; 201   12104 return *pool_;
183   } 202   }
184   203  
185   /** Notify that work has started. 204   /** Notify that work has started.
186   205  
187   Increments the outstanding work count. Must be paired 206   Increments the outstanding work count. Must be paired
188   with a subsequent call to @ref on_work_finished. 207   with a subsequent call to @ref on_work_finished.
189   208  
190   @see on_work_finished, work_guard 209   @see on_work_finished, work_guard
191   */ 210   */
192   BOOST_CAPY_DECL 211   BOOST_CAPY_DECL
193   void 212   void
194   on_work_started() const noexcept; 213   on_work_started() const noexcept;
195   214  
196   /** Notify that work has finished. 215   /** Notify that work has finished.
197   216  
198   Decrements the outstanding work count. When the count 217   Decrements the outstanding work count. When the count
199 - reaches zero after @ref thread_pool::join has been called, 218 + reaches zero after @ref thread_pool::join is called,
200   the pool's worker threads are signaled to stop. 219   the pool's worker threads are signaled to stop.
201   220  
202   @pre A preceding call to @ref on_work_started was made. 221   @pre A preceding call to @ref on_work_started was made.
203   222  
204   @see on_work_started, work_guard 223   @see on_work_started, work_guard
205   */ 224   */
206   BOOST_CAPY_DECL 225   BOOST_CAPY_DECL
207   void 226   void
208   on_work_finished() const noexcept; 227   on_work_finished() const noexcept;
209   228  
210   /** Dispatch a continuation for execution. 229   /** Dispatch a continuation for execution.
211   230  
212   If the calling thread is a worker of this pool, returns 231   If the calling thread is a worker of this pool, returns
213   `c.h` for symmetric transfer so the caller can resume the 232   `c.h` for symmetric transfer so the caller can resume the
214   continuation inline. Otherwise, posts the continuation to 233   continuation inline. Otherwise, posts the continuation to
215   the pool for execution on a worker thread and returns 234   the pool for execution on a worker thread and returns
216   `std::noop_coroutine()`. 235   `std::noop_coroutine()`.
217   236  
218   @param c The continuation to execute. On the post path, 237   @param c The continuation to execute. On the post path,
219   must remain at a stable address until dequeued 238   must remain at a stable address until dequeued
220   and resumed. 239   and resumed.
221   240  
222   @return `c.h` when the calling thread is a pool worker; 241   @return `c.h` when the calling thread is a pool worker;
223   `std::noop_coroutine()` otherwise. 242   `std::noop_coroutine()` otherwise.
224   */ 243   */
225   BOOST_CAPY_DECL 244   BOOST_CAPY_DECL
226   std::coroutine_handle<> 245   std::coroutine_handle<>
227   dispatch(continuation& c) const; 246   dispatch(continuation& c) const;
228   247  
229   /** Post a continuation to the thread pool. 248   /** Post a continuation to the thread pool.
230   249  
231 - The continuation will be resumed on one of the pool's 250 + The continuation is resumed on one of the pool's
232   worker threads. The continuation must remain at a stable 251   worker threads. The continuation must remain at a stable
233   address until it is dequeued and resumed. 252   address until it is dequeued and resumed.
234   253  
235   @param c The continuation to execute. 254   @param c The continuation to execute.
236   */ 255   */
237   BOOST_CAPY_DECL 256   BOOST_CAPY_DECL
238   void 257   void
239   post(continuation& c) const; 258   post(continuation& c) const;
240   259  
241 - /// Return true if two executors refer to the same thread pool. 260 + /** Return true if two executors refer to the same thread pool.
  261 +
  262 + @param other The executor to compare against.
  263 +
  264 + @return `true` if both executors refer to the same pool.
  265 + */
242   bool 266   bool
HITCBC 243   13 operator==(executor_type const& other) const noexcept 267   13 operator==(executor_type const& other) const noexcept
244   { 268   {
HITCBC 245   13 return pool_ == other.pool_; 269   13 return pool_ == other.pool_;
246   } 270   }
247   }; 271   };
248   272  
249   } // capy 273   } // capy
250   } // boost 274   } // boost
251   275  
252   #endif 276   #endif