StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
condition_variable_any.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
44
45#include <chrono>
46#include <cstdint>
47
52namespace StormByte {
53 namespace Safe {
61 public:
67
69
71
77
78 ConditionVariableAny& operator=(const ConditionVariableAny&) = delete;
79
81
85 void notify_one() noexcept;
86
90 void notify_all() noexcept;
91
99 template <class Lock>
100 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
101 void wait(Lock& lock) {
102 Enter();
103 lock.unlock();
104 try {
105 Park();
106 Leave();
107 lock.lock();
108 } catch (...) {
109 Leave();
110 lock.lock();
111 throw;
112 }
113 }
114
124 template <class Lock, class Predicate>
125 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
126 void wait(Lock& lock, Predicate pred) {
127 while (!pred())
128 wait(lock);
129 }
130
142 template <class Lock, class Rep, class Period>
143 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
144 CvStatus wait_for(Lock& lock, const std::chrono::duration<Rep, Period>& rel) {
145 return WaitFor(lock, std::chrono::duration_cast<std::chrono::nanoseconds>(rel).count());
146 }
147
161 template <class Lock, class Rep, class Period, class Predicate>
162 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
163 bool wait_for(Lock& lock, const std::chrono::duration<Rep, Period>& rel, Predicate pred) {
164 const auto deadline = std::chrono::steady_clock::now() + rel;
165 while (!pred()) {
166 if (wait_until(lock, deadline) == CvStatus::Timeout)
167 return pred();
168 }
169 return true;
170 }
171
183 template <class Lock, class Clock, class Duration>
184 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
185 CvStatus wait_until(Lock& lock, const std::chrono::time_point<Clock, Duration>& abs) {
186 const auto rel = abs - Clock::now();
187 if (rel <= Clock::duration::zero())
188 return CvStatus::Timeout;
189 return wait_for(lock, rel);
190 }
191
205 template <class Lock, class Clock, class Duration, class Predicate>
206 requires requires(Lock& lock) { lock.lock(); lock.unlock(); }
207 bool wait_until(Lock& lock, const std::chrono::time_point<Clock, Duration>& abs, Predicate pred) {
208 while (!pred()) {
209 if (wait_until(lock, abs) == CvStatus::Timeout)
210 return pred();
211 }
212 return true;
213 }
214
215 private:
225 template <class Lock>
226 CvStatus WaitFor(Lock& lock, std::int64_t nanos) {
227 if (nanos <= 0)
228 return CvStatus::Timeout;
229 Enter();
230 lock.unlock();
231 try {
232 const CvStatus status = ParkFor(nanos);
233 Leave();
234 lock.lock();
235 return status;
236 } catch (...) {
237 Leave();
238 lock.lock();
239 throw;
240 }
241 }
242
247 void Enter();
248
253 void Leave() noexcept;
254
260 void Park();
261
269 CvStatus ParkFor(std::int64_t nanos);
270
271 void* m_gate;
272 };
273 }
274}
ConditionVariableAny(ConditionVariableAny &&)=delete
CvStatus wait_until(Lock &lock, const std::chrono::time_point< Clock, Duration > &abs)
Wait until notified or abs is reached.
Definition condition_variable_any.hxx:185
void wait(Lock &lock, Predicate pred)
Wait until pred is true.
Definition condition_variable_any.hxx:126
CvStatus wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &rel)
Wait until notified or rel elapses.
Definition condition_variable_any.hxx:144
ConditionVariableAny(const ConditionVariableAny &)=delete
~ConditionVariableAny() noexcept
Destroys the signal on Base's heap.
void notify_one() noexcept
Wake one waiter.
bool wait_until(Lock &lock, const std::chrono::time_point< Clock, Duration > &abs, Predicate pred)
Wait until pred is true or abs is reached.
Definition condition_variable_any.hxx:207
void notify_all() noexcept
Wake every waiter.
ConditionVariableAny()
Condition with no waiters.
void wait(Lock &lock)
Wait until notified.
Definition condition_variable_any.hxx:101
bool wait_for(Lock &lock, const std::chrono::duration< Rep, Period > &rel, Predicate pred)
Wait until pred is true or rel elapses.
Definition condition_variable_any.hxx:163
Owned values that cross a DLL without the caller's CRT.
CvStatus
Why a timed wait returned.
Definition memory_order.hxx:73
@ Timeout
The deadline passed.
Definition memory_order.hxx:75
Root namespace of the StormByte suite.
#define STORMBYTE_PUBLIC
Definition visibility.h:52