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);
21
zpp::thread_data
tcb;
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
35
zpp::condition_variable
cv;
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(
72
thread_prio::preempt
(0),
73
thread_inherit_perms::no
,
74
thread_essential::no
,
75
thread_suspend::no
76
);
77
78
auto
t =
thread
(
79
tcb, tstack(), attr,
80
[]() noexcept {
81
// Wait until main() sends data
82
{
83
zpp::lock_guard<zpp::mutex>
lg(m);
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
{
105
zpp::lock_guard<zpp::mutex>
lg(m);
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
{
114
zpp::lock_guard<zpp::mutex>
lg(m);
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
}
zpp::condition_variable_ref
A class using a reference to another native condition variable or zpp::condition_variable.
Definition:
condition_variable.hpp:279
zpp::condition_variable
A condition variable class.
Definition:
condition_variable.hpp:234
zpp::lock_guard
lock_guard using zpp::mutex as a lock.
Definition:
lock_guard.hpp:23
zpp::mutex_ref
A recursive mutex class borrowing the native mutex.
Definition:
mutex.hpp:191
zpp::mutex
A recursive mutex class.
Definition:
mutex.hpp:150
zpp::thread_attr
Thread creation attributes.
Definition:
thread_attr.hpp:35
zpp::thread_data
thread_data holds the stack and thread control block memory
Definition:
thread_data.hpp:19
zpp::thread_prio::preempt
static constexpr thread_prio preempt(int prio) noexcept
Create a preemptive priority value.
Definition:
thread_prio.hpp:186
zpp::thread
The class thread repecents a single Zephyr thread.
Definition:
thread.hpp:147
zpp
Definition:
atomic_bitset.hpp:15
zpp::thread_essential::no
@ no
zpp::print
void print(const char *fmt, T_Args &&... args) noexcept
simple typesafe print function
Definition:
fmt.hpp:157
zpp::thread_suspend::no
@ no
zpp::thread_inherit_perms::no
@ no
ZTEST
ZTEST(zpp_atomic_tests, test_atomic_bitset)
Definition:
main.cpp:23
ZTEST_SUITE
ZTEST_SUITE(zpp_atomic_tests, NULL, NULL, NULL, NULL, NULL)
ZPP_THREAD_STACK_DEFINE
#define ZPP_THREAD_STACK_DEFINE(sym, size)
Definition:
thread_stack.hpp:49
zpp.hpp
tests
condition_variable
src
main.cpp
Generated by
1.9.1