zpp
Zephyr C++20 Framework
thread_prio.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_PRIO_HPP
8 #define ZPP_INCLUDE_ZPP_THREAD_PRIO_HPP
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/__assert.h>
12 
13 namespace zpp {
14 
18 class thread_prio {
19 public:
23  constexpr thread_prio() noexcept
24  {
25  }
26 
32  constexpr explicit thread_prio(int prio) noexcept
33  : m_prio(prio)
34  {
35  if (m_prio < -CONFIG_NUM_COOP_PRIORITIES) {
36  m_prio = -CONFIG_NUM_COOP_PRIORITIES;
37  } else if (m_prio > (CONFIG_NUM_PREEMPT_PRIORITIES - 1)) {
38  m_prio = CONFIG_NUM_PREEMPT_PRIORITIES - 1;
39  }
40 
41  __ASSERT_NO_MSG(m_prio == prio);
42  }
43 
49  constexpr int native_value() const noexcept
50  {
51  return m_prio;
52  }
53 
59  static constexpr thread_prio highest_coop() noexcept
60  {
61  return thread_prio(-CONFIG_NUM_COOP_PRIORITIES);
62  }
63 
69  static constexpr thread_prio lowest_coop() noexcept
70  {
71  return thread_prio(-1);
72  }
73 
79  static constexpr thread_prio highest_preempt() noexcept
80  {
81  return thread_prio(0);
82  }
83 
89  static constexpr thread_prio lowest_preempt() noexcept
90  {
91  return thread_prio(CONFIG_NUM_PREEMPT_PRIORITIES - 1);
92  }
93 
102  static constexpr thread_prio min_coop_numeric() noexcept
103  {
104  return highest_coop();
105  }
106 
115  static constexpr thread_prio max_coop_numeric() noexcept
116  {
117  return lowest_coop();
118  }
119 
128  static constexpr thread_prio min_preempt_numeric() noexcept
129  {
130  return highest_preempt();
131  }
132 
141  static constexpr thread_prio max_preempt_numeric() noexcept
142  {
143  return lowest_preempt();
144  }
145 
151  static constexpr thread_prio min_numeric() noexcept
152  {
153  return min_coop_numeric();
154  }
155 
161  static constexpr thread_prio max_numeric() noexcept
162  {
163  return max_preempt_numeric();
164  }
165 
166 
174  static constexpr thread_prio coop(int prio) noexcept
175  {
176  return thread_prio( lowest_coop().native_value() - prio );
177  }
178 
186  static constexpr thread_prio preempt(int prio) noexcept
187  {
188  return (prio >= CONFIG_NUM_PREEMPT_PRIORITIES) ?
189  highest_preempt() :
191  }
192 private:
193  int m_prio { 0 };
194 };
195 
204 constexpr bool
205 operator==(const thread_prio& lhs, const thread_prio& rhs) noexcept
206 {
207  return (lhs.native_value() == rhs.native_value());
208 }
209 
218 constexpr bool
219 operator!=(const thread_prio& lhs, const thread_prio& rhs) noexcept
220 {
221  return (lhs.native_value() != rhs.native_value());
222 }
223 
232 constexpr thread_prio
233 operator-(thread_prio lhs, int rhs) noexcept
234 {
235  return thread_prio(lhs.native_value() + rhs);
236 }
237 
246 constexpr thread_prio
247 operator+(thread_prio lhs, int rhs) noexcept
248 {
249  return thread_prio(lhs.native_value() - rhs);
250 }
251 
257 inline void print_arg(thread_prio prio) noexcept
258 {
259  printk("%d", prio.native_value());
260 }
261 
262 } // namespace zpp
263 
264 #endif // ZPP_INCLUDE_ZPP_THREAD_PRIO_HPP
Thread priority.
Definition: thread_prio.hpp:18
static constexpr thread_prio coop(int prio) noexcept
Create a cooperative priority value.
constexpr thread_prio() noexcept
Default constructor initializing priority to zero.
Definition: thread_prio.hpp:23
static constexpr thread_prio max_preempt_numeric() noexcept
Get the minium preempt numeric value.
static constexpr thread_prio min_numeric() noexcept
Get the minium numeric value.
static constexpr thread_prio highest_coop() noexcept
Get the most prior cooperative priority.
Definition: thread_prio.hpp:59
static constexpr thread_prio max_numeric() noexcept
Get the maxium numeric value.
static constexpr thread_prio lowest_preempt() noexcept
Get the least prior preemptive priority.
Definition: thread_prio.hpp:89
static constexpr thread_prio min_coop_numeric() noexcept
Get the minium coop numeric value.
static constexpr thread_prio min_preempt_numeric() noexcept
Get the minium preempt numeric value.
constexpr thread_prio(int prio) noexcept
Constructor initializing priority to prio.
Definition: thread_prio.hpp:32
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
constexpr int native_value() const noexcept
Get the Zephyr native priority value.
Definition: thread_prio.hpp:49
static constexpr thread_prio lowest_coop() noexcept
Get the least prior cooperative priority.
Definition: thread_prio.hpp:69
static constexpr thread_prio highest_preempt() noexcept
Get the most prior preemptive priority.
Definition: thread_prio.hpp:79
static constexpr thread_prio max_coop_numeric() noexcept
Get the minium coop numeric value.
constexpr thread_prio operator+(thread_prio lhs, int rhs) noexcept
Increase priority by rhs.
constexpr bool operator==(const result< T_Ok, T_Error > &lhs, bool rhs) noexcept
compare result with bool
Definition: result.hpp:655
constexpr thread_prio operator-(thread_prio lhs, int rhs) noexcept
Reduce priority by rhs.
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