1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2021 Gothel Software e.K.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef JAU_LATCH_HPP_
#define JAU_LATCH_HPP_
#include <cstdint>
#include <mutex>
#include <condition_variable>
#include <jau/ordered_atomic.hpp>
#include <jau/fraction_type.hpp>
#include <jau/basic_types.hpp>
namespace jau {
/** \addtogroup Concurrency
*
* @{
*/
/**
* Inspired by std::latch of C++20
*
* Adds count_up() as an extension, allowing to dynamically add *events* to required to complete.
*
* @see https://en.cppreference.com/w/cpp/thread/latch
*/
class latch {
private:
mutable std::mutex mtx_cd;
mutable std::condition_variable cv;
jau::sc_atomic_size_t count;
public:
/** Returns the maximum value of the internal counter supported by the implementation. */
static constexpr size_t max() noexcept { return std::numeric_limits<size_t>::max(); }
/**
* Initialize instance with counter zero.
*
* Useful for member instances in combination with count_up() or with set() before count_down().
*
* Extension of std::latch.
*/
latch() noexcept = default;
/**
* Initialize instance with given counter.
*
* Compatible with std::latch.
*
* @param count_
*/
latch(const size_t count_) noexcept
: count(count_) {}
/**
* No copy constructor nor move constructor.
*
* Compatible with std::latch.
*
* @param o
*/
latch(const latch& o) = delete;
/**
* Return the current atomic internal counter.
*
* Extension of std::latch.
*/
size_t value() const noexcept { return count; }
/**
* Atomically decrements the internal counter by n
* and notifies all blocked wait() threads if zero is reached.
*
* If n is greater than the value of the internal counter, the counter is set to zero.
*
* This operation strongly happens-before all calls that are unblocked on this latch.
*
* Compatible with std::latch.
*
* @param n the value by which the internal counter is decreased, defaults to 1
*/
void count_down(const size_t n = 1) noexcept {
bool notify;
{
std::unique_lock<std::mutex> lock(mtx_cd); // Avoid data-race on concurrent count_down() and wait*() calls
if( n < count ) {
count = count - n;
notify = false;
} else {
count = 0;
notify = true;
}
}
if( notify ) {
cv.notify_all();
}
}
/**
* Atomically increments the internal counter by n, i.e. adds value to count down.
*
* If internal counter + n is greater than the maximum value of the internal counter, the counter is set to its maximum.
*
* This operation strongly happens-before all calls that are unblocked on this latch.
*
* Extension of std::latch.
*
* @param n the value by which the internal counter is increased, defaults to 1
*/
void count_up(const size_t n = 1) noexcept {
std::unique_lock<std::mutex> lock(mtx_cd); // Avoid data-race on concurrent count_down() and wait*() calls
if( n <= std::numeric_limits<std::size_t>::max() - count ) {
count = count + n;
} else {
count = std::numeric_limits<std::size_t>::max();
}
}
/**
* Atomically set the internal counter by n.
*
* This operation strongly happens-before all calls that are unblocked on this latch.
*
* Extension of std::latch.
*
* @param n the value to be assigned to the internal counter
*/
void set(const size_t n) noexcept {
std::unique_lock<std::mutex> lock(mtx_cd); // Avoid data-race on concurrent count_down() and wait*() calls
count = n;
}
/**
* Returns true only if the internal counter has reached zero.
*
* Compatible with std::latch.
*/
bool try_wait() const noexcept {
return 0 == count;
}
/**
* Blocks the calling thread until the internal counter reaches 0.
*
* If the internal counter is zero already, returns immediately.
*
* Compatible with std::latch.
*/
void wait() const noexcept {
if( 0 < count ) {
std::unique_lock<std::mutex> lock(mtx_cd);
while( 0 < count ) {
cv.wait(lock);
}
}
}
/**
* Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero.
*
* Equivalent to `count_down(n); wait();`.
*
* Compatible with std::latch.
*
* @param n the value by which the internal counter is decreased, defaults to 1
*/
void arrive_and_wait(const size_t n = 1) noexcept {
count_down(n);
wait();
}
/**
* Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.
*
* If the internal counter is zero already, returns immediately.
*
* Implementation uses wait_for() w/ a monotonic clock and fraction_i64.
*
* Extension of std::latch.
*
* @param timeout_duration maximum duration in fractions of seconds to wait
* @return true if internal counter has reached zero, otherwise a timeout has occurred.
*/
bool wait_for(const fraction_i64& timeout_duration) const noexcept {
if( 0 < count ) {
std::unique_lock<std::mutex> lock(mtx_cd);
const fraction_timespec timeout_time = getMonotonicTime() + timeout_duration;
while( 0 < count ) {
std::cv_status s = wait_until(cv, lock, timeout_time);
if( 0 == count ) {
return true;
}
if( std::cv_status::timeout == s ) {
return false;
}
}
}
return true;
}
/**
* Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero
* or the given timeout duration has expired.
*
* Equivalent to `count_down(n); wait(timeout_duration);`.
*
* Implementation uses `std::chrono::steady_clock::now()` and fraction_i64.
*
* Extension of std::latch.
*
* @param timeout_duration maximum duration in fractions of seconds to wait
* @param n the value by which the internal counter is decreased, defaults to 1
* @return true if internal counter has reached zero, otherwise a timeout has occurred.
*/
bool arrive_and_wait_for(const fraction_i64& timeout_duration, const size_t n = 1) noexcept {
count_down(n);
return wait_for(timeout_duration);
}
/**
* Blocks the calling thread until the internal counter reaches 0 or the given timeout duration has expired.
*
* If the internal counter is zero already, returns immediately.
*
* Implementation uses `std::chrono::steady_clock::now()`.
*
* Extension of std::latch.
*
* @tparam Rep
* @tparam Period
* @param timeout_duration maximum duration to wait
* @return true if internal counter has reached zero, otherwise a timeout has occurred.
*/
template<typename Rep, typename Period>
bool wait_for(const std::chrono::duration<Rep, Period>& timeout_duration) const noexcept {
if( 0 < count ) {
std::unique_lock<std::mutex> lock(mtx_cd);
std::chrono::steady_clock::time_point timeout_time = std::chrono::steady_clock::now() + timeout_duration;
while( 0 < count ) {
std::cv_status s = cv.wait_until(lock, timeout_time );
if( 0 == count ) {
return true;
}
if( std::cv_status::timeout == s ) {
return false;
}
}
}
return true;
}
/**
* Atomically decrements the internal counter by n and (if necessary) blocks the calling thread until the counter reaches zero
* or the given timeout duration has expired.
*
* Equivalent to `count_down(n); wait(timeout_duration);`.
*
* Implementation uses `std::chrono::steady_clock::now()`.
*
* Extension of std::latch.
*
* @tparam Rep
* @tparam Period
* @param timeout_duration maximum duration to wait
* @param n the value by which the internal counter is decreased, defaults to 1
* @return true if internal counter has reached zero, otherwise a timeout has occurred.
*/
template<typename Rep, typename Period>
bool arrive_and_wait_for(const std::chrono::duration<Rep, Period>& timeout_duration, const size_t n = 1) noexcept {
count_down(n);
return wait_for(timeout_duration);
}
};
/**@}*/
} /* namespace jau */
#endif /* JAU_LATCH_HPP_ */
|