TLA Line data 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 HIT 106030 : work_guard(Ex ex) noexcept
82 106030 : : ex_(std::move(ex))
83 106030 : , owns_(true)
84 : {
85 106030 : ex_.on_work_started();
86 106030 : }
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 2 : work_guard(work_guard const& other) noexcept
103 2 : : ex_(other.ex_)
104 2 : , owns_(other.owns_)
105 : {
106 2 : if(owns_)
107 1 : ex_.on_work_started();
108 2 : }
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 1 : work_guard(work_guard&& other) noexcept
125 1 : : ex_(std::move(other.ex_))
126 1 : , owns_(other.owns_)
127 : {
128 1 : other.owns_ = false;
129 1 : }
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 106033 : ~work_guard()
140 : {
141 106033 : if(owns_)
142 106027 : ex_.on_work_finished();
143 106033 : }
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 317998 : executor() const noexcept
169 : {
170 317998 : 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 12 : owns_work() const noexcept
183 : {
184 12 : 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 5 : reset() noexcept
201 : {
202 5 : if(owns_)
203 : {
204 4 : ex_.on_work_finished();
205 4 : owns_ = false;
206 : }
207 5 : }
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 3 : make_work_guard(Ex ex)
224 : {
225 3 : return work_guard<Ex>(std::move(ex));
226 : }
227 :
228 : } // capy
229 : } // boost
230 :
231 : #endif
|