summaryrefslogtreecommitdiff
path: root/tests/lib/libpthread/t_thrd.c
blob: 69a9a27cac2f0787b35464b562c7dc93f5d95fd6 (plain)
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
/*	$NetBSD: t_thrd.c,v 1.1 2019/04/24 11:43:19 kamil Exp $	*/

/*-
 * Copyright (c) 2019 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Kamil Rytarowski.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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 <sys/cdefs.h>
__COPYRIGHT("@(#) Copyright (c) 2019\
 The NetBSD Foundation, inc. All rights reserved.");
__RCSID("$NetBSD: t_thrd.c,v 1.1 2019/04/24 11:43:19 kamil Exp $");

#include <signal.h>
#include <stdbool.h>
#include <threads.h>
#include <time.h>

#include <atf-c.h>

ATF_TC(thrd_create);
ATF_TC_HEAD(thrd_create, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_create(3)");
}

#define TC_ADDON 5

static int
tcr_func(void *arg)
{
	int a;

	a = (int)(intptr_t)arg;

	return a + TC_ADDON;
}

ATF_TC_BODY(thrd_create, tc)
{
	thrd_t t;
	const int a = 5;
	int b;
	void *v;

	v = (void *)(intptr_t)a;

	ATF_REQUIRE_EQ(thrd_create(&t, tcr_func, v), thrd_success);
	ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success);
	ATF_REQUIRE_EQ(a + TC_ADDON, b);
}

ATF_TC(thrd_current);
ATF_TC_HEAD(thrd_current, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_current(3)");
}

static int
tcur_func(void *arg __unused)
{

	return 0;
}

ATF_TC_BODY(thrd_current, tc)
{
	thrd_t s, t;

	s = thrd_current();

	ATF_REQUIRE(thrd_equal(s, s) != 0);
	ATF_REQUIRE_EQ(thrd_create(&t, tcur_func, NULL), thrd_success);
	ATF_REQUIRE(thrd_equal(t, s) == 0);
	ATF_REQUIRE(thrd_equal(s, t) == 0);
	ATF_REQUIRE(thrd_equal(t, t) != 0);

	ATF_REQUIRE_EQ(thrd_join(t, NULL), thrd_success);
}

ATF_TC(thrd_detach);
ATF_TC_HEAD(thrd_detach, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_detach(3)");
}

static int
tdet_func(void *arg __unused)
{

	return 0;
}

ATF_TC_BODY(thrd_detach, tc)
{
	thrd_t t;

	ATF_REQUIRE_EQ(thrd_create(&t, tdet_func, NULL), thrd_success);
	ATF_REQUIRE_EQ(thrd_detach(t), thrd_success);
}

ATF_TC(thrd_exit);
ATF_TC_HEAD(thrd_exit, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_exit(3)");
}

static void
tex_func2(void)
{

	thrd_exit(1);
}

static int
tex_func(void *arg __unused)
{

	tex_func2();

	return -1;
}

ATF_TC_BODY(thrd_exit, tc)
{
	thrd_t t;
	int b = 0;

	ATF_REQUIRE_EQ(thrd_create(&t, tex_func, NULL), thrd_success);
	ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success);
	ATF_REQUIRE_EQ(b, 1);
}

ATF_TC(thrd_sleep);
ATF_TC_HEAD(thrd_sleep, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_sleep(3)");
}

static int alarmed;

static void
alarm_handler(int signum)
{

	ATF_REQUIRE_EQ(signum, SIGALRM);
	++alarmed;
}

ATF_TC_BODY(thrd_sleep, tc)
{
	struct timespec start, stop, diff;
	struct timespec ts, rem;
	struct timespec zero;
	struct sigaction sa;
	struct itimerval timer;

	zero.tv_sec = 0;
	zero.tv_nsec = 0;

	ts.tv_sec = 1;
	ts.tv_nsec = -1;
	ATF_REQUIRE_EQ(!thrd_sleep(&ts, NULL), 0);

	ts.tv_sec = 0;
	ts.tv_nsec = 1000000000/100; /* 1/100 sec */
	ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0);
	ATF_REQUIRE_EQ(thrd_sleep(&ts, &rem), 0);
	ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0);
	timespecsub(&stop, &start, &diff);
	ATF_REQUIRE(timespeccmp(&diff, &ts, >=));
	ATF_REQUIRE(timespeccmp(&zero, &rem, ==));

	ts.tv_sec = 1;
	ts.tv_nsec = 0;
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = alarm_handler;
	sigaction(SIGALRM, &sa, NULL);
	memset(&timer, 0, sizeof(timer));
	timer.it_value.tv_sec = 0;
	timer.it_value.tv_usec = 100000; /* 100 msec */
	ATF_REQUIRE_EQ(setitimer(ITIMER_MONOTONIC, &timer, NULL), 0);
	ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0);
	ATF_REQUIRE_EQ(!thrd_sleep(&ts, &rem), 0);
	ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0);
	timespecsub(&stop, &start, &diff);
	ATF_REQUIRE(timespeccmp(&diff, &ts, <));
	ATF_REQUIRE(timespeccmp(&zero, &rem, !=));
	ATF_REQUIRE_EQ(alarmed, 1);
}

ATF_TC(thrd_yield);
ATF_TC_HEAD(thrd_yield, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test C11 thrd_yield(3)");
}

ATF_TC_BODY(thrd_yield, tc)
{

	thrd_yield();
}

ATF_TP_ADD_TCS(tp)
{
	ATF_TP_ADD_TC(tp, thrd_create);
	ATF_TP_ADD_TC(tp, thrd_current);
	ATF_TP_ADD_TC(tp, thrd_detach);
	ATF_TP_ADD_TC(tp, thrd_exit);
	ATF_TP_ADD_TC(tp, thrd_sleep);
	ATF_TP_ADD_TC(tp, thrd_yield);;

	return atf_no_error();
}