1 /* $NetBSD: t_condwait.c,v 1.8 2019/08/11 11:42:23 martin Exp $ */
2 
3 /*
4  * Copyright (c) 2013 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 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: t_condwait.c,v 1.8 2019/08/11 11:42:23 martin Exp $");
30 
31 #include <sys/time.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 
40 #include <atf-c.h>
41 
42 #include "isqemu.h"
43 
44 #include "h_common.h"
45 
46 #define WAITTIME 2	/* Timeout wait secound */
47 
48 static const int debug = 1;
49 
50 static void *
51 run(void *param)
52 {
53 	struct timespec ts, to, te, twmin, twmax;
54 	clockid_t clck;
55 	pthread_condattr_t attr;
56 	pthread_cond_t cond;
57 	pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
58 	int ret = 0;
59 
60 
61 	clck = *(clockid_t *)param;
62 	PTHREAD_REQUIRE(pthread_condattr_init(&attr));
63 	PTHREAD_REQUIRE(pthread_condattr_setclock(&attr, clck));
64 	pthread_cond_init(&cond, &attr);
65 
66 	ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
67 
68 	ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
69 	to = ts;
70 
71 	if (debug)
72 		printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
73 		    to.tv_nsec);
74 
75 	ts.tv_sec += WAITTIME;	/* Timeout wait */
76 
77 	switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
78 	case ETIMEDOUT:
79 		/* Timeout */
80 		ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
81 		timespecsub(&te, &to, &to);
82 		if (debug) {
83 			printf("timeout: %lld.%09ld sec\n",
84 			    (long long)te.tv_sec, te.tv_nsec);
85 			printf("elapsed: %lld.%09ld sec\n",
86 			    (long long)to.tv_sec, to.tv_nsec);
87 		}
88 		twmin.tv_sec = WAITTIME;
89 		twmin.tv_nsec = 0;
90 		if (isQEMU()) {
91 			struct timespec td, t;
92 			// td.tv_sec = 0;
93 			// td.tv_nsec = 900000000;
94 			t = twmin;
95 			// timespecsub(&t, &td, &twmin);
96 			td.tv_sec = 2;
97 			td.tv_nsec = 500000000;
98 			timespecadd(&t, &td, &twmax);
99 		} else {
100 			twmax = twmin;
101 			twmax.tv_sec++;
102 		}
103 		ATF_REQUIRE(timespeccmp(&to, &twmin, >=));
104 		ATF_REQUIRE(timespeccmp(&to, &twmax, <=));
105 		break;
106 	default:
107 		ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
108 	}
109 
110 	ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
111 	    "pthread_mutex_unlock: %s", strerror(ret));
112 	pthread_exit(&ret);
113 }
114 
115 static void
116 cond_wait(clockid_t clck, const char *msg) {
117 	pthread_t child;
118 
119 	if (debug)
120 		printf( "**** %s clock wait starting\n", msg);
121 	ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
122 	ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
123 	if (debug)
124 		printf( "**** %s clock wait ended\n", msg);
125 }
126 
127 ATF_TC(cond_wait_real);
128 ATF_TC_HEAD(cond_wait_real, tc)
129 {
130 	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
131 	    "with CLOCK_REALTIME");
132 }
133 
134 ATF_TC_BODY(cond_wait_real, tc) {
135 	cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
136 }
137 
138 ATF_TC(cond_wait_mono);
139 ATF_TC_HEAD(cond_wait_mono, tc)
140 {
141 	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
142 	    "with CLOCK_MONOTONIC");
143 }
144 
145 ATF_TC_BODY(cond_wait_mono, tc) {
146 	cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
147 }
148 
149 ATF_TP_ADD_TCS(tp)
150 {
151 	ATF_TP_ADD_TC(tp, cond_wait_real);
152 	ATF_TP_ADD_TC(tp, cond_wait_mono);
153 	return atf_no_error();
154 }
155