zpp
Zephyr C++20 Framework
zpp::sem_base< T_Sem > Class Template Reference

Counting semaphore base class. More...

#include <sem.hpp>

Public Types

using native_type = struct k_sem
 
using native_pointer = native_type *
 
using native_const_pointer = native_type const *
 
using counter_type = uint32_t
 Type used as counter. More...
 

Public Member Functions

bool take () noexcept
 Take the semaphore waiting forever. More...
 
bool try_take () noexcept
 Try to take the semaphore without waiting. More...
 
template<class T_Rep , class T_Period >
bool try_take_for (const std::chrono::duration< T_Rep, T_Period > &timeout_duration) noexcept
 Try to take the semaphore waiting a certain timeout. More...
 
void give () noexcept
 Give the semaphore. More...
 
void reset () noexcept
 Reset the semaphore counter to zero. More...
 
counter_type count () noexcept
 Get current semaphore count. More...
 
void operator++ (int) noexcept
 Give the semaphore. More...
 
void operator-- (int) noexcept
 Take the semaphore waiting forever. More...
 
void operator+= (int n) noexcept
 Give the semaphore n times. More...
 
void operator-= (int n) noexcept
 Take the semaphore n times, waiting forever. More...
 
auto native_handle () noexcept -> native_pointer
 get the native zephyr sem handle. More...
 
auto native_handle () const noexcept -> native_const_pointer
 get the native zephyr sem handle. More...
 
 sem_base (const sem_base &)=delete
 
 sem_base (sem_base &&)=delete
 
sem_baseoperator= (const sem_base &)=delete
 
sem_baseoperator= (sem_base &&)=delete
 

Static Public Attributes

constexpr static counter_type max_count
 Maximum value of the counter. More...
 

Protected Member Functions

constexpr sem_base () noexcept
 Default constructor, only allowed derived objects. More...
 

Detailed Description

template<typename T_Sem>
class zpp::sem_base< T_Sem >

Counting semaphore base class.

Definition at line 24 of file sem.hpp.

Member Typedef Documentation

◆ counter_type

template<typename T_Sem >
using zpp::sem_base< T_Sem >::counter_type = uint32_t

Type used as counter.

Definition at line 34 of file sem.hpp.

◆ native_const_pointer

template<typename T_Sem >
using zpp::sem_base< T_Sem >::native_const_pointer = native_type const *

Definition at line 29 of file sem.hpp.

◆ native_pointer

template<typename T_Sem >
using zpp::sem_base< T_Sem >::native_pointer = native_type*

Definition at line 28 of file sem.hpp.

◆ native_type

template<typename T_Sem >
using zpp::sem_base< T_Sem >::native_type = struct k_sem

Definition at line 27 of file sem.hpp.

Constructor & Destructor Documentation

◆ sem_base() [1/3]

template<typename T_Sem >
constexpr zpp::sem_base< T_Sem >::sem_base ( )
inlineconstexprprotectednoexcept

Default constructor, only allowed derived objects.

Definition at line 45 of file sem.hpp.

46  {
47  }

◆ sem_base() [2/3]

template<typename T_Sem >
zpp::sem_base< T_Sem >::sem_base ( const sem_base< T_Sem > &  )
delete

◆ sem_base() [3/3]

template<typename T_Sem >
zpp::sem_base< T_Sem >::sem_base ( sem_base< T_Sem > &&  )
delete

Member Function Documentation

◆ count()

template<typename T_Sem >
counter_type zpp::sem_base< T_Sem >::count ( )
inlinenoexcept

Get current semaphore count.

Returns
The current semaphore count.

Definition at line 117 of file sem.hpp.

118  {
119  return k_sem_count_get(native_handle());
120  }
auto native_handle() noexcept -> native_pointer
get the native zephyr sem handle.
Definition: sem.hpp:171

References zpp::sem_base< T_Sem >::native_handle().

◆ give()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::give ( )
inlinenoexcept

Give the semaphore.

Definition at line 99 of file sem.hpp.

100  {
101  k_sem_give(native_handle());
102  }

References zpp::sem_base< T_Sem >::native_handle().

Referenced by zpp::sem_base< T_Sem >::operator++(), and zpp::sem_base< T_Sem >::operator+=().

◆ native_handle() [1/2]

template<typename T_Sem >
auto zpp::sem_base< T_Sem >::native_handle ( ) const -> native_const_pointer
inlinenoexcept

get the native zephyr sem handle.

Returns
A pointer to the zephyr k_sem.

Definition at line 181 of file sem.hpp.

182  {
183  return static_cast<const T_Sem*>(this)->native_handle();
184  }

References zpp::sem_base< T_Sem >::native_handle().

◆ native_handle() [2/2]

template<typename T_Sem >
auto zpp::sem_base< T_Sem >::native_handle ( ) -> native_pointer
inlinenoexcept

get the native zephyr sem handle.

Returns
A pointer to the zephyr k_sem.

Definition at line 171 of file sem.hpp.

172  {
173  return static_cast<T_Sem*>(this)->native_handle();
174  }

Referenced by zpp::sem_base< T_Sem >::count(), zpp::sem_base< T_Sem >::give(), zpp::sem_base< T_Sem >::native_handle(), zpp::sem_base< T_Sem >::reset(), zpp::sem_base< T_Sem >::take(), zpp::sem_base< T_Sem >::try_take(), and zpp::sem_base< T_Sem >::try_take_for().

◆ operator++()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::operator++ ( int  )
inlinenoexcept

Give the semaphore.

Definition at line 125 of file sem.hpp.

126  {
127  give();
128  }
void give() noexcept
Give the semaphore.
Definition: sem.hpp:99

References zpp::sem_base< T_Sem >::give().

◆ operator+=()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::operator+= ( int  n)
inlinenoexcept

Give the semaphore n times.

Parameters
nThe number of times to give the semaphore

Definition at line 145 of file sem.hpp.

146  {
147  while (n-- > 0) {
148  give();
149  }
150  }

References zpp::sem_base< T_Sem >::give().

◆ operator--()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::operator-- ( int  )
inlinenoexcept

Take the semaphore waiting forever.

Definition at line 133 of file sem.hpp.

134  {
135  while (!take()) {
137  }
138  }
bool take() noexcept
Take the semaphore waiting forever.
Definition: sem.hpp:54
void yield() noexcept
Yield the current thread.
Definition: thread.hpp:50

References zpp::sem_base< T_Sem >::take(), and zpp::this_thread::yield().

◆ operator-=()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::operator-= ( int  n)
inlinenoexcept

Take the semaphore n times, waiting forever.

Parameters
nThe number of times to take the semaphore

Definition at line 157 of file sem.hpp.

158  {
159  while (n-- > 0) {
160  while (!take()) {
162  }
163  }
164  }

References zpp::sem_base< T_Sem >::take(), and zpp::this_thread::yield().

◆ operator=() [1/2]

template<typename T_Sem >
sem_base& zpp::sem_base< T_Sem >::operator= ( const sem_base< T_Sem > &  )
delete

◆ operator=() [2/2]

template<typename T_Sem >
sem_base& zpp::sem_base< T_Sem >::operator= ( sem_base< T_Sem > &&  )
delete

◆ reset()

template<typename T_Sem >
void zpp::sem_base< T_Sem >::reset ( )
inlinenoexcept

Reset the semaphore counter to zero.

Definition at line 107 of file sem.hpp.

108  {
109  k_sem_reset(native_handle());
110  }

References zpp::sem_base< T_Sem >::native_handle().

◆ take()

template<typename T_Sem >
bool zpp::sem_base< T_Sem >::take ( )
inlinenoexcept

Take the semaphore waiting forever.

Returns
true when semaphore was taken

Definition at line 54 of file sem.hpp.

55  {
56  if (k_sem_take(native_handle(), K_FOREVER) == 0) {
57  return true;
58  } else {
59  return false;
60  }
61  }

References zpp::sem_base< T_Sem >::native_handle().

Referenced by zpp::sem_base< T_Sem >::operator--(), and zpp::sem_base< T_Sem >::operator-=().

◆ try_take()

template<typename T_Sem >
bool zpp::sem_base< T_Sem >::try_take ( )
inlinenoexcept

Try to take the semaphore without waiting.

Returns
true when semaphore was taken

Definition at line 68 of file sem.hpp.

69  {
70  if (k_sem_take(native_handle(), K_NO_WAIT) == 0) {
71  return true;
72  } else {
73  return false;
74  }
75  }

References zpp::sem_base< T_Sem >::native_handle().

◆ try_take_for()

template<typename T_Sem >
template<class T_Rep , class T_Period >
bool zpp::sem_base< T_Sem >::try_take_for ( const std::chrono::duration< T_Rep, T_Period > &  timeout_duration)
inlinenoexcept

Try to take the semaphore waiting a certain timeout.

Parameters
timeout_durationThe timeout to wait before giving up
Returns
true when semaphore was taken

Definition at line 86 of file sem.hpp.

88  {
89  if (k_sem_take(native_handle(), to_timeout(timeout_duration)) == 0) {
90  return true;
91  } else {
92  return false;
93  }
94  }
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::sem_base< T_Sem >::native_handle(), and zpp::to_timeout().

Member Data Documentation

◆ max_count

template<typename T_Sem >
constexpr static counter_type zpp::sem_base< T_Sem >::max_count
staticconstexpr
Initial value:
=
std::numeric_limits<counter_type>::max()

Maximum value of the counter.

Definition at line 39 of file sem.hpp.


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