CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
atomic_ref_condition.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
8
9#if defined(COOPSYNC_TBB_DOXYGEN) || \
10 (defined(COOPSYNC_TBB_HAS_ATOMIC_REF_CONDITION) && \
11 COOPSYNC_TBB_HAS_ATOMIC_REF_CONDITION == 1)
12
13#include <atomic>
14#include <cstring>
15#include <type_traits>
16
17#include "coopsync_tbb/detail/macros.hpp"
18#include "coopsync_tbb/detail/wait_queue.hpp"
19
20namespace coopsync_tbb {
21
29template <typename T>
30class COOPSYNC_TBB_EXPORT atomic_ref_condition {
31 static_assert(std::is_trivially_copyable<T>::value,
32 "atomic_ref_condition requires trivially copyable type");
33 static_assert(
34 std::atomic_ref<T>::is_always_lock_free || !std::is_volatile<T>::value,
35 "If std::atomic<T> is not always lock-free, T must not be volatile");
36
37 public:
40 using value_type = typename std::atomic_ref<T>::value_type;
41
47 explicit atomic_ref_condition(T& value);
48
55
58
61
64
70
87 void wait(value_type old,
88 std::memory_order order = std::memory_order_seq_cst);
89
92 void notify_one();
93
95 void notify_all();
96
100 std::atomic_ref<T>& atomic() noexcept;
101
105 const std::atomic_ref<T>& atomic() const noexcept;
106
110 std::atomic_ref<T>& operator*() noexcept;
111
115 const std::atomic_ref<T>& operator*() const noexcept;
116
120 std::atomic_ref<T>* operator->() noexcept;
121
125 const std::atomic_ref<T>* operator->() const noexcept;
126
127 private:
128 std::atomic_ref<T> m_value;
129 detail::wait_queue m_waiters;
130};
131
132template <typename T>
133atomic_ref_condition<T>::atomic_ref_condition(T& value) : m_value(value) {}
134
135template <typename T>
137 : m_value(other.m_value) {}
138
139template <typename T>
141 assert(m_waiters.empty()); // LCOV_EXCL_LINE
142}
143
144template <typename T>
145void atomic_ref_condition<T>::wait(value_type old, std::memory_order order) {
146 assert(order != std::memory_order_release); // LCOV_EXCL_LINE
147 assert(order != std::memory_order_acq_rel); // LCOV_EXCL_LINE
148
149 while (true) {
150 // bitwise comparison instead of operator==
151 // TODO make this ignore padding bytes
152 const auto current = m_value.load(order);
153 if (std::memcmp(&current, &old, sizeof(value_type)) != 0) {
154 return;
155 }
156 m_waiters.wait_if([this, &old, order] {
157 const auto current = m_value.load(order);
158 return std::memcmp(&current, &old, sizeof(value_type)) == 0;
159 });
160 }
161}
162
163template <typename T>
165 m_waiters.resume_one();
166}
167
168template <typename T>
170 m_waiters.resume_all();
171}
172
173template <typename T>
174std::atomic_ref<T>& atomic_ref_condition<T>::atomic() noexcept {
175 return m_value;
176}
177
178template <typename T>
179const std::atomic_ref<T>& atomic_ref_condition<T>::atomic() const noexcept {
180 return m_value;
181}
182
183template <typename T>
184std::atomic_ref<T>& atomic_ref_condition<T>::operator*() noexcept {
185 return m_value;
186}
187
188template <typename T>
189const std::atomic_ref<T>& atomic_ref_condition<T>::operator*() const noexcept {
190 return m_value;
191}
192
193template <typename T>
194std::atomic_ref<T>* atomic_ref_condition<T>::operator->() noexcept {
195 return &m_value;
196}
197
198template <typename T>
199const std::atomic_ref<T>* atomic_ref_condition<T>::operator->() const noexcept {
200 return &m_value;
201}
202
203} // namespace coopsync_tbb
204#else
205#error \
206 "atomic_ref_condition is not supported on this platform. Please check the COOPSYNC_TBB_HAS_ATOMIC_REF_CONDITION macro."
207#endif
std::atomic_ref< T > & operator*() noexcept
Access to the internal atomic_ref value.
Definition atomic_ref_condition.hpp:184
std::atomic_ref< T > * operator->() noexcept
Access to the internal atomic_ref value.
Definition atomic_ref_condition.hpp:194
void notify_all()
Resumes all tasks suspended waiting on this atomic_ref_condition.
Definition atomic_ref_condition.hpp:169
typename std::atomic_ref< T >::value_type value_type
The value type of the atomic_ref_condition, same as the value type of the internal atomic_ref.
Definition atomic_ref_condition.hpp:40
void wait(value_type old, std::memory_order order=std::memory_order_seq_cst)
Suspends the calling task until the internal atomic_ref value is not equal to specified value....
Definition atomic_ref_condition.hpp:145
~atomic_ref_condition()
Destroys the atomic_ref_condition.
Definition atomic_ref_condition.hpp:140
atomic_ref_condition & operator=(atomic_ref_condition &&)=delete
The atomic_ref_condition is not move-assignable.
atomic_ref_condition(atomic_ref_condition &&)=delete
The atomic_ref_condition is not move-constructible.
atomic_ref_condition(T &value)
Constructs a new atomic_ref_condition that refers to the given value.
Definition atomic_ref_condition.hpp:133
std::atomic_ref< T > & atomic() noexcept
Access to the internal atomic_ref value.
Definition atomic_ref_condition.hpp:174
atomic_ref_condition & operator=(const atomic_ref_condition &)=delete
The atomic_ref_condition is not copy-assignable.
void notify_one()
Resumes one task suspended waiting on this atomic_ref_condition, if there is any.
Definition atomic_ref_condition.hpp:164
Feature test macros for the CoopSync_TBB public API.
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11