Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
concurrency.h
1// SPDX-FileCopyrightText: © 2024-2025 Alexandros Theodotou <alex@zrythm.org>
2// SPDX-License-Identifier: LicenseRef-ZrythmLicense
3
4#pragma once
5
6#include <atomic>
7#include <cassert>
8#include <semaphore>
9
10#include <moodycamel/lightweightsemaphore.h>
11
33class AtomicBoolRAII
34{
35private:
36 std::atomic<bool> &m_flag;
37
38public:
39 explicit AtomicBoolRAII (std::atomic<bool> &flag) : m_flag (flag)
40 {
41 m_flag.store (true, std::memory_order_release);
42 }
43
44 ~AtomicBoolRAII () { m_flag.store (false, std::memory_order_release); }
45
46 // Disable copy and move operations
47 AtomicBoolRAII (const AtomicBoolRAII &) = delete;
48 AtomicBoolRAII &operator= (const AtomicBoolRAII &) = delete;
49 AtomicBoolRAII (AtomicBoolRAII &&) = delete;
50 AtomicBoolRAII &operator= (AtomicBoolRAII &&) = delete;
51};
52
61template <typename SemaphoreType = std::binary_semaphore> class SemaphoreRAII
62{
63public:
69 explicit SemaphoreRAII (SemaphoreType &semaphore, bool force_acquire = false)
70 : semaphore_ (semaphore)
71 {
72 if (force_acquire)
73 {
74 if constexpr (
75 std::is_same_v<SemaphoreType, moodycamel::LightweightSemaphore>)
76 {
77 semaphore_.wait ();
78 }
79 else
80 {
81 semaphore_.acquire ();
82 }
83 acquired_ = true;
84 }
85 else
86 {
87 if constexpr (
88 std::is_same_v<SemaphoreType, moodycamel::LightweightSemaphore>)
89 {
90 acquired_ = semaphore_.tryWait ();
91 }
92 else
93 {
94 acquired_ = semaphore_.try_acquire ();
95 }
96 }
97 }
98
103 {
104 if (acquired_)
105 {
106 if constexpr (
107 std::is_same_v<SemaphoreType, moodycamel::LightweightSemaphore>)
108 {
109 semaphore_.signal ();
110 }
111 else
112 {
113 semaphore_.release ();
114 }
115 }
116 }
117
118 SemaphoreRAII (const SemaphoreRAII &) = delete;
119 SemaphoreRAII &operator= (const SemaphoreRAII &) = delete;
120 SemaphoreRAII (SemaphoreRAII &&) = default;
121 SemaphoreRAII &operator= (SemaphoreRAII &&) = default;
122
129 {
130 if (!acquired_)
131 {
132 if constexpr (
133 std::is_same_v<SemaphoreType, moodycamel::LightweightSemaphore>)
134 {
135 acquired_ = semaphore_.tryWait ();
136 }
137 else
138 {
139 acquired_ = semaphore_.try_acquire ();
140 }
141 }
142 return acquired_;
143 }
144
148 void release ()
149 {
150 if (acquired_)
151 {
152 if constexpr (
153 std::is_same_v<SemaphoreType, moodycamel::LightweightSemaphore>)
154 {
155 semaphore_.signal ();
156 }
157 else
158 {
159 semaphore_.release ();
160 }
161 acquired_ = false;
162 }
163 }
164
170 bool is_acquired () const { return acquired_; }
171
172private:
173 SemaphoreType &semaphore_;
174 bool acquired_ = false;
175};
RAII wrapper class for std::binary_semaphore.
Definition concurrency.h:62
~SemaphoreRAII()
Destructor that releases the semaphore if it was acquired.
bool try_acquire()
Attempts to acquire the semaphore if it hasn't been acquired already.
void release()
Releases the semaphore if it was previously acquired.
SemaphoreRAII(SemaphoreType &semaphore, bool force_acquire=false)
Constructor that attempts to acquire the semaphore.
Definition concurrency.h:69
bool is_acquired() const
Checks if the semaphore is currently acquired by the wrapper object.