CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
coopsync_tbb::atomic_flag Class Reference

Wrapper around std::atomic_flag that provides atomic waiting which suspends the calling task instead of blocking it, as well as exposing atomic test, set and clear operations. More...

#include <coopsync_tbb/atomic_flag.hpp>

Public Member Functions

 atomic_flag ()=default
 Constructs a new atomic_flag. Initializes to clear state.
 atomic_flag (const atomic_flag &)=delete
 The atomic_flag is not copy-constructible.
atomic_flag & operator= (const atomic_flag &)=delete
 The atomic_flag is not copy-assignable.
 atomic_flag (atomic_flag &&)=delete
 The atomic_flag is not move-constructible.
atomic_flag & operator= (atomic_flag &&)=delete
 The atomic_flag is not move-assignable.
 ~atomic_flag ()
 Destroys the atomic_flag.
void clear (std::memory_order order=std::memory_order_seq_cst) noexcept
 Atomically sets the state to clear.
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.
bool test (std::memory_order order=std::memory_order_seq_cst) const noexcept
 Atomically reads the current state and returns it.
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 to old, the function returns immediately. Otherwise, the task is suspended. The task will be resumed once notify_one or notify_all is called and the state is not equal to old.
void notify_one ()
 Resumes one task suspended waiting on this atomic_flag, if there is any.
void notify_all ()
 Resumes all tasks suspended waiting on this atomic_flag.
std::atomic_flag & atomic () noexcept
 Access to the internal atomic value.
const std::atomic_flag & atomic () const noexcept
 Access to the internal atomic value.
std::atomic_flag & operator* () noexcept
 Access to the internal atomic value.
const std::atomic_flag & operator* () const noexcept
 Access to the internal atomic value.
std::atomic_flag * operator-> () noexcept
 Access to the internal atomic value.
const std::atomic_flag * operator-> () const noexcept
 Access to the internal atomic value.

Detailed Description

Wrapper around std::atomic_flag that provides atomic waiting which suspends the calling task instead of blocking it, as well as exposing atomic test, set and clear operations.

Available only if the standard library supports std::atomic_flag::test and std::atomic_flag::clear with memory order parameters (C++20). Compliance can be checked with COOPSYNC_TBB_HAS_ATOMIC_FLAG.

Constructor & Destructor Documentation

◆ ~atomic_flag()

coopsync_tbb::atomic_flag::~atomic_flag ( )

Destroys the atomic_flag.

Note
The destructor must not be called while there are still tasks waiting on the atomic_flag. The destructor does not notify or resume any waiting tasks.

Member Function Documentation

◆ atomic() [1/2]

const std::atomic_flag & coopsync_tbb::atomic_flag::atomic ( ) const
noexcept

Access to the internal atomic value.

Returns
Const reference to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ atomic() [2/2]

std::atomic_flag & coopsync_tbb::atomic_flag::atomic ( )
noexcept

Access to the internal atomic value.

Returns
Reference to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ clear()

void coopsync_tbb::atomic_flag::clear ( std::memory_order order = std::memory_order_seq_cst)
noexcept

Atomically sets the state to clear.

Parameters
orderThe memory order to use for clearing the flag. Must not be std::memory_order_consume, std::memory_order_acquire or std::memory_order_acq_rel.

◆ operator*() [1/2]

const std::atomic_flag & coopsync_tbb::atomic_flag::operator* ( ) const
noexcept

Access to the internal atomic value.

Returns
Const reference to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ operator*() [2/2]

std::atomic_flag & coopsync_tbb::atomic_flag::operator* ( )
noexcept

Access to the internal atomic value.

Returns
Reference to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ operator->() [1/2]

const std::atomic_flag * coopsync_tbb::atomic_flag::operator-> ( ) const
noexcept

Access to the internal atomic value.

Returns
Const pointer to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ operator->() [2/2]

std::atomic_flag * coopsync_tbb::atomic_flag::operator-> ( )
noexcept

Access to the internal atomic value.

Returns
Pointer to the internal atomic value. Remains valid for the lifetime of the atomic_flag.

◆ test()

bool coopsync_tbb::atomic_flag::test ( std::memory_order order = std::memory_order_seq_cst) const
noexcept

Atomically reads the current state and returns it.

Parameters
orderThe memory order to use for reading the flag. Must not be std::memory_order_release, std::memory_order_acq_rel.

◆ test_and_set()

bool coopsync_tbb::atomic_flag::test_and_set ( std::memory_order order = std::memory_order_seq_cst)
noexcept

Atomically sets the state to set and returns the previous state.

Parameters
orderThe memory order to use for setting the flag.

◆ wait()

void coopsync_tbb::atomic_flag::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 to old, the function returns immediately. Otherwise, the task is suspended. The task will be resumed once notify_one or notify_all is called and the state is not equal to old.

Parameters
oldThe value to compare the internal atomic value to. The task will be suspended while the internal atomic value is equal to old.
orderThe memory order to use for loading the internal atomic value. Must not be std::memory_order_release or std::memory_order_acq_rel.
Note
Due to ABA problem, transitions from old to a different value and back to old may be missed.

The documentation for this class was generated from the following file: