1 /* $NetBSD: t_mutex.c,v 1.10 2016/07/31 13:01:29 christos Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31 The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_mutex.c,v 1.10 2016/07/31 13:01:29 christos Exp $");
39 #include <sys/sched.h>
40 #include <sys/param.h>
46 static pthread_mutex_t mutex;
47 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
51 mutex1_threadfunc(void *arg)
55 printf("2: Second thread.\n");
58 printf("2: Locking mutex\n");
59 pthread_mutex_lock(&mutex);
60 printf("2: Got mutex. *param = %d\n", *param);
61 ATF_REQUIRE_EQ(*param, 20);
64 pthread_mutex_unlock(&mutex);
70 ATF_TC_HEAD(mutex1, tc)
72 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
74 ATF_TC_BODY(mutex1, tc)
80 printf("1: Mutex-test 1\n");
82 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
84 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
85 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
86 printf("1: Before changing the value.\n");
89 printf("1: Before releasing the mutex.\n");
91 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
92 printf("1: After releasing the mutex.\n");
93 PTHREAD_REQUIRE(pthread_join(new, &joinval));
95 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
96 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
98 ATF_REQUIRE_EQ(x, 21);
99 ATF_REQUIRE_EQ(*(int *)joinval, 21);
100 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
104 mutex2_threadfunc(void *arg)
106 long count = *(int *)arg;
108 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
111 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
113 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
116 return (void *)count;
120 ATF_TC_HEAD(mutex2, tc)
122 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
124 #if defined(__powerpc__)
125 atf_tc_set_md_var(tc, "timeout", "40");
129 ATF_TC_BODY(mutex2, tc)
135 printf("1: Mutex-test 2\n");
138 #if defined(__powerpc__)
139 atf_tc_expect_timeout("PR port-powerpc/44387");
143 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
146 count = count2 = 10000000;
148 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
149 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
151 printf("1: Thread %p\n", pthread_self());
153 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
156 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
158 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
161 PTHREAD_REQUIRE(pthread_join(new, &joinval));
163 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
164 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
165 global_x, (long)joinval);
166 ATF_REQUIRE_EQ(global_x, 20000000);
169 #if defined(__powerpc__)
170 /* XXX force a timeout in ppc case since an un-triggered race
171 otherwise looks like a "failure" */
172 /* We sleep for longer than the timeout to make ATF not
173 complain about unexpected success */
180 mutex3_threadfunc(void *arg)
182 long count = *(int *)arg;
184 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
187 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
189 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
192 return (void *)count;
196 ATF_TC_HEAD(mutex3, tc)
198 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
201 #if defined(__powerpc__)
202 atf_tc_set_md_var(tc, "timeout", "40");
206 ATF_TC_BODY(mutex3, tc)
212 printf("1: Mutex-test 3\n");
215 #if defined(__powerpc__)
216 atf_tc_expect_timeout("PR port-powerpc/44387");
221 count = count2 = 10000000;
223 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
224 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
226 printf("1: Thread %p\n", pthread_self());
228 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
231 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
233 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
236 PTHREAD_REQUIRE(pthread_join(new, &joinval));
238 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
239 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
240 global_x, (long)joinval);
241 ATF_REQUIRE_EQ(global_x, 20000000);
244 #if defined(__powerpc__)
245 /* XXX force a timeout in ppc case since an un-triggered race
246 otherwise looks like a "failure" */
247 /* We sleep for longer than the timeout to make ATF not
248 complain about unexpected success */
255 mutex4_threadfunc(void *arg)
259 printf("2: Second thread.\n");
262 printf("2: Locking mutex\n");
263 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
264 printf("2: Got mutex. *param = %d\n", *param);
267 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
273 ATF_TC_HEAD(mutex4, tc)
275 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
277 ATF_TC_BODY(mutex4, tc)
281 pthread_mutexattr_t mattr;
284 printf("1: Mutex-test 4\n");
286 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
287 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
289 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
291 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
294 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
295 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
297 printf("1: Before recursively acquiring the mutex.\n");
298 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
300 printf("1: Before releasing the mutex once.\n");
302 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
303 printf("1: After releasing the mutex once.\n");
307 printf("1: Before releasing the mutex twice.\n");
309 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
310 printf("1: After releasing the mutex twice.\n");
312 PTHREAD_REQUIRE(pthread_join(new, &joinval));
314 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
315 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
317 ATF_REQUIRE_EQ(x, 21);
318 ATF_REQUIRE_EQ(*(int *)joinval, 21);
319 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
323 static pthread_mutexattr_t attr5;
324 static pthread_mutex_t mutex5;
325 static int min_fifo_prio, max_fifo_prio;
328 child_func(void* arg)
332 printf("child is waiting\n");
333 res = _sched_protect(-2);
334 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
335 ATF_REQUIRE_EQ(errno, ENOENT);
336 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
337 printf("child is owning resource\n");
338 res = _sched_protect(-2);
339 ATF_REQUIRE_EQ(res, max_fifo_prio);
340 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
341 printf("child is done\n");
347 ATF_TC_HEAD(mutex5, tc)
349 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
350 atf_tc_set_md_var(tc, "require.user", "root");
353 ATF_TC_BODY(mutex5, tc)
356 struct sched_param param;
359 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
360 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
361 printf("min prio for FIFO = %d\n", min_fifo_prio);
362 param.sched_priority = min_fifo_prio;
364 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
365 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
366 printf("previous policy used = %d\n", res);
368 res = sched_getscheduler(getpid());
369 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
372 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
373 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
374 PTHREAD_PRIO_PROTECT));
375 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
378 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
379 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex5));
380 printf("enter critical section for main\n");
381 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
382 printf("main starts to sleep\n");
384 printf("main completes\n");
385 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
386 PTHREAD_REQUIRE(pthread_join(child, NULL));
389 static pthread_mutex_t mutex6;
390 static int start = 0;
391 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
396 struct sched_param param;
398 param.sched_priority = min_fifo_prio + 10;
399 pthread_t childid = pthread_self();
401 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
402 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
403 printf("high protect = %d, prio = %d\n",
404 _sched_protect(-2), param.sched_priority);
405 ATF_REQUIRE_EQ(policy, 1);
406 printf("high prio = %d\n", param.sched_priority);
409 for (int i = 0; i < 20; i++) {
410 while (high_cnt < MAX_LOOP) {
411 tmp += (123456789 % 1234) * (987654321 % 54321);
417 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
418 if (start == 0) start = 2;
419 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
427 struct sched_param param;
429 param.sched_priority = min_fifo_prio;
430 pthread_t childid = pthread_self();
431 int res = _sched_protect(max_fifo_prio);
432 ATF_REQUIRE_EQ(res, 0);
433 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
434 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
435 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
436 param.sched_priority);
437 ATF_REQUIRE_EQ(policy, 1);
438 printf("low prio = %d\n", param.sched_priority);
441 for (int i = 0; i < 20; i++) {
442 while (low_cnt < MAX_LOOP) {
443 tmp += (123456789 % 1234) * (987654321 % 54321);
449 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex6));
452 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
458 ATF_TC_HEAD(mutex6, tc)
460 atf_tc_set_md_var(tc, "descr",
461 "Checks scheduling for priority ceiling");
462 atf_tc_set_md_var(tc, "require.user", "root");
466 * 1. main thread sets itself to be a realtime task and launched two tasks,
467 * one has higher priority and the other has lower priority.
468 * 2. each child thread(low and high priority thread) sets its scheduler and
470 * 3. each child thread did several rounds of computation, after each round it
472 * 4. the child thread with low priority will call _sched_protect to increase
473 * its protect priority.
474 * 5. We verify the thread with low priority runs first.
476 * Why does it work? From the main thread, we launched the high
477 * priority thread first. This gives this thread the benefit of
478 * starting first. The low priority thread did not call _sched_protect(2).
479 * The high priority thread should finish the task first. After each
480 * round of computation, we call sleep, to put the task into the
481 * sleep queue, and wake up again after the timer expires. This
482 * gives the scheduler the chance to decide which task to run. So,
483 * the thread with real high priority will always block the thread
484 * with real low priority.
487 ATF_TC_BODY(mutex6, tc)
489 struct sched_param param;
493 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
494 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
495 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
496 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
499 param.sched_priority = min_fifo_prio;
500 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
501 printf("previous policy used = %d\n", res);
503 res = sched_getscheduler(getpid());
504 ATF_REQUIRE_EQ(res, 1);
505 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
506 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
508 PTHREAD_REQUIRE(pthread_join(low, NULL));
509 PTHREAD_REQUIRE(pthread_join(high, NULL));
511 ATF_REQUIRE_EQ(start, 1);
516 ATF_TC_HEAD(mutexattr1, tc)
518 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
521 ATF_TC_BODY(mutexattr1, tc)
523 pthread_mutexattr_t mattr;
524 int protocol, target;
526 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
528 target = PTHREAD_PRIO_NONE;
529 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
530 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
531 ATF_REQUIRE_EQ(protocol, target);
534 target = PTHREAD_PRIO_INHERIT;
535 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
536 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
537 ATF_REQUIRE_EQ(protocol, target);
540 target = PTHREAD_PRIO_PROTECT;
541 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
542 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
543 ATF_REQUIRE_EQ(protocol, target);
547 ATF_TC_HEAD(mutexattr2, tc)
549 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
552 ATF_TC_BODY(mutexattr2, tc)
554 pthread_mutexattr_t mattr;
557 atf_tc_expect_fail("fails on i == 0 with: "
558 "pthread_mutexattr_setprioceiling(&mattr, i): Invalid argument "
562 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
563 int max_prio = sched_get_priority_max(SCHED_FIFO);
564 int min_prio = sched_get_priority_min(SCHED_FIFO);
565 for (int i = min_prio; i <= max_prio; i++) {
570 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
573 printf("priority: %d\nprotocol: %d\n", i, protocol);
575 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
576 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
579 printf("prioceiling: %d\n", prioceiling);
581 ATF_REQUIRE_EQ(i, prioceiling);
587 ATF_TP_ADD_TC(tp, mutex1);
588 ATF_TP_ADD_TC(tp, mutex2);
589 ATF_TP_ADD_TC(tp, mutex3);
590 ATF_TP_ADD_TC(tp, mutex4);
592 ATF_TP_ADD_TC(tp, mutex5);
593 ATF_TP_ADD_TC(tp, mutex6);
595 ATF_TP_ADD_TC(tp, mutexattr1);
596 ATF_TP_ADD_TC(tp, mutexattr2);
598 return atf_no_error();