zpp
Zephyr C++20 Framework
atomic_bitset.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_BITSET_HPP
8 #define ZPP_INCLUDE_ZPP_ATOMIC_BITSET_HPP
9 
10 #include <zephyr/sys/atomic.h>
11 #include <zephyr/sys/__assert.h>
12 
13 #include <cstddef>
14 
15 namespace zpp {
16 
22 template<size_t T_BitsetSize>
24 public:
31  constexpr atomic_bitset() noexcept = default;
32 
38  constexpr size_t bit_count() const noexcept
39  {
40  return T_BitsetSize;
41  }
42 
50  [[nodiscard]] bool load(size_t bit) const noexcept
51  {
52  __ASSERT_NO_MSG(bit < T_BitsetSize);
53  return atomic_test_bit(m_var, bit);
54  }
55 
62  void store(size_t bit, bool val) noexcept
63  {
64  __ASSERT_NO_MSG(bit < T_BitsetSize);
65  atomic_set_bit_to(m_var, bit, val);
66  }
67 
73  void set(size_t bit) noexcept
74  {
75  __ASSERT_NO_MSG(bit < T_BitsetSize);
76  atomic_set_bit(m_var, bit);
77  }
78 
84  void clear(size_t bit) noexcept
85  {
86  __ASSERT_NO_MSG(bit < T_BitsetSize);
87  atomic_clear_bit(m_var, bit);
88  }
89 
97  [[nodiscard]] bool fetch_and_clear(size_t bit) noexcept
98  {
99  __ASSERT_NO_MSG(bit < T_BitsetSize);
100  return atomic_test_and_clear_bit(m_var, bit);
101  }
102 
110  [[nodiscard]] bool fetch_and_set(size_t bit) noexcept
111  {
112  __ASSERT_NO_MSG(bit < T_BitsetSize);
113  return atomic_test_and_set_bit(m_var, bit);
114  }
115 private:
116  ATOMIC_DEFINE(m_var, T_BitsetSize) {};
117 public:
118  atomic_bitset(const atomic_bitset&) = delete;
122 };
123 
124 } // namespace zpp
125 
126 #endif // ZPP_INCLUDE_ZPP_ATOMIC_BITSET_HPP
class wrapping an atomic_var_t array
constexpr atomic_bitset() noexcept=default
default constructor
void clear(size_t bit) noexcept
atomically set a bit to false/0
atomic_bitset & operator=(const atomic_bitset &)=delete
bool load(size_t bit) const noexcept
atomically get a bit from the bitset
bool fetch_and_set(size_t bit) noexcept
atomically set a bit while returning the previous value.
bool fetch_and_clear(size_t bit) noexcept
atomically clear a bit while returning the previous value.
atomic_bitset(const atomic_bitset &)=delete
atomic_bitset(atomic_bitset &&)=delete
constexpr size_t bit_count() const noexcept
return size of the bitset
void store(size_t bit, bool val) noexcept
atomically set a bit a value
void set(size_t bit) noexcept
atomically set a bit to true/1
atomic_bitset & operator=(atomic_bitset &&)=delete