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
49static pthread_mutex_t mutex;
50static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
51static int global_x;
52
53#ifdef TIMEDMUTEX
54/* This code is used for verifying non-timed specific code */
55static 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 */
60static struct timespec ts_shortlived = {
61	.tv_sec = 0,
62	.tv_nsec = 120
63};
64
65static int
66mutex_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
78static void *
79mutex1_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
97ATF_TC(mutex1);
98ATF_TC_HEAD(mutex1, tc)
99{
100	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
101}
102ATF_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
131static void *
132mutex2_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
147ATF_TC(mutex2);
148ATF_TC_HEAD(mutex2, tc)
149{
150	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
151#ifdef __NetBSD__
152#if defined(__powerpc__)
153	atf_tc_set_md_var(tc, "timeout", "40");
154#endif
155#endif
156
157#ifdef __FreeBSD__
158#if defined(__riscv)
159	atf_tc_set_md_var(tc, "timeout", "600");
160#endif
161#endif
162}
163ATF_TC_BODY(mutex2, tc)
164{
165	int count, count2;
166	pthread_t new;
167	void *joinval;
168
169	printf("1: Mutex-test 2\n");
170
171#ifdef __NetBSD__
172#if defined(__powerpc__)
173	atf_tc_expect_timeout("PR port-powerpc/44387");
174#endif
175#endif
176
177	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
178
179	global_x = 0;
180	count = count2 = 10000000;
181
182	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
183	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
184
185	printf("1: Thread %p\n", pthread_self());
186
187	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
188
189	while (count--) {
190		PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
191		global_x++;
192		PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
193	}
194
195	PTHREAD_REQUIRE(pthread_join(new, &joinval));
196
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);
201
202#ifdef __NetBSD__
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 */
208	sleep(41);
209#endif
210#endif
211}
212
213static void *
214mutex3_threadfunc(void *arg)
215{
216	long count = *(int *)arg;
217
218	printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
219
220	while (count--) {
221		PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
222		global_x++;
223		PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
224	}
225
226	return (void *)count;
227}
228
229ATF_TC(mutex3);
230ATF_TC_HEAD(mutex3, tc)
231{
232	atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
233	    "initializer");
234#ifdef __NetBSD__
235#if defined(__powerpc__)
236	atf_tc_set_md_var(tc, "timeout", "40");
237#endif
238#endif
239
240#ifdef __FreeBSD__
241#if defined(__riscv)
242	atf_tc_set_md_var(tc, "timeout", "600");
243#endif
244#endif
245}
246ATF_TC_BODY(mutex3, tc)
247{
248	int count, count2;
249	pthread_t new;
250	void *joinval;
251
252	printf("1: Mutex-test 3\n");
253
254#ifdef __NetBSD__
255#if defined(__powerpc__)
256	atf_tc_expect_timeout("PR port-powerpc/44387");
257#endif
258#endif
259
260	global_x = 0;
261	count = count2 = 10000000;
262
263	PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
264	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
265
266	printf("1: Thread %p\n", pthread_self());
267
268	PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
269
270	while (count--) {
271		PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy));
272		global_x++;
273		PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
274	}
275
276	PTHREAD_REQUIRE(pthread_join(new, &joinval));
277
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);
282
283#ifdef __NetBSD__
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 */
289	sleep(41);
290#endif
291#endif
292}
293
294static void *
295mutex4_threadfunc(void *arg)
296{
297	int *param;
298
299	printf("2: Second thread.\n");
300
301	param = arg;
302	printf("2: Locking mutex\n");
303	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
304	printf("2: Got mutex. *param = %d\n", *param);
305	(*param)++;
306
307	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
308
309	return param;
310}
311
312ATF_TC(mutex4);
313ATF_TC_HEAD(mutex4, tc)
314{
315	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
316}
317ATF_TC_BODY(mutex4, tc)
318{
319	int x;
320	pthread_t new;
321	pthread_mutexattr_t mattr;
322	void *joinval;
323
324	printf("1: Mutex-test 4\n");
325
326	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
327	PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
328
329	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
330
331	PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
332
333	x = 1;
334	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
335	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
336
337	printf("1: Before recursively acquiring the mutex.\n");
338	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
339
340	printf("1: Before releasing the mutex once.\n");
341	sleep(2);
342	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
343	printf("1: After releasing the mutex once.\n");
344
345	x = 20;
346
347	printf("1: Before releasing the mutex twice.\n");
348	sleep(2);
349	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
350	printf("1: After releasing the mutex twice.\n");
351
352	PTHREAD_REQUIRE(pthread_join(new, &joinval));
353
354	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
355	printf("1: Thread joined. X was %d. Return value (int) was %d\n",
356		x, *(int *)joinval);
357	ATF_REQUIRE_EQ(x, 21);
358	ATF_REQUIRE_EQ(*(int *)joinval, 21);
359	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
360}
361
362#ifdef __NetBSD__
363static pthread_mutexattr_t attr5;
364static pthread_mutex_t mutex5;
365static int min_fifo_prio, max_fifo_prio;
366
367static void *
368child_func(void* arg)
369{
370	int res;
371
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");
382
383	return 0;
384}
385
386ATF_TC(mutex5);
387ATF_TC_HEAD(mutex5, tc)
388{
389	atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting");
390	atf_tc_set_md_var(tc, "require.user", "root");
391}
392
393ATF_TC_BODY(mutex5, tc)
394{
395	int res;
396	struct sched_param param;
397	pthread_t child;
398
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;
403
404	/* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */
405	res = sched_setscheduler(getpid(), SCHED_FIFO, &param);
406	printf("previous policy used = %d\n", res);
407
408	res = sched_getscheduler(getpid());
409	ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res,
410	    SCHED_FIFO);
411
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,
416	    max_fifo_prio));
417
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");
423	sleep(10);
424	printf("main completes\n");
425	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5));
426	PTHREAD_REQUIRE(pthread_join(child, NULL));
427}
428
429static pthread_mutex_t mutex6;
430static int start = 0;
431static uintmax_t high_cnt = 0, low_cnt = 0, MAX_LOOP = 100000000;
432
433static void *
434high_prio(void* arg)
435{
436	struct sched_param param;
437	int policy;
438	param.sched_priority = min_fifo_prio + 10;
439	pthread_t childid = pthread_self();
440
441	PTHREAD_REQUIRE(pthread_setschedparam(childid, 1, &param));
442	PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, &param));
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);
447	sleep(1);
448	long tmp = 0;
449	for (int i = 0; i < 20; i++) {
450		while (high_cnt < MAX_LOOP) {
451			tmp += (123456789 % 1234) * (987654321 % 54321);
452			high_cnt += 1;
453		}
454		high_cnt = 0;
455		sleep(1);
456	}
457	PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
458	if (start == 0) start = 2;
459	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
460
461	return 0;
462}
463
464static void *
465low_prio(void* arg)
466{
467	struct sched_param param;
468	int policy;
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, &param));
474	PTHREAD_REQUIRE(pthread_getschedparam(childid, &policy, &param));
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);
479	sleep(1);
480	long tmp = 0;
481	for (int i = 0; i < 20; i++) {
482		while (low_cnt < MAX_LOOP) {
483			tmp += (123456789 % 1234) * (987654321 % 54321);
484			low_cnt += 1;
485		}
486		low_cnt = 0;
487		sleep(1);
488	}
489	PTHREAD_REQUIRE(mutex_lock(&mutex6, &ts_lengthy));
490	if (start == 0)
491		start = 1;
492	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex6));
493
494	return 0;
495}
496
497ATF_TC(mutex6);
498ATF_TC_HEAD(mutex6, tc)
499{
500	atf_tc_set_md_var(tc, "descr",
501	    "Checks scheduling for priority ceiling");
502	atf_tc_set_md_var(tc, "require.user", "root");
503}
504
505/*
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
509 *    priority.
510 * 3. each child thread did several rounds of computation, after each round it
511 *    sleep 1 second.
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.
515 *
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.
525 *
526 */
527ATF_TC_BODY(mutex6, tc)
528{
529	struct sched_param param;
530	int res;
531	pthread_t high, low;
532
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,
537	    max_fifo_prio);
538
539	param.sched_priority = min_fifo_prio;
540	res = sched_setscheduler(getpid(), SCHED_FIFO, &param);
541	printf("previous policy used = %d\n", res);
542
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));
547	sleep(5);
548	PTHREAD_REQUIRE(pthread_join(low, NULL));
549	PTHREAD_REQUIRE(pthread_join(high, NULL));
550
551	ATF_REQUIRE_EQ(start, 1);
552}
553#endif
554
555ATF_TC(mutexattr1);
556ATF_TC_HEAD(mutexattr1, tc)
557{
558	atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
559}
560
561ATF_TC_BODY(mutexattr1, tc)
562{
563	pthread_mutexattr_t mattr;
564	int protocol, target;
565
566	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
567
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);
572
573	/*
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);
578	*/
579
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);
584}
585
586ATF_TC(mutexattr2);
587ATF_TC_HEAD(mutexattr2, tc)
588{
589	atf_tc_set_md_var(tc, "descr", "Checks mutexattr");
590}
591
592ATF_TC_BODY(mutexattr2, tc)
593{
594	pthread_mutexattr_t mattr;
595
596#ifdef __FreeBSD__
597	atf_tc_expect_fail("fails on i == 0 with: "
598	    "pthread_mutexattr_setprioceiling(&mattr, i): Invalid argument "
599	    "-- PR # 211802");
600#endif
601
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++) {
606		int prioceiling;
607		int protocol;
608
609		PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr,
610		    &protocol));
611
612		printf("priority: %d\nprotocol: %d\n", i, protocol);
613		PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i));
614		PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr,
615		    &prioceiling));
616		printf("prioceiling: %d\n", prioceiling);
617		ATF_REQUIRE_EQ(i, prioceiling);
618	}
619}
620
621#ifdef TIMEDMUTEX
622ATF_TC(timedmutex1);
623ATF_TC_HEAD(timedmutex1, tc)
624{
625	atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock");
626}
627
628ATF_TC_BODY(timedmutex1, tc)
629{
630
631	printf("Timed mutex-test 1\n");
632
633	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
634
635	printf("Before acquiring mutex\n");
636	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
637
638	printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
639	PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
640	    ETIMEDOUT);
641
642	printf("Unlocking mutex\n");
643	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
644}
645
646ATF_TC(timedmutex2);
647ATF_TC_HEAD(timedmutex2, tc)
648{
649	atf_tc_set_md_var(tc, "descr",
650	    "Checks timeout on selflock with timedlock");
651}
652
653ATF_TC_BODY(timedmutex2, tc)
654{
655
656	printf("Timed mutex-test 2\n");
657
658	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
659
660	printf("Before acquiring mutex with timedlock\n");
661	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
662
663	printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
664	PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
665	    ETIMEDOUT);
666
667	printf("Unlocking mutex\n");
668	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
669}
670
671ATF_TC(timedmutex3);
672ATF_TC_HEAD(timedmutex3, tc)
673{
674	atf_tc_set_md_var(tc, "descr",
675	    "Checks timeout on selflock in a new thread");
676}
677
678static void *
679timedmtx_thrdfunc(void *arg)
680{
681	printf("Before endeavor to reacquire timed-mutex (timeout expected)\n");
682	PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived),
683	    ETIMEDOUT);
684
685	return NULL;
686}
687
688ATF_TC_BODY(timedmutex3, tc)
689{
690	pthread_t new;
691
692	printf("Timed mutex-test 3\n");
693
694	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
695
696	printf("Before acquiring mutex with timedlock\n");
697	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
698
699	printf("Before creating new thread\n");
700	PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
701
702	printf("Before joining the mutex\n");
703	PTHREAD_REQUIRE(pthread_join(new, NULL));
704
705	printf("Unlocking mutex\n");
706	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
707}
708
709ATF_TC(timedmutex4);
710ATF_TC_HEAD(timedmutex4, tc)
711{
712	atf_tc_set_md_var(tc, "descr",
713	    "Checks timeout on selflock with timedlock in a new thread");
714}
715
716ATF_TC_BODY(timedmutex4, tc)
717{
718	pthread_t new;
719
720	printf("Timed mutex-test 4\n");
721
722	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
723
724	printf("Before acquiring mutex with timedlock\n");
725	PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy));
726
727	printf("Before creating new thread\n");
728	PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL));
729
730	printf("Before joining the mutex\n");
731	PTHREAD_REQUIRE(pthread_join(new, NULL));
732
733	printf("Unlocking mutex\n");
734	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
735}
736#endif
737
738ATF_TP_ADD_TCS(tp)
739{
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);
744#ifdef __NetBSD__
745	ATF_TP_ADD_TC(tp, mutex5);
746	ATF_TP_ADD_TC(tp, mutex6);
747#endif
748	ATF_TP_ADD_TC(tp, mutexattr1);
749	ATF_TP_ADD_TC(tp, mutexattr2);
750
751#ifdef TIMEDMUTEX
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);
756#endif
757
758	return atf_no_error();
759}
760