zpp
Zephyr C++20 Framework
main.cpp
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 #include <zephyr/ztest.h>
8 #include <zephyr/sys/__assert.h>
9 
10 #include<string.h>
11 
12 #include <zephyr/kernel.h>
13 
14 #include <zpp.hpp>
15 
16 ZTEST_SUITE(test_zpp_condition_variable, NULL, NULL, NULL, NULL, NULL);
17 
18 namespace {
19 
20 ZPP_THREAD_STACK_DEFINE(tstack, 1024);
22 
23 
24 bool ready = false;
25 bool processed = false;
26 
27 char data[128] = {};
28 
29 zpp::mutex m;
30 
31 K_MUTEX_DEFINE(g_mutex);
32 
33 zpp::mutex_ref m_ref(&g_mutex);
34 
36 
37 K_CONDVAR_DEFINE(g_condvar);
38 
39 zpp::condition_variable_ref cv_ref(&g_condvar);
40 
41 } // namespace
42 
43 ZTEST(test_zpp_condition_variable, test_condition_variable_cmp)
44 {
45  bool res;
46 
47  res = cv == cv_ref;
48  zassert_false(res, "unable to compare condition_variable == condition_variable_ref\n");
49 
50  res = cv != cv_ref;
51  zassert_true(res, "unable to compare condition_variable != condition_variable_ref\n");
52 
53  res = cv_ref == &g_condvar;
54  zassert_true(res, "unable to compare condition_variable_ref == k_condvar*\n");
55 
56  res = cv_ref != &g_condvar;
57  zassert_false(res, "unable to compare condition_variable_ref != k_condvar*\n");
58 
59  res = cv == &g_condvar;
60  zassert_false(res, "unable to compare condition_variable == k_condvar*\n");
61 
62  res = cv != &g_condvar;
63  zassert_true(res, "unable to compare condition_variable != k_condvar*\n");
64 }
65 
66 ZTEST(test_zpp_condition_variable, test_condition_variable)
67 {
68  using namespace zpp;
69  using namespace std::chrono;
70 
71  const thread_attr attr(
76  );
77 
78  auto t = thread(
79  tcb, tstack(), attr,
80  []() noexcept {
81  // Wait until main() sends data
82  {
84 
85  auto rc = cv.wait(m, []{return ready;});
86  __ASSERT_NO_MSG(rc == true);
87 
88  // after the wait, we own the lock.
89  print("Worker thread is processing data\n");
90 
91  strcat(data, " after processing");
92 
93  // Send data back to main()
94  processed = true;
95  print("Worker thread signals data processing completed\n");
96  }
97 
98  auto rc = cv.notify_one();
99  __ASSERT_NO_MSG(rc == true);
100  });
101 
102  strcpy(data, "Example data");
103  // send data to the worker thread
104  {
106  ready = true;
107  print("main() signals data ready for processing\n");
108  }
109  auto rc = cv.notify_one();
110  __ASSERT_NO_MSG(rc == true);
111 
112  // wait for the worker
113  {
115  rc = cv.wait(m, []{return processed;});
116  __ASSERT_NO_MSG(rc == true);
117  }
118  print("Back in main(), data = {}\n", data);
119 
120  rc = t.join();
121  __ASSERT_NO_MSG(rc == true);
122 }
A class using a reference to another native condition variable or zpp::condition_variable.
A condition variable class.
lock_guard using zpp::mutex as a lock.
Definition: lock_guard.hpp:23
A recursive mutex class borrowing the native mutex.
Definition: mutex.hpp:191
A recursive mutex class.
Definition: mutex.hpp:150
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
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)