-
-
Notifications
You must be signed in to change notification settings - Fork 495
/
synch_test.c
216 lines (192 loc) · 4.46 KB
/
synch_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// Copyright 2024 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt). A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
#include <nng/nng.h>
#include <nuts.h>
// Notify tests for verifying condvars.
struct notifyarg {
int did;
nng_duration when;
nng_mtx *mx;
nng_cv *cv;
bool wake;
bool fini;
};
void
notifyafter(void *a)
{
struct notifyarg *na = a;
nng_msleep(na->when);
nng_mtx_lock(na->mx);
na->did = 1;
nng_cv_wake(na->cv);
nng_mtx_unlock(na->mx);
}
static void
test_mutex_lock_unlock(void)
{
nng_mtx *mtx;
NUTS_PASS(nng_mtx_alloc(&mtx));
for (int i = 1; i < 100; i ) {
nng_mtx_lock(mtx);
nng_mtx_unlock(mtx);
}
nng_mtx_free(mtx);
}
static void
test_mutex_block(void)
{
nng_thread *thr;
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
arg.did = 0;
arg.when = 0;
nng_mtx_lock(arg.mx);
NUTS_PASS(nng_thread_create(&thr, notifyafter, &arg));
nng_thread_set_name(thr, "notify thread");
nng_msleep(10);
NUTS_TRUE(arg.did == 0);
nng_mtx_unlock(arg.mx);
nng_msleep(10);
nng_mtx_lock(arg.mx);
while (!arg.did) {
nng_cv_wait(arg.cv);
}
NUTS_TRUE(arg.did != 0);
nng_mtx_unlock(arg.mx);
nng_thread_destroy(thr);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
static void
test_cv_wake(void)
{
nng_thread *thr;
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
arg.did = 0;
arg.when = 10;
NUTS_PASS(nng_thread_create(&thr, notifyafter, &arg));
nng_thread_set_name(thr, "notify thread");
nng_mtx_lock(arg.mx);
if (!arg.did) {
nng_cv_wait(arg.cv);
}
nng_mtx_unlock(arg.mx);
nng_thread_destroy(thr);
NUTS_TRUE(arg.did == 1);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
static void
waiter(void *arg)
{
struct notifyarg *na = arg;
nng_mtx_lock(na->mx);
while (!na->wake && !na->fini) {
nng_cv_wait(na->cv);
}
if ((!na->fini) && na->wake) {
na->did ;
}
nng_mtx_unlock(na->mx);
}
static void
test_cv_wake_only_one(void)
{
nng_thread *thr1;
nng_thread *thr2;
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
arg.did = 0;
arg.when = 10;
NUTS_PASS(nng_thread_create(&thr1, waiter, &arg));
nng_thread_set_name(thr1, "one");
NUTS_PASS(nng_thread_create(&thr2, waiter, &arg));
nng_thread_set_name(thr2, "two");
nng_msleep(200);
nng_mtx_lock(arg.mx);
arg.wake = true;
nng_cv_wake1(arg.cv);
nng_mtx_unlock(arg.mx);
nng_msleep(200);
nng_mtx_lock(arg.mx);
NUTS_TRUE(arg.did == 1);
NUTS_MSG("arg.did was %d", arg.did);
arg.fini = true;
nng_cv_wake(arg.cv);
nng_mtx_unlock(arg.mx);
nng_thread_destroy(thr1);
nng_thread_destroy(thr2);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
static void
test_cv_timeout(void)
{
nng_thread *thr;
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
arg.did = 0;
arg.when = 200;
NUTS_PASS(nng_thread_create(&thr, notifyafter, &arg));
nng_thread_set_name(thr, "notify thread");
nng_mtx_lock(arg.mx);
if (!arg.did) {
nng_cv_until(arg.cv, nng_clock() 10);
}
NUTS_TRUE(arg.did == 0);
nng_mtx_unlock(arg.mx);
nng_thread_destroy(thr);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
static void
test_cv_poll(void)
{
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
nng_mtx_lock(arg.mx);
NUTS_FAIL(nng_cv_until(arg.cv, 0), NNG_EAGAIN);
nng_mtx_unlock(arg.mx);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
static void
test_cv_timeout_no_thread(void)
{
struct notifyarg arg = { 0 };
NUTS_PASS(nng_mtx_alloc(&arg.mx));
NUTS_PASS(nng_cv_alloc(&arg.cv, arg.mx));
arg.did = 0;
arg.when = 1;
nng_mtx_lock(arg.mx);
if (!arg.did) {
nng_cv_until(arg.cv, nng_clock() 10);
}
NUTS_TRUE(arg.did == 0);
nng_mtx_unlock(arg.mx);
nng_cv_free(arg.cv);
nng_mtx_free(arg.mx);
}
NUTS_TESTS = {
{ "mutex lock unlock", test_mutex_lock_unlock },
{ "mutex lock block", test_mutex_block },
{ "cv wake", test_cv_wake },
{ "cv timeout", test_cv_timeout },
{ "cv poll", test_cv_poll },
{ "cv timeout no thread", test_cv_timeout_no_thread },
{ "cv wake only one", test_cv_wake_only_one },
{ NULL, NULL },
};