zpp
Zephyr C++20 Framework
main.cpp File Reference
#include <zephyr/ztest.h>
#include <zpp/thread.hpp>
#include <zpp/sem.hpp>
#include <zpp/fmt.hpp>
#include <chrono>
Include dependency graph for main.cpp:

Go to the source code of this file.

Functions

 ZTEST_SUITE (zpp_thread_tests, NULL, NULL, NULL, NULL, NULL)
 
 ZTEST (zpp_thread_tests, test_thread_creation)
 
 ZTEST (zpp_thread_tests, test_thread_creation_void)
 
 ZTEST (zpp_thread_tests, test_thread_creation_pointer)
 
 ZTEST (zpp_thread_tests, test_thread_creation_params)
 

Function Documentation

◆ ZTEST() [1/4]

ZTEST ( zpp_thread_tests  ,
test_thread_creation   
)

Definition at line 27 of file main.cpp.

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 }
A counting semaphore class.
Definition: sem.hpp:196
Thread creation attributes.
Definition: thread_attr.hpp:35
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 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

References zpp::this_thread::get_id(), zpp::no, zpp::thread_prio::preempt(), and zpp::print().

◆ ZTEST() [2/4]

ZTEST ( zpp_thread_tests  ,
test_thread_creation_params   
)

Definition at line 146 of file main.cpp.

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 }

References zpp::this_thread::get_id(), zpp::no, zpp::thread_prio::preempt(), and zpp::print().

◆ ZTEST() [3/4]

ZTEST ( zpp_thread_tests  ,
test_thread_creation_pointer   
)

Definition at line 90 of file main.cpp.

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 }
auto join() noexcept
join the thread this object mamages.
Definition: thread.hpp:483

References zpp::this_thread::get_id(), zpp::thread::join(), zpp::no, zpp::thread_prio::preempt(), and zpp::print().

◆ ZTEST() [4/4]

ZTEST ( zpp_thread_tests  ,
test_thread_creation_void   
)

Definition at line 65 of file main.cpp.

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 }

References zpp::this_thread::get_id(), zpp::thread::join(), zpp::no, zpp::thread_prio::preempt(), and zpp::print().

◆ ZTEST_SUITE()

ZTEST_SUITE ( zpp_thread_tests  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)