1 /* $NetBSD: t_mutex.c,v 1.15 2017/01/16 16:23:41 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.15 2017/01/16 16:23:41 christos Exp $");
34 #include <sys/time.h> /* For timespecadd */
35 #include <inttypes.h> /* For UINT16_MAX */
42 #include <sys/sched.h>
43 #include <sys/param.h>
49 static pthread_mutex_t mutex;
50 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
54 /* This code is used for verifying non-timed specific code */
55 static struct timespec ts_lengthy = {
59 /* This code is used for verifying timed-only specific code */
60 static struct timespec ts_shortlived = {
66 mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
68 struct timespec ts_wait;
69 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
70 timespecadd(&ts_wait, ts, &ts_wait);
72 return pthread_mutex_timedlock(m, &ts_wait);
75 #define mutex_lock(a, b) pthread_mutex_lock(a)
79 mutex1_threadfunc(void *arg)
83 printf("2: Second thread.\n");
86 printf("2: Locking mutex\n");
87 mutex_lock(&mutex, &ts_lengthy);
88 printf("2: Got mutex. *param = %d\n", *param);
89 ATF_REQUIRE_EQ(*param, 20);
92 pthread_mutex_unlock(&mutex);
98 ATF_TC_HEAD(mutex1, tc)
100 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
102 ATF_TC_BODY(mutex1, tc)
108 printf("1: Mutex-test 1\n");
110 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
112 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
113 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
114 printf("1: Before changing the value.\n");
117 printf("1: Before releasing the mutex.\n");
119 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
120 printf("1: After releasing the mutex.\n");
121 PTHREAD_REQUIRE(pthread_join(new, &joinval));
123 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
124 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
126 ATF_REQUIRE_EQ(x, 21);
127 ATF_REQUIRE_EQ(*(int *)joinval, 21);
128 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
132 mutex2_threadfunc(void *arg)
134 long count = *(int *)arg;
136 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
139 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
141 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
144 return (void *)count;
148 ATF_TC_HEAD(mutex2, tc)
150 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
152 #if defined(__powerpc__)
153 atf_tc_set_md_var(tc, "timeout", "40");
159 atf_tc_set_md_var(tc, "timeout", "600");
163 ATF_TC_BODY(mutex2, tc)
169 printf("1: Mutex-test 2\n");
172 #if defined(__powerpc__)
173 atf_tc_expect_timeout("PR port-powerpc/44387");
177 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
180 count = count2 = 10000000;
182 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
183 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
185 printf("1: Thread %p\n", pthread_self());
187 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
190 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
192 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
195 PTHREAD_REQUIRE(pthread_join(new, &joinval));
197 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
198 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
199 global_x, (long)joinval);
200 ATF_REQUIRE_EQ(global_x, 20000000);
203 #if defined(__powerpc__)
204 /* XXX force a timeout in ppc case since an un-triggered race
205 otherwise looks like a "failure" */
206 /* We sleep for longer than the timeout to make ATF not
207 complain about unexpected success */
214 mutex3_threadfunc(void *arg)
216 long count = *(int *)arg;
218 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
221 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
223 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
226 return (void *)count;
230 ATF_TC_HEAD(mutex3, tc)
232 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
235 #if defined(__powerpc__)
236 atf_tc_set_md_var(tc, "timeout", "40");
242 atf_tc_set_md_var(tc, "timeout", "600");
246 ATF_TC_BODY(mutex3, tc)
252 printf("1: Mutex-test 3\n");
255 #if defined(__powerpc__)
256 atf_tc_expect_timeout("PR port-powerpc/44387");
261 count = count2 = 10000000;
263 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
264 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
266 printf("1: Thread %p\n", pthread_self());
268 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
271 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
273 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
276 PTHREAD_REQUIRE(pthread_join(new, &joinval));
278 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
279 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
280 global_x, (long)joinval);
281 ATF_REQUIRE_EQ(global_x, 20000000);
284 #if defined(__powerpc__)
285 /* XXX force a timeout in ppc case since an un-triggered race
286 otherwise looks like a "failure" */
287 /* We sleep for longer than the timeout to make ATF not
288 complain about unexpected success */
295 mutex4_threadfunc(void *arg)
299 printf("2: Second thread.\n");
302 printf("2: Locking mutex\n");
303 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
304 printf("2: Got mutex. *param = %d\n", *param);
307 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
313 ATF_TC_HEAD(mutex4, tc)
315 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
317 ATF_TC_BODY(mutex4, tc)
321 pthread_mutexattr_t mattr;
324 printf("1: Mutex-test 4\n");
326 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
327 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
329 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
331 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
334 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
335 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
337 printf("1: Before recursively acquiring the mutex.\n");
338 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
340 printf("1: Before releasing the mutex once.\n");
342 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
343 printf("1: After releasing the mutex once.\n");
347 printf("1: Before releasing the mutex twice.\n");
349 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
350 printf("1: After releasing the mutex twice.\n");
352 PTHREAD_REQUIRE(pthread_join(new, &joinval));
354 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
355 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
357 ATF_REQUIRE_EQ(x, 21);
358 ATF_REQUIRE_EQ(*(int *)joinval, 21);
359 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
363 static pthread_mutexattr_t attr5;
364 static pthread_mutex_t mutex5;
365 static int min_fifo_prio, max_fifo_prio;
368 child_func(void* arg)
372 printf("child is waiting\n");
373 res = _sched_protect(-2);
374 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
375 ATF_REQUIRE_EQ(errno, ENOENT);
376 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
377 printf("child is owning resource\n");
378 res = _sched_protect(-2);
379 ATF_REQUIRE_EQ(res, max_fifo_prio);
380 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
381 printf("child is done\n");
387 ATF_TC_HEAD(mutex5, tc)
389 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
390 atf_tc_set_md_var(tc, "require.user", "root");
393 ATF_TC_BODY(mutex5, tc)
396 struct sched_param param;
399 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
400 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
401 printf("min prio for FIFO = %d\n", min_fifo_prio);
402 param.sched_priority = min_fifo_prio;
404 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
405 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
406 printf("previous policy used = %d\n", res);
408 res = sched_getscheduler(getpid());
409 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
412 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
413 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
414 PTHREAD_PRIO_PROTECT));
415 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
418 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
419 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
420 printf("enter critical section for main\n");
421 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
422 printf("main starts to sleep\n");
424 printf("main completes\n");
425 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
426 PTHREAD_REQUIRE(pthread_join(child, NULL));
429 static pthread_mutex_t mutex6;
430 static int start = 0;
431 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
436 struct sched_param param;
438 param.sched_priority = min_fifo_prio + 10;
439 pthread_t childid = pthread_self();
441 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
442 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
443 printf("high protect = %d, prio = %d\n",
444 _sched_protect(-2), param.sched_priority);
445 ATF_REQUIRE_EQ(policy, 1);
446 printf("high prio = %d\n", param.sched_priority);
449 for (int i = 0; i < 20; i++) {
450 while (high_cnt < MAX_LOOP) {
451 tmp += (123456789 % 1234) * (987654321 % 54321);
457 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
458 if (start == 0) start = 2;
459 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
467 struct sched_param param;
469 param.sched_priority = min_fifo_prio;
470 pthread_t childid = pthread_self();
471 int res = _sched_protect(max_fifo_prio);
472 ATF_REQUIRE_EQ(res, 0);
473 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
474 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
475 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
476 param.sched_priority);
477 ATF_REQUIRE_EQ(policy, 1);
478 printf("low prio = %d\n", param.sched_priority);
481 for (int i = 0; i < 20; i++) {
482 while (low_cnt < MAX_LOOP) {
483 tmp += (123456789 % 1234) * (987654321 % 54321);
489 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
492 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
498 ATF_TC_HEAD(mutex6, tc)
500 atf_tc_set_md_var(tc, "descr",
501 "Checks scheduling for priority ceiling");
502 atf_tc_set_md_var(tc, "require.user", "root");
506 * 1. main thread sets itself to be a realtime task and launched two tasks,
507 * one has higher priority and the other has lower priority.
508 * 2. each child thread(low and high priority thread) sets its scheduler and
510 * 3. each child thread did several rounds of computation, after each round it
512 * 4. the child thread with low priority will call _sched_protect to increase
513 * its protect priority.
514 * 5. We verify the thread with low priority runs first.
516 * Why does it work? From the main thread, we launched the high
517 * priority thread first. This gives this thread the benefit of
518 * starting first. The low priority thread did not call _sched_protect(2).
519 * The high priority thread should finish the task first. After each
520 * round of computation, we call sleep, to put the task into the
521 * sleep queue, and wake up again after the timer expires. This
522 * gives the scheduler the chance to decide which task to run. So,
523 * the thread with real high priority will always block the thread
524 * with real low priority.
527 ATF_TC_BODY(mutex6, tc)
529 struct sched_param param;
533 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
534 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
535 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
536 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
539 param.sched_priority = min_fifo_prio;
540 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
541 printf("previous policy used = %d\n", res);
543 res = sched_getscheduler(getpid());
544 ATF_REQUIRE_EQ(res, 1);
545 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
546 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
548 PTHREAD_REQUIRE(pthread_join(low, NULL));
549 PTHREAD_REQUIRE(pthread_join(high, NULL));
551 ATF_REQUIRE_EQ(start, 1);
556 ATF_TC_HEAD(mutexattr1, tc)
558 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
561 ATF_TC_BODY(mutexattr1, tc)
563 pthread_mutexattr_t mattr;
564 int protocol, target;
566 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
568 target = PTHREAD_PRIO_NONE;
569 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
570 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
571 ATF_REQUIRE_EQ(protocol, target);
574 target = PTHREAD_PRIO_INHERIT;
575 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
576 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
577 ATF_REQUIRE_EQ(protocol, target);
580 target = PTHREAD_PRIO_PROTECT;
581 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
582 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
583 ATF_REQUIRE_EQ(protocol, target);
587 ATF_TC_HEAD(mutexattr2, tc)
589 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
592 ATF_TC_BODY(mutexattr2, tc)
594 pthread_mutexattr_t mattr;
597 atf_tc_expect_fail("fails on i == 0 with: "
598 "pthread_mutexattr_setprioceiling(&mattr, i): Invalid argument "
602 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
603 int max_prio = sched_get_priority_max(SCHED_FIFO);
604 int min_prio = sched_get_priority_min(SCHED_FIFO);
605 for (int i = min_prio; i <= max_prio; i++) {
609 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
612 printf("priority: %d\nprotocol: %d\n", i, protocol);
613 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
614 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
616 printf("prioceiling: %d\n", prioceiling);
617 ATF_REQUIRE_EQ(i, prioceiling);
623 ATF_TC_HEAD(timedmutex1, tc)
625 atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
628 ATF_TC_BODY(timedmutex1, tc)
631 printf("Timed mutex-test 1\n");
633 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
635 printf("Before acquiring mutex\n");
636 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
638 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
639 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
642 printf("Unlocking mutex\n");
643 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
647 ATF_TC_HEAD(timedmutex2, tc)
649 atf_tc_set_md_var(tc, "descr",
650 "Checks timeout on selflock with timedlock");
653 ATF_TC_BODY(timedmutex2, tc)
656 printf("Timed mutex-test 2\n");
658 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
660 printf("Before acquiring mutex with timedlock\n");
661 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
663 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
664 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
667 printf("Unlocking mutex\n");
668 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
672 ATF_TC_HEAD(timedmutex3, tc)
674 atf_tc_set_md_var(tc, "descr",
675 "Checks timeout on selflock in a new thread");
679 timedmtx_thrdfunc(void *arg)
681 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
682 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
688 ATF_TC_BODY(timedmutex3, tc)
692 printf("Timed mutex-test 3\n");
694 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
696 printf("Before acquiring mutex with timedlock\n");
697 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
699 printf("Before creating new thread\n");
700 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
702 printf("Before joining the mutex\n");
703 PTHREAD_REQUIRE(pthread_join(new, NULL));
705 printf("Unlocking mutex\n");
706 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
710 ATF_TC_HEAD(timedmutex4, tc)
712 atf_tc_set_md_var(tc, "descr",
713 "Checks timeout on selflock with timedlock in a new thread");
716 ATF_TC_BODY(timedmutex4, tc)
720 printf("Timed mutex-test 4\n");
722 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
724 printf("Before acquiring mutex with timedlock\n");
725 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
727 printf("Before creating new thread\n");
728 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
730 printf("Before joining the mutex\n");
731 PTHREAD_REQUIRE(pthread_join(new, NULL));
733 printf("Unlocking mutex\n");
734 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
740 ATF_TP_ADD_TC(tp, mutex1);
741 ATF_TP_ADD_TC(tp, mutex2);
742 ATF_TP_ADD_TC(tp, mutex3);
743 ATF_TP_ADD_TC(tp, mutex4);
745 ATF_TP_ADD_TC(tp, mutex5);
746 ATF_TP_ADD_TC(tp, mutex6);
748 ATF_TP_ADD_TC(tp, mutexattr1);
749 ATF_TP_ADD_TC(tp, mutexattr2);
752 ATF_TP_ADD_TC(tp, timedmutex1);
753 ATF_TP_ADD_TC(tp, timedmutex2);
754 ATF_TP_ADD_TC(tp, timedmutex3);
755 ATF_TP_ADD_TC(tp, timedmutex4);
758 return atf_no_error();