zpp
Zephyr C++20 Framework
zpp::unique_lock< T_Mutex > Class Template Reference

zpp::unique_lock using zpp::mutex as a lock. More...

#include <unique_lock.hpp>

Public Types

using native_pointer = T_Mutex::native_pointer
 
using native_const_pointer = T_Mutex::native_const_pointer
 

Public Member Functions

 unique_lock () noexcept=default
 Try locking the mutex without waiting. More...
 
 unique_lock (T_Mutex &lock) noexcept
 Try locking the mutex without waiting. More...
 
 unique_lock (unique_lock &&src) noexcept
 Try locking the mutex without waiting. More...
 
unique_lockoperator= (unique_lock &&src) noexcept
 Try locking the mutex without waiting. More...
 
 ~unique_lock () noexcept
 Try locking the mutex without waiting. More...
 
auto lock () noexcept
 Lock the mutex. Wait for ever until it is locked. More...
 
auto try_lock () noexcept
 Try locking the mutex without waiting. More...
 
template<class T_Rep , class T_Period >
auto try_lock_for (const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
 Try locking the mutex with a timeout. More...
 
auto unlock () noexcept
 Unlock the mutex. More...
 
constexpr T_Mutex * release () noexcept
 Try locking the mutex without waiting. More...
 
constexpr bool owns_lock () const noexcept
 Try locking the mutex without waiting. More...
 
constexpr operator bool () const noexcept
 Try locking the mutex without waiting. More...
 
constexpr T_Mutex * mutex () const noexcept
 Try locking the mutex without waiting. More...
 
constexpr auto native_handle () noexcept -> native_pointer
 get the native zephyr mutex handle. More...
 
constexpr auto native_handle () const noexcept -> native_const_pointer
 get the native zephyr mutex handle. More...
 
 unique_lock (const unique_lock &)=delete
 
unique_lockoperator= (const unique_lock &)=delete
 

Detailed Description

template<typename T_Mutex>
class zpp::unique_lock< T_Mutex >

zpp::unique_lock using zpp::mutex as a lock.

Definition at line 24 of file unique_lock.hpp.

Member Typedef Documentation

◆ native_const_pointer

template<typename T_Mutex >
using zpp::unique_lock< T_Mutex >::native_const_pointer = T_Mutex::native_const_pointer

Definition at line 27 of file unique_lock.hpp.

◆ native_pointer

template<typename T_Mutex >
using zpp::unique_lock< T_Mutex >::native_pointer = T_Mutex::native_pointer

Definition at line 26 of file unique_lock.hpp.

Constructor & Destructor Documentation

◆ unique_lock() [1/4]

template<typename T_Mutex >
zpp::unique_lock< T_Mutex >::unique_lock ( )
defaultnoexcept

Try locking the mutex without waiting.

◆ unique_lock() [2/4]

template<typename T_Mutex >
zpp::unique_lock< T_Mutex >::unique_lock ( T_Mutex &  lock)
inlineexplicitnoexcept

Try locking the mutex without waiting.

Definition at line 37 of file unique_lock.hpp.

38  : m_lock(&lock)
39  {
40  __ASSERT_NO_MSG(m_lock != nullptr);
41  auto res = m_lock->lock();
42  __ASSERT_NO_MSG(res != false);
43  m_is_owner = true;
44  }
auto lock() noexcept
Lock the mutex. Wait for ever until it is locked.
Definition: unique_lock.hpp:92

◆ unique_lock() [3/4]

template<typename T_Mutex >
zpp::unique_lock< T_Mutex >::unique_lock ( unique_lock< T_Mutex > &&  src)
inlinenoexcept

Try locking the mutex without waiting.

Definition at line 49 of file unique_lock.hpp.

50  : m_lock(src.m_lock)
51  , m_is_owner(src.m_is_owner)
52  {
53  src.m_lock = nullptr;
54  src.m_is_owner = false;
55  }

◆ ~unique_lock()

template<typename T_Mutex >
zpp::unique_lock< T_Mutex >::~unique_lock ( )
inlinenoexcept

Try locking the mutex without waiting.

Definition at line 79 of file unique_lock.hpp.

80  {
81  if (m_lock != nullptr && m_is_owner) {
82  auto res = m_lock->unlock();
83  __ASSERT_NO_MSG(res != false);
84  }
85  }

◆ unique_lock() [4/4]

template<typename T_Mutex >
zpp::unique_lock< T_Mutex >::unique_lock ( const unique_lock< T_Mutex > &  )
delete

Member Function Documentation

◆ lock()

template<typename T_Mutex >
auto zpp::unique_lock< T_Mutex >::lock ( )
inlinenoexcept

Lock the mutex. Wait for ever until it is locked.

Returns
true if successfully locked.

Definition at line 92 of file unique_lock.hpp.

93  {
94  result<void, error_code> res;
95 
96  if (m_lock == nullptr) {
97  res.assign_error(error_code::k_inval);
98  } else if (m_is_owner == true) {
99  res.assign_error(error_code::k_deadlk);
100  } else {
101  res = m_lock->lock();
102  m_is_owner = (bool)res;
103  }
104 
105  return res;
106  }
@ k_inval
Invalid argument.
@ k_deadlk
Resource deadlock avoided.

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::k_deadlk, and zpp::k_inval.

◆ mutex()

template<typename T_Mutex >
constexpr T_Mutex* zpp::unique_lock< T_Mutex >::mutex ( ) const
inlineconstexprnoexcept

Try locking the mutex without waiting.

Definition at line 203 of file unique_lock.hpp.

204  {
205  return m_lock;
206  }

◆ native_handle() [1/2]

template<typename T_Mutex >
constexpr auto zpp::unique_lock< T_Mutex >::native_handle ( ) const -> native_const_pointer
inlineconstexprnoexcept

get the native zephyr mutex handle.

Returns
A pointer to the zephyr k_mutex.

Definition at line 226 of file unique_lock.hpp.

227  {
228  if (m_lock != nullptr)
229  return m_lock->native_handle();
230  else
231  return nullptr;
232  }

◆ native_handle() [2/2]

template<typename T_Mutex >
constexpr auto zpp::unique_lock< T_Mutex >::native_handle ( ) -> native_pointer
inlineconstexprnoexcept

get the native zephyr mutex handle.

Returns
A pointer to the zephyr k_mutex.

Definition at line 213 of file unique_lock.hpp.

214  {
215  if (m_lock != nullptr)
216  return m_lock->native_handle();
217  else
218  return nullptr;
219  }

◆ operator bool()

template<typename T_Mutex >
constexpr zpp::unique_lock< T_Mutex >::operator bool ( ) const
inlineexplicitconstexprnoexcept

Try locking the mutex without waiting.

Definition at line 195 of file unique_lock.hpp.

196  {
197  return owns_lock();
198  }
constexpr bool owns_lock() const noexcept
Try locking the mutex without waiting.

References zpp::unique_lock< T_Mutex >::owns_lock().

◆ operator=() [1/2]

template<typename T_Mutex >
unique_lock& zpp::unique_lock< T_Mutex >::operator= ( const unique_lock< T_Mutex > &  )
delete

◆ operator=() [2/2]

template<typename T_Mutex >
unique_lock& zpp::unique_lock< T_Mutex >::operator= ( unique_lock< T_Mutex > &&  src)
inlinenoexcept

Try locking the mutex without waiting.

Definition at line 60 of file unique_lock.hpp.

61  {
62  if (m_lock != nullptr && m_is_owner) {
63  auto res = m_lock->unlock();
64  __ASSERT_NO_MSG(res != false);
65  }
66 
67  m_lock = src.m_lock;
68  m_is_owner = src.m_is_owner;
69 
70  src.m_lock = nullptr;
71  src.m_is_owner = false;
72 
73  return *this;
74  }

◆ owns_lock()

template<typename T_Mutex >
constexpr bool zpp::unique_lock< T_Mutex >::owns_lock ( ) const
inlineconstexprnoexcept

Try locking the mutex without waiting.

Definition at line 187 of file unique_lock.hpp.

188  {
189  return m_is_owner;
190  }

Referenced by zpp::unique_lock< T_Mutex >::operator bool().

◆ release()

template<typename T_Mutex >
constexpr T_Mutex* zpp::unique_lock< T_Mutex >::release ( )
inlineconstexprnoexcept

Try locking the mutex without waiting.

Definition at line 176 of file unique_lock.hpp.

177  {
178  auto ret = m_lock;
179  m_lock = nullptr;
180  m_is_owner = false;
181  return ret;
182  }

◆ try_lock()

template<typename T_Mutex >
auto zpp::unique_lock< T_Mutex >::try_lock ( )
inlinenoexcept

Try locking the mutex without waiting.

Returns
true if successfully locked.

Definition at line 113 of file unique_lock.hpp.

114  {
115  result<void, error_code> res;
116 
117  if (m_lock == nullptr) {
118  res.assign_error(error_code::k_inval);
119  } else if (m_is_owner == true) {
120  res.assign_error(error_code::k_deadlk);
121  } else {
122  res = m_lock->try_lock();
123  m_is_owner = (bool)res;
124  }
125 
126  return res;
127  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::k_deadlk, and zpp::k_inval.

◆ try_lock_for()

template<typename T_Mutex >
template<class T_Rep , class T_Period >
auto zpp::unique_lock< T_Mutex >::try_lock_for ( const std::chrono::duration< T_Rep, T_Period > &  timeout)
inlinenoexcept

Try locking the mutex with a timeout.

Parameters
timeoutThe time to wait before returning
Returns
true if successfully locked.

Definition at line 138 of file unique_lock.hpp.

139  {
140  result<void, error_code> res;
141 
142  if (m_lock == nullptr) {
143  res.assign_error(error_code::k_inval);
144  } else if (m_is_owner == true) {
145  res.assign_error(error_code::k_deadlk);
146  } else {
147  res = m_lock->try_lock_for(timeout);
148  m_is_owner = (bool)res;
149  }
150 
151  return res;
152  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::k_deadlk, and zpp::k_inval.

◆ unlock()

template<typename T_Mutex >
auto zpp::unique_lock< T_Mutex >::unlock ( )
inlinenoexcept

Unlock the mutex.

Definition at line 157 of file unique_lock.hpp.

158  {
159  result<void, error_code> res;
160 
161  if (m_is_owner == false) {
162  res.assign_error(error_code::k_perm);
163  } else if (m_lock == nullptr) {
164  res.assign_error(error_code::k_inval);
165  } else {
166  res = m_lock->unlock();
167  m_is_owner = false;
168  }
169 
170  return res;
171  }
@ k_perm
Not owner.

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::k_inval, and zpp::k_perm.


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