zpp
Zephyr C++20 Framework
thread_attr.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_ATTR_HPP
8 #define ZPP_INCLUDE_ZPP_THREAD_ATTR_HPP
9 
10 #include <zpp/thread_prio.hpp>
11 
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 
15 #include <chrono>
16 #include <utility>
17 
18 namespace zpp {
19 
20 enum class thread_suspend { yes, no };
21 enum class thread_essential { yes, no };
22 enum class thread_user { yes, no };
23 enum class thread_inherit_perms { yes, no };
24 enum class thread_fp_regs { yes, no };
25 enum class thread_sse_regs { yes, no };
26 
27 template<class T_Rep, class T_Period>
29  std::chrono::duration<T_Rep, T_Period> t;
30 };
31 
35 class thread_attr {
36 public:
37  thread_attr(const thread_attr&) noexcept = default;
38  thread_attr(thread_attr&&) noexcept = default;
39  thread_attr& operator=(const thread_attr&) noexcept = default;
40  thread_attr& operator=(thread_attr&&) noexcept = default;
41 
52  template <class ...T_Args>
53  constexpr explicit thread_attr(T_Args&&... args) noexcept
54  {
55  set(std::forward<T_Args>(args)...);
56  }
57 
58  template <class T_FirstArg, class ...T_Args>
59  constexpr void set(T_FirstArg&& first, T_Args&&... rest) noexcept
60  {
61  set(std::forward<T_FirstArg>(first));
62  set(std::forward<T_Args>(rest)...);
63  }
64 
65  constexpr void set() const noexcept
66  {
67  }
68 
74  constexpr void set(thread_prio prio) noexcept
75  {
76  m_prio = prio;
77  }
78 
84  template<class T_Rep, class T_Period>
85  constexpr void
87  {
88  using namespace std::chrono;
89 
90  //auto ms = duration_cast<milliseconds>(delay.t);
91 
92  if (delay.t <= decltype(delay.t)::zero()) {
93  m_delay = K_NO_WAIT;
94  } else {
95  m_delay = to_tick(delay.t);
96  }
97  }
98 
104  constexpr void set(thread_suspend v) noexcept
105  {
106  if (v == thread_suspend::yes) {
107  m_delay = K_FOREVER;
108  } else {
109  if (K_TIMEOUT_EQ(m_delay, K_FOREVER)) {
110  m_delay = K_NO_WAIT;
111  }
112  }
113  }
114 
120  constexpr void set(thread_essential v) noexcept
121  {
122  if (v == thread_essential::yes) {
123  m_options |= K_ESSENTIAL;
124  } else {
125  m_options &= ~K_ESSENTIAL;
126  }
127  }
128 
134  constexpr void set(thread_user v) noexcept
135  {
136  if (v == thread_user::yes) {
137  m_options |= K_USER;
138  } else {
139  m_options &= ~K_USER;
140  }
141  }
142 
148  constexpr void set(thread_inherit_perms v) noexcept
149  {
150  if (v == thread_inherit_perms::yes) {
151  m_options |= K_INHERIT_PERMS;
152  } else {
153  m_options &= ~K_INHERIT_PERMS;
154  }
155  }
156 
162  constexpr void set(thread_fp_regs v) noexcept
163  {
164 #ifdef K_FP_REGS
165  if (v == thread_fp_regs::yes) {
166  m_options |= K_FP_REGS;
167  } else {
168  m_options &= ~K_FP_REGS;
169  }
170 #endif
171  }
172 
178  constexpr void set(thread_sse_regs v) noexcept
179  {
180 #ifdef K_SSE_REGS
181  if (v == thread_sse_regs::yes) {
182  m_options |= K_SSE_REGS;
183  } else {
184  m_options &= ~K_SSE_REGS;
185  }
186 #endif
187  }
188 
194  constexpr auto native_delay() const noexcept
195  {
196  return m_delay;
197  }
198 
204  constexpr auto native_prio() const noexcept
205  {
206  return m_prio.native_value();
207  }
208 
214  constexpr auto native_options() const noexcept
215  {
216  return m_options;
217  }
218 private:
219  thread_prio m_prio{ };
220  uint32_t m_options{ 0 };
221  k_timeout_t m_delay{ K_NO_WAIT };
222 };
223 
224 } // namespace zpp
225 
226 #endif // ZPP_INCLUDE_ZPP_THREAD_ATTR_HPP
Thread creation attributes.
Definition: thread_attr.hpp:35
constexpr auto native_delay() const noexcept
get the Zephyr native delay value
constexpr void set(thread_sse_regs v) noexcept
Set the thread SSE regs flag.
constexpr void set(T_FirstArg &&first, T_Args &&... rest) noexcept
Definition: thread_attr.hpp:59
constexpr void set(thread_suspend v) noexcept
Set the thread start suspended flag.
constexpr void set(thread_fp_regs v) noexcept
Set the thread FP regs flag.
constexpr auto native_prio() const noexcept
get the Zephyr native priority value
constexpr void set() const noexcept
Definition: thread_attr.hpp:65
constexpr auto native_options() const noexcept
get the Zephyr native options value
constexpr void set(thread_user v) noexcept
Set the thread user flag.
thread_attr(const thread_attr &) noexcept=default
constexpr void set(thread_essential v) noexcept
Set the thread essential flag.
thread_attr(thread_attr &&) noexcept=default
constexpr void set(const thread_start_delay< T_Rep, T_Period > &delay) noexcept
Set the thread start delay.
Definition: thread_attr.hpp:86
constexpr void set(thread_inherit_perms v) noexcept
Set the thread inherit permisions flag.
constexpr void set(thread_prio prio) noexcept
Set the thread priority.
Definition: thread_attr.hpp:74
Thread priority.
Definition: thread_prio.hpp:18
constexpr int native_value() const noexcept
Get the Zephyr native priority value.
Definition: thread_prio.hpp:49
thread_essential
Definition: thread_attr.hpp:21
thread_fp_regs
Definition: thread_attr.hpp:24
thread_suspend
Definition: thread_attr.hpp:20
thread_user
Definition: thread_attr.hpp:22
thread_inherit_perms
Definition: thread_attr.hpp:23
thread_sse_regs
Definition: thread_attr.hpp:25
constexpr k_ticks_t to_tick(const std::chrono::duration< T_Rep, T_Period > &d) noexcept
convert a duration to tick
Definition: clock.hpp:72
std::chrono::duration< T_Rep, T_Period > t
Definition: thread_attr.hpp:29