include/boost/capy/ex/work_guard.hpp

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