1 /* $NetBSD: t_mutex.c,v 1.14 2016/10/31 23:51:20 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.14 2016/10/31 23:51:20 christos Exp $");
35 #include <sys/time.h> /* For timespecadd */
36 #include <inttypes.h> /* For UINT16_MAX */
44 #include <sys/sched.h>
45 #include <sys/param.h>
51 static pthread_mutex_t mutex;
52 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
56 /* This code is used for verifying non-timed specific code */
57 static struct timespec ts_lengthy = {
61 /* This code is used for verifying timed-only specific code */
62 static struct timespec ts_shortlived = {
68 mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
70 struct timespec ts_wait;
71 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
72 timespecadd(&ts_wait, ts, &ts_wait);
74 return pthread_mutex_timedlock(m, &ts_wait);
77 #define mutex_lock(a, b) pthread_mutex_lock(a)
81 mutex1_threadfunc(void *arg)
85 printf("2: Second thread.\n");
88 printf("2: Locking mutex\n");
89 mutex_lock(&mutex, &ts_lengthy);
90 printf("2: Got mutex. *param = %d\n", *param);
91 ATF_REQUIRE_EQ(*param, 20);
94 pthread_mutex_unlock(&mutex);
100 ATF_TC_HEAD(mutex1, tc)
102 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
104 ATF_TC_BODY(mutex1, tc)
110 printf("1: Mutex-test 1\n");
112 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
114 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
115 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
116 printf("1: Before changing the value.\n");
119 printf("1: Before releasing the mutex.\n");
121 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
122 printf("1: After releasing the mutex.\n");
123 PTHREAD_REQUIRE(pthread_join(new, &joinval));
125 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
126 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
128 ATF_REQUIRE_EQ(x, 21);
129 ATF_REQUIRE_EQ(*(int *)joinval, 21);
130 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
134 mutex2_threadfunc(void *arg)
136 long count = *(int *)arg;
138 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
141 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
143 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
146 return (void *)count;
150 ATF_TC_HEAD(mutex2, tc)
152 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
154 #if defined(__powerpc__)
155 atf_tc_set_md_var(tc, "timeout", "40");
159 ATF_TC_BODY(mutex2, tc)
165 printf("1: Mutex-test 2\n");
168 #if defined(__powerpc__)
169 atf_tc_expect_timeout("PR port-powerpc/44387");
173 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
176 count = count2 = 10000000;
178 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
179 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
181 printf("1: Thread %p\n", pthread_self());
183 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
186 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
188 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
191 PTHREAD_REQUIRE(pthread_join(new, &joinval));
193 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
194 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
195 global_x, (long)joinval);
196 ATF_REQUIRE_EQ(global_x, 20000000);
199 #if defined(__powerpc__)
200 /* XXX force a timeout in ppc case since an un-triggered race
201 otherwise looks like a "failure" */
202 /* We sleep for longer than the timeout to make ATF not
203 complain about unexpected success */
210 mutex3_threadfunc(void *arg)
212 long count = *(int *)arg;
214 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
217 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
219 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
222 return (void *)count;
226 ATF_TC_HEAD(mutex3, tc)
228 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
231 #if defined(__powerpc__)
232 atf_tc_set_md_var(tc, "timeout", "40");
236 ATF_TC_BODY(mutex3, tc)
242 printf("1: Mutex-test 3\n");
245 #if defined(__powerpc__)
246 atf_tc_expect_timeout("PR port-powerpc/44387");
251 count = count2 = 10000000;
253 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
254 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
256 printf("1: Thread %p\n", pthread_self());
258 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
261 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
263 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
266 PTHREAD_REQUIRE(pthread_join(new, &joinval));
268 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
269 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
270 global_x, (long)joinval);
271 ATF_REQUIRE_EQ(global_x, 20000000);
274 #if defined(__powerpc__)
275 /* XXX force a timeout in ppc case since an un-triggered race
276 otherwise looks like a "failure" */
277 /* We sleep for longer than the timeout to make ATF not
278 complain about unexpected success */
285 mutex4_threadfunc(void *arg)
289 printf("2: Second thread.\n");
292 printf("2: Locking mutex\n");
293 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
294 printf("2: Got mutex. *param = %d\n", *param);
297 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
303 ATF_TC_HEAD(mutex4, tc)
305 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
307 ATF_TC_BODY(mutex4, tc)
311 pthread_mutexattr_t mattr;
314 printf("1: Mutex-test 4\n");
316 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
317 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
319 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
321 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
324 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
325 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
327 printf("1: Before recursively acquiring the mutex.\n");
328 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
330 printf("1: Before releasing the mutex once.\n");
332 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
333 printf("1: After releasing the mutex once.\n");
337 printf("1: Before releasing the mutex twice.\n");
339 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
340 printf("1: After releasing the mutex twice.\n");
342 PTHREAD_REQUIRE(pthread_join(new, &joinval));
344 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
345 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
347 ATF_REQUIRE_EQ(x, 21);
348 ATF_REQUIRE_EQ(*(int *)joinval, 21);
349 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
353 static pthread_mutexattr_t attr5;
354 static pthread_mutex_t mutex5;
355 static int min_fifo_prio, max_fifo_prio;
358 child_func(void* arg)
362 printf("child is waiting\n");
363 res = _sched_protect(-2);
364 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
365 ATF_REQUIRE_EQ(errno, ENOENT);
366 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
367 printf("child is owning resource\n");
368 res = _sched_protect(-2);
369 ATF_REQUIRE_EQ(res, max_fifo_prio);
370 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
371 printf("child is done\n");
377 ATF_TC_HEAD(mutex5, tc)
379 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
380 atf_tc_set_md_var(tc, "require.user", "root");
383 ATF_TC_BODY(mutex5, tc)
386 struct sched_param param;
389 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
390 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
391 printf("min prio for FIFO = %d\n", min_fifo_prio);
392 param.sched_priority = min_fifo_prio;
394 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
395 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
396 printf("previous policy used = %d\n", res);
398 res = sched_getscheduler(getpid());
399 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
402 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
403 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
404 PTHREAD_PRIO_PROTECT));
405 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
408 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
409 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
410 printf("enter critical section for main\n");
411 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
412 printf("main starts to sleep\n");
414 printf("main completes\n");
415 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
416 PTHREAD_REQUIRE(pthread_join(child, NULL));
419 static pthread_mutex_t mutex6;
420 static int start = 0;
421 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
426 struct sched_param param;
428 param.sched_priority = min_fifo_prio + 10;
429 pthread_t childid = pthread_self();
431 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
432 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
433 printf("high protect = %d, prio = %d\n",
434 _sched_protect(-2), param.sched_priority);
435 ATF_REQUIRE_EQ(policy, 1);
436 printf("high prio = %d\n", param.sched_priority);
439 for (int i = 0; i < 20; i++) {
440 while (high_cnt < MAX_LOOP) {
441 tmp += (123456789 % 1234) * (987654321 % 54321);
447 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
448 if (start == 0) start = 2;
449 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
457 struct sched_param param;
459 param.sched_priority = min_fifo_prio;
460 pthread_t childid = pthread_self();
461 int res = _sched_protect(max_fifo_prio);
462 ATF_REQUIRE_EQ(res, 0);
463 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
464 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
465 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
466 param.sched_priority);
467 ATF_REQUIRE_EQ(policy, 1);
468 printf("low prio = %d\n", param.sched_priority);
471 for (int i = 0; i < 20; i++) {
472 while (low_cnt < MAX_LOOP) {
473 tmp += (123456789 % 1234) * (987654321 % 54321);
479 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
482 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
488 ATF_TC_HEAD(mutex6, tc)
490 atf_tc_set_md_var(tc, "descr",
491 "Checks scheduling for priority ceiling");
492 atf_tc_set_md_var(tc, "require.user", "root");
496 * 1. main thread sets itself to be a realtime task and launched two tasks,
497 * one has higher priority and the other has lower priority.
498 * 2. each child thread(low and high priority thread) sets its scheduler and
500 * 3. each child thread did several rounds of computation, after each round it
502 * 4. the child thread with low priority will call _sched_protect to increase
503 * its protect priority.
504 * 5. We verify the thread with low priority runs first.
506 * Why does it work? From the main thread, we launched the high
507 * priority thread first. This gives this thread the benefit of
508 * starting first. The low priority thread did not call _sched_protect(2).
509 * The high priority thread should finish the task first. After each
510 * round of computation, we call sleep, to put the task into the
511 * sleep queue, and wake up again after the timer expires. This
512 * gives the scheduler the chance to decide which task to run. So,
513 * the thread with real high priority will always block the thread
514 * with real low priority.
517 ATF_TC_BODY(mutex6, tc)
519 struct sched_param param;
523 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
524 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
525 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
526 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
529 param.sched_priority = min_fifo_prio;
530 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
531 printf("previous policy used = %d\n", res);
533 res = sched_getscheduler(getpid());
534 ATF_REQUIRE_EQ(res, 1);
535 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
536 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
538 PTHREAD_REQUIRE(pthread_join(low, NULL));
539 PTHREAD_REQUIRE(pthread_join(high, NULL));
541 ATF_REQUIRE_EQ(start, 1);
546 ATF_TC_HEAD(mutexattr1, tc)
548 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
551 ATF_TC_BODY(mutexattr1, tc)
553 pthread_mutexattr_t mattr;
554 int protocol, target;
556 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
558 target = PTHREAD_PRIO_NONE;
559 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
560 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
561 ATF_REQUIRE_EQ(protocol, target);
564 target = PTHREAD_PRIO_INHERIT;
565 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
566 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
567 ATF_REQUIRE_EQ(protocol, target);
570 target = PTHREAD_PRIO_PROTECT;
571 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
572 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
573 ATF_REQUIRE_EQ(protocol, target);
577 ATF_TC_HEAD(mutexattr2, tc)
579 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
582 ATF_TC_BODY(mutexattr2, tc)
584 pthread_mutexattr_t mattr;
587 atf_tc_expect_fail("fails on i == 0 with: "
588 "pthread_mutexattr_setprioceiling(&mattr, i): Invalid argument "
592 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
593 int max_prio = sched_get_priority_max(SCHED_FIFO);
594 int min_prio = sched_get_priority_min(SCHED_FIFO);
595 for (int i = min_prio; i <= max_prio; i++) {
600 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
603 printf("priority: %d\nprotocol: %d\n", i, protocol);
605 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
606 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
609 printf("prioceiling: %d\n", prioceiling);
611 ATF_REQUIRE_EQ(i, prioceiling);
617 ATF_TC_HEAD(timedmutex1, tc)
619 atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
622 ATF_TC_BODY(timedmutex1, tc)
625 printf("Timed mutex-test 1\n");
627 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
629 printf("Before acquiring mutex\n");
630 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
632 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
633 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
636 printf("Unlocking mutex\n");
637 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
641 ATF_TC_HEAD(timedmutex2, tc)
643 atf_tc_set_md_var(tc, "descr",
644 "Checks timeout on selflock with timedlock");
647 ATF_TC_BODY(timedmutex2, tc)
650 printf("Timed mutex-test 2\n");
652 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
654 printf("Before acquiring mutex with timedlock\n");
655 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
657 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
658 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
661 printf("Unlocking mutex\n");
662 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
666 ATF_TC_HEAD(timedmutex3, tc)
668 atf_tc_set_md_var(tc, "descr",
669 "Checks timeout on selflock in a new thread");
673 timedmtx_thrdfunc(void *arg)
675 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
676 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
682 ATF_TC_BODY(timedmutex3, tc)
686 printf("Timed mutex-test 3\n");
688 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
690 printf("Before acquiring mutex with timedlock\n");
691 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
693 printf("Before creating new thread\n");
694 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
696 printf("Before joining the mutex\n");
697 PTHREAD_REQUIRE(pthread_join(new, NULL));
699 printf("Unlocking mutex\n");
700 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
704 ATF_TC_HEAD(timedmutex4, tc)
706 atf_tc_set_md_var(tc, "descr",
707 "Checks timeout on selflock with timedlock in a new thread");
710 ATF_TC_BODY(timedmutex4, tc)
714 printf("Timed mutex-test 4\n");
716 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
718 printf("Before acquiring mutex with timedlock\n");
719 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
721 printf("Before creating new thread\n");
722 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
724 printf("Before joining the mutex\n");
725 PTHREAD_REQUIRE(pthread_join(new, NULL));
727 printf("Unlocking mutex\n");
728 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
734 ATF_TP_ADD_TC(tp, mutex1);
735 ATF_TP_ADD_TC(tp, mutex2);
736 ATF_TP_ADD_TC(tp, mutex3);
737 ATF_TP_ADD_TC(tp, mutex4);
739 ATF_TP_ADD_TC(tp, mutex5);
740 ATF_TP_ADD_TC(tp, mutex6);
742 ATF_TP_ADD_TC(tp, mutexattr1);
743 ATF_TP_ADD_TC(tp, mutexattr2);
746 ATF_TP_ADD_TC(tp, timedmutex1);
747 ATF_TP_ADD_TC(tp, timedmutex2);
748 ATF_TP_ADD_TC(tp, timedmutex3);
749 ATF_TP_ADD_TC(tp, timedmutex4);
752 return atf_no_error();