zpp
Zephyr C++20 Framework
main.cpp
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 #include <zephyr/ztest.h>
8 
9 #include <zpp/thread.hpp>
10 #include <zpp/sem.hpp>
11 #include <zpp/fmt.hpp>
12 
13 #include <chrono>
14 
15 
16 ZTEST_SUITE(zpp_thread_tests, NULL, NULL, NULL, NULL, NULL);
17 
18 namespace {
19 
20 ZPP_THREAD_STACK_DEFINE(tstack, 1024);
22 
23 zpp::heap<1024> theap;
24 
25 } // namespace
26 
27 ZTEST(zpp_thread_tests, test_thread_creation)
28 {
29  using namespace zpp;
30  using namespace std::chrono;
31 
32  const thread_attr attr(
37  );
38 
39  int dummy=42;
40  sem done;
41 
42  auto t = thread(
43  tcb, tstack(), attr, &theap,
44  [&dummy, &done]() noexcept {
45  print("Hello from thread tid={}\n",
47 
48  print("dummy = {}\n", (void*)&dummy);
49  print("done = {}\n", (void*)&done);
50 
51  zassert_true(dummy == 42, "dummy not 42\n");
52 
53  done++;
54  });
55 
56  // wait until the thread does done++
57  done--;
58 
59  auto rc = t.join();
60  zassert_true(rc == true, "join failed");
61 
62  print("Hello from main tid={}\n", this_thread::get_id());
63 }
64 
65 ZTEST(zpp_thread_tests, test_thread_creation_void)
66 {
67  using namespace zpp;
68  using namespace std::chrono;
69 
70  const thread_attr attr(
75  );
76 
77  auto t = thread(
78  tcb, tstack(), attr,
79  []() noexcept {
80  print("Hello from thread tid={}\n",
82  });
83 
84  auto rc = t.join();
85  zassert_true(rc == true, "join failed");
86 
87  print("Hello from main tid={}\n", this_thread::get_id());
88 }
89 
90 ZTEST(zpp_thread_tests, test_thread_creation_pointer)
91 {
92  using namespace zpp;
93  using namespace std::chrono;
94 
95  const thread_attr attr(
100  );
101 
102  struct S {
103  S() noexcept {
104  print("S() {} {} {}",
105  (void*)this, a, b);
106  }
107 
108  S(const S& other) noexcept : a(other.a), b(other.b) {
109  print("S(&{} {} {}) {} {} {}",
110  (void*)&other, other.a, other.b,
111  (void*)this, a, b);
112  }
113  ~S() noexcept {
114  print("~S() {} {} {}",
115  (void*)this, a, b);
116  }
117 
118  int a{};
119  int b{};
120  };
121 
122  S s;
123 
124  auto t = thread(
125  tcb, tstack(), attr,
126  [](S* s) noexcept {
127  print("Hello from thread tid={} s->a={} s->b={}\n",
129  s->a, s->b);
130 
131  s->a = 21;
132  s->b = 43;
133 
134  }, &s);
135 
136  auto rc = t.join();
137  zassert_true(rc == true, "join failed");
138 
139  zassert_true(s.a == 21, "s.a != 21\n");
140  zassert_true(s.b == 43, "s.a != 43\n");
141 
142  print("Hello from main tid={}\n", this_thread::get_id());
143 }
144 
145 
146 ZTEST(zpp_thread_tests, test_thread_creation_params)
147 {
148  using namespace zpp;
149  using namespace std::chrono;
150 
151  const thread_attr attr(
156  );
157 
158  struct S {
159  S() noexcept {
160  print("S() {} {} {}",
161  (void*)this, a, b);
162  }
163 
164  S(const S& other) noexcept : a(other.a), b(other.b) {
165  print("S(&{} {} {}) {} {} {}",
166  (void*)&other, other.a, other.b,
167  (void*)this, a, b);
168  }
169  ~S() noexcept {
170  print("~S() {} {} {}",
171  (void*)this, a, b);
172  }
173 
174  int a{};
175  int b{};
176  };
177 
178  sem done;
179 
180  S s;
181  int a = 12;
182  int b = 34;
183 
184  auto t = thread(
185  tcb, tstack(), attr, &theap,
186  [&done](S s, int a, int b) noexcept {
187  print("Hello from thread tid={} s.a={} s.b={} a={} b={}\n",
189  s.a, s.b, a, b);
190 
191  s.a = a;
192  s.b = b;
193 
194  done++;
195  }, s, a, b);
196 
197  // wait until the thread does done++
198  done--;
199 
200  auto rc = t.join();
201  zassert_true(rc == true, "join failed");
202 
203  print("Hello from main tid={}\n", this_thread::get_id());
204 }
heap class
Definition: heap.hpp:159
A counting semaphore class.
Definition: sem.hpp:196
Thread creation attributes.
Definition: thread_attr.hpp:35
thread_data holds the stack and thread control block memory
Definition: thread_data.hpp:19
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
The class thread repecents a single Zephyr thread.
Definition: thread.hpp:147
auto join() noexcept
join the thread this object mamages.
Definition: thread.hpp:483
auto get_id() noexcept
Get the thread ID of the current thread.
Definition: thread.hpp:42
void print(const char *fmt, T_Args &&... args) noexcept
simple typesafe print function
Definition: fmt.hpp:157
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition: main.cpp:23
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)
#define ZPP_THREAD_STACK_DEFINE(sym, size)