xref: /minix/tests/lib/libpthread/t_condwait.c (revision 11be35a1)
1*11be35a1SLionel Sambuc /* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */
2*11be35a1SLionel Sambuc 
3*11be35a1SLionel Sambuc /*
4*11be35a1SLionel Sambuc  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc  * All rights reserved.
6*11be35a1SLionel Sambuc  *
7*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
8*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
9*11be35a1SLionel Sambuc  * are met:
10*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
11*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
12*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
13*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
14*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
15*11be35a1SLionel Sambuc  *
16*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*11be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*11be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*11be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*11be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*11be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*11be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*11be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*11be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*11be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc #include <sys/cdefs.h>
29*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $");
30*11be35a1SLionel Sambuc 
31*11be35a1SLionel Sambuc #include <errno.h>
32*11be35a1SLionel Sambuc #include <pthread.h>
33*11be35a1SLionel Sambuc #include <stdio.h>
34*11be35a1SLionel Sambuc #include <stdlib.h>
35*11be35a1SLionel Sambuc #include <string.h>
36*11be35a1SLionel Sambuc #include <time.h>
37*11be35a1SLionel Sambuc #include <unistd.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc #include <atf-c.h>
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc #include "isqemu.h"
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #define WAITTIME 2	/* Timeout wait secound */
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc static const int debug = 1;
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc static void *
run(void * param)48*11be35a1SLionel Sambuc run(void *param)
49*11be35a1SLionel Sambuc {
50*11be35a1SLionel Sambuc 	struct timespec ts, to, te;
51*11be35a1SLionel Sambuc 	clockid_t clck;
52*11be35a1SLionel Sambuc 	pthread_condattr_t attr;
53*11be35a1SLionel Sambuc 	pthread_cond_t cond;
54*11be35a1SLionel Sambuc 	pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
55*11be35a1SLionel Sambuc 	int ret = 0;
56*11be35a1SLionel Sambuc 
57*11be35a1SLionel Sambuc 
58*11be35a1SLionel Sambuc 	clck = *(clockid_t *)param;
59*11be35a1SLionel Sambuc 	pthread_condattr_init(&attr);
60*11be35a1SLionel Sambuc 	pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */
61*11be35a1SLionel Sambuc 	pthread_cond_init(&cond, &attr);
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
64*11be35a1SLionel Sambuc 
65*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
66*11be35a1SLionel Sambuc 	to = ts;
67*11be35a1SLionel Sambuc 
68*11be35a1SLionel Sambuc 	if (debug)
69*11be35a1SLionel Sambuc 		printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
70*11be35a1SLionel Sambuc 		    to.tv_nsec);
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 	ts.tv_sec += WAITTIME;	/* Timeout wait */
73*11be35a1SLionel Sambuc 
74*11be35a1SLionel Sambuc 	switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
75*11be35a1SLionel Sambuc 	case ETIMEDOUT:
76*11be35a1SLionel Sambuc 		/* Timeout */
77*11be35a1SLionel Sambuc 		ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
78*11be35a1SLionel Sambuc 		timespecsub(&te, &to, &to);
79*11be35a1SLionel Sambuc 		if (debug) {
80*11be35a1SLionel Sambuc 			printf("timeout: %lld.%09ld sec\n",
81*11be35a1SLionel Sambuc 			    (long long)te.tv_sec, te.tv_nsec);
82*11be35a1SLionel Sambuc 			printf("elapsed: %lld.%09ld sec\n",
83*11be35a1SLionel Sambuc 			    (long long)to.tv_sec, to.tv_nsec);
84*11be35a1SLionel Sambuc 		}
85*11be35a1SLionel Sambuc 		if (isQEMU()) {
86*11be35a1SLionel Sambuc 			double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec;
87*11be35a1SLionel Sambuc 			ATF_REQUIRE(to_seconds >= WAITTIME * 0.9);
88*11be35a1SLionel Sambuc 			/* Loose upper limit because of qemu timing bugs */
89*11be35a1SLionel Sambuc 			ATF_REQUIRE(to_seconds < WAITTIME * 2.5);
90*11be35a1SLionel Sambuc 		} else {
91*11be35a1SLionel Sambuc 			ATF_REQUIRE_EQ(to.tv_sec, WAITTIME);
92*11be35a1SLionel Sambuc 		}
93*11be35a1SLionel Sambuc 		break;
94*11be35a1SLionel Sambuc 	default:
95*11be35a1SLionel Sambuc 		ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
96*11be35a1SLionel Sambuc 	}
97*11be35a1SLionel Sambuc 
98*11be35a1SLionel Sambuc 	ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
99*11be35a1SLionel Sambuc 	    "pthread_mutex_unlock: %s", strerror(ret));
100*11be35a1SLionel Sambuc 	pthread_exit(&ret);
101*11be35a1SLionel Sambuc }
102*11be35a1SLionel Sambuc 
103*11be35a1SLionel Sambuc static void
cond_wait(clockid_t clck,const char * msg)104*11be35a1SLionel Sambuc cond_wait(clockid_t clck, const char *msg) {
105*11be35a1SLionel Sambuc 	pthread_t child;
106*11be35a1SLionel Sambuc 
107*11be35a1SLionel Sambuc 	if (debug)
108*11be35a1SLionel Sambuc 		printf( "**** %s clock wait starting\n", msg);
109*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
110*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
111*11be35a1SLionel Sambuc 	if (debug)
112*11be35a1SLionel Sambuc 		printf( "**** %s clock wait ended\n", msg);
113*11be35a1SLionel Sambuc }
114*11be35a1SLionel Sambuc 
115*11be35a1SLionel Sambuc ATF_TC(cond_wait_real);
ATF_TC_HEAD(cond_wait_real,tc)116*11be35a1SLionel Sambuc ATF_TC_HEAD(cond_wait_real, tc)
117*11be35a1SLionel Sambuc {
118*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
119*11be35a1SLionel Sambuc 	    "with CLOCK_REALTIME");
120*11be35a1SLionel Sambuc }
121*11be35a1SLionel Sambuc 
ATF_TC_BODY(cond_wait_real,tc)122*11be35a1SLionel Sambuc ATF_TC_BODY(cond_wait_real, tc) {
123*11be35a1SLionel Sambuc 	cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
124*11be35a1SLionel Sambuc }
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc ATF_TC(cond_wait_mono);
ATF_TC_HEAD(cond_wait_mono,tc)127*11be35a1SLionel Sambuc ATF_TC_HEAD(cond_wait_mono, tc)
128*11be35a1SLionel Sambuc {
129*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
130*11be35a1SLionel Sambuc 	    "with CLOCK_MONOTONIC");
131*11be35a1SLionel Sambuc }
132*11be35a1SLionel Sambuc 
ATF_TC_BODY(cond_wait_mono,tc)133*11be35a1SLionel Sambuc ATF_TC_BODY(cond_wait_mono, tc) {
134*11be35a1SLionel Sambuc 	cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
135*11be35a1SLionel Sambuc }
136*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)137*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
138*11be35a1SLionel Sambuc {
139*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cond_wait_real);
140*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cond_wait_mono);
141*11be35a1SLionel Sambuc 	return 0;
142*11be35a1SLionel Sambuc }
143