zpp
Zephyr C++20 Framework
zpp::this_thread Namespace Reference

provide functions that access the current thread of execution. More...

Functions

auto get_id () noexcept
 Get the thread ID of the current thread. More...
 
void yield () noexcept
 Yield the current thread. More...
 
template<class T_Rep , class T_Period >
void busy_wait_for (const std::chrono::duration< T_Rep, T_Period > &wait_duration)
 Busy wait for a specified time duration. More...
 
template<class T_Rep , class T_Period >
auto sleep_for (const std::chrono::duration< T_Rep, T_Period > &sleep_duration)
 Suspend the current thread for a specified time duration. More...
 
template<class T_Clock , class T_Duration >
void sleep_until (const std::chrono::time_point< T_Clock, T_Duration > &sleep_time)
 Suspend the current thread until a specified time point. More...
 
void abort () noexcept
 Abort the current thread. More...
 
void suspend () noexcept
 Suspend the current thread. More...
 
thread_prio get_priority () noexcept
 Get the current thread's priority. More...
 
void set_priority (thread_prio prio) noexcept
 Set the current thread's priority. More...
 

Detailed Description

provide functions that access the current thread of execution.

Function Documentation

◆ abort()

void zpp::this_thread::abort ( )
inlinenoexcept

Abort the current thread.

Definition at line 104 of file thread.hpp.

105 {
106  k_thread_abort(k_current_get());
107 }

◆ busy_wait_for()

template<class T_Rep , class T_Period >
void zpp::this_thread::busy_wait_for ( const std::chrono::duration< T_Rep, T_Period > &  wait_duration)
inline

Busy wait for a specified time duration.

Parameters
wait_durationThe time to busy wait

Definition at line 62 of file thread.hpp.

63 {
64  using namespace std::chrono;
65 
66  microseconds us = duration_cast<microseconds>(wait_duration);
67  k_busy_wait(us.count());
68 }

◆ get_id()

auto zpp::this_thread::get_id ( )
inlinenoexcept

Get the thread ID of the current thread.

Returns
The thread ID of the current thread.

Definition at line 42 of file thread.hpp.

43 {
44  return ::zpp::thread_id(k_current_get());
45 }

Referenced by main(), and ZTEST().

◆ get_priority()

thread_prio zpp::this_thread::get_priority ( )
inlinenoexcept

Get the current thread's priority.

Returns
The priority of the current thread

Definition at line 122 of file thread.hpp.

123 {
124  return thread_prio( k_thread_priority_get(k_current_get()) );
125 }

Referenced by ZTEST().

◆ set_priority()

void zpp::this_thread::set_priority ( thread_prio  prio)
inlinenoexcept

Set the current thread's priority.

Parameters
prioThe new priority of the current thread

Definition at line 132 of file thread.hpp.

133 {
134  k_thread_priority_set(k_current_get(), prio.native_value());
135 }

Referenced by ZTEST().

◆ sleep_for()

template<class T_Rep , class T_Period >
auto zpp::this_thread::sleep_for ( const std::chrono::duration< T_Rep, T_Period > &  sleep_duration)
inline

Suspend the current thread for a specified time duration.

Parameters
wait_durationThe time to sleep

Definition at line 77 of file thread.hpp.

78 {
79  auto res = k_sleep(to_timeout(sleep_duration));
80 
81  return std::chrono::milliseconds(res);
82 }
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::to_timeout().

Referenced by inc_count(), main(), and ZTEST().

◆ sleep_until()

template<class T_Clock , class T_Duration >
void zpp::this_thread::sleep_until ( const std::chrono::time_point< T_Clock, T_Duration > &  sleep_time)
inline

Suspend the current thread until a specified time point.

Parameters
wait_durationThe time point util the current thread will sleep

Definition at line 91 of file thread.hpp.

92 {
93  using namespace std::chrono;
94 
95  T_Duration dt;
96  while ( (dt = sleep_time - T_Clock::now()) > T_Duration::zero()) {
97  k_sleep(to_timeout(dt));
98  }
99 }

References zpp::to_timeout().

◆ suspend()

void zpp::this_thread::suspend ( )
inlinenoexcept

Suspend the current thread.

Definition at line 112 of file thread.hpp.

113 {
114  k_thread_suspend(k_current_get());
115 }

◆ yield()

void zpp::this_thread::yield ( )
inlinenoexcept

Yield the current thread.

Definition at line 50 of file thread.hpp.

51 {
52  k_yield();
53 }

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