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

Go to the source code of this file.

Functions

 ZTEST_SUITE (test_zpp_condition_variable, NULL, NULL, NULL, NULL, NULL)
 
 ZTEST (test_zpp_condition_variable, test_condition_variable_cmp)
 
 ZTEST (test_zpp_condition_variable, test_condition_variable)
 

Function Documentation

◆ ZTEST() [1/2]

ZTEST ( test_zpp_condition_variable  ,
test_condition_variable   
)

Definition at line 66 of file main.cpp.

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 }
lock_guard using zpp::mutex as a lock.
Definition: lock_guard.hpp:23
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
void print(const char *fmt, T_Args &&... args) noexcept
simple typesafe print function
Definition: fmt.hpp:157

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

◆ ZTEST() [2/2]

ZTEST ( test_zpp_condition_variable  ,
test_condition_variable_cmp   
)

Definition at line 43 of file main.cpp.

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 }

◆ ZTEST_SUITE()

ZTEST_SUITE ( test_zpp_condition_variable  ,
NULL  ,
NULL  ,
NULL  ,
NULL  ,
NULL   
)