zpp
Zephyr C++20 Framework
zpp::atomic_var Class Reference

class wrapping an atomic_var_t More...

#include <atomic_var.hpp>

Public Types

using value_type = atomic_val_t
 the type used to store the value More...
 

Public Member Functions

constexpr atomic_var () noexcept=default
 default constructor that sets the value to 0 More...
 
 atomic_var (value_type v) noexcept
 constructor that sets the value to v More...
 
 atomic_var (const atomic_var &rhs) noexcept
 copy constructor More...
 
atomic_varoperator= (const atomic_var &rhs) noexcept
 copy operator More...
 
constexpr size_t bit_count () const noexcept
 the size in bits of value_type More...
 
bool cas (value_type old_val, value_type new_val) noexcept
 Atomic compare-and-set. More...
 
value_type fetch_add (value_type val) noexcept
 Atomic addition. More...
 
value_type fetch_sub (value_type val) noexcept
 Atomic substraction. More...
 
value_type fetch_or (value_type val) noexcept
 Atomic bitwise OR. More...
 
value_type fetch_xor (value_type val) noexcept
 Atomic bitwise XOR. More...
 
value_type fetch_and (value_type val) noexcept
 Atomic bitwise AND. More...
 
value_type fetch_nand (value_type val) noexcept
 Atomic bitwise NAND. More...
 
value_type fetch_inc () noexcept
 Atomic increment. More...
 
value_type fetch_dec () noexcept
 Atomic decrement. More...
 
value_type load () const noexcept
 Atomically loads and returns the current value of the atomic variable. More...
 
value_type store (value_type val) noexcept
 Atomically replace current value of the atomic variable. More...
 
value_type clear () noexcept
 Atomically clear the atomic variable. More...
 
bool load (size_t bit) const noexcept
 atomically get a bit from the bitset More...
 
void store (size_t bit, bool val) noexcept
 atomically set a bit a value More...
 
void set (size_t bit) noexcept
 atomically set a bit to true/1 More...
 
void clear (size_t bit) noexcept
 atomically set a bit to false/0 More...
 
bool fetch_and_clear (size_t bit) noexcept
 atomically clear a bit while returning the previous value. More...
 
bool fetch_and_set (size_t bit) noexcept
 atomically set a bit while returning the previous value. More...
 
 operator value_type () const noexcept
 Atomically loads and returns the current value of the atomic variable. More...
 
value_type operator= (value_type val) noexcept
 Atomically replace current value of the atomic variable. More...
 
value_type operator++ () noexcept
 Perform atomic pre-increment. More...
 
value_type operator++ (int) noexcept
 Perform atomic post-increment. More...
 
value_type operator-- () noexcept
 Perform atomic pre-decrement. More...
 
value_type operator-- (int) noexcept
 Perform atomic post-decrement. More...
 
value_type operator+= (value_type val) noexcept
 Perform atomic addition. More...
 
value_type operator-= (value_type val) noexcept
 Perform atomic substraction. More...
 
value_type operator&= (value_type val) noexcept
 Perform atomic bitwise AND. More...
 
value_type operator|= (value_type val) noexcept
 Perform atomic bitwise OR. More...
 
value_type operator^= (value_type val) noexcept
 Perform atomic bitwise XOR. More...
 

Detailed Description

class wrapping an atomic_var_t

Definition at line 20 of file atomic_var.hpp.

Member Typedef Documentation

◆ value_type

using zpp::atomic_var::value_type = atomic_val_t

the type used to store the value

Definition at line 25 of file atomic_var.hpp.

Constructor & Destructor Documentation

◆ atomic_var() [1/3]

constexpr zpp::atomic_var::atomic_var ( )
constexprdefaultnoexcept

default constructor that sets the value to 0

◆ atomic_var() [2/3]

zpp::atomic_var::atomic_var ( value_type  v)
inlineexplicitnoexcept

constructor that sets the value to v

Parameters
vthe value to initialize the atomic_var with

Definition at line 37 of file atomic_var.hpp.

38  {
39  store(v);
40  }
value_type store(value_type val) noexcept
Atomically replace current value of the atomic variable.
Definition: atomic_var.hpp:227

References store().

◆ atomic_var() [3/3]

zpp::atomic_var::atomic_var ( const atomic_var rhs)
inlinenoexcept

copy constructor

Parameters
rhsthe value to initialize the atomic_var with

Definition at line 47 of file atomic_var.hpp.

48  {
49  store(rhs.load());
50  }

References store().

Member Function Documentation

◆ bit_count()

constexpr size_t zpp::atomic_var::bit_count ( ) const
inlineconstexprnoexcept

the size in bits of value_type

Returns
the size of value_type in bits

Definition at line 70 of file atomic_var.hpp.

71  {
72  return sizeof(value_type) * 8;
73  }
atomic_val_t value_type
the type used to store the value
Definition: atomic_var.hpp:25

◆ cas()

bool zpp::atomic_var::cas ( value_type  old_val,
value_type  new_val 
)
inlinenoexcept

Atomic compare-and-set.

This routine performs an atomic compare-and-set. If the current value equals old_val, the value is set to new_val. If the current value does not equal old_val, the value is unchanged.

Parameters
old_valOriginal value to compare against.
new_valNew value to store.
Returns
true if new_val is written, false otherwise.

Definition at line 88 of file atomic_var.hpp.

89  {
90  return atomic_cas(&m_var, old_val, new_val);
91  }

◆ clear() [1/2]

value_type zpp::atomic_var::clear ( )
inlinenoexcept

Atomically clear the atomic variable.

Returns
The value immediately preceding the effect or this function.

Definition at line 237 of file atomic_var.hpp.

238  {
239  return atomic_clear(&m_var);
240  }

◆ clear() [2/2]

void zpp::atomic_var::clear ( size_t  bit)
inlinenoexcept

atomically set a bit to false/0

Parameters
bitthe index of the bit to set

Definition at line 283 of file atomic_var.hpp.

284  {
285  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
286  atomic_clear_bit(&m_var, bit);
287  }

◆ fetch_add()

value_type zpp::atomic_var::fetch_add ( value_type  val)
inlinenoexcept

Atomic addition.

Atomically replace the current value with the result of the aritmetic addition of the value and val.

Parameters
valThe other argument of the arithmetic addition.
Returns
The value immediately preceding the effect or this function.

Definition at line 103 of file atomic_var.hpp.

104  {
105  return atomic_add(&m_var, val);
106  }

Referenced by operator++(), and operator+=().

◆ fetch_and()

value_type zpp::atomic_var::fetch_and ( value_type  val)
inlinenoexcept

Atomic bitwise AND.

Atomically replace the current value with the result of the bitwise AND of the value and val.

Parameters
valThe other argument of the bitwise AND.
Returns
The value immediately preceding the effect or this function.

Definition at line 163 of file atomic_var.hpp.

164  {
165  return atomic_and(&m_var, val);
166  }

Referenced by operator&=().

◆ fetch_and_clear()

bool zpp::atomic_var::fetch_and_clear ( size_t  bit)
inlinenoexcept

atomically clear a bit while returning the previous value.

Parameters
bitthe index of the bit to set
Returns
the bit value before it was cleared

Definition at line 296 of file atomic_var.hpp.

297  {
298  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
299  return atomic_test_and_clear_bit(&m_var, bit);
300  }

◆ fetch_and_set()

bool zpp::atomic_var::fetch_and_set ( size_t  bit)
inlinenoexcept

atomically set a bit while returning the previous value.

Parameters
bitthe index of the bit to set
Returns
the bit value before it was set

Definition at line 309 of file atomic_var.hpp.

310  {
311  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
312  return atomic_test_and_set_bit(&m_var, bit);
313  }

◆ fetch_dec()

value_type zpp::atomic_var::fetch_dec ( )
inlinenoexcept

Atomic decrement.

Atomically replace the current value with the result of the aritmetic substraction of the value and 1.

Returns
The value immediately preceding the effect or this function.

Definition at line 204 of file atomic_var.hpp.

205  {
206  return atomic_dec(&m_var);
207  }

◆ fetch_inc()

value_type zpp::atomic_var::fetch_inc ( )
inlinenoexcept

Atomic increment.

Atomically replace the current value with the result of the aritmetic addition of the value and 1.

Returns
The value immediately preceding the effect or this function.

Definition at line 191 of file atomic_var.hpp.

192  {
193  return atomic_inc(&m_var);
194  }

◆ fetch_nand()

value_type zpp::atomic_var::fetch_nand ( value_type  val)
inlinenoexcept

Atomic bitwise NAND.

Atomically replace the current value with the result of the bitwise NAND of the value and val.

Parameters
valThe other argument of the bitwise NAND.
Returns
The value immediately preceding the effect or this function.

Definition at line 178 of file atomic_var.hpp.

179  {
180  return atomic_nand(&m_var, val);
181  }

◆ fetch_or()

value_type zpp::atomic_var::fetch_or ( value_type  val)
inlinenoexcept

Atomic bitwise OR.

Atomically replace the current value with the result of the bitwise OR of the value and val.

Parameters
valThe other argument of the bitwise OR.
Returns
The value immediately preceding the effect or this function.

Definition at line 133 of file atomic_var.hpp.

134  {
135  return atomic_or(&m_var, val);
136  }

Referenced by operator|=().

◆ fetch_sub()

value_type zpp::atomic_var::fetch_sub ( value_type  val)
inlinenoexcept

Atomic substraction.

Atomically replace the current value with the result of the aritmetic substraction of the value and val.

Parameters
valThe other argument of the arithmetic substraction.
Returns
The value immediately preceding the effect or this function.

Definition at line 118 of file atomic_var.hpp.

119  {
120  return atomic_sub(&m_var, val);
121  }

Referenced by operator--(), and operator-=().

◆ fetch_xor()

value_type zpp::atomic_var::fetch_xor ( value_type  val)
inlinenoexcept

Atomic bitwise XOR.

Atomically replace the current value with the result of the bitwise XOR of the value and val.

Parameters
valThe other argument of the bitwise XOR.
Returns
The value immediately preceding the effect or this function.

Definition at line 148 of file atomic_var.hpp.

149  {
150  return atomic_xor(&m_var, val);
151  }

Referenced by operator^=().

◆ load() [1/2]

value_type zpp::atomic_var::load ( ) const
inlinenoexcept

Atomically loads and returns the current value of the atomic variable.

Returns
The current value of the atomic variable.

Definition at line 215 of file atomic_var.hpp.

216  {
217  return atomic_get(&m_var);
218  }

Referenced by operator value_type().

◆ load() [2/2]

bool zpp::atomic_var::load ( size_t  bit) const
inlinenoexcept

atomically get a bit from the bitset

Parameters
bitthe index of the bit to return
Returns
the requested bit value

Definition at line 249 of file atomic_var.hpp.

250  {
251  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
252  return atomic_test_bit(&m_var, bit);
253  }

◆ operator value_type()

zpp::atomic_var::operator value_type ( ) const
inlinenoexcept

Atomically loads and returns the current value of the atomic variable.

Returns
The current value of the atomic variable.

Definition at line 321 of file atomic_var.hpp.

322  {
323  return load();
324  }
value_type load() const noexcept
Atomically loads and returns the current value of the atomic variable.
Definition: atomic_var.hpp:215

References load().

◆ operator&=()

value_type zpp::atomic_var::operator&= ( value_type  val)
inlinenoexcept

Perform atomic bitwise AND.

Perform atomic bitwise AND, equivalent to fetch_and(val) & val

Parameters
valThe argument for the arithmetic operation
Returns
The value immediately after the effect or this function.

Definition at line 423 of file atomic_var.hpp.

424  {
425  return fetch_and(val) & val;
426  }
value_type fetch_and(value_type val) noexcept
Atomic bitwise AND.
Definition: atomic_var.hpp:163

References fetch_and().

◆ operator++() [1/2]

value_type zpp::atomic_var::operator++ ( )
inlinenoexcept

Perform atomic pre-increment.

Perform atomic pre-increment, equivalent to fetch_add(1) + 1

Returns
The value immediately after the effect or this function.

Definition at line 345 of file atomic_var.hpp.

346  {
347  return fetch_add(1) + 1;
348  }
value_type fetch_add(value_type val) noexcept
Atomic addition.
Definition: atomic_var.hpp:103

References fetch_add().

◆ operator++() [2/2]

value_type zpp::atomic_var::operator++ ( int  )
inlinenoexcept

Perform atomic post-increment.

Perform atomic pre-increment, equivalent to fetch_add(1)

Returns
The value immediately before the effect or this function.

Definition at line 357 of file atomic_var.hpp.

358  {
359  return fetch_add(1);
360  }

References fetch_add().

◆ operator+=()

value_type zpp::atomic_var::operator+= ( value_type  val)
inlinenoexcept

Perform atomic addition.

Perform atomic addition, equivalent to fetch_add(val) + val

Parameters
valThe argument for the arithmetic operation
Returns
The value immediately after the effect or this function.

Definition at line 395 of file atomic_var.hpp.

396  {
397  return fetch_add(val) + val;
398  }

References fetch_add().

◆ operator--() [1/2]

value_type zpp::atomic_var::operator-- ( )
inlinenoexcept

Perform atomic pre-decrement.

Perform atomic pre-decrement, equivalent to fetch_sub(1) - 1

Returns
The value immediately after the effect or this function.

Definition at line 369 of file atomic_var.hpp.

370  {
371  return fetch_sub(1) - 1;
372  }
value_type fetch_sub(value_type val) noexcept
Atomic substraction.
Definition: atomic_var.hpp:118

References fetch_sub().

◆ operator--() [2/2]

value_type zpp::atomic_var::operator-- ( int  )
inlinenoexcept

Perform atomic post-decrement.

Perform atomic pre-decrement, equivalent to fetch_sub(1)

Returns
The value immediately before the effect or this function.

Definition at line 381 of file atomic_var.hpp.

382  {
383  return fetch_sub(1);
384  }

References fetch_sub().

◆ operator-=()

value_type zpp::atomic_var::operator-= ( value_type  val)
inlinenoexcept

Perform atomic substraction.

Perform atomic addition, equivalent to fetch_sub(val) - val

Parameters
valThe argument for the arithmetic operation
Returns
The value immediately after the effect or this function.

Definition at line 409 of file atomic_var.hpp.

410  {
411  return fetch_sub(val) - val;
412  }

References fetch_sub().

◆ operator=() [1/2]

atomic_var& zpp::atomic_var::operator= ( const atomic_var rhs)
inlinenoexcept

copy operator

Parameters
rhsthe value to initialize the atomic_var with
Returns
*this

Definition at line 59 of file atomic_var.hpp.

60  {
61  store(rhs.load());
62  return *this;
63  }

References store().

◆ operator=() [2/2]

value_type zpp::atomic_var::operator= ( value_type  val)
inlinenoexcept

Atomically replace current value of the atomic variable.

Parameters
valThe value to store in the atomic variable
Returns
The value immediately preceding the effect or this function.

Definition at line 333 of file atomic_var.hpp.

334  {
335  return store(val);
336  }

References store().

◆ operator^=()

value_type zpp::atomic_var::operator^= ( value_type  val)
inlinenoexcept

Perform atomic bitwise XOR.

Perform atomic bitwise XOR, equivalent to fetch_xor(val) ^ val

Parameters
valThe argument for the arithmetic operation
Returns
The value immediately after the effect or this function.

Definition at line 451 of file atomic_var.hpp.

452  {
453  return fetch_xor(val) ^ val;
454  }
value_type fetch_xor(value_type val) noexcept
Atomic bitwise XOR.
Definition: atomic_var.hpp:148

References fetch_xor().

◆ operator|=()

value_type zpp::atomic_var::operator|= ( value_type  val)
inlinenoexcept

Perform atomic bitwise OR.

Perform atomic bitwise OR, equivalent to fetch_or(val) | val

Parameters
valThe argument for the arithmetic operation
Returns
The value immediately after the effect or this function.

Definition at line 437 of file atomic_var.hpp.

438  {
439  return fetch_or(val) | val;
440  }
value_type fetch_or(value_type val) noexcept
Atomic bitwise OR.
Definition: atomic_var.hpp:133

References fetch_or().

◆ set()

void zpp::atomic_var::set ( size_t  bit)
inlinenoexcept

atomically set a bit to true/1

Parameters
bitthe index of the bit to set

Definition at line 272 of file atomic_var.hpp.

273  {
274  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
275  atomic_set_bit(&m_var, bit);
276  }

◆ store() [1/2]

void zpp::atomic_var::store ( size_t  bit,
bool  val 
)
inlinenoexcept

atomically set a bit a value

Parameters
bitthe index of the bit to set
valthe value the bit should be set to

Definition at line 261 of file atomic_var.hpp.

262  {
263  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
264  atomic_set_bit_to(&m_var, bit, val);
265  }

◆ store() [2/2]

value_type zpp::atomic_var::store ( value_type  val)
inlinenoexcept

Atomically replace current value of the atomic variable.

Parameters
valThe value to store in the atomic variable
Returns
The value immediately preceding the effect or this function.

Definition at line 227 of file atomic_var.hpp.

228  {
229  return atomic_set(&m_var, val);
230  }

Referenced by atomic_var(), and operator=().


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