zpp
Zephyr C++20 Framework
atomic_var.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #ifndef ZPP_INCLUDE_ZPP_ATOMIC_VAR_HPP
8 #define ZPP_INCLUDE_ZPP_ATOMIC_VAR_HPP
9 
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/sys/__assert.h>
12 
13 #include <cstddef>
14 
15 namespace zpp {
16 
20 class atomic_var {
21 public:
25  using value_type = atomic_val_t;
26 public:
30  constexpr atomic_var() noexcept = default;
31 
37  explicit atomic_var(value_type v) noexcept
38  {
39  store(v);
40  }
41 
47  atomic_var(const atomic_var& rhs) noexcept
48  {
49  store(rhs.load());
50  }
51 
59  atomic_var& operator=(const atomic_var& rhs) noexcept
60  {
61  store(rhs.load());
62  return *this;
63  }
64 
70  constexpr size_t bit_count() const noexcept
71  {
72  return sizeof(value_type) * 8;
73  }
74 
88  [[nodiscard]] bool cas(value_type old_val, value_type new_val) noexcept
89  {
90  return atomic_cas(&m_var, old_val, new_val);
91  }
92 
104  {
105  return atomic_add(&m_var, val);
106  }
107 
119  {
120  return atomic_sub(&m_var, val);
121  }
122 
134  {
135  return atomic_or(&m_var, val);
136  }
137 
149  {
150  return atomic_xor(&m_var, val);
151  }
152 
164  {
165  return atomic_and(&m_var, val);
166  }
167 
179  {
180  return atomic_nand(&m_var, val);
181  }
182 
192  {
193  return atomic_inc(&m_var);
194  }
195 
205  {
206  return atomic_dec(&m_var);
207  }
208 
215  [[nodiscard]] value_type load() const noexcept
216  {
217  return atomic_get(&m_var);
218  }
219 
228  {
229  return atomic_set(&m_var, val);
230  }
231 
237  value_type clear() noexcept
238  {
239  return atomic_clear(&m_var);
240  }
241 
249  [[nodiscard]] bool load(size_t bit) const noexcept
250  {
251  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
252  return atomic_test_bit(&m_var, bit);
253  }
254 
261  void store(size_t bit, bool val) noexcept
262  {
263  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
264  atomic_set_bit_to(&m_var, bit, val);
265  }
266 
272  void set(size_t bit) noexcept
273  {
274  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
275  atomic_set_bit(&m_var, bit);
276  }
277 
283  void clear(size_t bit) noexcept
284  {
285  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
286  atomic_clear_bit(&m_var, bit);
287  }
288 
296  [[nodiscard]] bool fetch_and_clear(size_t bit) noexcept
297  {
298  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
299  return atomic_test_and_clear_bit(&m_var, bit);
300  }
301 
309  [[nodiscard]] bool fetch_and_set(size_t bit) noexcept
310  {
311  __ASSERT_NO_MSG(bit < (sizeof(value_type) * 8));
312  return atomic_test_and_set_bit(&m_var, bit);
313  }
314 
321  operator value_type () const noexcept
322  {
323  return load();
324  }
325 
334  {
335  return store(val);
336  }
337 
346  {
347  return fetch_add(1) + 1;
348  }
349 
357  value_type operator++(int) noexcept
358  {
359  return fetch_add(1);
360  }
361 
370  {
371  return fetch_sub(1) - 1;
372  }
373 
381  value_type operator--(int) noexcept
382  {
383  return fetch_sub(1);
384  }
385 
396  {
397  return fetch_add(val) + val;
398  }
399 
410  {
411  return fetch_sub(val) - val;
412  }
413 
424  {
425  return fetch_and(val) & val;
426  }
427 
438  {
439  return fetch_or(val) | val;
440  }
441 
452  {
453  return fetch_xor(val) ^ val;
454  }
455 private:
456  atomic_t m_var{};
457 };
458 
459 } // namespace zpp
460 
461 #endif // ZPP_INCLUDE_ZPP_ATOMIC_VAR_HPP
class wrapping an atomic_var_t
Definition: atomic_var.hpp:20
value_type operator&=(value_type val) noexcept
Perform atomic bitwise AND.
Definition: atomic_var.hpp:423
void clear(size_t bit) noexcept
atomically set a bit to false/0
Definition: atomic_var.hpp:283
void set(size_t bit) noexcept
atomically set a bit to true/1
Definition: atomic_var.hpp:272
value_type fetch_and(value_type val) noexcept
Atomic bitwise AND.
Definition: atomic_var.hpp:163
void store(size_t bit, bool val) noexcept
atomically set a bit a value
Definition: atomic_var.hpp:261
value_type store(value_type val) noexcept
Atomically replace current value of the atomic variable.
Definition: atomic_var.hpp:227
value_type fetch_inc() noexcept
Atomic increment.
Definition: atomic_var.hpp:191
value_type operator++(int) noexcept
Perform atomic post-increment.
Definition: atomic_var.hpp:357
value_type fetch_xor(value_type val) noexcept
Atomic bitwise XOR.
Definition: atomic_var.hpp:148
value_type operator--() noexcept
Perform atomic pre-decrement.
Definition: atomic_var.hpp:369
bool fetch_and_clear(size_t bit) noexcept
atomically clear a bit while returning the previous value.
Definition: atomic_var.hpp:296
value_type fetch_dec() noexcept
Atomic decrement.
Definition: atomic_var.hpp:204
value_type fetch_or(value_type val) noexcept
Atomic bitwise OR.
Definition: atomic_var.hpp:133
atomic_val_t value_type
the type used to store the value
Definition: atomic_var.hpp:25
value_type operator-=(value_type val) noexcept
Perform atomic substraction.
Definition: atomic_var.hpp:409
bool load(size_t bit) const noexcept
atomically get a bit from the bitset
Definition: atomic_var.hpp:249
atomic_var(const atomic_var &rhs) noexcept
copy constructor
Definition: atomic_var.hpp:47
value_type fetch_nand(value_type val) noexcept
Atomic bitwise NAND.
Definition: atomic_var.hpp:178
bool cas(value_type old_val, value_type new_val) noexcept
Atomic compare-and-set.
Definition: atomic_var.hpp:88
value_type fetch_add(value_type val) noexcept
Atomic addition.
Definition: atomic_var.hpp:103
constexpr size_t bit_count() const noexcept
the size in bits of value_type
Definition: atomic_var.hpp:70
constexpr atomic_var() noexcept=default
default constructor that sets the value to 0
value_type operator++() noexcept
Perform atomic pre-increment.
Definition: atomic_var.hpp:345
value_type operator|=(value_type val) noexcept
Perform atomic bitwise OR.
Definition: atomic_var.hpp:437
value_type fetch_sub(value_type val) noexcept
Atomic substraction.
Definition: atomic_var.hpp:118
value_type operator--(int) noexcept
Perform atomic post-decrement.
Definition: atomic_var.hpp:381
value_type operator=(value_type val) noexcept
Atomically replace current value of the atomic variable.
Definition: atomic_var.hpp:333
value_type operator+=(value_type val) noexcept
Perform atomic addition.
Definition: atomic_var.hpp:395
value_type clear() noexcept
Atomically clear the atomic variable.
Definition: atomic_var.hpp:237
value_type load() const noexcept
Atomically loads and returns the current value of the atomic variable.
Definition: atomic_var.hpp:215
bool fetch_and_set(size_t bit) noexcept
atomically set a bit while returning the previous value.
Definition: atomic_var.hpp:309
atomic_var & operator=(const atomic_var &rhs) noexcept
copy operator
Definition: atomic_var.hpp:59
value_type operator^=(value_type val) noexcept
Perform atomic bitwise XOR.
Definition: atomic_var.hpp:451