12 #ifndef ROC_CORE_SEQLOCK_H_
13 #define ROC_CORE_SEQLOCK_H_
15 #include "roc_core/atomic_ops.h"
30 return (ver & 1) != 0;
54 return load_version_();
66 return try_store_(value, ver);
72 return try_store_(value, ver);
84 exclusive_store_(value, ver);
90 exclusive_store_(value, ver);
100 return try_load_repeat_(value, ver);
106 return try_load_repeat_(value, ver);
115 wait_load_(value, ver);
122 wait_load_(value, ver);
135 volatile_copy_(val_, value);
153 volatile_copy_(val_, value);
163 while (!try_load_(value, ver)) {
172 if (try_load_(value, ver)) {
175 if (try_load_(value, ver)) {
178 if (try_load_(value, ver)) {
188 volatile_copy_(value, val_);
192 return ((ver0 & 1) == 0 && ver0 == ver);
199 static void volatile_copy_(
volatile T& dst,
const volatile T& src) {
200 volatile char* dst_ptr =
reinterpret_cast<volatile char*
>(&dst);
201 const volatile char* src_ptr =
reinterpret_cast<const volatile char*
>(&src);
203 for (
size_t n = 0; n <
sizeof(T); n++) {
204 dst_ptr[n] = src_ptr[n];
static void store_relaxed(T1 &var, T2 val)
Atomic store (no barrier).
static void fence_seq_cst()
Full memory barrier.
static void fence_acquire()
Acquire memory barrier.
static T load_relaxed(const T &var)
Atomic load (no barrier).
static void fence_release()
Release memory barrier.
static bool compare_exchange_relaxed(T1 &var, T1 &exp, T2 des)
Atomic compare-and-swap (no barrier).
static T load_seq_cst(const T &var)
Atomic load (full barrier).
Base class for non-copyable objects.
bool try_store(const T &value)
Store value. Can be called concurrently, but only one concurrent call will succeed....
void exclusive_store(const T &value)
Store value. Can NOT be called concurrently, assumes that writes are serialized. Is both lock-free an...
bool try_load_ver(T &value, seqlock_version_t &ver) const
Try to load value and version. Like try_load(), but also returns version.
seqlock_version_t version() const
Load value version. Wait-free.
bool try_store_ver(const T &value, seqlock_version_t &ver)
Store value. Like try_store(), but also returns updated version.
void exclusive_store_ver(const T &value, seqlock_version_t &ver)
Store value. Like exclusive_store(), but also returns updated version.
Seqlock(T value)
Initialize with given value.
void wait_load_ver(T &value, seqlock_version_t &ver) const
Load value and version. Like wait_load(), but also returns version.
T wait_load() const
Load value. May spin until concurrent store completes. Is NOT lock-free (or wait-free).
bool try_load(T &value) const
Try to load value. Returns true if the value was loaded. May return false if concurrent store is curr...
CPU-specific instructions.
bool seqlock_version_is_dirty(seqlock_version_t ver)
Check if given seqlock version corresponds to dirty value.
void cpu_relax()
CPU pause instruction.
uint32_t seqlock_version_t
Type for holding seqlock value version. Version is changed each value update. May wrap.
Commonly used types and functions.