zpp
Zephyr C++20 Framework
main.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2017 Wind River Systems, Inc.
3 // Copyright (c) 2019 Erwin Rol <erwin@erwinrol.com>
4 //
5 // SPDX-License-Identifier: Apache-2.0
6 //
7 
8 #include <zephyr/ztest.h>
9 
10 #include <zephyr/kernel.h>
11 
12 #include <zpp/poll.hpp>
13 #include <zpp/sem.hpp>
14 #include <zpp/fifo.hpp>
15 
16 
17 ZTEST_SUITE(zpp_poll_tests, NULL, NULL, NULL, NULL, NULL);
18 
19 namespace {
20 
21 #define SIGNAL_RESULT 0x1ee7d00d
22 #define FIFO_MSG_VALUE 0xdeadbeef
23 
24 ZPP_THREAD_STACK_DEFINE(test_thread_stack, 1024);
25 zpp::thread_data test_thread;
26 
27 
28 struct fifo_msg {
29  void* fifo_reserved{};
30  uint32_t msg { FIFO_MSG_VALUE };
31 };
32 
33 fifo_msg wait_msg;
34 
35 zpp::sem wait_sem;
36 zpp::fifo<fifo_msg> wait_fifo;
37 zpp::poll_signal wait_signal;
38 
39 zpp::poll_event_set wait_events {
40  wait_sem,
41  wait_fifo,
42  wait_signal,
43  nullptr
44 };
45 
46 } // namespace
47 
48 ZTEST(zpp_poll_tests, test_poll_wait)
49 {
50  using namespace std::chrono;
51 
52  const zpp::thread_prio main_low_prio{ 10 };
53 
54  auto old_prio = zpp::this_thread::get_priority();
55 
56  //
57  // Wait for 3 non-ready events to become ready from a higher priority
58  // thread.
59  //
60  zpp::this_thread::set_priority(main_low_prio);
61 
62  zpp::thread_attr attr(
63  main_low_prio + 1,
67 
68  auto poll_helper = [](bool do_fifo) noexcept {
70 
71  wait_sem++;
72 
73  if (do_fifo) {
74  wait_fifo.push_back(&wait_msg);
75  }
76 
77  wait_signal.raise(SIGNAL_RESULT);
78  };
79 
80  auto t = zpp::thread(test_thread, test_thread_stack(), attr, poll_helper, true);
81 
82  auto rc = wait_events.try_poll_for(1s);
83 
85 
86  zassert_equal(rc, true, "");
87 
88  zassert_equal(wait_events[0].is_ready(), true, "");
89  zassert_equal(wait_sem.try_take(), true, "");
90 
91  zassert_equal(wait_events[1].is_ready(), true, "");
92  auto msg_ptr = wait_fifo.try_pop_front();
93  zassert_not_null(msg_ptr, "");
94  zassert_equal(msg_ptr, &wait_msg, "");
95  zassert_equal(msg_ptr->msg, FIFO_MSG_VALUE, "");
96 
97  zassert_equal(wait_events[2].is_ready(), true, "");
98  zassert_equal(wait_signal.check().value_or(-1), SIGNAL_RESULT, "");
99 
100  zassert_equal(wait_events[3].is_ready(), false, "");
101 
102  //
103  // verify events are not ready anymore
104  //
105  wait_events[0].reset();
106  wait_events[1].reset();
107  wait_events[2].reset();
108  wait_events[3].reset();
109  wait_signal.reset();
110 
111  zassert_equal(wait_events.try_poll_for(1s), false, "");
112 
113  zassert_equal(wait_events[0].is_ready(), false, "");
114  zassert_equal(wait_events[1].is_ready(), false, "");
115  zassert_equal(wait_events[2].is_ready(), false, "");
116  zassert_equal(wait_events[3].is_ready(), false, "");
117 
118  //
119  // Wait for 2 out of 3 non-ready events to become ready from a higher
120  // priority thread.
121  //
122  zpp::this_thread::set_priority(main_low_prio);
123 
124  attr.set(old_prio + 1);
125  t = zpp::thread(test_thread, test_thread_stack(), attr, poll_helper, false);
126 
127  rc = wait_events.try_poll_for(1s);
128 
130 
131  zassert_equal(rc, true, "");
132 
133  zassert_equal(wait_events[0].is_ready(), true, "");
134  zassert_equal(wait_sem.try_take(), true, "");
135 
136  zassert_equal(wait_events[1].is_ready(), false, "");
137  msg_ptr = wait_fifo.try_pop_front();
138  zassert_is_null(msg_ptr, "");
139 
140  zassert_equal(wait_events[2].is_ready(), true, "");
141  zassert_equal(wait_signal.check().value_or(-1), SIGNAL_RESULT, "");
142 
143  zassert_equal(wait_events[3].is_ready(), false, "");
144 
145 
146  //
147  // Wait for each event to be ready from a lower priority thread, one at
148  // a time.
149  //
150  wait_events[0].reset();
151  wait_events[1].reset();
152  wait_events[2].reset();
153  wait_events[3].reset();
154  wait_signal.reset();
155 
156  attr.set(old_prio - 1);
157  t = zpp::thread(test_thread, test_thread_stack(), attr, poll_helper, true);
158 
159  //
160  // semaphore
161  //
162  rc = wait_events.try_poll_for(1s);
163 
164  zassert_equal(rc, true, "");
165 
166  zassert_equal(wait_events[0].is_ready(), true, "");
167  zassert_equal(wait_sem.try_take(), true, "");
168 
169  zassert_equal(wait_events[1].is_ready(), false, "");
170  msg_ptr = wait_fifo.try_pop_front();
171  zassert_is_null(msg_ptr, "");
172 
173  zassert_equal(wait_events[2].is_ready(), false, "");
174 
175  wait_events[0].reset();
176 
177  //
178  // fifo
179  //
180  rc = wait_events.try_poll_for(1s);
181 
182  zassert_equal(rc, true, "");
183 
184  zassert_equal(wait_events[0].is_ready(), false, "");
185  zassert_equal(wait_sem.try_take(), false, "");
186 
187  zassert_equal(wait_events[1].is_ready(), true, "");
188  msg_ptr = wait_fifo.try_pop_front();
189  zassert_not_null(msg_ptr, "");
190 
191  zassert_equal(wait_events[2].is_ready(), false, "");
192 
193  wait_events[1].reset();
194 
195  //
196  // poll signal
197  //
198  rc = wait_events.try_poll_for(1s);
199 
200  zassert_equal(rc, true, "");
201 
202  zassert_equal(wait_events[0].is_ready(), false, "");
203  zassert_equal(wait_sem.try_take(), false, "");
204 
205  zassert_equal(wait_events[1].is_ready(), false, "");
206  msg_ptr = wait_fifo.try_pop_front();
207  zassert_is_null(msg_ptr, "");
208 
209  zassert_equal(wait_events[2].is_ready(), true, "");
210  zassert_equal(wait_signal.check().value_or(-1), SIGNAL_RESULT, "");
211 
212  wait_events[2].reset();
213  wait_signal.reset();
214 }
fifo that manages a k_fifo object
Definition: fifo.hpp:175
A set of poll events.
class owning a k_poll_signal
A counting semaphore class.
Definition: sem.hpp:196
Thread creation attributes.
Definition: thread_attr.hpp:35
constexpr void set(T_FirstArg &&first, T_Args &&... rest) noexcept
Definition: thread_attr.hpp:59
thread_data holds the stack and thread control block memory
Definition: thread_data.hpp:19
Thread priority.
Definition: thread_prio.hpp:18
The class thread repecents a single Zephyr thread.
Definition: thread.hpp:147
void set_priority(thread_prio prio) noexcept
Set the current thread's priority.
Definition: thread.hpp:132
auto sleep_for(const std::chrono::duration< T_Rep, T_Period > &sleep_duration)
Suspend the current thread for a specified time duration.
Definition: thread.hpp:77
thread_prio get_priority() noexcept
Get the current thread's priority.
Definition: thread.hpp:122
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition: main.cpp:23
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)
#define FIFO_MSG_VALUE
Definition: main.cpp:22
#define SIGNAL_RESULT
Definition: main.cpp:21
#define ZPP_THREAD_STACK_DEFINE(sym, size)