summaryrefslogtreecommitdiffstats
path: root/testsuite/condvar01/test_main.c
blob: 53c981521d89ffbb50df549663167aec9d3537db (plain) (blame)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <machine/rtems-bsd-kernel-space.h>

#include <rtems/bsd/sys/param.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <rtems/libcsupport.h>
#include <rtems.h>

#define TEST_NAME "LIBBSD CONDVAR 1"

#define PRIO_MASTER 3

#define EVENT_LOCK RTEMS_EVENT_0

#define EVENT_UNLOCK RTEMS_EVENT_1

#define EVENT_WAIT RTEMS_EVENT_2

#define EVENT_WAIT_SIG RTEMS_EVENT_3

#define EVENT_WAIT_UNLOCK RTEMS_EVENT_4

#define EVENT_TIMEDWAIT RTEMS_EVENT_5

#define EVENT_TIMEDWAIT_SIG RTEMS_EVENT_6

#define WORKER_COUNT 2

typedef struct {
	struct mtx mtx;
	struct cv cv;
	int rv;
	int timo;
	rtems_id worker_task[WORKER_COUNT];
	bool done[WORKER_COUNT];
} test_context;

static test_context test_instance;

static const rtems_task_priority prio_worker[2] = { 2, 1 };

static void
set_self_prio(rtems_task_priority prio)
{
	rtems_status_code sc;

	sc = rtems_task_set_priority(RTEMS_SELF, prio, &prio);
	assert(sc == RTEMS_SUCCESSFUL);
}

static void
worker_task(rtems_task_argument index)
{
	test_context *ctx = &test_instance;
	struct mtx *mtx = &ctx->mtx;
	struct cv *cv = &ctx->cv;

	while (true) {
		rtems_status_code sc;
		rtems_event_set events;

		sc = rtems_event_receive(
			RTEMS_ALL_EVENTS,
			RTEMS_EVENT_ANY | RTEMS_WAIT,
			RTEMS_NO_TIMEOUT,
			&events
		);
		assert(sc == RTEMS_SUCCESSFUL);

		if ((events & EVENT_LOCK) != 0) {
			mtx_lock(mtx);
			ctx->done[index] = true;
		}

		if ((events & EVENT_UNLOCK) != 0) {
			mtx_unlock(mtx);
			ctx->done[index] = true;
		}

		if ((events & EVENT_WAIT) != 0) {
			cv_wait(cv, mtx);
			ctx->done[index] = true;
		}

		if ((events & EVENT_WAIT_SIG) != 0) {
			int rv = cv_wait_sig(cv, mtx);
			assert(rv == 0);
			ctx->done[index] = true;
		}

		if ((events & EVENT_WAIT_UNLOCK) != 0) {
			cv_wait_unlock(cv, mtx);
			ctx->done[index] = true;
		}

		if ((events & EVENT_TIMEDWAIT) != 0) {
			ctx->rv = cv_timedwait(cv, mtx, ctx->timo);
			ctx->done[index] = true;
		}

		if ((events & EVENT_TIMEDWAIT_SIG) != 0) {
			ctx->rv = cv_timedwait_sig(cv, mtx, ctx->timo);
			ctx->done[index] = true;
		}
	}
}

static void
send_events(test_context *ctx, rtems_event_set events, size_t index)
{
	rtems_status_code sc;

	sc = rtems_event_send(ctx->worker_task[index], events);
	assert(sc == RTEMS_SUCCESSFUL);
}

static void
start_worker(test_context *ctx)
{
	size_t i;

	for (i = 0; i < WORKER_COUNT; ++i) {
		rtems_status_code sc;

		sc = rtems_task_create(
			rtems_build_name('W', 'O', 'R', 'K'),
			prio_worker[i],
			RTEMS_MINIMUM_STACK_SIZE,
			RTEMS_DEFAULT_MODES,
			RTEMS_FLOATING_POINT,
			&ctx->worker_task[i]
		);
		assert(sc == RTEMS_SUCCESSFUL);

		sc = rtems_task_start(
			ctx->worker_task[i],
			worker_task,
			i
		);
		assert(sc == RTEMS_SUCCESSFUL);
	}
}

static void
delete_worker(test_context *ctx)
{
	size_t i;

	for (i = 0; i < WORKER_COUNT; ++i) {
		rtems_status_code sc;

		sc = rtems_task_delete(ctx->worker_task[i]);
		assert(sc == RTEMS_SUCCESSFUL);
	}
}

static void
test_cv_wait_signal(test_context *ctx, rtems_event_set event, const char *name)
{
	struct mtx *mtx = &ctx->mtx;
	struct cv *cv = &ctx->cv;
	size_t low = 0;
	size_t high = 1;

	printf("test cv %s signal\n", name);

	mtx_lock(mtx);
	mtx_unlock(mtx);

	ctx->timo = 0;

	ctx->done[low] = false;
	send_events(ctx, EVENT_LOCK, low);
	assert(ctx->done[low]);

	ctx->done[high] = false;
	send_events(ctx, EVENT_LOCK, high);
	assert(!ctx->done[high]);

	ctx->done[low] = false;
	send_events(ctx, event, low);
	assert(!ctx->done[low]);
	assert(ctx->done[high]);

	ctx->done[low] = false;
	ctx->done[high] = false;
	send_events(ctx, event, high);
	assert(!ctx->done[low]);
	assert(!ctx->done[high]);

	cv_signal(cv);
	assert(!ctx->done[low]);
	assert(ctx->done[high]);

	if (event != EVENT_WAIT_UNLOCK) {
		cv_signal(cv);
		assert(!ctx->done[low]);

		ctx->done[high] = false;
		send_events(ctx, EVENT_UNLOCK, high);
		assert(ctx->done[high]);
		assert(ctx->done[low]);

		ctx->done[low] = false;
		send_events(ctx, EVENT_UNLOCK, low);
		assert(ctx->done[low]);
	} else {
		cv_signal(cv);
		assert(ctx->done[low]);
	}

	mtx_lock(mtx);
	mtx_unlock(mtx);
}

static void
test_cv_wait_broadcast(test_context *ctx, rtems_event_set event, const char *name)
{
	struct mtx *mtx = &ctx->mtx;
	struct cv *cv = &ctx->cv;
	size_t low = 0;
	size_t high = 1;

	printf("test cv %s broadcast\n", name);

	mtx_lock(mtx);
	mtx_unlock(mtx);

	ctx->timo = 0;

	ctx->done[low] = false;
	send_events(ctx, EVENT_LOCK, low);
	assert(ctx->done[low]);

	ctx->done[high] = false;
	send_events(ctx, EVENT_LOCK, high);
	assert(!ctx->done[high]);

	ctx->done[low] = false;
	send_events(ctx, event, low);
	assert(!ctx->done[low]);
	assert(ctx->done[high]);

	ctx->done[low] = false;
	ctx->done[high] = false;
	send_events(ctx, event, high);
	assert(!ctx->done[low]);
	assert(!ctx->done[high]);

	if (event != EVENT_WAIT_UNLOCK) {
		cv_broadcast(cv);
		assert(!ctx->done[low]);

		ctx->done[high] = false;
		send_events(ctx, EVENT_UNLOCK, high);
		assert(ctx->done[high]);
		assert(ctx->done[low]);

		ctx->done[low] = false;
		send_events(ctx, EVENT_UNLOCK, low);
		assert(ctx->done[low]);
	} else {
		cv_broadcast(cv);
		assert(ctx->done[low]);
		assert(ctx->done[high]);
	}

	mtx_lock(mtx);
	mtx_unlock(mtx);
}

static void
test_cv_wait(test_context *ctx, rtems_event_set event, const char *name)
{
	test_cv_wait_signal(ctx, event, name);
	test_cv_wait_broadcast(ctx, event, name);
}

static void
test_cv_wait_timeout(test_context *ctx)
{
	size_t index = 0;
	rtems_status_code sc;

	puts("test cv wait timeout");

	ctx->timo = 2;

	ctx->done[index] = false;
	send_events(ctx, EVENT_LOCK, index);
	assert(ctx->done[index]);

	ctx->done[index] = false;
	ctx->rv = 0;
	send_events(ctx, EVENT_TIMEDWAIT, index);
	assert(!ctx->done[index]);

	sc = rtems_task_wake_after(ctx->timo);
	assert(sc == RTEMS_SUCCESSFUL);
	assert(ctx->done[index]);
	assert(ctx->rv == EWOULDBLOCK);

	ctx->done[index] = false;
	ctx->rv = 0;
	send_events(ctx, EVENT_TIMEDWAIT_SIG, index);
	assert(!ctx->done[index]);

	sc = rtems_task_wake_after(ctx->timo);
	assert(sc == RTEMS_SUCCESSFUL);
	assert(ctx->done[index]);
	assert(ctx->rv == EWOULDBLOCK);

	ctx->done[index] = false;
	send_events(ctx, EVENT_UNLOCK, index);
	assert(ctx->done[index]);
}

static void
alloc_basic_resources(void)
{
	curthread;
}

static void
test_main(void)
{
	test_context *ctx = &test_instance;
	rtems_resource_snapshot snapshot;

	alloc_basic_resources();

	rtems_resource_snapshot_take(&snapshot);

	cv_init(&ctx->cv, "test");
	assert(strcmp(cv_wmesg(&ctx->cv), "test") == 0);

	set_self_prio(PRIO_MASTER);
	start_worker(ctx);

	test_cv_wait(ctx, EVENT_WAIT, "wait");
	test_cv_wait(ctx, EVENT_WAIT_SIG, "wait sig");
	test_cv_wait(ctx, EVENT_WAIT_UNLOCK, "wait unlock");
	test_cv_wait(ctx, EVENT_TIMEDWAIT, "timed wait");
	test_cv_wait(ctx, EVENT_TIMEDWAIT_SIG, "timed wait sig");

	test_cv_wait_timeout(ctx);

	delete_worker(ctx);
	cv_destroy(&ctx->cv);

	assert(rtems_resource_snapshot_check(&snapshot));

	exit(0);
}

#include <rtems/bsd/test/default-init.h>