zpp
Zephyr C++20 Framework
thread_stack.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2021 Erwin Rol <erwin@erwinrol.com>
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 
7 #ifndef ZPP_INCLUDE_ZPP_THREAD_STACK_HPP
8 #define ZPP_INCLUDE_ZPP_THREAD_STACK_HPP
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/arch_interface.h>
12 #include <zephyr/sys/__assert.h>
13 
14 namespace zpp {
15 
21 class thread_stack {
22 public:
23  constexpr thread_stack(const thread_stack&) noexcept = default;
24  constexpr thread_stack& operator=(const thread_stack&) noexcept = default;
25 
26  constexpr thread_stack(k_thread_stack_t* data, size_t size) noexcept
27  : m_data(data)
28  , m_size(size)
29  {
30  __ASSERT_NO_MSG(m_data != nullptr);
31  }
32 
33  constexpr auto size() const noexcept
34  {
35  return m_size;
36  }
37 
38  constexpr auto data() const noexcept
39  {
40  return m_data;
41  }
42 private:
43  k_thread_stack_t* m_data;
44  size_t m_size;
45 public:
46  thread_stack() = delete;
47 };
48 
49 #define ZPP_THREAD_STACK_DEFINE(sym, size) \
50  K_THREAD_STACK_DEFINE(sym##_native,(size)); \
51  consteval auto sym() noexcept { \
52  return zpp::thread_stack(sym##_native, size); \
53  }
54 
55 #define ZPP_THREAD_PINNED_STACK_DEFINE(sym, size) \
56  K_THREAD_PINNED_STACK_DEFINE(sym##_native,(size)); \
57  consteval auto sym() noexcept { \
58  return zpp::thread_stack(sym##_native, size); \
59  }
60 
61 #define ZPP_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
62  K_THREAD_STACK_ARRAY_DEFINE(sym##_native, nmemb, size); \
63  constexpr auto sym(size_t n) noexcept { \
64  return zpp::thread_stack(sym##_native[n], size); \
65  }
66 
67 #define ZPP_THREAD_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
68  K_THREAD_PINNED_STACK_ARRAY_DEFINE(sym##_native, nmemb, size); \
69  constexpr auto sym(size_t n) noexcept { \
70  return zpp::thread_stack(sym##_native[n], size); \
71  }
72 
73 
74 #define ZPP_KERNEL_STACK_DEFINE(sym, size) \
75  K_KERNEL_STACK_DEFINE(sym##_native,(size)); \
76  consteval auto sym() noexcept { \
77  return zpp::thread_stack(sym##_native, size); \
78  }
79 
80 #define ZPP_KERNEL_PINNED_STACK_DEFINE(sym, size) \
81  K_KERNEL_PINNED_STACK_DEFINE(sym##_native,(size)); \
82  consteval auto sym() noexcept { \
83  return zpp::thread_stack(sym##_native, size); \
84  }
85 
86 #define ZPP_KERNEL_STACK_ARRAY_DEFINE(sym, nmemb, size) \
87  K_KERNEL_STACK_ARRAY_DEFINE(sym##_native, nmemb, size); \
88  constexpr auto sym(size_t n) noexcept { \
89  return zpp::thread_stack(sym##_native[n], size); \
90  }
91 
92 #define ZPP_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym, nmemb, size) \
93  K_KERNEL_PINNED_STACK_ARRAY_DEFINE(sym##_native, nmemb, size); \
94  constexpr auto sym(size_t n) noexcept { \
95  return zpp::thread_stack(sym##_native[n], size); \
96  }
97 
98 } // namespace zpp
99 
100 #endif // ZPP_INCLUDE_ZPP_THREAD_STACK_HPP
thread_stack holds the stack and thread control block memory
constexpr thread_stack & operator=(const thread_stack &) noexcept=default
thread_stack()=delete
constexpr thread_stack(const thread_stack &) noexcept=default
constexpr auto data() const noexcept
constexpr auto size() const noexcept
constexpr thread_stack(k_thread_stack_t *data, size_t size) noexcept