zpp
Zephyr C++20 Framework
zpp::thread Class Reference

The class thread repecents a single Zephyr thread. More...

#include <thread.hpp>

Public Member Functions

constexpr thread () noexcept
 Creates a object which doesn't represent a Zephyr thread. More...
 
constexpr thread (thread_id tid) noexcept
 Creates a object which represents Zephyr thread with tid. More...
 
template<typename T_Heap , typename T_Callback , typename... T_CallbackArgs, std::enable_if_t< std::is_nothrow_invocable_v< T_Callback, T_CallbackArgs... >, bool > = true>
constexpr thread (thread_data &td, thread_stack &&tstack, const thread_attr &attr, T_Heap *heap, T_Callback &&f, T_CallbackArgs &&... args) noexcept
 Creates a object which represents a new Zephyr thread. More...
 
template<typename T_Callback , typename T_CallbackArg , std::enable_if_t< std::is_nothrow_invocable_v< T_Callback, T_CallbackArg >, bool > = true>
constexpr thread (thread_data &td, thread_stack &&tstack, const thread_attr &attr, T_Callback f, T_CallbackArg arg) noexcept
 Creates a object which represents a new Zephyr thread. More...
 
template<typename T_Callback , std::enable_if_t< std::is_nothrow_invocable_v< T_Callback >, bool > = true>
constexpr thread (thread_data &td, thread_stack &&tstack, const thread_attr &attr, T_Callback f) noexcept
 Creates a object which represents a new Zephyr thread. More...
 
constexpr thread (thread &&other) noexcept
 Move constructor. More...
 
constexpr threadoperator= (thread &&other) noexcept
 Move assignment operator. More...
 
 ~thread () noexcept
 Destructor, will abort the thread. More...
 
constexpr operator bool () const noexcept
 check if this object manages a thread More...
 
constexpr void detach () noexcept
 Detach this object from the thread. More...
 
auto wakeup () noexcept
 wakeup the thread this object mamages. More...
 
auto start () noexcept
 start the thread this object mamages. More...
 
auto abort () noexcept
 abort the thread this object mamages. More...
 
auto resume () noexcept
 resume the thread this object mamages. More...
 
auto join () noexcept
 join the thread this object mamages. More...
 
auto suspend () noexcept
 suspend the thread this object mamages. More...
 
auto priority () noexcept
 Get priority of the thread this object mamages. More...
 
auto set_priority (thread_prio prio) const noexcept
 Set priority of the thread this object mamages. More...
 
auto set_name (const char *name) noexcept
 Set name of the thread this object mamages. More...
 
auto name () const noexcept
 Get name of the thread this object mamages. More...
 
 thread (const thread &)=delete
 
threadoperator= (const thread &)=delete
 

Detailed Description

The class thread repecents a single Zephyr thread.

Definition at line 147 of file thread.hpp.

Constructor & Destructor Documentation

◆ thread() [1/7]

constexpr zpp::thread::thread ( )
inlineconstexprnoexcept

Creates a object which doesn't represent a Zephyr thread.

Definition at line 199 of file thread.hpp.

200  {
201  }

◆ thread() [2/7]

constexpr zpp::thread::thread ( thread_id  tid)
inlineexplicitconstexprnoexcept

Creates a object which represents Zephyr thread with tid.

Parameters
tidThe ID to manage

Definition at line 208 of file thread.hpp.

209  : m_tid(tid)
210  {
211  }

◆ thread() [3/7]

template<typename T_Heap , typename T_Callback , typename... T_CallbackArgs, std::enable_if_t< std::is_nothrow_invocable_v< T_Callback, T_CallbackArgs... >, bool > = true>
constexpr zpp::thread::thread ( thread_data td,
thread_stack &&  tstack,
const thread_attr attr,
T_Heap *  heap,
T_Callback &&  f,
T_CallbackArgs &&...  args 
)
inlineconstexprnoexcept

Creates a object which represents a new Zephyr thread.

Parameters
tcbThe TCB to use
attrThe creation attributes to use
fThe thread entry point
argsThe arguments to pass to f

Definition at line 224 of file thread.hpp.

231  {
232  typedef typename std::decay<T_Heap>::type CallInfoHeap;
233  typedef typename std::decay<T_Callback>::type CallInfoF;
234  typedef std::tuple<typename std::decay<T_CallbackArgs>::type...> CallInfoArgs;
235 
236  static_assert(std::is_invocable_v<T_Callback, T_CallbackArgs...>);
237  static_assert(std::is_nothrow_invocable_v<T_Callback, T_CallbackArgs...>);
238 
239  struct call_info {
240  CallInfoHeap* m_heap;
241  CallInfoF m_f;
242  CallInfoArgs m_args;
243  };
244 
245  void* vp = heap->try_allocate(sizeof(call_info), alignof(call_info));
246 
247  if (vp != nullptr) {
248 
249  auto cip = std::construct_at(reinterpret_cast<call_info*>(vp),
250  heap,
251  decay_copy(std::forward<T_Callback>(f)),
252  decay_copy(std::forward_as_tuple(args...)));
253 
254  __ASSERT_NO_MSG(cip != nullptr);
255 
256  auto tid = k_thread_create(
257  td.native_handle(),
258  tstack.data(),
259  tstack.size(),
260  &callback_helper<call_info>,
261  reinterpret_cast<void*>(cip),
262  nullptr,
263  nullptr,
264  attr.native_prio(),
265  attr.native_options(),
266  attr.native_delay());
267 
268  m_tid = thread_id(tid);
269  }
270  }
std::decay< T >::type decay_copy(T &&v) noexcept
Definition: thread.hpp:139

References zpp::decay_copy(), and zpp::base_heap< T_Heap >::try_allocate().

◆ thread() [4/7]

template<typename T_Callback , typename T_CallbackArg , std::enable_if_t< std::is_nothrow_invocable_v< T_Callback, T_CallbackArg >, bool > = true>
constexpr zpp::thread::thread ( thread_data td,
thread_stack &&  tstack,
const thread_attr attr,
T_Callback  f,
T_CallbackArg  arg 
)
inlineconstexprnoexcept

Creates a object which represents a new Zephyr thread.

Parameters
tcbThe TCB to use
attrThe creation attributes to use
fThe thread entry point
argsThe arguments to pass to f

Definition at line 284 of file thread.hpp.

290  {
291  using func_t = void (*)(T_CallbackArg) noexcept;
292 
293  static_assert(sizeof(T_CallbackArg) <= sizeof(void*));
294  static_assert(std::is_invocable_v<T_Callback, T_CallbackArg>);
295  static_assert(std::is_nothrow_invocable_v<T_Callback, T_CallbackArg>);
296  static_assert(std::is_invocable_v<func_t, T_CallbackArg>);
297  static_assert(std::is_nothrow_invocable_v<func_t, T_CallbackArg>);
298  static_assert(sizeof(func_t) <= sizeof(void*));
299  static_assert(alignof(T_CallbackArg) <= alignof(void*));
300  static_assert(std::is_trivial_v<T_CallbackArg>);
301 
302  func_t fp = f;
303 
304  void* arg_vp{};
305  std::construct_at(reinterpret_cast<T_CallbackArg*>(&arg_vp), arg);
306 
307  auto tid = k_thread_create(
308  td.native_handle(),
309  tstack.data(),
310  tstack.size(),
311  &callback_helper<func_t, T_CallbackArg>,
312  reinterpret_cast<void*>(fp),
313  arg_vp,
314  nullptr,
315  attr.native_prio(),
316  attr.native_options(),
317  attr.native_delay());
318 
319  m_tid = thread_id(tid);
320  }

◆ thread() [5/7]

template<typename T_Callback , std::enable_if_t< std::is_nothrow_invocable_v< T_Callback >, bool > = true>
constexpr zpp::thread::thread ( thread_data td,
thread_stack &&  tstack,
const thread_attr attr,
T_Callback  f 
)
inlineconstexprnoexcept

Creates a object which represents a new Zephyr thread.

Parameters
tcbThe TCB to use
attrThe creation attributes to use
fThe thread entry point
argsThe arguments to pass to f

Definition at line 334 of file thread.hpp.

339  {
340  using func_t = void (*)() noexcept;
341 
342  static_assert(std::is_invocable_v<T_Callback>);
343  static_assert(std::is_nothrow_invocable_v<T_Callback>);
344  static_assert(std::is_invocable_v<func_t>);
345  static_assert(std::is_nothrow_invocable_v<func_t>);
346  static_assert(sizeof(func_t) <= sizeof(void*));
347 
348  func_t fp = f;
349 
350  auto tid = k_thread_create(
351  td.native_handle(),
352  tstack.data(),
353  tstack.size(),
354  &callback_helper_void<func_t>,
355  reinterpret_cast<void*>(fp),
356  nullptr,
357  nullptr,
358  attr.native_prio(),
359  attr.native_options(),
360  attr.native_delay());
361 
362  m_tid = thread_id(tid);
363  }

◆ thread() [6/7]

constexpr zpp::thread::thread ( thread &&  other)
inlineconstexprnoexcept

Move constructor.

Parameters
otherthe thread to move to this thread object, after the move other will not manage the thread anymore

Definition at line 372 of file thread.hpp.

373  : m_tid(other.m_tid)
374  {
375  other.m_tid = thread_id::any();
376  }
constexpr static thread_id any() noexcept
Create an ID with value K_ANY.
Definition: thread_id.hpp:57

References zpp::thread_id::any().

◆ ~thread()

zpp::thread::~thread ( )
inlinenoexcept

Destructor, will abort the thread.

Definition at line 395 of file thread.hpp.

396  {
397  if (m_tid) {
398  k_thread_abort(m_tid.native_handle());
399  }
400  }
constexpr auto native_handle() const noexcept
Get the Zephyr native ID value.
Definition: thread_id.hpp:67

◆ thread() [7/7]

zpp::thread::thread ( const thread )
delete

Member Function Documentation

◆ abort()

auto zpp::thread::abort ( )
inlinenoexcept

abort the thread this object mamages.

Definition at line 452 of file thread.hpp.

453  {
454  result<void, error_code> res(error_result(error_code::k_inval));
455 
456  if (m_tid) {
457  k_thread_abort(m_tid.native_handle());
458  m_tid = thread_id::any();
459  res.assign_value();
460  }
461 
462  return res;
463  }
@ k_inval
Invalid argument.

References zpp::thread_id::any(), zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.

◆ detach()

constexpr void zpp::thread::detach ( )
inlineconstexprnoexcept

Detach this object from the thread.

Definition at line 414 of file thread.hpp.

415  {
416  m_tid = thread_id::any();
417  }

References zpp::thread_id::any().

◆ join()

auto zpp::thread::join ( )
inlinenoexcept

join the thread this object mamages.

Definition at line 483 of file thread.hpp.

484  {
485  result<void, error_code> res(error_result(error_code::k_inval));
486 
487  if (m_tid) {
488  auto rc = k_thread_join(m_tid.native_handle(), K_FOREVER);
489  if (rc == 0) {
490  res.assign_value();
491  } else {
492  res.assign_error(to_error_code(-rc));
493  }
494  }
495 
496  return res;
497  }
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::k_inval, and zpp::to_error_code().

Referenced by ZTEST().

◆ name()

auto zpp::thread::name ( ) const
inlinenoexcept

Get name of the thread this object mamages.

Returns
The thread name or nullptr

Definition at line 573 of file thread.hpp.

574  {
575  result<const char*, error_code> res(error_result(error_code::k_inval));
576 
577  if (m_tid) {
578  auto rc = k_thread_name_get(m_tid.native_handle());
579  if (rc == nullptr) {
580  res.assign_error(error_code::k_notsup); // TODO
581  } else {
582  res.assign_value(rc);
583  }
584  }
585 
586  return res;
587  }
@ k_notsup
Unsupported value.

References zpp::result< T_Ok, T_Error >::assign_error(), zpp::result< T_Ok, T_Error >::assign_value(), zpp::k_inval, and zpp::k_notsup.

◆ operator bool()

constexpr zpp::thread::operator bool ( ) const
inlineexplicitconstexprnoexcept

check if this object manages a thread

Returns
true if this object manages a thread

Definition at line 407 of file thread.hpp.

407  {
408  return !!m_tid;
409  }

◆ operator=() [1/2]

thread& zpp::thread::operator= ( const thread )
delete

◆ operator=() [2/2]

constexpr thread& zpp::thread::operator= ( thread &&  other)
inlineconstexprnoexcept

Move assignment operator.

Parameters
otherthe thread to move to this thread object, after the move other will not manage the thread anymore

Definition at line 384 of file thread.hpp.

385  {
386  m_tid = other.m_tid;
387  other.m_tid = thread_id::any();
388 
389  return *this;
390  }

References zpp::thread_id::any().

◆ priority()

auto zpp::thread::priority ( )
inlinenoexcept

Get priority of the thread this object mamages.

Returns
Thread priority

Definition at line 519 of file thread.hpp.

520  {
521  result<thread_prio, error_code> res(error_result(error_code::k_inval));
522 
523  if (m_tid) {
524  res = thread_prio( k_thread_priority_get(m_tid.native_handle()) );
525  }
526 
527  return res;
528  }

References zpp::k_inval.

◆ resume()

auto zpp::thread::resume ( )
inlinenoexcept

resume the thread this object mamages.

Definition at line 468 of file thread.hpp.

469  {
470  result<void, error_code> res(error_result(error_code::k_inval));
471 
472  if (m_tid) {
473  k_thread_resume(m_tid.native_handle());
474  res.assign_value();
475  }
476 
477  return res;
478  }

References zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.

◆ set_name()

auto zpp::thread::set_name ( const char *  name)
inlinenoexcept

Set name of the thread this object mamages.

Parameters
nameThe new thread name

Definition at line 552 of file thread.hpp.

553  {
554  result<void, error_code> res(error_result(error_code::k_inval));
555 
556  if (m_tid) {
557  auto rc = k_thread_name_set(m_tid.native_handle(), name);
558  if (rc == 0) {
559  res.assign_value();
560  } else {
561  res.assign_error(to_error_code(-rc));
562  }
563  }
564 
565  return res;
566  }
auto name() const noexcept
Get name of the thread this object mamages.
Definition: thread.hpp:573

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

◆ set_priority()

auto zpp::thread::set_priority ( thread_prio  prio) const
inlinenoexcept

Set priority of the thread this object mamages.

Parameters
prioThe new thread priority

Definition at line 535 of file thread.hpp.

536  {
537  result<void, error_code> res(error_result(error_code::k_inval));
538 
539  if (m_tid) {
540  k_thread_priority_set(m_tid.native_handle(), prio.native_value());
541  res.assign_value();
542  }
543 
544  return res;
545  }

References zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.

◆ start()

auto zpp::thread::start ( )
inlinenoexcept

start the thread this object mamages.

Definition at line 437 of file thread.hpp.

438  {
439  result<void, error_code> res(error_result(error_code::k_inval));
440 
441  if (m_tid) {
442  k_thread_start(m_tid.native_handle());
443  res.assign_value();
444  }
445 
446  return res;
447  }

References zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.

◆ suspend()

auto zpp::thread::suspend ( )
inlinenoexcept

suspend the thread this object mamages.

Definition at line 502 of file thread.hpp.

503  {
504  result<void, error_code> res(error_result(error_code::k_inval));
505 
506  if (m_tid) {
507  k_thread_suspend(m_tid.native_handle());
508  res.assign_value();
509  }
510 
511  return res;
512  }

References zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.

◆ wakeup()

auto zpp::thread::wakeup ( )
inlinenoexcept

wakeup the thread this object mamages.

Definition at line 422 of file thread.hpp.

423  {
424  result<void, error_code> res(error_result(error_code::k_inval));
425 
426  if (m_tid) {
427  k_wakeup(m_tid.native_handle());
428  res.assign_value();
429  }
430 
431  return res;
432  }

References zpp::result< T_Ok, T_Error >::assign_value(), and zpp::k_inval.


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