t_mutex.c revision 1.20 1 1.20 rin /* $NetBSD: t_mutex.c,v 1.20 2022/05/07 05:13:17 rin Exp $ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1 jmmv * All rights reserved.
6 1.1 jmmv *
7 1.1 jmmv * Redistribution and use in source and binary forms, with or without
8 1.1 jmmv * modification, are permitted provided that the following conditions
9 1.1 jmmv * are met:
10 1.1 jmmv * 1. Redistributions of source code must retain the above copyright
11 1.1 jmmv * notice, this list of conditions and the following disclaimer.
12 1.1 jmmv * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmmv * notice, this list of conditions and the following disclaimer in the
14 1.1 jmmv * documentation and/or other materials provided with the distribution.
15 1.1 jmmv *
16 1.1 jmmv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmmv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmmv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmmv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmmv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmmv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmmv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmmv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmmv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmmv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmmv * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1 jmmv #include <sys/cdefs.h>
30 1.1 jmmv __COPYRIGHT("@(#) Copyright (c) 2008\
31 1.1 jmmv The NetBSD Foundation, inc. All rights reserved.");
32 1.20 rin __RCSID("$NetBSD: t_mutex.c,v 1.20 2022/05/07 05:13:17 rin Exp $");
33 1.1 jmmv
34 1.15 christos #include <sys/time.h> /* For timespecadd */
35 1.15 christos #include <inttypes.h> /* For UINT16_MAX */
36 1.1 jmmv #include <pthread.h>
37 1.1 jmmv #include <stdio.h>
38 1.4 riz #include <string.h>
39 1.8 christos #include <errno.h>
40 1.12 christos #include <time.h>
41 1.1 jmmv #include <unistd.h>
42 1.8 christos #include <sys/sched.h>
43 1.8 christos #include <sys/param.h>
44 1.1 jmmv
45 1.1 jmmv #include <atf-c.h>
46 1.1 jmmv
47 1.1 jmmv #include "h_common.h"
48 1.1 jmmv
49 1.1 jmmv static pthread_mutex_t mutex;
50 1.3 jmmv static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
51 1.1 jmmv static int global_x;
52 1.1 jmmv
53 1.12 christos #ifdef TIMEDMUTEX
54 1.12 christos /* This code is used for verifying non-timed specific code */
55 1.12 christos static struct timespec ts_lengthy = {
56 1.12 christos .tv_sec = UINT16_MAX,
57 1.12 christos .tv_nsec = 0
58 1.12 christos };
59 1.12 christos /* This code is used for verifying timed-only specific code */
60 1.12 christos static struct timespec ts_shortlived = {
61 1.12 christos .tv_sec = 0,
62 1.12 christos .tv_nsec = 120
63 1.12 christos };
64 1.12 christos
65 1.12 christos static int
66 1.12 christos mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
67 1.12 christos {
68 1.12 christos struct timespec ts_wait;
69 1.12 christos ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
70 1.12 christos timespecadd(&ts_wait, ts, &ts_wait);
71 1.12 christos
72 1.12 christos return pthread_mutex_timedlock(m, &ts_wait);
73 1.12 christos }
74 1.12 christos #else
75 1.12 christos #define mutex_lock(a, b) pthread_mutex_lock(a)
76 1.12 christos #endif
77 1.12 christos
78 1.1 jmmv static void *
79 1.1 jmmv mutex1_threadfunc(void *arg)
80 1.1 jmmv {
81 1.1 jmmv int *param;
82 1.1 jmmv
83 1.1 jmmv printf("2: Second thread.\n");
84 1.1 jmmv
85 1.1 jmmv param = arg;
86 1.1 jmmv printf("2: Locking mutex\n");
87 1.12 christos mutex_lock(&mutex, &ts_lengthy);
88 1.1 jmmv printf("2: Got mutex. *param = %d\n", *param);
89 1.1 jmmv ATF_REQUIRE_EQ(*param, 20);
90 1.1 jmmv (*param)++;
91 1.1 jmmv
92 1.1 jmmv pthread_mutex_unlock(&mutex);
93 1.1 jmmv
94 1.1 jmmv return param;
95 1.1 jmmv }
96 1.1 jmmv
97 1.1 jmmv ATF_TC(mutex1);
98 1.1 jmmv ATF_TC_HEAD(mutex1, tc)
99 1.1 jmmv {
100 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
101 1.1 jmmv }
102 1.1 jmmv ATF_TC_BODY(mutex1, tc)
103 1.1 jmmv {
104 1.1 jmmv int x;
105 1.1 jmmv pthread_t new;
106 1.1 jmmv void *joinval;
107 1.1 jmmv
108 1.1 jmmv printf("1: Mutex-test 1\n");
109 1.1 jmmv
110 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
111 1.1 jmmv x = 1;
112 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
113 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
114 1.1 jmmv printf("1: Before changing the value.\n");
115 1.1 jmmv sleep(2);
116 1.1 jmmv x = 20;
117 1.1 jmmv printf("1: Before releasing the mutex.\n");
118 1.1 jmmv sleep(2);
119 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
120 1.1 jmmv printf("1: After releasing the mutex.\n");
121 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
122 1.1 jmmv
123 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
124 1.1 jmmv printf("1: Thread joined. X was %d. Return value (int) was %d\n",
125 1.1 jmmv x, *(int *)joinval);
126 1.1 jmmv ATF_REQUIRE_EQ(x, 21);
127 1.1 jmmv ATF_REQUIRE_EQ(*(int *)joinval, 21);
128 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
129 1.1 jmmv }
130 1.1 jmmv
131 1.1 jmmv static void *
132 1.1 jmmv mutex2_threadfunc(void *arg)
133 1.1 jmmv {
134 1.1 jmmv long count = *(int *)arg;
135 1.1 jmmv
136 1.1 jmmv printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
137 1.1 jmmv
138 1.1 jmmv while (count--) {
139 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
140 1.1 jmmv global_x++;
141 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
142 1.1 jmmv }
143 1.1 jmmv
144 1.1 jmmv return (void *)count;
145 1.1 jmmv }
146 1.1 jmmv
147 1.1 jmmv ATF_TC(mutex2);
148 1.1 jmmv ATF_TC_HEAD(mutex2, tc)
149 1.1 jmmv {
150 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
151 1.20 rin atf_tc_set_md_var(tc, "timeout", "600");
152 1.1 jmmv }
153 1.1 jmmv ATF_TC_BODY(mutex2, tc)
154 1.1 jmmv {
155 1.1 jmmv int count, count2;
156 1.1 jmmv pthread_t new;
157 1.1 jmmv void *joinval;
158 1.1 jmmv
159 1.1 jmmv printf("1: Mutex-test 2\n");
160 1.1 jmmv
161 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
162 1.4 riz
163 1.1 jmmv global_x = 0;
164 1.1 jmmv count = count2 = 10000000;
165 1.1 jmmv
166 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
167 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
168 1.1 jmmv
169 1.1 jmmv printf("1: Thread %p\n", pthread_self());
170 1.1 jmmv
171 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
172 1.1 jmmv
173 1.1 jmmv while (count--) {
174 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
175 1.1 jmmv global_x++;
176 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
177 1.1 jmmv }
178 1.1 jmmv
179 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
180 1.1 jmmv
181 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
182 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
183 1.1 jmmv global_x, (long)joinval);
184 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
185 1.1 jmmv }
186 1.1 jmmv
187 1.1 jmmv static void *
188 1.1 jmmv mutex3_threadfunc(void *arg)
189 1.1 jmmv {
190 1.1 jmmv long count = *(int *)arg;
191 1.1 jmmv
192 1.1 jmmv printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
193 1.1 jmmv
194 1.1 jmmv while (count--) {
195 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
196 1.1 jmmv global_x++;
197 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
198 1.1 jmmv }
199 1.1 jmmv
200 1.1 jmmv return (void *)count;
201 1.1 jmmv }
202 1.1 jmmv
203 1.1 jmmv ATF_TC(mutex3);
204 1.1 jmmv ATF_TC_HEAD(mutex3, tc)
205 1.1 jmmv {
206 1.3 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
207 1.3 jmmv "initializer");
208 1.20 rin atf_tc_set_md_var(tc, "timeout", "600");
209 1.1 jmmv }
210 1.1 jmmv ATF_TC_BODY(mutex3, tc)
211 1.1 jmmv {
212 1.1 jmmv int count, count2;
213 1.1 jmmv pthread_t new;
214 1.1 jmmv void *joinval;
215 1.1 jmmv
216 1.1 jmmv printf("1: Mutex-test 3\n");
217 1.1 jmmv
218 1.1 jmmv global_x = 0;
219 1.1 jmmv count = count2 = 10000000;
220 1.1 jmmv
221 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
222 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
223 1.1 jmmv
224 1.1 jmmv printf("1: Thread %p\n", pthread_self());
225 1.3 jmmv
226 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
227 1.1 jmmv
228 1.1 jmmv while (count--) {
229 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
230 1.1 jmmv global_x++;
231 1.3 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
232 1.1 jmmv }
233 1.1 jmmv
234 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
235 1.1 jmmv
236 1.12 christos PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
237 1.1 jmmv printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
238 1.1 jmmv global_x, (long)joinval);
239 1.1 jmmv ATF_REQUIRE_EQ(global_x, 20000000);
240 1.1 jmmv }
241 1.1 jmmv
242 1.1 jmmv static void *
243 1.1 jmmv mutex4_threadfunc(void *arg)
244 1.1 jmmv {
245 1.1 jmmv int *param;
246 1.1 jmmv
247 1.1 jmmv printf("2: Second thread.\n");
248 1.1 jmmv
249 1.1 jmmv param = arg;
250 1.1 jmmv printf("2: Locking mutex\n");
251 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
252 1.1 jmmv printf("2: Got mutex. *param = %d\n", *param);
253 1.1 jmmv (*param)++;
254 1.1 jmmv
255 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
256 1.1 jmmv
257 1.1 jmmv return param;
258 1.1 jmmv }
259 1.1 jmmv
260 1.1 jmmv ATF_TC(mutex4);
261 1.1 jmmv ATF_TC_HEAD(mutex4, tc)
262 1.1 jmmv {
263 1.1 jmmv atf_tc_set_md_var(tc, "descr", "Checks mutexes");
264 1.1 jmmv }
265 1.1 jmmv ATF_TC_BODY(mutex4, tc)
266 1.1 jmmv {
267 1.1 jmmv int x;
268 1.1 jmmv pthread_t new;
269 1.1 jmmv pthread_mutexattr_t mattr;
270 1.1 jmmv void *joinval;
271 1.1 jmmv
272 1.1 jmmv printf("1: Mutex-test 4\n");
273 1.1 jmmv
274 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
275 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
276 1.1 jmmv
277 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
278 1.1 jmmv
279 1.1 jmmv PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
280 1.1 jmmv
281 1.1 jmmv x = 1;
282 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
283 1.1 jmmv PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
284 1.1 jmmv
285 1.1 jmmv printf("1: Before recursively acquiring the mutex.\n");
286 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
287 1.1 jmmv
288 1.1 jmmv printf("1: Before releasing the mutex once.\n");
289 1.1 jmmv sleep(2);
290 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
291 1.1 jmmv printf("1: After releasing the mutex once.\n");
292 1.1 jmmv
293 1.1 jmmv x = 20;
294 1.1 jmmv
295 1.1 jmmv printf("1: Before releasing the mutex twice.\n");
296 1.1 jmmv sleep(2);
297 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
298 1.1 jmmv printf("1: After releasing the mutex twice.\n");
299 1.1 jmmv
300 1.1 jmmv PTHREAD_REQUIRE(pthread_join(new, &joinval));
301 1.1 jmmv
302 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
303 1.1 jmmv printf("1: Thread joined. X was %d. Return value (int) was %d\n",
304 1.1 jmmv x, *(int *)joinval);
305 1.1 jmmv ATF_REQUIRE_EQ(x, 21);
306 1.1 jmmv ATF_REQUIRE_EQ(*(int *)joinval, 21);
307 1.1 jmmv PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
308 1.1 jmmv }
309 1.1 jmmv
310 1.8 christos static pthread_mutexattr_t attr5;
311 1.8 christos static pthread_mutex_t mutex5;
312 1.8 christos static int min_fifo_prio, max_fifo_prio;
313 1.8 christos
314 1.8 christos static void *
315 1.8 christos child_func(void* arg)
316 1.8 christos {
317 1.8 christos int res;
318 1.8 christos
319 1.8 christos printf("child is waiting\n");
320 1.8 christos res = _sched_protect(-2);
321 1.10 christos ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
322 1.8 christos ATF_REQUIRE_EQ(errno, ENOENT);
323 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
324 1.8 christos printf("child is owning resource\n");
325 1.8 christos res = _sched_protect(-2);
326 1.8 christos ATF_REQUIRE_EQ(res, max_fifo_prio);
327 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
328 1.8 christos printf("child is done\n");
329 1.8 christos
330 1.8 christos return 0;
331 1.8 christos }
332 1.8 christos
333 1.8 christos ATF_TC(mutex5);
334 1.8 christos ATF_TC_HEAD(mutex5, tc)
335 1.8 christos {
336 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
337 1.10 christos atf_tc_set_md_var(tc, "require.user", "root");
338 1.8 christos }
339 1.8 christos
340 1.8 christos ATF_TC_BODY(mutex5, tc)
341 1.8 christos {
342 1.8 christos int res;
343 1.8 christos struct sched_param param;
344 1.8 christos pthread_t child;
345 1.8 christos
346 1.8 christos min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
347 1.8 christos max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
348 1.8 christos printf("min prio for FIFO = %d\n", min_fifo_prio);
349 1.8 christos param.sched_priority = min_fifo_prio;
350 1.9 christos
351 1.8 christos /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
352 1.8 christos res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
353 1.8 christos printf("previous policy used = %d\n", res);
354 1.8 christos
355 1.8 christos res = sched_getscheduler(getpid());
356 1.10 christos ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
357 1.10 christos SCHED_FIFO);
358 1.8 christos
359 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
360 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
361 1.8 christos PTHREAD_PRIO_PROTECT));
362 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
363 1.8 christos max_fifo_prio));
364 1.8 christos
365 1.8 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
366 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
367 1.8 christos printf("enter critical section for main\n");
368 1.8 christos PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
369 1.8 christos printf("main starts to sleep\n");
370 1.8 christos sleep(10);
371 1.8 christos printf("main completes\n");
372 1.8 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
373 1.8 christos PTHREAD_REQUIRE(pthread_join(child, NULL));
374 1.8 christos }
375 1.8 christos
376 1.8 christos ATF_TC(mutexattr1);
377 1.8 christos ATF_TC_HEAD(mutexattr1, tc)
378 1.8 christos {
379 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
380 1.8 christos }
381 1.8 christos
382 1.8 christos ATF_TC_BODY(mutexattr1, tc)
383 1.8 christos {
384 1.8 christos pthread_mutexattr_t mattr;
385 1.8 christos int protocol, target;
386 1.8 christos
387 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
388 1.8 christos
389 1.8 christos target = PTHREAD_PRIO_NONE;
390 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
391 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
392 1.8 christos ATF_REQUIRE_EQ(protocol, target);
393 1.8 christos
394 1.8 christos /*
395 1.8 christos target = PTHREAD_PRIO_INHERIT;
396 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
397 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
398 1.8 christos ATF_REQUIRE_EQ(protocol, target);
399 1.8 christos */
400 1.8 christos
401 1.8 christos target = PTHREAD_PRIO_PROTECT;
402 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
403 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
404 1.8 christos ATF_REQUIRE_EQ(protocol, target);
405 1.8 christos }
406 1.8 christos
407 1.8 christos ATF_TC(mutexattr2);
408 1.8 christos ATF_TC_HEAD(mutexattr2, tc)
409 1.8 christos {
410 1.8 christos atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
411 1.8 christos }
412 1.8 christos
413 1.8 christos ATF_TC_BODY(mutexattr2, tc)
414 1.8 christos {
415 1.8 christos pthread_mutexattr_t mattr;
416 1.8 christos
417 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
418 1.8 christos int max_prio = sched_get_priority_max(SCHED_FIFO);
419 1.8 christos int min_prio = sched_get_priority_min(SCHED_FIFO);
420 1.8 christos for (int i = min_prio; i <= max_prio; i++) {
421 1.8 christos int prioceiling;
422 1.15 christos int protocol;
423 1.15 christos
424 1.15 christos PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
425 1.15 christos &protocol));
426 1.15 christos
427 1.15 christos printf("priority: %d\nprotocol: %d\n", i, protocol);
428 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
429 1.8 christos PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
430 1.8 christos &prioceiling));
431 1.15 christos printf("prioceiling: %d\n", prioceiling);
432 1.8 christos ATF_REQUIRE_EQ(i, prioceiling);
433 1.8 christos }
434 1.8 christos }
435 1.8 christos
436 1.12 christos #ifdef TIMEDMUTEX
437 1.12 christos ATF_TC(timedmutex1);
438 1.12 christos ATF_TC_HEAD(timedmutex1, tc)
439 1.12 christos {
440 1.12 christos atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
441 1.12 christos }
442 1.12 christos
443 1.12 christos ATF_TC_BODY(timedmutex1, tc)
444 1.12 christos {
445 1.12 christos
446 1.12 christos printf("Timed mutex-test 1\n");
447 1.12 christos
448 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
449 1.12 christos
450 1.14 christos printf("Before acquiring mutex\n");
451 1.12 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
452 1.12 christos
453 1.12 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
454 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
455 1.12 christos ETIMEDOUT);
456 1.12 christos
457 1.14 christos printf("Unlocking mutex\n");
458 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
459 1.12 christos }
460 1.12 christos
461 1.12 christos ATF_TC(timedmutex2);
462 1.12 christos ATF_TC_HEAD(timedmutex2, tc)
463 1.12 christos {
464 1.12 christos atf_tc_set_md_var(tc, "descr",
465 1.12 christos "Checks timeout on selflock with timedlock");
466 1.12 christos }
467 1.12 christos
468 1.12 christos ATF_TC_BODY(timedmutex2, tc)
469 1.12 christos {
470 1.12 christos
471 1.13 christos printf("Timed mutex-test 2\n");
472 1.12 christos
473 1.12 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
474 1.12 christos
475 1.14 christos printf("Before acquiring mutex with timedlock\n");
476 1.12 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
477 1.12 christos
478 1.13 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
479 1.12 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
480 1.12 christos ETIMEDOUT);
481 1.12 christos
482 1.14 christos printf("Unlocking mutex\n");
483 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
484 1.14 christos }
485 1.14 christos
486 1.14 christos ATF_TC(timedmutex3);
487 1.14 christos ATF_TC_HEAD(timedmutex3, tc)
488 1.14 christos {
489 1.14 christos atf_tc_set_md_var(tc, "descr",
490 1.14 christos "Checks timeout on selflock in a new thread");
491 1.14 christos }
492 1.14 christos
493 1.14 christos static void *
494 1.14 christos timedmtx_thrdfunc(void *arg)
495 1.14 christos {
496 1.14 christos printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
497 1.14 christos PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
498 1.14 christos ETIMEDOUT);
499 1.14 christos
500 1.14 christos return NULL;
501 1.14 christos }
502 1.14 christos
503 1.14 christos ATF_TC_BODY(timedmutex3, tc)
504 1.14 christos {
505 1.14 christos pthread_t new;
506 1.14 christos
507 1.14 christos printf("Timed mutex-test 3\n");
508 1.14 christos
509 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
510 1.14 christos
511 1.14 christos printf("Before acquiring mutex with timedlock\n");
512 1.14 christos PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
513 1.14 christos
514 1.14 christos printf("Before creating new thread\n");
515 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
516 1.14 christos
517 1.14 christos printf("Before joining the mutex\n");
518 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
519 1.14 christos
520 1.14 christos printf("Unlocking mutex\n");
521 1.14 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
522 1.14 christos }
523 1.14 christos
524 1.14 christos ATF_TC(timedmutex4);
525 1.14 christos ATF_TC_HEAD(timedmutex4, tc)
526 1.14 christos {
527 1.14 christos atf_tc_set_md_var(tc, "descr",
528 1.14 christos "Checks timeout on selflock with timedlock in a new thread");
529 1.14 christos }
530 1.14 christos
531 1.14 christos ATF_TC_BODY(timedmutex4, tc)
532 1.14 christos {
533 1.14 christos pthread_t new;
534 1.14 christos
535 1.14 christos printf("Timed mutex-test 4\n");
536 1.14 christos
537 1.14 christos PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
538 1.14 christos
539 1.14 christos printf("Before acquiring mutex with timedlock\n");
540 1.14 christos PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
541 1.14 christos
542 1.14 christos printf("Before creating new thread\n");
543 1.14 christos PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
544 1.14 christos
545 1.14 christos printf("Before joining the mutex\n");
546 1.14 christos PTHREAD_REQUIRE(pthread_join(new, NULL));
547 1.14 christos
548 1.14 christos printf("Unlocking mutex\n");
549 1.12 christos PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
550 1.12 christos }
551 1.12 christos #endif
552 1.12 christos
553 1.1 jmmv ATF_TP_ADD_TCS(tp)
554 1.1 jmmv {
555 1.1 jmmv ATF_TP_ADD_TC(tp, mutex1);
556 1.1 jmmv ATF_TP_ADD_TC(tp, mutex2);
557 1.1 jmmv ATF_TP_ADD_TC(tp, mutex3);
558 1.1 jmmv ATF_TP_ADD_TC(tp, mutex4);
559 1.8 christos ATF_TP_ADD_TC(tp, mutex5);
560 1.8 christos ATF_TP_ADD_TC(tp, mutexattr1);
561 1.8 christos ATF_TP_ADD_TC(tp, mutexattr2);
562 1.12 christos
563 1.12 christos #ifdef TIMEDMUTEX
564 1.12 christos ATF_TP_ADD_TC(tp, timedmutex1);
565 1.12 christos ATF_TP_ADD_TC(tp, timedmutex2);
566 1.14 christos ATF_TP_ADD_TC(tp, timedmutex3);
567 1.14 christos ATF_TP_ADD_TC(tp, timedmutex4);
568 1.12 christos #endif
569 1.12 christos
570 1.1 jmmv return atf_no_error();
571 1.1 jmmv }
572