100.00% Lines (31/31) 100.00% Functions (8/8)
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_WORK_GUARD_HPP 11   #ifndef BOOST_CAPY_WORK_GUARD_HPP
12   #define BOOST_CAPY_WORK_GUARD_HPP 12   #define BOOST_CAPY_WORK_GUARD_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/ex/execution_context.hpp> 15   #include <boost/capy/ex/execution_context.hpp>
16   #include <boost/capy/concept/executor.hpp> 16   #include <boost/capy/concept/executor.hpp>
17   17  
18   #include <utility> 18   #include <utility>
19   19  
20   namespace boost { 20   namespace boost {
21   namespace capy { 21   namespace capy {
22   22  
23 - /** RAII guard that keeps an executor's context from completing. 23 + /** Hold work on an executor so its execution context does not finish.
24   24  
25 - This class holds "work" on an executor, preventing the associated 25 + This class holds "work" on an executor. That keeps the execution
26 - execution context's `run()` function from returning due to lack of 26 + context from finishing for lack of work: for example, `run()` on an
27 - work. It calls `on_work_started()` on construction and 27 + event loop, or @ref thread_pool::join, keeps waiting. It calls
28 - `on_work_finished()` on destruction, ensuring proper work tracking. 28 + `on_work_started()` on construction and `on_work_finished()` on
  29 + destruction, ensuring proper work tracking.
29   30  
30   The guard is useful when you need to keep an execution context 31   The guard is useful when you need to keep an execution context
31   running while waiting for external events or when work is 32   running while waiting for external events or when work is
32   posted later. 33   posted later.
33   34  
34   @par RAII Semantics 35   @par RAII Semantics
35   36  
36   @li Construction calls `ex.on_work_started()`. 37   @li Construction calls `ex.on_work_started()`.
37   @li Destruction calls `ex.on_work_finished()` if `owns_work()`. 38   @li Destruction calls `ex.on_work_finished()` if `owns_work()`.
38   @li Copy construction creates a new work reference (calls 39   @li Copy construction creates a new work reference (calls
39 - `on_work_started()` again). 40 + `on_work_started()` again) if `other` owns work.
40   @li Move construction transfers ownership without additional calls. 41   @li Move construction transfers ownership without additional calls.
41   42  
42   @par Thread Safety 43   @par Thread Safety
43   44  
44   Distinct objects may be accessed concurrently. Access to a single 45   Distinct objects may be accessed concurrently. Access to a single
45   object requires external synchronization. 46   object requires external synchronization.
46   47  
47   @par Example 48   @par Example
48   @par !example work_guard 49   @par !example work_guard
49   50  
50 - @note The executor is returned by reference, allowing callers to  
51 - manage the executor's lifetime directly. This is essential in  
52 - coroutine-first designs where the executor often outlives individual  
53 - coroutine frames.  
54 -  
55   51  
56   @tparam Ex A type satisfying the Executor concept. 52   @tparam Ex A type satisfying the Executor concept.
57   53  
58   @see @ref make_work_guard, @ref Executor 54   @see @ref make_work_guard, @ref Executor
59   */ 55   */
60   template<Executor Ex> 56   template<Executor Ex>
61   class work_guard 57   class work_guard
62   { 58   {
63   Ex ex_; 59   Ex ex_;
64   bool owns_; 60   bool owns_;
65   61  
66   public: 62   public:
67   /** Names the executor type this `work_guard<Ex>` guards. */ 63   /** Names the executor type this `work_guard<Ex>` guards. */
68   using executor_type = Ex; 64   using executor_type = Ex;
69   65  
70   /** Construct a work guard. 66   /** Construct a work guard.
71   67  
72   Calls `ex.on_work_started()` to inform the executor that 68   Calls `ex.on_work_started()` to inform the executor that
73   work is outstanding. 69   work is outstanding.
74   70  
75   @par Exception Safety 71   @par Exception Safety
76   No-throw guarantee. 72   No-throw guarantee.
77   73  
78   @par Postconditions 74   @par Postconditions
79   @li `owns_work() == true` 75   @li `owns_work() == true`
80   @li `executor() == ex` 76   @li `executor() == ex`
81   77  
82   @param ex The executor to hold work on. Moved into the guard. 78   @param ex The executor to hold work on. Moved into the guard.
83   */ 79   */
84   explicit 80   explicit
HITCBC 85   106022 work_guard(Ex ex) noexcept 81   106030 work_guard(Ex ex) noexcept
HITCBC 86   106022 : ex_(std::move(ex)) 82   106030 : ex_(std::move(ex))
HITCBC 87   106022 , owns_(true) 83   106030 , owns_(true)
88   { 84   {
HITCBC 89   106022 ex_.on_work_started(); 85   106030 ex_.on_work_started();
HITCBC 90   106022 } 86   106030 }
91   87  
92   /** Construct a copy. 88   /** Construct a copy.
93   89  
94 - Creates a new work guard holding work on the same executor. 90 + Creates a new work guard on the same executor. If `other`
95 - Calls `on_work_started()` on the executor. 91 + owns work, calls `on_work_started()` on the executor.
96   92  
97   @par Exception Safety 93   @par Exception Safety
98   No-throw guarantee. 94   No-throw guarantee.
99   95  
100   @par Postconditions 96   @par Postconditions
101   @li `owns_work() == other.owns_work()` 97   @li `owns_work() == other.owns_work()`
102   @li `executor() == other.executor()` 98   @li `executor() == other.executor()`
103   99  
104   @param other The work guard to copy from. 100   @param other The work guard to copy from.
105   */ 101   */
HITCBC 106   2 work_guard(work_guard const& other) noexcept 102   2 work_guard(work_guard const& other) noexcept
HITCBC 107   2 : ex_(other.ex_) 103   2 : ex_(other.ex_)
HITCBC 108   2 , owns_(other.owns_) 104   2 , owns_(other.owns_)
109   { 105   {
HITCBC 110   2 if(owns_) 106   2 if(owns_)
HITCBC 111   1 ex_.on_work_started(); 107   1 ex_.on_work_started();
HITCBC 112   2 } 108   2 }
113   109  
114   /** Construct by moving. 110   /** Construct by moving.
115   111  
116   Transfers work ownership from `other` to `*this`. Does not 112   Transfers work ownership from `other` to `*this`. Does not
117   call `on_work_started()` or `on_work_finished()`. 113   call `on_work_started()` or `on_work_finished()`.
118   114  
119   @par Exception Safety 115   @par Exception Safety
120   No-throw guarantee. 116   No-throw guarantee.
121   117  
122   @par Postconditions 118   @par Postconditions
123   @li `owns_work()` equals the prior value of `other.owns_work()` 119   @li `owns_work()` equals the prior value of `other.owns_work()`
124   @li `other.owns_work() == false` 120   @li `other.owns_work() == false`
125   121  
126   @param other The work guard to move from. 122   @param other The work guard to move from.
127   */ 123   */
HITCBC 128   1 work_guard(work_guard&& other) noexcept 124   1 work_guard(work_guard&& other) noexcept
HITCBC 129   1 : ex_(std::move(other.ex_)) 125   1 : ex_(std::move(other.ex_))
HITCBC 130   1 , owns_(other.owns_) 126   1 , owns_(other.owns_)
131   { 127   {
HITCBC 132   1 other.owns_ = false; 128   1 other.owns_ = false;
HITCBC 133   1 } 129   1 }
134   130  
135 - /** Destructor. 131 + /** Destroy the guard, releasing its work if it owns any.
136   132  
137   If `owns_work()` is `true`, calls `on_work_finished()` on 133   If `owns_work()` is `true`, calls `on_work_finished()` on
138   the executor. 134   the executor.
139   135  
140   @par Exception Safety 136   @par Exception Safety
141   No-throw guarantee. 137   No-throw guarantee.
142   */ 138   */
HITCBC 143   106025 ~work_guard() 139   106033 ~work_guard()
144   { 140   {
HITCBC 145   106025 if(owns_) 141   106033 if(owns_)
HITCBC 146   106019 ex_.on_work_finished(); 142   106027 ex_.on_work_finished();
HITCBC 147   106025 } 143   106033 }
148   144  
149   /** Copy assignment is disabled. 145   /** Copy assignment is disabled.
150   146  
151   A guard takes its work reference at construction and releases it at 147   A guard takes its work reference at construction and releases it at
152   destruction or through @ref reset. No operation rebinds an existing 148   destruction or through @ref reset. No operation rebinds an existing
153   guard to a different executor. 149   guard to a different executor.
154   150  
155   @param other The work guard that would be assigned from. 151   @param other The work guard that would be assigned from.
156   152  
157   @return A reference to `*this`. 153   @return A reference to `*this`.
158   */ 154   */
159   work_guard& operator=(work_guard const& other) = delete; 155   work_guard& operator=(work_guard const& other) = delete;
160   156  
161   /** Return the underlying executor by reference. 157   /** Return the underlying executor by reference.
162   158  
163 - The reference remains valid for the lifetime of this guard, 159 + The guard holds its own copy of the executor, so the reference
164 - enabling callers to manage executor lifetime explicitly. 160 + remains valid only for the lifetime of this guard.
165   161  
166   @par Exception Safety 162   @par Exception Safety
167   No-throw guarantee. 163   No-throw guarantee.
168   164  
169   @return A reference to the stored executor. 165   @return A reference to the stored executor.
170   */ 166   */
171   executor_type const& 167   executor_type const&
HITCBC 172   317974 executor() const noexcept 168   317998 executor() const noexcept
173   { 169   {
HITCBC 174   317974 return ex_; 170   317998 return ex_;
175   } 171   }
176   172  
177 - /** Return whether the guard owns work. 173 + /** Check whether the guard owns work.
178   174  
179   @par Exception Safety 175   @par Exception Safety
180   No-throw guarantee. 176   No-throw guarantee.
181   177  
182   @return `true` if this guard calls `on_work_finished()` 178   @return `true` if this guard calls `on_work_finished()`
183   on destruction, `false` otherwise. 179   on destruction, `false` otherwise.
184   */ 180   */
185   bool 181   bool
HITCBC 186   12 owns_work() const noexcept 182   12 owns_work() const noexcept
187   { 183   {
HITCBC 188   12 return owns_; 184   12 return owns_;
189   } 185   }
190   186  
191   /** Release ownership of the work. 187   /** Release ownership of the work.
192   188  
193   If `owns_work()` is `true`, calls `on_work_finished()` on 189   If `owns_work()` is `true`, calls `on_work_finished()` on
194   the executor and sets ownership to `false`. Otherwise, has 190   the executor and sets ownership to `false`. Otherwise, has
195   no effect. 191   no effect.
196   192  
197   @par Exception Safety 193   @par Exception Safety
198   No-throw guarantee. 194   No-throw guarantee.
199   195  
200   @par Postconditions 196   @par Postconditions
201   @li `owns_work() == false` 197   @li `owns_work() == false`
202   */ 198   */
203   void 199   void
HITCBC 204   5 reset() noexcept 200   5 reset() noexcept
205   { 201   {
HITCBC 206   5 if(owns_) 202   5 if(owns_)
207   { 203   {
HITCBC 208   4 ex_.on_work_finished(); 204   4 ex_.on_work_finished();
HITCBC 209   4 owns_ = false; 205   4 owns_ = false;
210   } 206   }
HITCBC 211   5 } 207   5 }
212   }; 208   };
213   209  
214   /** Create a work guard from an executor. 210   /** Create a work guard from an executor.
215   211  
216   @par Exception Safety 212   @par Exception Safety
217   No-throw guarantee. 213   No-throw guarantee.
218   214  
219   @param ex The executor to create the guard for. 215   @param ex The executor to create the guard for.
220   216  
221   @return A `work_guard` holding work on `ex`. 217   @return A `work_guard` holding work on `ex`.
222   218  
223   @see @ref work_guard 219   @see @ref work_guard
224   */ 220   */
225   template<Executor Ex> 221   template<Executor Ex>
226   work_guard<Ex> 222   work_guard<Ex>
HITCBC 227   3 make_work_guard(Ex ex) 223   3 make_work_guard(Ex ex)
228   { 224   {
HITCBC 229   3 return work_guard<Ex>(std::move(ex)); 225   3 return work_guard<Ex>(std::move(ex));
230   } 226   }
231   227  
232   } // capy 228   } // capy
233   } // boost 229   } // boost
234   230  
235   #endif 231   #endif