7 #ifndef ZPP_INCLUDE_ZPP_UNIQUE_LOCK_HPP
8 #define ZPP_INCLUDE_ZPP_UNIQUE_LOCK_HPP
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
23 template<
typename T_Mutex>
40 __ASSERT_NO_MSG(m_lock !=
nullptr);
41 auto res = m_lock->lock();
42 __ASSERT_NO_MSG(res !=
false);
51 , m_is_owner(src.m_is_owner)
54 src.m_is_owner =
false;
62 if (m_lock !=
nullptr && m_is_owner) {
63 auto res = m_lock->unlock();
64 __ASSERT_NO_MSG(res !=
false);
68 m_is_owner = src.m_is_owner;
71 src.m_is_owner =
false;
81 if (m_lock !=
nullptr && m_is_owner) {
82 auto res = m_lock->unlock();
83 __ASSERT_NO_MSG(res !=
false);
92 [[nodiscard]]
auto lock() noexcept
96 if (m_lock ==
nullptr) {
98 }
else if (m_is_owner ==
true) {
101 res = m_lock->lock();
102 m_is_owner = (bool)res;
117 if (m_lock ==
nullptr) {
119 }
else if (m_is_owner ==
true) {
122 res = m_lock->try_lock();
123 m_is_owner = (bool)res;
136 template<
class T_Rep,
class T_Period>
138 try_lock_for(
const std::chrono::duration<T_Rep, T_Period>& timeout) noexcept
142 if (m_lock ==
nullptr) {
144 }
else if (m_is_owner ==
true) {
147 res = m_lock->try_lock_for(timeout);
148 m_is_owner = (bool)res;
161 if (m_is_owner ==
false) {
163 }
else if (m_lock ==
nullptr) {
166 res = m_lock->unlock();
195 explicit constexpr
operator bool() const noexcept
203 constexpr T_Mutex*
mutex() const noexcept
215 if (m_lock !=
nullptr)
216 return m_lock->native_handle();
228 if (m_lock !=
nullptr)
229 return m_lock->native_handle();
234 T_Mutex* m_lock{
nullptr};
235 bool m_is_owner{
false};
void assign_error(const T_Error &e) noexcept
zpp::unique_lock using zpp::mutex as a lock.
unique_lock() noexcept=default
Try locking the mutex without waiting.
auto lock() noexcept
Lock the mutex. Wait for ever until it is locked.
constexpr auto native_handle() const noexcept -> native_const_pointer
get the native zephyr mutex handle.
unique_lock(unique_lock &&src) noexcept
Try locking the mutex without waiting.
constexpr T_Mutex * mutex() const noexcept
Try locking the mutex without waiting.
auto try_lock_for(const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
Try locking the mutex with a timeout.
constexpr auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
unique_lock & operator=(const unique_lock &)=delete
unique_lock & operator=(unique_lock &&src) noexcept
Try locking the mutex without waiting.
T_Mutex::native_const_pointer native_const_pointer
auto unlock() noexcept
Unlock the mutex.
T_Mutex::native_pointer native_pointer
constexpr bool owns_lock() const noexcept
Try locking the mutex without waiting.
auto try_lock() noexcept
Try locking the mutex without waiting.
unique_lock(const unique_lock &)=delete
constexpr T_Mutex * release() noexcept
Try locking the mutex without waiting.
~unique_lock() noexcept
Try locking the mutex without waiting.
@ k_inval
Invalid argument.
@ k_deadlk
Resource deadlock avoided.