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

A userspace mutex class. More...

#include <sys_mutex.hpp>

Public Types

using native_type = struct sys_mutex
 
using native_pointer = native_type *
 
using native_cont_pointer = native_type const *
 

Public Member Functions

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

Protected Member Functions

constexpr sys_mutex_base () noexcept=default
 Protected default contructor so only derived objects can be created. More...
 

Detailed Description

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

A userspace mutex class.

Definition at line 23 of file sys_mutex.hpp.

Member Typedef Documentation

◆ native_cont_pointer

template<typename T_Mutex >
using zpp::sys_mutex_base< T_Mutex >::native_cont_pointer = native_type const *

Definition at line 28 of file sys_mutex.hpp.

◆ native_pointer

template<typename T_Mutex >
using zpp::sys_mutex_base< T_Mutex >::native_pointer = native_type*

Definition at line 27 of file sys_mutex.hpp.

◆ native_type

template<typename T_Mutex >
using zpp::sys_mutex_base< T_Mutex >::native_type = struct sys_mutex

Definition at line 26 of file sys_mutex.hpp.

Constructor & Destructor Documentation

◆ sys_mutex_base() [1/3]

template<typename T_Mutex >
constexpr zpp::sys_mutex_base< T_Mutex >::sys_mutex_base ( )
constexprprotecteddefaultnoexcept

Protected default contructor so only derived objects can be created.

◆ sys_mutex_base() [2/3]

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

◆ sys_mutex_base() [3/3]

template<typename T_Mutex >
zpp::sys_mutex_base< T_Mutex >::sys_mutex_base ( sys_mutex_base< T_Mutex > &&  )
delete

Member Function Documentation

◆ lock()

template<typename T_Mutex >
bool zpp::sys_mutex_base< T_Mutex >::lock ( )
inlinenoexcept

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

Returns
true if successfully locked.

Definition at line 41 of file sys_mutex.hpp.

42  {
43  if (sys_mutex_lock(native_handle(), K_FOREVER) == 0) {
44  return true;
45  } else {
46  return false;
47  }
48  }
auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
Definition: sys_mutex.hpp:102

References zpp::sys_mutex_base< T_Mutex >::native_handle().

◆ native_handle() [1/2]

template<typename T_Mutex >
auto zpp::sys_mutex_base< T_Mutex >::native_handle ( ) const -> native_const_pointer
inlinenoexcept

get the native zephyr mutex handle.

Returns
A pointer to the zephyr sys_mutex.

Definition at line 112 of file sys_mutex.hpp.

113  {
114  return static_cast<const T_Mutex*>(this)->native_handle();
115  }

References zpp::sys_mutex_base< T_Mutex >::native_handle().

◆ native_handle() [2/2]

template<typename T_Mutex >
auto zpp::sys_mutex_base< T_Mutex >::native_handle ( ) -> native_pointer
inlinenoexcept

get the native zephyr mutex handle.

Returns
A pointer to the zephyr sys_mutex.

Definition at line 102 of file sys_mutex.hpp.

103  {
104  return static_cast<T_Mutex*>(this)->native_handle();
105  }

Referenced by zpp::sys_mutex_base< T_Mutex >::lock(), zpp::sys_mutex_base< T_Mutex >::native_handle(), zpp::sys_mutex_base< T_Mutex >::try_lock(), zpp::sys_mutex_base< T_Mutex >::try_lock_for(), and zpp::sys_mutex_base< T_Mutex >::unlock().

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ try_lock()

template<typename T_Mutex >
bool zpp::sys_mutex_base< T_Mutex >::try_lock ( )
inlinenoexcept

Try locking the mutex without waiting.

Returns
true if successfully locked.

Definition at line 55 of file sys_mutex.hpp.

56  {
57  if (sys_mutex_lock(native_handle(), K_NO_WAIT) == 0) {
58  return true;
59  } else {
60  return false;
61  }
62  }

References zpp::sys_mutex_base< T_Mutex >::native_handle().

◆ try_lock_for()

template<typename T_Mutex >
template<class T_Rep , class T_Period >
bool zpp::sys_mutex_base< 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 73 of file sys_mutex.hpp.

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  }
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

References zpp::sys_mutex_base< T_Mutex >::native_handle(), and zpp::to_timeout().

◆ unlock()

template<typename T_Mutex >
bool zpp::sys_mutex_base< T_Mutex >::unlock ( )
inlinenoexcept

Unlock the mutex.

Definition at line 88 of file sys_mutex.hpp.

89  {
90  if (sys_mutex_unlock(native_handle()) == 0) {
91  return true;
92  } else {
93  return false;
94  }
95  }

References zpp::sys_mutex_base< T_Mutex >::native_handle().


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