StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
condition_variable.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 {
57 namespace Safe {
65 public:
71
73
75
81
82 ConditionVariable& operator=(const ConditionVariable&) = delete;
83
84 ConditionVariable& operator=(ConditionVariable&&) = delete;
85
89 void notify_one() noexcept;
90
94 void notify_all() noexcept;
95
102 void wait(UniqueLock& lock);
103
112 template <class Predicate>
113 void wait(UniqueLock& lock, Predicate pred) {
114 while (!pred())
115 wait(lock);
116 }
117
128 template <class Rep, class Period>
129 CvStatus wait_for(UniqueLock& lock, const std::chrono::duration<Rep, Period>& rel) {
130 return WaitFor(lock, std::chrono::duration_cast<std::chrono::nanoseconds>(rel).count());
131 }
132
145 template <class Rep, class Period, class Predicate>
146 bool wait_for(UniqueLock& lock, const std::chrono::duration<Rep, Period>& rel, Predicate pred) {
147 const auto deadline = std::chrono::steady_clock::now() + rel;
148 while (!pred()) {
149 if (wait_until(lock, deadline) == CvStatus::Timeout)
150 return pred();
151 }
152 return true;
153 }
154
165 template <class Clock, class Duration>
166 CvStatus wait_until(UniqueLock& lock, const std::chrono::time_point<Clock, Duration>& abs) {
167 const auto rel = abs - Clock::now();
168 if (rel <= Clock::duration::zero())
169 return CvStatus::Timeout;
170 return wait_for(lock, rel);
171 }
172
185 template <class Clock, class Duration, class Predicate>
186 bool wait_until(UniqueLock& lock, const std::chrono::time_point<Clock, Duration>& abs, Predicate pred) {
187 while (!pred()) {
188 if (wait_until(lock, abs) == CvStatus::Timeout)
189 return pred();
190 }
191 return true;
192 }
193
194 private:
203 CvStatus WaitFor(UniqueLock& lock, std::int64_t nanos);
204
205 void* m_signal;
206 };
207 }
208}
ConditionVariable(ConditionVariable &&)=delete
void notify_one() noexcept
Wake one waiter.
CvStatus wait_for(UniqueLock &lock, const std::chrono::duration< Rep, Period > &rel)
Wait until notified or rel elapses.
Definition condition_variable.hxx:129
~ConditionVariable() noexcept
Destroys the signal on Base's heap.
void notify_all() noexcept
Wake every waiter.
CvStatus wait_until(UniqueLock &lock, const std::chrono::time_point< Clock, Duration > &abs)
Wait until notified or abs is reached.
Definition condition_variable.hxx:166
bool wait_for(UniqueLock &lock, const std::chrono::duration< Rep, Period > &rel, Predicate pred)
Wait until pred is true or rel elapses.
Definition condition_variable.hxx:146
ConditionVariable(const ConditionVariable &)=delete
void wait(UniqueLock &lock)
Wait until notified.
bool wait_until(UniqueLock &lock, const std::chrono::time_point< Clock, Duration > &abs, Predicate pred)
Wait until pred is true or abs is reached.
Definition condition_variable.hxx:186
ConditionVariable()
Condition with no waiters.
Owns at most one Mutex.
Definition unique_lock.hxx:64
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