t_thrd.c revision 1.1.2.2 1 1.1.2.2 christos /* $NetBSD: t_thrd.c,v 1.1.2.2 2019/06/10 22:10:07 christos Exp $ */
2 1.1.2.2 christos
3 1.1.2.2 christos /*-
4 1.1.2.2 christos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1.2.2 christos * All rights reserved.
6 1.1.2.2 christos *
7 1.1.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 christos * by Kamil Rytarowski.
9 1.1.2.2 christos *
10 1.1.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 christos * modification, are permitted provided that the following conditions
12 1.1.2.2 christos * are met:
13 1.1.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.1.2.2 christos *
19 1.1.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 christos */
31 1.1.2.2 christos
32 1.1.2.2 christos #include <sys/cdefs.h>
33 1.1.2.2 christos __COPYRIGHT("@(#) Copyright (c) 2019\
34 1.1.2.2 christos The NetBSD Foundation, inc. All rights reserved.");
35 1.1.2.2 christos __RCSID("$NetBSD: t_thrd.c,v 1.1.2.2 2019/06/10 22:10:07 christos Exp $");
36 1.1.2.2 christos
37 1.1.2.2 christos #include <signal.h>
38 1.1.2.2 christos #include <stdbool.h>
39 1.1.2.2 christos #include <threads.h>
40 1.1.2.2 christos #include <time.h>
41 1.1.2.2 christos
42 1.1.2.2 christos #include <atf-c.h>
43 1.1.2.2 christos
44 1.1.2.2 christos ATF_TC(thrd_create);
45 1.1.2.2 christos ATF_TC_HEAD(thrd_create, tc)
46 1.1.2.2 christos {
47 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_create(3)");
48 1.1.2.2 christos }
49 1.1.2.2 christos
50 1.1.2.2 christos #define TC_ADDON 5
51 1.1.2.2 christos
52 1.1.2.2 christos static int
53 1.1.2.2 christos tcr_func(void *arg)
54 1.1.2.2 christos {
55 1.1.2.2 christos int a;
56 1.1.2.2 christos
57 1.1.2.2 christos a = (int)(intptr_t)arg;
58 1.1.2.2 christos
59 1.1.2.2 christos return a + TC_ADDON;
60 1.1.2.2 christos }
61 1.1.2.2 christos
62 1.1.2.2 christos ATF_TC_BODY(thrd_create, tc)
63 1.1.2.2 christos {
64 1.1.2.2 christos thrd_t t;
65 1.1.2.2 christos const int a = 5;
66 1.1.2.2 christos int b;
67 1.1.2.2 christos void *v;
68 1.1.2.2 christos
69 1.1.2.2 christos v = (void *)(intptr_t)a;
70 1.1.2.2 christos
71 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_create(&t, tcr_func, v), thrd_success);
72 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success);
73 1.1.2.2 christos ATF_REQUIRE_EQ(a + TC_ADDON, b);
74 1.1.2.2 christos }
75 1.1.2.2 christos
76 1.1.2.2 christos ATF_TC(thrd_current);
77 1.1.2.2 christos ATF_TC_HEAD(thrd_current, tc)
78 1.1.2.2 christos {
79 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_current(3)");
80 1.1.2.2 christos }
81 1.1.2.2 christos
82 1.1.2.2 christos static int
83 1.1.2.2 christos tcur_func(void *arg __unused)
84 1.1.2.2 christos {
85 1.1.2.2 christos
86 1.1.2.2 christos return 0;
87 1.1.2.2 christos }
88 1.1.2.2 christos
89 1.1.2.2 christos ATF_TC_BODY(thrd_current, tc)
90 1.1.2.2 christos {
91 1.1.2.2 christos thrd_t s, t;
92 1.1.2.2 christos
93 1.1.2.2 christos s = thrd_current();
94 1.1.2.2 christos
95 1.1.2.2 christos ATF_REQUIRE(thrd_equal(s, s) != 0);
96 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_create(&t, tcur_func, NULL), thrd_success);
97 1.1.2.2 christos ATF_REQUIRE(thrd_equal(t, s) == 0);
98 1.1.2.2 christos ATF_REQUIRE(thrd_equal(s, t) == 0);
99 1.1.2.2 christos ATF_REQUIRE(thrd_equal(t, t) != 0);
100 1.1.2.2 christos
101 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_join(t, NULL), thrd_success);
102 1.1.2.2 christos }
103 1.1.2.2 christos
104 1.1.2.2 christos ATF_TC(thrd_detach);
105 1.1.2.2 christos ATF_TC_HEAD(thrd_detach, tc)
106 1.1.2.2 christos {
107 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_detach(3)");
108 1.1.2.2 christos }
109 1.1.2.2 christos
110 1.1.2.2 christos static int
111 1.1.2.2 christos tdet_func(void *arg __unused)
112 1.1.2.2 christos {
113 1.1.2.2 christos
114 1.1.2.2 christos return 0;
115 1.1.2.2 christos }
116 1.1.2.2 christos
117 1.1.2.2 christos ATF_TC_BODY(thrd_detach, tc)
118 1.1.2.2 christos {
119 1.1.2.2 christos thrd_t t;
120 1.1.2.2 christos
121 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_create(&t, tdet_func, NULL), thrd_success);
122 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_detach(t), thrd_success);
123 1.1.2.2 christos }
124 1.1.2.2 christos
125 1.1.2.2 christos ATF_TC(thrd_exit);
126 1.1.2.2 christos ATF_TC_HEAD(thrd_exit, tc)
127 1.1.2.2 christos {
128 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_exit(3)");
129 1.1.2.2 christos }
130 1.1.2.2 christos
131 1.1.2.2 christos static void
132 1.1.2.2 christos tex_func2(void)
133 1.1.2.2 christos {
134 1.1.2.2 christos
135 1.1.2.2 christos thrd_exit(1);
136 1.1.2.2 christos }
137 1.1.2.2 christos
138 1.1.2.2 christos static int
139 1.1.2.2 christos tex_func(void *arg __unused)
140 1.1.2.2 christos {
141 1.1.2.2 christos
142 1.1.2.2 christos tex_func2();
143 1.1.2.2 christos
144 1.1.2.2 christos return -1;
145 1.1.2.2 christos }
146 1.1.2.2 christos
147 1.1.2.2 christos ATF_TC_BODY(thrd_exit, tc)
148 1.1.2.2 christos {
149 1.1.2.2 christos thrd_t t;
150 1.1.2.2 christos int b = 0;
151 1.1.2.2 christos
152 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_create(&t, tex_func, NULL), thrd_success);
153 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success);
154 1.1.2.2 christos ATF_REQUIRE_EQ(b, 1);
155 1.1.2.2 christos }
156 1.1.2.2 christos
157 1.1.2.2 christos ATF_TC(thrd_sleep);
158 1.1.2.2 christos ATF_TC_HEAD(thrd_sleep, tc)
159 1.1.2.2 christos {
160 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_sleep(3)");
161 1.1.2.2 christos }
162 1.1.2.2 christos
163 1.1.2.2 christos static int alarmed;
164 1.1.2.2 christos
165 1.1.2.2 christos static void
166 1.1.2.2 christos alarm_handler(int signum)
167 1.1.2.2 christos {
168 1.1.2.2 christos
169 1.1.2.2 christos ATF_REQUIRE_EQ(signum, SIGALRM);
170 1.1.2.2 christos ++alarmed;
171 1.1.2.2 christos }
172 1.1.2.2 christos
173 1.1.2.2 christos ATF_TC_BODY(thrd_sleep, tc)
174 1.1.2.2 christos {
175 1.1.2.2 christos struct timespec start, stop, diff;
176 1.1.2.2 christos struct timespec ts, rem;
177 1.1.2.2 christos struct timespec zero;
178 1.1.2.2 christos struct sigaction sa;
179 1.1.2.2 christos struct itimerval timer;
180 1.1.2.2 christos
181 1.1.2.2 christos zero.tv_sec = 0;
182 1.1.2.2 christos zero.tv_nsec = 0;
183 1.1.2.2 christos
184 1.1.2.2 christos ts.tv_sec = 1;
185 1.1.2.2 christos ts.tv_nsec = -1;
186 1.1.2.2 christos ATF_REQUIRE_EQ(!thrd_sleep(&ts, NULL), 0);
187 1.1.2.2 christos
188 1.1.2.2 christos ts.tv_sec = 0;
189 1.1.2.2 christos ts.tv_nsec = 1000000000/100; /* 1/100 sec */
190 1.1.2.2 christos ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0);
191 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_sleep(&ts, &rem), 0);
192 1.1.2.2 christos ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0);
193 1.1.2.2 christos timespecsub(&stop, &start, &diff);
194 1.1.2.2 christos ATF_REQUIRE(timespeccmp(&diff, &ts, >=));
195 1.1.2.2 christos ATF_REQUIRE(timespeccmp(&zero, &rem, ==));
196 1.1.2.2 christos
197 1.1.2.2 christos ts.tv_sec = 1;
198 1.1.2.2 christos ts.tv_nsec = 0;
199 1.1.2.2 christos memset(&sa, 0, sizeof(sa));
200 1.1.2.2 christos sa.sa_handler = alarm_handler;
201 1.1.2.2 christos sigaction(SIGALRM, &sa, NULL);
202 1.1.2.2 christos memset(&timer, 0, sizeof(timer));
203 1.1.2.2 christos timer.it_value.tv_sec = 0;
204 1.1.2.2 christos timer.it_value.tv_usec = 100000; /* 100 msec */
205 1.1.2.2 christos ATF_REQUIRE_EQ(setitimer(ITIMER_MONOTONIC, &timer, NULL), 0);
206 1.1.2.2 christos ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0);
207 1.1.2.2 christos ATF_REQUIRE_EQ(!thrd_sleep(&ts, &rem), 0);
208 1.1.2.2 christos ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0);
209 1.1.2.2 christos timespecsub(&stop, &start, &diff);
210 1.1.2.2 christos ATF_REQUIRE(timespeccmp(&diff, &ts, <));
211 1.1.2.2 christos ATF_REQUIRE(timespeccmp(&zero, &rem, !=));
212 1.1.2.2 christos ATF_REQUIRE_EQ(alarmed, 1);
213 1.1.2.2 christos }
214 1.1.2.2 christos
215 1.1.2.2 christos ATF_TC(thrd_yield);
216 1.1.2.2 christos ATF_TC_HEAD(thrd_yield, tc)
217 1.1.2.2 christos {
218 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 thrd_yield(3)");
219 1.1.2.2 christos }
220 1.1.2.2 christos
221 1.1.2.2 christos ATF_TC_BODY(thrd_yield, tc)
222 1.1.2.2 christos {
223 1.1.2.2 christos
224 1.1.2.2 christos thrd_yield();
225 1.1.2.2 christos }
226 1.1.2.2 christos
227 1.1.2.2 christos ATF_TP_ADD_TCS(tp)
228 1.1.2.2 christos {
229 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_create);
230 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_current);
231 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_detach);
232 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_exit);
233 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_sleep);
234 1.1.2.2 christos ATF_TP_ADD_TC(tp, thrd_yield);;
235 1.1.2.2 christos
236 1.1.2.2 christos return atf_no_error();
237 1.1.2.2 christos }
238