-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.c
358 lines (308 loc) · 7.95 KB
/
check.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* Copyright (c) 2013-2015, Drew Thoreson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <errno.h>
#include <check.h>
#include "ufiber.h"
#define NR_FIBERS 30
#ifndef ck_assert_ptr_eq
#define ck_assert_ptr_eq(a, b) ck_assert((void*)a == (void*)b)
#endif
static int counter;
static ufiber_t other;
static ufiber_mutex_t mutex;
static ufiber_barrier_t barrier;
static ufiber_rwlock_t rwlock;
static ufiber_cond_t cond;
static inline void ck_ufiber_create(ufiber_t *fiber, unsigned long flags,
void *(*start_routine)(void*), void *arg)
{
if (ufiber_create(fiber, flags, start_routine, arg))
ck_abort_msg("ufiber_create() failed");
}
static inline void ck_ufiber_join(ufiber_t fiber, void **retval)
{
if (ufiber_join(fiber, retval))
ck_abort_msg("ufiber_join() failed");
}
static void *uf_create(void *data)
{
ck_assert_ptr_eq(data, &counter);
*((int*)data) = 1;
return &counter;
}
START_TEST(test_ufiber_create)
{
ufiber_t fid;
void *retval;
counter = 0;
ck_ufiber_create(&fid, 0, uf_create, &counter);
ck_ufiber_join(fid, &retval);
ck_assert_int_ne(counter, 0);
ck_assert_ptr_eq(retval, &counter);
}
END_TEST
static void *uf_join(void *data)
{
for (int i = 0; i < 3; i++)
ufiber_yield();
counter = 1;
return &counter;
}
START_TEST(test_ufiber_join)
{
ufiber_t fid;
void *retval;
counter = 0;
ck_ufiber_create(&fid, 0, uf_join, NULL);
ck_ufiber_join(fid, &retval);
ck_assert_int_eq(counter, 1);
ck_assert_ptr_eq(retval, &counter);
}
END_TEST
static void *uf_self(void *data)
{
other = ufiber_self();
return NULL;
}
START_TEST(test_ufiber_self)
{
ufiber_t fid;
ufiber_t self = ufiber_self();
ck_ufiber_create(&fid, 0, uf_self, NULL);
ck_ufiber_join(fid, NULL);
ck_assert(other != self);
ck_assert(fid == other);
}
END_TEST
static void *uf_yield(void *data)
{
counter++;
return NULL;
}
START_TEST(test_ufiber_yield)
{
ufiber_t fid[NR_FIBERS];
for (int i = 0; i < NR_FIBERS; i++)
ck_ufiber_create(&fid[i], 0, uf_yield, NULL);
counter = 0;
for (int i = 0; i < NR_FIBERS; i++)
ufiber_yield();
ck_assert_int_eq(counter, NR_FIBERS);
}
END_TEST
struct s_yield_to {
int ord;
ufiber_t next;
};
static void *uf_yield_to(void *data)
{
struct s_yield_to *s = data;
ck_assert_int_eq(s->ord, counter);
counter++;
ufiber_yield_to(s->next);
return NULL;
}
START_TEST(test_ufiber_yield_to)
{
ufiber_t fid[NR_FIBERS + 1];
struct s_yield_to s[NR_FIBERS];
for (int i = 0; i < NR_FIBERS; i++)
ck_ufiber_create(&fid[i], 0, uf_yield_to, &s[i]);
fid[NR_FIBERS] = ufiber_self();
for (int i = 0; i < NR_FIBERS; i++) {
s[i].ord = i;
s[i].next = fid[i + 1];
}
counter = 0;
ufiber_yield_to(fid[0]);
ck_assert_int_eq(counter, NR_FIBERS);
}
END_TEST
static void *uf_exit(void *data)
{
ufiber_exit(NULL);
ck_abort_msg("running after ufiber_exit()");
return NULL;
}
START_TEST(test_ufiber_exit)
{
ufiber_t fid;
ck_ufiber_create(&fid, 0, uf_exit, NULL);
ufiber_join(fid, NULL);
}
END_TEST
static void *uf_mutex(void *data)
{
ufiber_mutex_lock(&mutex);
for (int i = 0; i < NR_FIBERS; i++) {
counter = *((int*)data);
ufiber_yield();
ck_assert_int_eq(*((int*)data), counter);
}
ufiber_mutex_unlock(&mutex);
return NULL;
}
START_TEST(test_ufiber_mutex)
{
int uid[NR_FIBERS];
ufiber_t fid[NR_FIBERS];
ufiber_mutex_init(&mutex);
for (int i = 0; i < NR_FIBERS; i++) {
uid[i] = i;
ck_ufiber_create(&fid[i], 0, uf_mutex, &uid[i]);
}
for (int i = 0; i < NR_FIBERS; i++) {
ck_assert_int_eq(ufiber_join(fid[i], NULL), 0);
}
ck_assert_int_eq(ufiber_mutex_trylock(&mutex), 0);
ck_assert_int_eq(ufiber_mutex_trylock(&mutex), EBUSY);
}
END_TEST
static void *uf_barrier(void *data)
{
ufiber_barrier_wait(&barrier);
(*((int*)data))++;
return NULL;
}
START_TEST(test_ufiber_barrier)
{
int counts[NR_FIBERS];
ufiber_t fid[NR_FIBERS];
ufiber_barrier_init(&barrier, NR_FIBERS+1);
for (int i = 0; i < NR_FIBERS; i++) {
counts[i] = 0;
ck_ufiber_create(&fid[i], 0, uf_barrier, &counts[i]);
}
for (int i = 0; i < NR_FIBERS; i++) {
ufiber_yield_to(fid[i]);
ck_assert_int_eq(counts[i], 0);
}
ufiber_barrier_wait(&barrier);
for (int i = 0; i < NR_FIBERS; i++) {
ufiber_join(fid[i], NULL);
ck_assert_int_eq(counts[i], 1);
}
}
END_TEST
static void *uf_rwlock_reader(void *data)
{
ufiber_rwlock_rdlock(&rwlock);
ck_assert_int_eq(counter, 0);
ufiber_barrier_wait(&barrier);
ck_assert_int_eq(counter, 0);
ufiber_rwlock_unlock(&rwlock);
return NULL;
}
static void *uf_rwlock_writer(void *data)
{
ufiber_rwlock_wrlock(&rwlock);
counter = 1;
ufiber_rwlock_unlock(&rwlock);
return NULL;
}
START_TEST(test_ufiber_rwlock)
{
ufiber_t writer;
ufiber_t fid[NR_FIBERS];
ufiber_barrier_init(&barrier, NR_FIBERS+1);
ufiber_rwlock_init(&rwlock);
for (int i = 0; i < NR_FIBERS; i++) {
ck_ufiber_create(&fid[i], 0, uf_rwlock_reader, NULL);
ufiber_yield_to(fid[i]);
}
ck_ufiber_create(&writer, 0, uf_rwlock_writer, NULL);
ufiber_yield_to(writer);
ufiber_barrier_wait(&barrier);
ck_ufiber_join(writer, NULL);
for (int i = 0; i < NR_FIBERS; i++)
ck_ufiber_join(fid[i], NULL);
}
END_TEST
static void *uf_cond(void *data)
{
ufiber_cond_wait(&cond, NULL);
counter++;
return NULL;
}
START_TEST(test_ufiber_cond)
{
ufiber_t fid[NR_FIBERS];
counter = 0;
ufiber_cond_init(&cond);
for (int i = 0; i < NR_FIBERS; i++) {
ck_ufiber_create(&fid[i], 0, uf_cond, NULL);
ufiber_yield_to(fid[i]);
}
ck_assert_int_eq(counter, 0);
ufiber_cond_signal(&cond);
for (int i = 0; i < NR_FIBERS; i++)
ufiber_yield();
ck_assert_int_eq(counter, 1);
ufiber_cond_broadcast(&cond);
for (int i = 0; i < NR_FIBERS; i++)
ufiber_join(fid[i], NULL);
ck_assert_int_eq(counter, NR_FIBERS);
}
END_TEST
START_TEST(test_deadlock)
{
ufiber_mutex_init(&mutex);
ufiber_mutex_lock(&mutex);
ck_assert(ufiber_mutex_lock(&mutex) == EDEADLK);
}
END_TEST
int main(void)
{
int failed;
TCase *tc;
Suite *s;
SRunner *sr;
ufiber_init();
s = suite_create("ufibers");
tc = tcase_create("core");
tcase_add_test(tc, test_ufiber_create);
tcase_add_test(tc, test_ufiber_join);
tcase_add_test(tc, test_ufiber_self);
tcase_add_test(tc, test_ufiber_yield);
tcase_add_test(tc, test_ufiber_yield_to);
tcase_add_test(tc, test_ufiber_exit);
tcase_add_test(tc, test_ufiber_mutex);
tcase_add_test(tc, test_ufiber_barrier);
tcase_add_test(tc, test_ufiber_rwlock);
tcase_add_test(tc, test_ufiber_cond);
tcase_add_test(tc, test_deadlock);
suite_add_tcase(s, tc);
sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
failed = srunner_ntests_failed(sr);
srunner_free(sr);
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}