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

A recursive mutex CRTP base class. More...

#include <mutex.hpp>

Public Types

using native_type = struct k_mutex
 
using native_pointer = native_type *
 
using native_const_pointer = native_type const *
 

Public Member Functions

auto lock () noexcept
 Lock the mutex. Wait forever 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...
 
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...
 
 mutex_base (const mutex_base &)=delete
 
 mutex_base (mutex_base &&)=delete
 
mutex_baseoperator= (const mutex_base &)=delete
 
mutex_baseoperator= (mutex_base &&)=delete
 

Protected Member Functions

constexpr mutex_base () noexcept
 Protected default constructor so only derived objects can be created. More...
 

Detailed Description

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

A recursive mutex CRTP base class.

Definition at line 24 of file mutex.hpp.

Member Typedef Documentation

◆ native_const_pointer

template<typename T_Mutex >
using zpp::mutex_base< T_Mutex >::native_const_pointer = native_type const *

Definition at line 29 of file mutex.hpp.

◆ native_pointer

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

Definition at line 28 of file mutex.hpp.

◆ native_type

template<typename T_Mutex >
using zpp::mutex_base< T_Mutex >::native_type = struct k_mutex

Definition at line 27 of file mutex.hpp.

Constructor & Destructor Documentation

◆ mutex_base() [1/3]

template<typename T_Mutex >
constexpr zpp::mutex_base< T_Mutex >::mutex_base ( )
inlineconstexprprotectednoexcept

Protected default constructor so only derived objects can be created.

Definition at line 34 of file mutex.hpp.

35  {
36  }

◆ mutex_base() [2/3]

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

◆ mutex_base() [3/3]

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

Member Function Documentation

◆ lock()

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

Lock the mutex. Wait forever until it is locked.

Returns
true if successfully locked.

Definition at line 44 of file mutex.hpp.

45  {
46  result<void, error_code> res;
47 
48  auto rc = k_mutex_lock(native_handle(), K_FOREVER);
49  if (rc == 0) {
50  res.assign_value();
51  } else {
52  res.assign_error(to_error_code(-rc));
53  }
54 
55  return res;
56  }
auto native_handle() noexcept -> native_pointer
get the native zephyr mutex handle.
Definition: mutex.hpp:126
constexpr error_code to_error_code(int v) noexcept
Definition: error_code.hpp:102

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::mutex_base< T_Mutex >::native_handle(), and zpp::to_error_code().

◆ native_handle() [1/2]

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

get the native zephyr mutex handle.

Returns
A pointer to the zephyr k_mutex.

Definition at line 136 of file mutex.hpp.

137  {
138  return static_cast<const T_Mutex*>(this)->native_handle();
139  }

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

◆ native_handle() [2/2]

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

get the native zephyr mutex handle.

Returns
A pointer to the zephyr k_mutex.

Definition at line 126 of file mutex.hpp.

127  {
128  return static_cast<T_Mutex*>(this)->native_handle();
129  }

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ try_lock()

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

Try locking the mutex without waiting.

Returns
true if successfully locked.

Definition at line 63 of file mutex.hpp.

64  {
65  result<void, error_code> res;
66 
67  auto rc = k_mutex_lock(native_handle(), K_NO_WAIT);
68  if (rc == 0) {
69  res.assign_value();
70  } else {
71  res.assign_error(to_error_code(-rc));
72  }
73 
74  return res;
75  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::mutex_base< T_Mutex >::native_handle(), and zpp::to_error_code().

◆ try_lock_for()

template<typename T_Mutex >
template<class T_Rep , class T_Period >
auto zpp::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 86 of file mutex.hpp.

87  {
88  using namespace std::chrono;
89 
90  result<void, error_code> res;
91 
92  auto rc = k_mutex_lock(native_handle(), to_timeout(timeout));
93  if (rc == 0) {
94  res.assign_value();
95  } else {
96  res.assign_error(to_error_code(-rc));
97  }
98 
99  return res;
100  }
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::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::mutex_base< T_Mutex >::native_handle(), zpp::to_error_code(), and zpp::to_timeout().

◆ unlock()

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

Unlock the mutex.

Returns
true on success

Definition at line 107 of file mutex.hpp.

108  {
109  result<void, error_code> res;
110 
111  auto rc = k_mutex_unlock(native_handle());
112  if (rc == 0) {
113  res.assign_value();
114  } else {
115  res.assign_error(to_error_code(-rc));
116  }
117 
118  return res;
119  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::mutex_base< T_Mutex >::native_handle(), and zpp::to_error_code().


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