zpp
Zephyr C++20 Framework
zpp::condition_variable_base< T_ConditionVariable > Class Template Reference

A condition variable CRTP base class. More...

#include <condition_variable.hpp>

Public Types

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

Public Member Functions

auto notify_one () noexcept
 Notify one waiter. More...
 
auto notify_all () noexcept
 Notify all waiters. More...
 
template<class T_Mutex >
auto wait (T_Mutex &m) noexcept
 wait for ever until the variable is signaled. More...
 
template<class T_Mutex , class T_Rep , class T_Period >
auto try_wait_for (T_Mutex &m, const std::chrono::duration< T_Rep, T_Period > &timeout) noexcept
 Try waiting with a timeout to see if the variable is signaled. More...
 
template<class T_Mutex , class T_Predecate >
auto wait (T_Mutex &m, T_Predecate pred) noexcept
 wait for ever until the variable is signaled. More...
 
template<class T_Mutex , class T_Rep , class T_Period , class T_Predecate >
auto try_wait_for (T_Mutex &m, const std::chrono::duration< T_Rep, T_Period > &timeout, T_Predecate pred) noexcept
 Try waiting with a timeout to see if the variable is signaled. More...
 
auto native_handle () noexcept -> native_pointer
 get the native zephyr k_condvar pointer. More...
 
auto native_handle () const noexcept -> native_const_pointer
 get the native zephyr k_condvar pointer. More...
 
 condition_variable_base (const condition_variable_base &)=delete
 
 condition_variable_base (condition_variable_base &&)=delete
 
condition_variable_baseoperator= (const condition_variable_base &)=delete
 
condition_variable_baseoperator= (condition_variable_base &&)=delete
 

Protected Member Functions

constexpr condition_variable_base () noexcept=default
 Protected default constructor so only derived classes can be created. More...
 

Detailed Description

template<typename T_ConditionVariable>
class zpp::condition_variable_base< T_ConditionVariable >

A condition variable CRTP base class.

Definition at line 26 of file condition_variable.hpp.

Member Typedef Documentation

◆ native_const_pointer

template<typename T_ConditionVariable >
using zpp::condition_variable_base< T_ConditionVariable >::native_const_pointer = native_type const *

Definition at line 31 of file condition_variable.hpp.

◆ native_pointer

template<typename T_ConditionVariable >
using zpp::condition_variable_base< T_ConditionVariable >::native_pointer = native_type*

Definition at line 30 of file condition_variable.hpp.

◆ native_type

template<typename T_ConditionVariable >
using zpp::condition_variable_base< T_ConditionVariable >::native_type = struct k_condvar

Definition at line 29 of file condition_variable.hpp.

Constructor & Destructor Documentation

◆ condition_variable_base() [1/3]

template<typename T_ConditionVariable >
constexpr zpp::condition_variable_base< T_ConditionVariable >::condition_variable_base ( )
constexprprotecteddefaultnoexcept

Protected default constructor so only derived classes can be created.

◆ condition_variable_base() [2/3]

template<typename T_ConditionVariable >
zpp::condition_variable_base< T_ConditionVariable >::condition_variable_base ( const condition_variable_base< T_ConditionVariable > &  )
delete

◆ condition_variable_base() [3/3]

template<typename T_ConditionVariable >
zpp::condition_variable_base< T_ConditionVariable >::condition_variable_base ( condition_variable_base< T_ConditionVariable > &&  )
delete

Member Function Documentation

◆ native_handle() [1/2]

template<typename T_ConditionVariable >
auto zpp::condition_variable_base< T_ConditionVariable >::native_handle ( ) const -> native_const_pointer
inlinenoexcept

get the native zephyr k_condvar pointer.

Returns
A pointer to the zephyr k_condvar pointer.

Definition at line 219 of file condition_variable.hpp.

220  {
221  return static_cast<const T_ConditionVariable*>(this)->native_handle();
222  }
auto native_handle() noexcept -> native_pointer
get the native zephyr k_condvar pointer.

References zpp::condition_variable_base< T_ConditionVariable >::native_handle().

◆ native_handle() [2/2]

template<typename T_ConditionVariable >
auto zpp::condition_variable_base< T_ConditionVariable >::native_handle ( ) -> native_pointer
inlinenoexcept

◆ notify_all()

template<typename T_ConditionVariable >
auto zpp::condition_variable_base< T_ConditionVariable >::notify_all ( )
inlinenoexcept

Notify all waiters.

Returns
true if successfull.

Definition at line 63 of file condition_variable.hpp.

64  {
65  result<void, error_code> res;
66 
67  auto rc = k_condvar_broadcast(native_handle());
68  if (rc == 0) {
69  res.assign_value();
70  } else {
71  res.assign_error(to_error_code(-rc));
72  }
73 
74  return res;
75  }
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::condition_variable_base< T_ConditionVariable >::native_handle(), and zpp::to_error_code().

◆ notify_one()

template<typename T_ConditionVariable >
auto zpp::condition_variable_base< T_ConditionVariable >::notify_one ( )
inlinenoexcept

Notify one waiter.

Returns
true if successfull.

Definition at line 44 of file condition_variable.hpp.

45  {
46  result<void, error_code> res;
47 
48  auto rc = k_condvar_signal(native_handle());
49  if (rc == 0) {
50  res.assign_value();
51  } else {
52  res.assign_error(to_error_code(-rc));
53  }
54 
55  return res;
56  }

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

◆ operator=() [1/2]

template<typename T_ConditionVariable >
condition_variable_base& zpp::condition_variable_base< T_ConditionVariable >::operator= ( condition_variable_base< T_ConditionVariable > &&  )
delete

◆ operator=() [2/2]

template<typename T_ConditionVariable >
condition_variable_base& zpp::condition_variable_base< T_ConditionVariable >::operator= ( const condition_variable_base< T_ConditionVariable > &  )
delete

◆ try_wait_for() [1/2]

template<typename T_ConditionVariable >
template<class T_Mutex , class T_Rep , class T_Period >
auto zpp::condition_variable_base< T_ConditionVariable >::try_wait_for ( T_Mutex &  m,
const std::chrono::duration< T_Rep, T_Period > &  timeout 
)
inlinenoexcept

Try waiting with a timeout to see if the variable is signaled.

Parameters
mThe mutex to use
timeoutThe time to wait before returning
Returns
true if successfull.

Definition at line 114 of file condition_variable.hpp.

115  {
116  using namespace std::chrono;
117 
118  result<void, error_code> res;
119 
120  auto h = m.native_handle();
121  if (h == nullptr) {
122  res.assign_error(error_code::k_inval);
123  } else {
124  auto rc = k_condvar_wait(native_handle(), h, to_timeout(timeout));
125  if (rc == 0) {
126  res.assign_value();
127  } else {
128  res.assign_error(to_error_code(-rc));
129  }
130  }
131 
132  return res;
133  }
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
@ k_inval
Invalid argument.

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::k_inval, zpp::condition_variable_base< T_ConditionVariable >::native_handle(), zpp::to_error_code(), and zpp::to_timeout().

◆ try_wait_for() [2/2]

template<typename T_ConditionVariable >
template<class T_Mutex , class T_Rep , class T_Period , class T_Predecate >
auto zpp::condition_variable_base< T_ConditionVariable >::try_wait_for ( T_Mutex &  m,
const std::chrono::duration< T_Rep, T_Period > &  timeout,
T_Predecate  pred 
)
inlinenoexcept

Try waiting with a timeout to see if the variable is signaled.

Parameters
mThe mutex to use
timeoutThe time to wait before returning
predThe predecate that must be true before the wait returns
Returns
true if successfull.

Definition at line 178 of file condition_variable.hpp.

179  {
180  using namespace std::chrono;
181 
182  result<void, error_code> res;
183 
184  auto h = m.native_handle();
185  if (h == nullptr) {
186  res.assign_error(error_code::k_inval);
187  } else {
188  while(pred() == false) {
189  auto rc = k_condvar_wait(native_handle(), h, to_timeout(timeout));
190  if (rc != 0) {
191  res.assign_error(to_error_code(-rc));
192  return res;
193  }
194 
195  // TODO update timeout
196  }
197 
198  res.assign_value();
199  }
200 
201  return res;
202  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::k_inval, zpp::condition_variable_base< T_ConditionVariable >::native_handle(), zpp::to_error_code(), and zpp::to_timeout().

◆ wait() [1/2]

template<typename T_ConditionVariable >
template<class T_Mutex >
auto zpp::condition_variable_base< T_ConditionVariable >::wait ( T_Mutex &  m)
inlinenoexcept

wait for ever until the variable is signaled.

Parameters
mThe mutex to use
Returns
true if successfull.

Definition at line 85 of file condition_variable.hpp.

86  {
87  result<void, error_code> res;
88 
89  auto h = m.native_handle();
90  if (h == nullptr) {
91  res.assign_error(error_code::k_inval);
92  } else {
93  auto rc = k_condvar_wait(native_handle(), h, K_FOREVER);
94  if (rc == 0) {
95  res.assign_value();
96  } else {
97  res.assign_error(to_error_code(-rc));
98  }
99  }
100 
101  return res;
102  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::k_inval, zpp::condition_variable_base< T_ConditionVariable >::native_handle(), and zpp::to_error_code().

◆ wait() [2/2]

template<typename T_ConditionVariable >
template<class T_Mutex , class T_Predecate >
auto zpp::condition_variable_base< T_ConditionVariable >::wait ( T_Mutex &  m,
T_Predecate  pred 
)
inlinenoexcept

wait for ever until the variable is signaled.

Parameters
mThe mutex to use
predThe predecate that must be true before the wait returns
Returns
true if successfull.

Definition at line 145 of file condition_variable.hpp.

146  {
147  result<void, error_code> res;
148 
149  auto h = m.native_handle();
150  if (h == nullptr) {
151  res.assign_error(error_code::k_inval);
152  } else {
153  while (pred() == false) {
154  auto rc = k_condvar_wait(native_handle(), h, K_FOREVER);
155  if (rc != 0) {
156  res.assign_error(to_error_code(-rc));
157  return res;
158  }
159  }
160 
161  res.assign_value();
162  }
163 
164  return res;
165  }

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::k_inval, zpp::condition_variable_base< T_ConditionVariable >::native_handle(), and zpp::to_error_code().


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