zpp
Zephyr C++20 Framework
thread_id.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #ifndef ZPP_INCLUDE_ZPP_THREAD_ID_HPP
8 #define ZPP_INCLUDE_ZPP_THREAD_ID_HPP
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
12 
13 namespace zpp {
14 
18 class thread_id {
19 public:
20  thread_id(const thread_id&) noexcept = default;
21  thread_id(thread_id&&) noexcept = default;
22  thread_id& operator=(const thread_id&) noexcept = default;
23  thread_id& operator=(thread_id&&) noexcept = default;
24 
28  constexpr thread_id() noexcept
29  {
30  }
31 
37  constexpr explicit thread_id(k_tid_t tid) noexcept
38  : m_tid(tid)
39  {
40  }
41 
47  constexpr explicit operator bool() const noexcept
48  {
49  return m_tid != K_ANY;
50  }
51 
57  constexpr static thread_id any() noexcept
58  {
59  return thread_id(K_ANY);
60  }
61 
67  constexpr auto native_handle() const noexcept
68  {
69  return m_tid;
70  }
71 private:
72  k_tid_t m_tid { K_ANY };
73 };
74 
83 constexpr bool
84 operator==(const thread_id& lhs, const thread_id& rhs) noexcept
85 {
86  return (lhs.native_handle() == rhs.native_handle());
87 }
88 
97 constexpr bool
98 operator!=(const thread_id& lhs, const thread_id& rhs) noexcept
99 {
100  return (lhs.native_handle() != rhs.native_handle());
101 }
102 
108 inline void
109 print_arg(thread_id id) noexcept
110 {
111  printk("%p", id.native_handle());
112 }
113 
114 } // namespace zpp
115 
116 #endif // ZPP_INCLUDE_ZPP_THREAD_ID_HPP
Thead ID.
Definition: thread_id.hpp:18
thread_id(thread_id &&) noexcept=default
constexpr static thread_id any() noexcept
Create an ID with value K_ANY.
Definition: thread_id.hpp:57
constexpr thread_id() noexcept
Default constructor inializing ID to K_ANY.
Definition: thread_id.hpp:28
thread_id(const thread_id &) noexcept=default
constexpr thread_id(k_tid_t tid) noexcept
Constructor inializing ID to tid.
Definition: thread_id.hpp:37
constexpr auto native_handle() const noexcept
Get the Zephyr native ID value.
Definition: thread_id.hpp:67
constexpr bool operator==(const result< T_Ok, T_Error > &lhs, bool rhs) noexcept
compare result with bool
Definition: result.hpp:655
void print_arg(thread_id id) noexcept
Helper to output the ID with zpp::print("{}", id)
Definition: thread_id.hpp:109
constexpr bool operator!=(const result< T_Ok, T_Error > &lhs, bool rhs) noexcept
compare result with bool
Definition: result.hpp:681