AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
AConditionVariable.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13#include <condition_variable>
14#include "AThread.h"
15#include "AUI/Performance/APerformanceSection.h"
16
24{
25private:
26 std::condition_variable_any mImpl;
27
28 struct WaitHelper {
29 WaitHelper(AConditionVariable& var) {
30 auto thread = AThread::current();
31 std::unique_lock lock(thread->mCurrentCV.mutex);
33 thread->mCurrentCV.cv = &var;
34 }
35 ~WaitHelper() noexcept(false) {
36 auto thread = AThread::current();
37 std::unique_lock lock(thread->mCurrentCV.mutex);
38 thread->mCurrentCV.cv = nullptr;
39 }
40 };
41
42 template<typename Predicate>
43 struct PredicateHelper {
44 Predicate predicate;
45 _<AAbstractThread> thread;
46
47 PredicateHelper(Predicate predicate) : predicate(std::move(predicate)), thread(AThread::current()) {}
48 bool operator()() noexcept {
49 if (thread->isInterrupted()) return true;
50 return predicate();
51 }
52 };
53
54public:
62 void notify_all() noexcept { mImpl.notify_all(); }
63
71 void notify_one() noexcept { mImpl.notify_one(); }
72
73 template<typename Lock>
74 void wait(Lock& lock) {
75 APerformanceSection section("Conditional Variable");
76 {
77 WaitHelper w(*this);
78 mImpl.wait(lock);
79 }
81 }
82
88 template<typename Lock, typename Predicate>
89 void wait(Lock& lock, Predicate&& predicate) {
90 APerformanceSection section("Conditional Variable");
91 {
92 WaitHelper w(*this);
93 mImpl.wait(lock, PredicateHelper(std::forward<Predicate>(predicate)));
94 }
96 }
97
103 template<typename Lock, typename Duration>
104 void wait_for(Lock& lock, Duration duration) {
105 APerformanceSection section("Conditional Variable");
106 {
107 WaitHelper w(*this);
108 mImpl.wait_for(lock, duration);
109 }
111 }
112
119 template<typename Lock, typename Duration, typename Predicate>
120 void wait_for(Lock& lock, Duration duration, Predicate&& predicate) {
121 APerformanceSection section("Conditional Variable");
122 {
123 WaitHelper w(*this);
124 mImpl.wait_for(lock, duration, PredicateHelper(std::forward<Predicate>(predicate)));
125 }
127 }
128
134 template<typename Lock, typename Timepoint>
135 void wait_until(Lock& lock, Timepoint timepoint) {
136 APerformanceSection section("Conditional Variable");
137 {
138 WaitHelper w(*this);
139 mImpl.wait_until(lock, timepoint);
140 }
142 }
143
150 template<typename Lock, typename Duration, typename Predicate>
151 void wait_until(Lock& lock, Duration duration, Predicate&& predicate) {
152 APerformanceSection section("Conditional Variable");
153 {
154 WaitHelper w(*this);
155 mImpl.wait_until(lock, duration, PredicateHelper(std::forward<Predicate>(predicate)));
156 }
158 }
159};
Represents a condition variable.
Definition: AConditionVariable.h:24
void notify_one() noexcept
Definition: AConditionVariable.h:71
void wait_until(Lock &lock, Duration duration, Predicate &&predicate)
Definition: AConditionVariable.h:151
void wait_until(Lock &lock, Timepoint timepoint)
Definition: AConditionVariable.h:135
void wait(Lock &lock, Predicate &&predicate)
Definition: AConditionVariable.h:89
void wait_for(Lock &lock, Duration duration, Predicate &&predicate)
Definition: AConditionVariable.h:120
void notify_all() noexcept
Definition: AConditionVariable.h:62
void wait_for(Lock &lock, Duration duration)
Definition: AConditionVariable.h:104
Defines performance profiling named (and colored) span within RAII range.
Definition: APerformanceSection.h:27
static _< AAbstractThread > current()
Definition: AThread.cpp:221
static void interruptionPoint()
Interruption point.
Definition: AThread.cpp:232
An std::weak_ptr with AUI extensions.
Definition: SharedPtrTypes.h:177