zpp
Zephyr C++20 Framework
sys_mutex.hpp
Go to the documentation of this file.
1 
7 #ifndef ZPP_INCLUDE_ZPP_SYS_MUTEX_HPP
8 #define ZPP_INCLUDE_ZPP_SYS_MUTEX_HPP
9 
10 #ifdef CONFIG_USERSPACE
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 
15 #include <chrono>
16 
17 namespace zpp {
18 
22 template<typename T_Mutex>
24 {
25 public:
26  using native_type = struct sys_mutex;
29 protected:
33  constexpr sys_mutex_base() noexcept = default;
34 
35 public:
41  [[nodiscard]] bool lock() noexcept
42  {
43  if (sys_mutex_lock(native_handle(), K_FOREVER) == 0) {
44  return true;
45  } else {
46  return false;
47  }
48  }
49 
55  [[nodiscard]] bool try_lock() noexcept
56  {
57  if (sys_mutex_lock(native_handle(), K_NO_WAIT) == 0) {
58  return true;
59  } else {
60  return false;
61  }
62  }
63 
71  template<class T_Rep, class T_Period>
72  [[nodiscard]] bool
73  try_lock_for(const std::chrono::duration<T_Rep, T_Period>& timeout) noexcept
74  {
75  using namespace std::chrono;
76 
77  if (sys_mutex_lock(native_handle(), to_timeout(timeout)) == 0)
78  {
79  return true;
80  } else {
81  return false;
82  }
83  }
84 
88  [[nodiscard]] bool unlock() noexcept
89  {
90  if (sys_mutex_unlock(native_handle()) == 0) {
91  return true;
92  } else {
93  return false;
94  }
95  }
96 
102  auto native_handle() noexcept -> native_pointer
103  {
104  return static_cast<T_Mutex*>(this)->native_handle();
105  }
106 
112  auto native_handle() const noexcept -> native_const_pointer
113  {
114  return static_cast<const T_Mutex*>(this)->native_handle();
115  }
116 public:
117  sys_mutex_base(const sys_mutex_base&) = delete;
121 };
122 
126 class sys_mutex : public sys_mutex_base<sys_mutex> {
127 public:
131  sys_mutex() noexcept
132  {
133  sys_mutex_init(&m_mutex);
134  }
135 
141  constexpr auto native_handle() noexcept -> native_pointer
142  {
143  return &m_mutex;
144  }
145 
151  constexpr auto native_handle() const noexcept -> native_const_pointer
152  {
153  return &m_mutex;
154  }
155 private:
156  native_type m_mutex;
157 public:
158  sys_mutex(const sys_mutex&) = delete;
159  sys_mutex(sys_mutex&&) = delete;
160  sys_mutex& operator=(const sys_mutex&) = delete;
162 };
163 
167 class sys_mutex_ref : public sys_mutex_base<sys_mutex_ref> {
168 public:
175  explicit constexpr sys_mutex_ref(native_pointer m) noexcept
176  : m_mutex_ptr(m)
177  {
178  __ASSERT_NO_MSG(m_mutex_ptr != nullptr);
179  }
180 
187  template<class T_Mutex>
188  explicit constexpr sys_mutex_ref(T_Mutex& m) noexcept
189  : m_mutex_ptr(m.native_handle())
190  {
191  __ASSERT_NO_MSG(m_mutex_ptr != nullptr);
192  }
193 
200  constexpr sys_mutex_ref& operator=(native_pointer m) noexcept
201  {
202  m_mutex_ptr = m;
203  __ASSERT_NO_MSG(m_mutex_ptr != nullptr);
204  return *this;
205  }
206 
213  template<class T_Mutex>
214  constexpr sys_mutex_ref& operator=(T_Mutex& m) noexcept
215  {
216  m_mutex_ptr = m.native_handle();
217  __ASSERT_NO_MSG(m_mutex_ptr != nullptr);
218  return *this;
219  }
220 
226  constexpr auto native_handle() noexcept -> native_pointer
227  {
228  return m_mutex_ptr;
229  }
230 
236  constexpr auto native_handle() const noexcept -> native_const_pointer
237  {
238  return m_mutex_ptr;
239  }
240 private:
241  native_pointer m_mutex_ptr{ nullptr };
242 public:
243  sys_mutex_ref() = delete;
244 };
245 
246 } // namespace zpp
247 
248 #endif // CONFIG_USERSPACE
249 
250 #endif // ZPP_INCLUDE_ZPP_SYS_MUTEX_HPP
A userspace mutex class.
Definition: sys_mutex.hpp:24
bool lock() noexcept
Lock the mutex. Wait for ever until it is locked.
Definition: sys_mutex.hpp:41
sys_mutex_base(const sys_mutex_base &)=delete
native_type * native_pointer
Definition: sys_mutex.hpp:27
sys_mutex_base & operator=(sys_mutex_base &&)=delete
auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:112
bool unlock() noexcept
Unlock the mutex.
Definition: sys_mutex.hpp:88
native_type const * native_cont_pointer
Definition: sys_mutex.hpp:28
struct sys_mutex native_type
Definition: sys_mutex.hpp:26
auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:102
bool try_lock() noexcept
Try locking the mutex without waiting.
Definition: sys_mutex.hpp:55
constexpr sys_mutex_base() noexcept=default
Protected default contructor so only derived objects can be created.
bool try_lock_for(const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
Try locking the mutex with a timeout.
Definition: sys_mutex.hpp:73
sys_mutex_base & operator=(const sys_mutex_base &)=delete
sys_mutex_base(sys_mutex_base &&)=delete
A recursive mutex class borrowing the native mutex.
Definition: sys_mutex.hpp:167
constexpr sys_mutex_ref(T_Mutex &m) noexcept
Construct a mutex using a native sys_mutex*.
Definition: sys_mutex.hpp:188
sys_mutex_ref()=delete
constexpr sys_mutex_ref & operator=(T_Mutex &m) noexcept
Construct a mutex using a native sys_mutex*.
Definition: sys_mutex.hpp:214
constexpr sys_mutex_ref(native_pointer m) noexcept
Construct a mutex using a native sys_mutex*.
Definition: sys_mutex.hpp:175
constexpr auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:236
constexpr auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:226
constexpr sys_mutex_ref & operator=(native_pointer m) noexcept
Construct a mutex using a native sys_mutex*.
Definition: sys_mutex.hpp:200
A recursive mutex class.
Definition: sys_mutex.hpp:126
constexpr auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:151
sys_mutex & operator=(const sys_mutex &)=delete
sys_mutex(sys_mutex &&)=delete
sys_mutex(const sys_mutex &)=delete
sys_mutex() noexcept
Default constructor.
Definition: sys_mutex.hpp:131
sys_mutex & operator=(sys_mutex &&)=delete
constexpr auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:141
constexpr k_timeout_t to_timeout(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
Definition: clock.hpp:88