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

Go to the source code of this file.

Macros

#define SIGNAL_RESULT   0x1ee7d00d
 
#define FIFO_MSG_VALUE   0xdeadbeef
 

Functions

 ZTEST_SUITE (zpp_poll_tests, NULL, NULL, NULL, NULL, NULL)
 
 ZTEST (zpp_poll_tests, test_poll_wait)
 

Macro Definition Documentation

◆ FIFO_MSG_VALUE

#define FIFO_MSG_VALUE   0xdeadbeef

Definition at line 22 of file main.cpp.

◆ SIGNAL_RESULT

#define SIGNAL_RESULT   0x1ee7d00d

Definition at line 21 of file main.cpp.

Function Documentation

◆ ZTEST()

ZTEST ( zpp_poll_tests  ,
test_poll_wait   
)

Definition at line 48 of file main.cpp.

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 }
Thread creation attributes.
Definition: thread_attr.hpp:35
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
#define FIFO_MSG_VALUE
Definition: main.cpp:22
#define SIGNAL_RESULT
Definition: main.cpp:21

References FIFO_MSG_VALUE, zpp::this_thread::get_priority(), zpp::no, zpp::thread_attr::set(), zpp::this_thread::set_priority(), SIGNAL_RESULT, zpp::this_thread::sleep_for(), and zpp::yes.

◆ ZTEST_SUITE()

ZTEST_SUITE ( zpp_poll_tests  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)