CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
atomic_flag.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) || (defined(COOPSYNC_TBB_HAS_ATOMIC_FLAG) && \
10 COOPSYNC_TBB_HAS_ATOMIC_FLAG == 1)
11
12#include <atomic>
13
14#include "coopsync_tbb/detail/macros.hpp"
15#include "coopsync_tbb/detail/wait_queue.hpp"
16
17namespace coopsync_tbb {
18
26class COOPSYNC_TBB_EXPORT atomic_flag {
27
28 public:
30 atomic_flag() = default;
31
33 atomic_flag(const atomic_flag&) = delete;
34
37
40
43
49
54 void clear(std::memory_order order = std::memory_order_seq_cst) noexcept;
55
59 std::memory_order order = std::memory_order_seq_cst) noexcept;
60
64 bool test(
65 std::memory_order order = std::memory_order_seq_cst) const noexcept;
66
79 void wait(bool old, std::memory_order order = std::memory_order_seq_cst);
80
83 void notify_one();
84
86 void notify_all();
87
91 std::atomic_flag& atomic() noexcept;
92
96 const std::atomic_flag& atomic() const noexcept;
97
101 std::atomic_flag& operator*() noexcept;
102
106 const std::atomic_flag& operator*() const noexcept;
107
111 std::atomic_flag* operator->() noexcept;
112
116 const std::atomic_flag* operator->() const noexcept;
117
118 private:
119 std::atomic_flag m_value{};
120 detail::wait_queue m_waiters;
121};
122
126COOPSYNC_TBB_EXPORT void atomic_flag_clear(atomic_flag* object) noexcept;
127
134COOPSYNC_TBB_EXPORT void atomic_flag_clear_explicit(
135 atomic_flag* object, std::memory_order order) noexcept;
136
140COOPSYNC_TBB_EXPORT bool atomic_flag_test_and_set(atomic_flag* object) noexcept;
141
146COOPSYNC_TBB_EXPORT bool atomic_flag_test_and_set_explicit(
147 atomic_flag* object, std::memory_order order) noexcept;
148
152COOPSYNC_TBB_EXPORT bool atomic_flag_test(atomic_flag* object) noexcept;
153
159COOPSYNC_TBB_EXPORT bool atomic_flag_test_explicit(
160 atomic_flag* object, std::memory_order order) noexcept;
161
172COOPSYNC_TBB_EXPORT void atomic_flag_wait(atomic_flag* object, bool old);
173
186COOPSYNC_TBB_EXPORT void atomic_flag_wait_explicit(atomic_flag* object,
187 bool old,
188 std::memory_order order);
189
194COOPSYNC_TBB_EXPORT void atomic_flag_notify_one(atomic_flag* object);
195
199COOPSYNC_TBB_EXPORT void atomic_flag_notify_all(atomic_flag* object);
200
201} // namespace coopsync_tbb
202
203#else
204#error "Atomic flag condition variable is not supported on this platform."
205#endif
Wrapper around std::atomic_flag that provides atomic waiting which suspends the calling task instead ...
Definition atomic_flag.hpp:26
void notify_one()
Resumes one task suspended waiting on this atomic_flag, if there is any.
void wait(bool old, std::memory_order order=std::memory_order_seq_cst)
Suspends the calling task until the state is not equal to specified value. If the state is not equal ...
void notify_all()
Resumes all tasks suspended waiting on this atomic_flag.
atomic_flag()=default
Constructs a new atomic_flag. Initializes to clear state.
atomic_flag(atomic_flag &&)=delete
The atomic_flag is not move-constructible.
void clear(std::memory_order order=std::memory_order_seq_cst) noexcept
Atomically sets the state to clear.
bool test(std::memory_order order=std::memory_order_seq_cst) const noexcept
Atomically reads the current state and returns it.
atomic_flag(const atomic_flag &)=delete
The atomic_flag is not copy-constructible.
bool test_and_set(std::memory_order order=std::memory_order_seq_cst) noexcept
Atomically sets the state to set and returns the previous state.
~atomic_flag()
Destroys the atomic_flag.
atomic_flag & operator=(atomic_flag &&)=delete
The atomic_flag is not move-assignable.
std::atomic_flag & atomic() noexcept
Access to the internal atomic value.
atomic_flag & operator=(const atomic_flag &)=delete
The atomic_flag is not copy-assignable.
Feature test macros for the CoopSync_TBB public API.
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11
COOPSYNC_TBB_EXPORT void atomic_flag_notify_one(atomic_flag *object)
Resumes one task suspended waiting on the atomic_flag, if there is any. Same as object....
COOPSYNC_TBB_EXPORT void atomic_flag_notify_all(atomic_flag *object)
Resumes all tasks suspended waiting on the atomic_flag. Same as object.notify_all().
COOPSYNC_TBB_EXPORT void atomic_flag_clear_explicit(atomic_flag *object, std::memory_order order) noexcept
Atomically sets the state to clear. Same as object->clear(order).
COOPSYNC_TBB_EXPORT void atomic_flag_clear(atomic_flag *object) noexcept
Atomically sets the state to clear. Same as object->clear().
COOPSYNC_TBB_EXPORT void atomic_flag_wait_explicit(atomic_flag *object, bool old, std::memory_order order)
Suspends the calling task until the state is not equal to specified value. If the internal atomic the...
COOPSYNC_TBB_EXPORT bool atomic_flag_test_and_set(atomic_flag *object) noexcept
Atomically sets the state to set and returns the previous state. Same as object->test_and_set().
COOPSYNC_TBB_EXPORT bool atomic_flag_test_and_set_explicit(atomic_flag *object, std::memory_order order) noexcept
Atomically sets the state to set and returns the previous state. Same as object->test_and_set(order).
COOPSYNC_TBB_EXPORT void atomic_flag_wait(atomic_flag *object, bool old)
Suspends the calling task until the state is not equal to specified value. If the internal atomic the...
COOPSYNC_TBB_EXPORT bool atomic_flag_test(atomic_flag *object) noexcept
Atomically reads the current state and returns it. Same as object->test().
COOPSYNC_TBB_EXPORT bool atomic_flag_test_explicit(atomic_flag *object, std::memory_order order) noexcept
Atomically reads the current state and returns it. Same as object->test(order).