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");
157 ATF_TC_BODY(mutex2, tc)
163 printf("1: Mutex-test 2\n");
166 #if defined(__powerpc__)
167 atf_tc_expect_timeout("PR port-powerpc/44387");
171 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
174 count = count2 = 10000000;
176 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
177 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
179 printf("1: Thread %p\n", pthread_self());
181 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
184 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
186 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
189 PTHREAD_REQUIRE(pthread_join(new, &joinval));
191 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
192 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
193 global_x, (long)joinval);
194 ATF_REQUIRE_EQ(global_x, 20000000);
197 #if defined(__powerpc__)
198 /* XXX force a timeout in ppc case since an un-triggered race
199 otherwise looks like a "failure" */
200 /* We sleep for longer than the timeout to make ATF not
201 complain about unexpected success */
208 mutex3_threadfunc(void *arg)
210 long count = *(int *)arg;
212 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
215 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
217 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
220 return (void *)count;
224 ATF_TC_HEAD(mutex3, tc)
226 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
229 #if defined(__powerpc__)
230 atf_tc_set_md_var(tc, "timeout", "40");
234 ATF_TC_BODY(mutex3, tc)
240 printf("1: Mutex-test 3\n");
243 #if defined(__powerpc__)
244 atf_tc_expect_timeout("PR port-powerpc/44387");
249 count = count2 = 10000000;
251 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
252 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
254 printf("1: Thread %p\n", pthread_self());
256 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
259 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
261 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
264 PTHREAD_REQUIRE(pthread_join(new, &joinval));
266 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
267 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
268 global_x, (long)joinval);
269 ATF_REQUIRE_EQ(global_x, 20000000);
272 #if defined(__powerpc__)
273 /* XXX force a timeout in ppc case since an un-triggered race
274 otherwise looks like a "failure" */
275 /* We sleep for longer than the timeout to make ATF not
276 complain about unexpected success */
283 mutex4_threadfunc(void *arg)
287 printf("2: Second thread.\n");
290 printf("2: Locking mutex\n");
291 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
292 printf("2: Got mutex. *param = %d\n", *param);
295 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
301 ATF_TC_HEAD(mutex4, tc)
303 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
305 ATF_TC_BODY(mutex4, tc)
309 pthread_mutexattr_t mattr;
312 printf("1: Mutex-test 4\n");
314 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
315 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
317 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
319 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
322 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
323 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
325 printf("1: Before recursively acquiring the mutex.\n");
326 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
328 printf("1: Before releasing the mutex once.\n");
330 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
331 printf("1: After releasing the mutex once.\n");
335 printf("1: Before releasing the mutex twice.\n");
337 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
338 printf("1: After releasing the mutex twice.\n");
340 PTHREAD_REQUIRE(pthread_join(new, &joinval));
342 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
343 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
345 ATF_REQUIRE_EQ(x, 21);
346 ATF_REQUIRE_EQ(*(int *)joinval, 21);
347 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
351 static pthread_mutexattr_t attr5;
352 static pthread_mutex_t mutex5;
353 static int min_fifo_prio, max_fifo_prio;
356 child_func(void* arg)
360 printf("child is waiting\n");
361 res = _sched_protect(-2);
362 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
363 ATF_REQUIRE_EQ(errno, ENOENT);
364 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
365 printf("child is owning resource\n");
366 res = _sched_protect(-2);
367 ATF_REQUIRE_EQ(res, max_fifo_prio);
368 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
369 printf("child is done\n");
375 ATF_TC_HEAD(mutex5, tc)
377 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
378 atf_tc_set_md_var(tc, "require.user", "root");
381 ATF_TC_BODY(mutex5, tc)
384 struct sched_param param;
387 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
388 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
389 printf("min prio for FIFO = %d\n", min_fifo_prio);
390 param.sched_priority = min_fifo_prio;
392 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
393 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
394 printf("previous policy used = %d\n", res);
396 res = sched_getscheduler(getpid());
397 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
400 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
401 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
402 PTHREAD_PRIO_PROTECT));
403 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
406 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
407 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
408 printf("enter critical section for main\n");
409 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
410 printf("main starts to sleep\n");
412 printf("main completes\n");
413 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
414 PTHREAD_REQUIRE(pthread_join(child, NULL));
417 static pthread_mutex_t mutex6;
418 static int start = 0;
419 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
424 struct sched_param param;
426 param.sched_priority = min_fifo_prio + 10;
427 pthread_t childid = pthread_self();
429 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
430 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
431 printf("high protect = %d, prio = %d\n",
432 _sched_protect(-2), param.sched_priority);
433 ATF_REQUIRE_EQ(policy, 1);
434 printf("high prio = %d\n", param.sched_priority);
437 for (int i = 0; i < 20; i++) {
438 while (high_cnt < MAX_LOOP) {
439 tmp += (123456789 % 1234) * (987654321 % 54321);
445 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
446 if (start == 0) start = 2;
447 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
455 struct sched_param param;
457 param.sched_priority = min_fifo_prio;
458 pthread_t childid = pthread_self();
459 int res = _sched_protect(max_fifo_prio);
460 ATF_REQUIRE_EQ(res, 0);
461 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
462 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
463 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
464 param.sched_priority);
465 ATF_REQUIRE_EQ(policy, 1);
466 printf("low prio = %d\n", param.sched_priority);
469 for (int i = 0; i < 20; i++) {
470 while (low_cnt < MAX_LOOP) {
471 tmp += (123456789 % 1234) * (987654321 % 54321);
477 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
480 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
486 ATF_TC_HEAD(mutex6, tc)
488 atf_tc_set_md_var(tc, "descr",
489 "Checks scheduling for priority ceiling");
490 atf_tc_set_md_var(tc, "require.user", "root");
494 * 1. main thread sets itself to be a realtime task and launched two tasks,
495 * one has higher priority and the other has lower priority.
496 * 2. each child thread(low and high priority thread) sets its scheduler and
498 * 3. each child thread did several rounds of computation, after each round it
500 * 4. the child thread with low priority will call _sched_protect to increase
501 * its protect priority.
502 * 5. We verify the thread with low priority runs first.
504 * Why does it work? From the main thread, we launched the high
505 * priority thread first. This gives this thread the benefit of
506 * starting first. The low priority thread did not call _sched_protect(2).
507 * The high priority thread should finish the task first. After each
508 * round of computation, we call sleep, to put the task into the
509 * sleep queue, and wake up again after the timer expires. This
510 * gives the scheduler the chance to decide which task to run. So,
511 * the thread with real high priority will always block the thread
512 * with real low priority.
515 ATF_TC_BODY(mutex6, tc)
517 struct sched_param param;
521 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
522 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
523 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
524 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
527 param.sched_priority = min_fifo_prio;
528 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
529 printf("previous policy used = %d\n", res);
531 res = sched_getscheduler(getpid());
532 ATF_REQUIRE_EQ(res, 1);
533 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
534 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
536 PTHREAD_REQUIRE(pthread_join(low, NULL));
537 PTHREAD_REQUIRE(pthread_join(high, NULL));
539 ATF_REQUIRE_EQ(start, 1);
544 ATF_TC_HEAD(mutexattr1, tc)
546 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
549 ATF_TC_BODY(mutexattr1, tc)
551 pthread_mutexattr_t mattr;
552 int protocol, target;
554 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
556 target = PTHREAD_PRIO_NONE;
557 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
558 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
559 ATF_REQUIRE_EQ(protocol, target);
562 target = PTHREAD_PRIO_INHERIT;
563 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
564 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
565 ATF_REQUIRE_EQ(protocol, target);
568 target = PTHREAD_PRIO_PROTECT;
569 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
570 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
571 ATF_REQUIRE_EQ(protocol, target);
575 ATF_TC_HEAD(mutexattr2, tc)
577 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
580 ATF_TC_BODY(mutexattr2, tc)
582 pthread_mutexattr_t mattr;
585 atf_tc_expect_fail("fails on i == 0 with: "
586 "pthread_mutexattr_setprioceiling(&mattr, i): Invalid argument "
590 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
591 int max_prio = sched_get_priority_max(SCHED_FIFO);
592 int min_prio = sched_get_priority_min(SCHED_FIFO);
593 for (int i = min_prio; i <= max_prio; i++) {
597 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
600 printf("priority: %d\nprotocol: %d\n", i, protocol);
601 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
602 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
604 printf("prioceiling: %d\n", prioceiling);
605 ATF_REQUIRE_EQ(i, prioceiling);
611 ATF_TC_HEAD(timedmutex1, tc)
613 atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
616 ATF_TC_BODY(timedmutex1, tc)
619 printf("Timed mutex-test 1\n");
621 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
623 printf("Before acquiring mutex\n");
624 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
626 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
627 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
630 printf("Unlocking mutex\n");
631 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
635 ATF_TC_HEAD(timedmutex2, tc)
637 atf_tc_set_md_var(tc, "descr",
638 "Checks timeout on selflock with timedlock");
641 ATF_TC_BODY(timedmutex2, tc)
644 printf("Timed mutex-test 2\n");
646 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
648 printf("Before acquiring mutex with timedlock\n");
649 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
651 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
652 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
655 printf("Unlocking mutex\n");
656 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
660 ATF_TC_HEAD(timedmutex3, tc)
662 atf_tc_set_md_var(tc, "descr",
663 "Checks timeout on selflock in a new thread");
667 timedmtx_thrdfunc(void *arg)
669 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
670 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
676 ATF_TC_BODY(timedmutex3, tc)
680 printf("Timed mutex-test 3\n");
682 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
684 printf("Before acquiring mutex with timedlock\n");
685 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
687 printf("Before creating new thread\n");
688 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
690 printf("Before joining the mutex\n");
691 PTHREAD_REQUIRE(pthread_join(new, NULL));
693 printf("Unlocking mutex\n");
694 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
698 ATF_TC_HEAD(timedmutex4, tc)
700 atf_tc_set_md_var(tc, "descr",
701 "Checks timeout on selflock with timedlock in a new thread");
704 ATF_TC_BODY(timedmutex4, tc)
708 printf("Timed mutex-test 4\n");
710 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
712 printf("Before acquiring mutex with timedlock\n");
713 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
715 printf("Before creating new thread\n");
716 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
718 printf("Before joining the mutex\n");
719 PTHREAD_REQUIRE(pthread_join(new, NULL));
721 printf("Unlocking mutex\n");
722 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
728 ATF_TP_ADD_TC(tp, mutex1);
729 ATF_TP_ADD_TC(tp, mutex2);
730 ATF_TP_ADD_TC(tp, mutex3);
731 ATF_TP_ADD_TC(tp, mutex4);
733 ATF_TP_ADD_TC(tp, mutex5);
734 ATF_TP_ADD_TC(tp, mutex6);
736 ATF_TP_ADD_TC(tp, mutexattr1);
737 ATF_TP_ADD_TC(tp, mutexattr2);
740 ATF_TP_ADD_TC(tp, timedmutex1);
741 ATF_TP_ADD_TC(tp, timedmutex2);
742 ATF_TP_ADD_TC(tp, timedmutex3);
743 ATF_TP_ADD_TC(tp, timedmutex4);
746 return atf_no_error();