t_mutex.c revision 1.15 1 /* $NetBSD: t_mutex.c,v 1.15 2017/01/16 16:23:41 christos Exp $ */
2
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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.
27 */
28
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 $");
33
34 #include <sys/time.h> /* For timespecadd */
35 #include <inttypes.h> /* For UINT16_MAX */
36 #include <pthread.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <time.h>
41 #include <unistd.h>
42 #include <sys/sched.h>
43 #include <sys/param.h>
44
45 #include <atf-c.h>
46
47 #include "h_common.h"
48
49 static pthread_mutex_t mutex;
50 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
51 static int global_x;
52
53 #ifdef TIMEDMUTEX
54 /* This code is used for verifying non-timed specific code */
55 static struct timespec ts_lengthy = {
56 .tv_sec = UINT16_MAX,
57 .tv_nsec = 0
58 };
59 /* This code is used for verifying timed-only specific code */
60 static struct timespec ts_shortlived = {
61 .tv_sec = 0,
62 .tv_nsec = 120
63 };
64
65 static int
66 mutex_lock(pthread_mutex_t *m, const struct timespec *ts)
67 {
68 struct timespec ts_wait;
69 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1);
70 timespecadd(&ts_wait, ts, &ts_wait);
71
72 return pthread_mutex_timedlock(m, &ts_wait);
73 }
74 #else
75 #define mutex_lock(a, b) pthread_mutex_lock(a)
76 #endif
77
78 static void *
79 mutex1_threadfunc(void *arg)
80 {
81 int *param;
82
83 printf("2: Second thread.\n");
84
85 param = arg;
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);
90 (*param)++;
91
92 pthread_mutex_unlock(&mutex);
93
94 return param;
95 }
96
97 ATF_TC(mutex1);
98 ATF_TC_HEAD(mutex1, tc)
99 {
100 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
101 }
102 ATF_TC_BODY(mutex1, tc)
103 {
104 int x;
105 pthread_t new;
106 void *joinval;
107
108 printf("1: Mutex-test 1\n");
109
110 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
111 x = 1;
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");
115 sleep(2);
116 x = 20;
117 printf("1: Before releasing the mutex.\n");
118 sleep(2);
119 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
120 printf("1: After releasing the mutex.\n");
121 PTHREAD_REQUIRE(pthread_join(new, &joinval));
122
123 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
124 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
125 x, *(int *)joinval);
126 ATF_REQUIRE_EQ(x, 21);
127 ATF_REQUIRE_EQ(*(int *)joinval, 21);
128 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
129 }
130
131 static void *
132 mutex2_threadfunc(void *arg)
133 {
134 long count = *(int *)arg;
135
136 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
137
138 while (count--) {
139 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
140 global_x++;
141 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
142 }
143
144 return (void *)count;
145 }
146
147 ATF_TC(mutex2);
148 ATF_TC_HEAD(mutex2, tc)
149 {
150 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
151 #if defined(__powerpc__)
152 atf_tc_set_md_var(tc, "timeout", "40");
153 #endif
154 }
155 ATF_TC_BODY(mutex2, tc)
156 {
157 int count, count2;
158 pthread_t new;
159 void *joinval;
160
161 printf("1: Mutex-test 2\n");
162
163 #if defined(__powerpc__)
164 atf_tc_expect_timeout("PR port-powerpc/44387");
165 #endif
166
167 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
168
169 global_x = 0;
170 count = count2 = 10000000;
171
172 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
173 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
174
175 printf("1: Thread %p\n", pthread_self());
176
177 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
178
179 while (count--) {
180 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
181 global_x++;
182 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
183 }
184
185 PTHREAD_REQUIRE(pthread_join(new, &joinval));
186
187 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
188 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
189 global_x, (long)joinval);
190 ATF_REQUIRE_EQ(global_x, 20000000);
191
192 #if defined(__powerpc__)
193 /* XXX force a timeout in ppc case since an un-triggered race
194 otherwise looks like a "failure" */
195 /* We sleep for longer than the timeout to make ATF not
196 complain about unexpected success */
197 sleep(41);
198 #endif
199 }
200
201 static void *
202 mutex3_threadfunc(void *arg)
203 {
204 long count = *(int *)arg;
205
206 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
207
208 while (count--) {
209 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
210 global_x++;
211 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
212 }
213
214 return (void *)count;
215 }
216
217 ATF_TC(mutex3);
218 ATF_TC_HEAD(mutex3, tc)
219 {
220 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
221 "initializer");
222 #if defined(__powerpc__)
223 atf_tc_set_md_var(tc, "timeout", "40");
224 #endif
225 }
226 ATF_TC_BODY(mutex3, tc)
227 {
228 int count, count2;
229 pthread_t new;
230 void *joinval;
231
232 printf("1: Mutex-test 3\n");
233
234 #if defined(__powerpc__)
235 atf_tc_expect_timeout("PR port-powerpc/44387");
236 #endif
237
238 global_x = 0;
239 count = count2 = 10000000;
240
241 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
242 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
243
244 printf("1: Thread %p\n", pthread_self());
245
246 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
247
248 while (count--) {
249 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
250 global_x++;
251 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
252 }
253
254 PTHREAD_REQUIRE(pthread_join(new, &joinval));
255
256 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
257 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
258 global_x, (long)joinval);
259 ATF_REQUIRE_EQ(global_x, 20000000);
260
261 #if defined(__powerpc__)
262 /* XXX force a timeout in ppc case since an un-triggered race
263 otherwise looks like a "failure" */
264 /* We sleep for longer than the timeout to make ATF not
265 complain about unexpected success */
266 sleep(41);
267 #endif
268 }
269
270 static void *
271 mutex4_threadfunc(void *arg)
272 {
273 int *param;
274
275 printf("2: Second thread.\n");
276
277 param = arg;
278 printf("2: Locking mutex\n");
279 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
280 printf("2: Got mutex. *param = %d\n", *param);
281 (*param)++;
282
283 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
284
285 return param;
286 }
287
288 ATF_TC(mutex4);
289 ATF_TC_HEAD(mutex4, tc)
290 {
291 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
292 }
293 ATF_TC_BODY(mutex4, tc)
294 {
295 int x;
296 pthread_t new;
297 pthread_mutexattr_t mattr;
298 void *joinval;
299
300 printf("1: Mutex-test 4\n");
301
302 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
303 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
304
305 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
306
307 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
308
309 x = 1;
310 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
311 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
312
313 printf("1: Before recursively acquiring the mutex.\n");
314 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
315
316 printf("1: Before releasing the mutex once.\n");
317 sleep(2);
318 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
319 printf("1: After releasing the mutex once.\n");
320
321 x = 20;
322
323 printf("1: Before releasing the mutex twice.\n");
324 sleep(2);
325 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
326 printf("1: After releasing the mutex twice.\n");
327
328 PTHREAD_REQUIRE(pthread_join(new, &joinval));
329
330 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
331 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
332 x, *(int *)joinval);
333 ATF_REQUIRE_EQ(x, 21);
334 ATF_REQUIRE_EQ(*(int *)joinval, 21);
335 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
336 }
337
338 static pthread_mutexattr_t attr5;
339 static pthread_mutex_t mutex5;
340 static int min_fifo_prio, max_fifo_prio;
341
342 static void *
343 child_func(void* arg)
344 {
345 int res;
346
347 printf("child is waiting\n");
348 res = _sched_protect(-2);
349 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res);
350 ATF_REQUIRE_EQ(errno, ENOENT);
351 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
352 printf("child is owning resource\n");
353 res = _sched_protect(-2);
354 ATF_REQUIRE_EQ(res, max_fifo_prio);
355 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
356 printf("child is done\n");
357
358 return 0;
359 }
360
361 ATF_TC(mutex5);
362 ATF_TC_HEAD(mutex5, tc)
363 {
364 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
365 atf_tc_set_md_var(tc, "require.user", "root");
366 }
367
368 ATF_TC_BODY(mutex5, tc)
369 {
370 int res;
371 struct sched_param param;
372 pthread_t child;
373
374 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
375 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
376 printf("min prio for FIFO = %d\n", min_fifo_prio);
377 param.sched_priority = min_fifo_prio;
378
379 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
380 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
381 printf("previous policy used = %d\n", res);
382
383 res = sched_getscheduler(getpid());
384 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
385 SCHED_FIFO);
386
387 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5));
388 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5,
389 PTHREAD_PRIO_PROTECT));
390 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5,
391 max_fifo_prio));
392
393 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5));
394 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy));
395 printf("enter critical section for main\n");
396 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL));
397 printf("main starts to sleep\n");
398 sleep(10);
399 printf("main completes\n");
400 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
401 PTHREAD_REQUIRE(pthread_join(child, NULL));
402 }
403
404 static pthread_mutex_t mutex6;
405 static int start = 0;
406 static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
407
408 static void *
409 high_prio(void* arg)
410 {
411 struct sched_param param;
412 int policy;
413 param.sched_priority = min_fifo_prio + 10;
414 pthread_t childid = pthread_self();
415
416 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
417 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
418 printf("high protect = %d, prio = %d\n",
419 _sched_protect(-2), param.sched_priority);
420 ATF_REQUIRE_EQ(policy, 1);
421 printf("high prio = %d\n", param.sched_priority);
422 sleep(1);
423 long tmp = 0;
424 for (int i = 0; i < 20; i++) {
425 while (high_cnt < MAX_LOOP) {
426 tmp += (123456789 % 1234) * (987654321 % 54321);
427 high_cnt += 1;
428 }
429 high_cnt = 0;
430 sleep(1);
431 }
432 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
433 if (start == 0) start = 2;
434 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
435
436 return 0;
437 }
438
439 static void *
440 low_prio(void* arg)
441 {
442 struct sched_param param;
443 int policy;
444 param.sched_priority = min_fifo_prio;
445 pthread_t childid = pthread_self();
446 int res = _sched_protect(max_fifo_prio);
447 ATF_REQUIRE_EQ(res, 0);
448 PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, ¶m));
449 PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, ¶m));
450 printf("low protect = %d, prio = %d\n", _sched_protect(-2),
451 param.sched_priority);
452 ATF_REQUIRE_EQ(policy, 1);
453 printf("low prio = %d\n", param.sched_priority);
454 sleep(1);
455 long tmp = 0;
456 for (int i = 0; i < 20; i++) {
457 while (low_cnt < MAX_LOOP) {
458 tmp += (123456789 % 1234) * (987654321 % 54321);
459 low_cnt += 1;
460 }
461 low_cnt = 0;
462 sleep(1);
463 }
464 PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
465 if (start == 0)
466 start = 1;
467 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
468
469 return 0;
470 }
471
472 ATF_TC(mutex6);
473 ATF_TC_HEAD(mutex6, tc)
474 {
475 atf_tc_set_md_var(tc, "descr",
476 "Checks scheduling for priority ceiling");
477 atf_tc_set_md_var(tc, "require.user", "root");
478 }
479
480 /*
481 * 1. main thread sets itself to be a realtime task and launched two tasks,
482 * one has higher priority and the other has lower priority.
483 * 2. each child thread(low and high priority thread) sets its scheduler and
484 * priority.
485 * 3. each child thread did several rounds of computation, after each round it
486 * sleep 1 second.
487 * 4. the child thread with low priority will call _sched_protect to increase
488 * its protect priority.
489 * 5. We verify the thread with low priority runs first.
490 *
491 * Why does it work? From the main thread, we launched the high
492 * priority thread first. This gives this thread the benefit of
493 * starting first. The low priority thread did not call _sched_protect(2).
494 * The high priority thread should finish the task first. After each
495 * round of computation, we call sleep, to put the task into the
496 * sleep queue, and wake up again after the timer expires. This
497 * gives the scheduler the chance to decide which task to run. So,
498 * the thread with real high priority will always block the thread
499 * with real low priority.
500 *
501 */
502 ATF_TC_BODY(mutex6, tc)
503 {
504 struct sched_param param;
505 int res;
506 pthread_t high, low;
507
508 min_fifo_prio = sched_get_priority_min(SCHED_FIFO);
509 max_fifo_prio = sched_get_priority_max(SCHED_FIFO);
510 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
511 printf("min_fifo_prio = %d, max_fifo_info = %d\n", min_fifo_prio,
512 max_fifo_prio);
513
514 param.sched_priority = min_fifo_prio;
515 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m);
516 printf("previous policy used = %d\n", res);
517
518 res = sched_getscheduler(getpid());
519 ATF_REQUIRE_EQ(res, 1);
520 PTHREAD_REQUIRE(pthread_create(&high, NULL, high_prio, NULL));
521 PTHREAD_REQUIRE(pthread_create(&low, NULL, low_prio, NULL));
522 sleep(5);
523 PTHREAD_REQUIRE(pthread_join(low, NULL));
524 PTHREAD_REQUIRE(pthread_join(high, NULL));
525
526 ATF_REQUIRE_EQ(start, 1);
527 }
528
529 ATF_TC(mutexattr1);
530 ATF_TC_HEAD(mutexattr1, tc)
531 {
532 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
533 }
534
535 ATF_TC_BODY(mutexattr1, tc)
536 {
537 pthread_mutexattr_t mattr;
538 int protocol, target;
539
540 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
541
542 target = PTHREAD_PRIO_NONE;
543 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
544 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
545 ATF_REQUIRE_EQ(protocol, target);
546
547 /*
548 target = PTHREAD_PRIO_INHERIT;
549 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
550 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
551 ATF_REQUIRE_EQ(protocol, target);
552 */
553
554 target = PTHREAD_PRIO_PROTECT;
555 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target));
556 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol));
557 ATF_REQUIRE_EQ(protocol, target);
558 }
559
560 ATF_TC(mutexattr2);
561 ATF_TC_HEAD(mutexattr2, tc)
562 {
563 atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
564 }
565
566 ATF_TC_BODY(mutexattr2, tc)
567 {
568 pthread_mutexattr_t mattr;
569
570 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
571 int max_prio = sched_get_priority_max(SCHED_FIFO);
572 int min_prio = sched_get_priority_min(SCHED_FIFO);
573 for (int i = min_prio; i <= max_prio; i++) {
574 int prioceiling;
575 int protocol;
576
577 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
578 &protocol));
579
580 printf("priority: %d\nprotocol: %d\n", i, protocol);
581 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
582 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
583 &prioceiling));
584 printf("prioceiling: %d\n", prioceiling);
585 ATF_REQUIRE_EQ(i, prioceiling);
586 }
587 }
588
589 #ifdef TIMEDMUTEX
590 ATF_TC(timedmutex1);
591 ATF_TC_HEAD(timedmutex1, tc)
592 {
593 atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
594 }
595
596 ATF_TC_BODY(timedmutex1, tc)
597 {
598
599 printf("Timed mutex-test 1\n");
600
601 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
602
603 printf("Before acquiring mutex\n");
604 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
605
606 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
607 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
608 ETIMEDOUT);
609
610 printf("Unlocking mutex\n");
611 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
612 }
613
614 ATF_TC(timedmutex2);
615 ATF_TC_HEAD(timedmutex2, tc)
616 {
617 atf_tc_set_md_var(tc, "descr",
618 "Checks timeout on selflock with timedlock");
619 }
620
621 ATF_TC_BODY(timedmutex2, tc)
622 {
623
624 printf("Timed mutex-test 2\n");
625
626 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
627
628 printf("Before acquiring mutex with timedlock\n");
629 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
630
631 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
632 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
633 ETIMEDOUT);
634
635 printf("Unlocking mutex\n");
636 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
637 }
638
639 ATF_TC(timedmutex3);
640 ATF_TC_HEAD(timedmutex3, tc)
641 {
642 atf_tc_set_md_var(tc, "descr",
643 "Checks timeout on selflock in a new thread");
644 }
645
646 static void *
647 timedmtx_thrdfunc(void *arg)
648 {
649 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
650 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
651 ETIMEDOUT);
652
653 return NULL;
654 }
655
656 ATF_TC_BODY(timedmutex3, tc)
657 {
658 pthread_t new;
659
660 printf("Timed mutex-test 3\n");
661
662 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
663
664 printf("Before acquiring mutex with timedlock\n");
665 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
666
667 printf("Before creating new thread\n");
668 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
669
670 printf("Before joining the mutex\n");
671 PTHREAD_REQUIRE(pthread_join(new, NULL));
672
673 printf("Unlocking mutex\n");
674 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
675 }
676
677 ATF_TC(timedmutex4);
678 ATF_TC_HEAD(timedmutex4, tc)
679 {
680 atf_tc_set_md_var(tc, "descr",
681 "Checks timeout on selflock with timedlock in a new thread");
682 }
683
684 ATF_TC_BODY(timedmutex4, tc)
685 {
686 pthread_t new;
687
688 printf("Timed mutex-test 4\n");
689
690 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
691
692 printf("Before acquiring mutex with timedlock\n");
693 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
694
695 printf("Before creating new thread\n");
696 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
697
698 printf("Before joining the mutex\n");
699 PTHREAD_REQUIRE(pthread_join(new, NULL));
700
701 printf("Unlocking mutex\n");
702 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
703 }
704 #endif
705
706 ATF_TP_ADD_TCS(tp)
707 {
708 ATF_TP_ADD_TC(tp, mutex1);
709 ATF_TP_ADD_TC(tp, mutex2);
710 ATF_TP_ADD_TC(tp, mutex3);
711 ATF_TP_ADD_TC(tp, mutex4);
712 ATF_TP_ADD_TC(tp, mutex5);
713 ATF_TP_ADD_TC(tp, mutex6);
714 ATF_TP_ADD_TC(tp, mutexattr1);
715 ATF_TP_ADD_TC(tp, mutexattr2);
716
717 #ifdef TIMEDMUTEX
718 ATF_TP_ADD_TC(tp, timedmutex1);
719 ATF_TP_ADD_TC(tp, timedmutex2);
720 ATF_TP_ADD_TC(tp, timedmutex3);
721 ATF_TP_ADD_TC(tp, timedmutex4);
722 #endif
723
724 return atf_no_error();
725 }
726