summaryrefslogtreecommitdiffstats
path: root/testsuite/sleep01/test_main.c
blob: 929c738d488e6b197d15f6ac8204fefb62e8b1cc (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
/*
 * Copyright (c) 2013 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-config.h>

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

#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

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

#define TEST_NAME "LIBBSD SLEEP 1"

#define SLAVE_COUNT 3

#define PRIO_HIGH 1

#define PRIO_LOW 5

/*
 * This priority assignment ensures that we can test the priority thread queue
 * order.
 */
static const rtems_task_priority slave_prios[SLAVE_COUNT] = { 4, 3, 2 };

static rtems_id slave_ids[SLAVE_COUNT];

static int slave_counters[SLAVE_COUNT];

static int channel;

static int counter;

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
slave_task(rtems_task_argument slave)
{
	while (true) {
		int timo = 0;
		int priority = 0;
		const char *wmesg = "slave_task";
		int eno;

		++counter;
		slave_counters[slave] = counter;

		printf("slave[%" PRIuPTR "]: %i\n", slave, counter);

		eno = tsleep(&channel, priority, wmesg, timo);
		assert(eno == 0);
	}
}

static void
start_slaves(void)
{
	size_t i;

	set_self_prio(PRIO_HIGH);

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

		sc = rtems_task_create(
			rtems_build_name('S', 'L', 'A', 'V'),
			slave_prios[i],
			RTEMS_MINIMUM_STACK_SIZE,
			RTEMS_DEFAULT_MODES,
			RTEMS_DEFAULT_ATTRIBUTES,
			&slave_ids[i]
		);
		assert(sc == RTEMS_SUCCESSFUL);

		sc = rtems_task_start(slave_ids[i], slave_task, i);
		assert(sc == RTEMS_SUCCESSFUL);
	}

	set_self_prio(PRIO_LOW);
}

static void
test_timeouts(void)
{
	int eno;
	struct mtx mtx;
	int priority = 0;
	const char *wmesg;
	int timo = 2;

	puts("test timeouts");

	mtx_init(&mtx, "test timeouts: mtx", NULL, MTX_DEF);
	mtx_lock(&mtx);
	wmesg = "test timeouts: msleep";
	eno = msleep(&channel, &mtx, priority, wmesg, timo);
	assert(eno == EWOULDBLOCK);
	mtx_unlock(&mtx);
	mtx_destroy(&mtx);

	mtx_init(&mtx, "test timeouts: mtx spin", NULL, MTX_SPIN);
	mtx_lock_spin(&mtx);
	wmesg = "test timeouts: msleep_spin";
	eno = msleep_spin(&channel, &mtx, wmesg, timo);
	assert(eno == EWOULDBLOCK);
	mtx_unlock_spin(&mtx);
	mtx_destroy(&mtx);

	wmesg = "test timeouts: pause";
	pause(wmesg, timo);

	wmesg = "test timeouts: tsleep";
	eno = tsleep(&channel, priority, wmesg, timo);
	assert(eno == EWOULDBLOCK);
}

static void
test_wakeup_without_waiter(void)
{
	int some_channel;

	puts("test wakeup without waiter");

	wakeup_one(&some_channel);
	wakeup(&some_channel);
}

static void
test_wakeup_one(void)
{
	puts("test wakeup one");

	assert(slave_counters[0] == 3);
	assert(slave_counters[1] == 2);
	assert(slave_counters[2] == 1);

	wakeup_one(&channel);

	assert(slave_counters[0] == 3);
	assert(slave_counters[1] == 2);
	assert(slave_counters[2] == 4);

	wakeup_one(&channel);

	assert(slave_counters[0] == 3);
	assert(slave_counters[1] == 2);
	assert(slave_counters[2] == 5);
}

static void
test_wakeup(void)
{
	puts("test wakeup");

	assert(slave_counters[0] == 3);
	assert(slave_counters[1] == 2);
	assert(slave_counters[2] == 5);

	wakeup(&channel);

	assert(slave_counters[0] == 8);
	assert(slave_counters[1] == 7);
	assert(slave_counters[2] == 6);
}

static void
alloc_basic_resources(void)
{
	rtems_status_code sc;

	curthread;

	sc = rtems_task_wake_after(2);
	assert(sc == RTEMS_SUCCESSFUL);
}

static void
test_main(void)
{
	rtems_resource_snapshot snapshot;

	alloc_basic_resources();
	start_slaves();

	rtems_resource_snapshot_take(&snapshot);

	test_timeouts();
	test_wakeup_without_waiter();
	test_wakeup_one();
	test_wakeup();

	assert(rtems_resource_snapshot_check(&snapshot));

	rtems_stack_checker_report_usage_with_plugin(NULL,
	    rtems_printf_plugin);

	puts("*** END OF " TEST_NAME " TEST ***");
	exit(0);
}

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