xref: /freebsd/contrib/netbsd-tests/lib/libpthread/t_condwait.c (revision f9bc005b8ef3b507a5ecbc2d7fe4411ef70eff38)
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 struct run_param {
51 	clockid_t clck;
52 	bool clockwait;
53 	bool pshared;
54 };
55 
56 static void *
57 run(void *param)
58 {
59 	struct timespec ts, to, te, twmin, twmax;
60 	struct run_param *rp;
61 	pthread_condattr_t cattr;
62 	pthread_cond_t cond;
63 	pthread_mutexattr_t mattr;
64 	pthread_mutex_t m;
65 	int ret = 0;
66 
67 	rp = param;
68 
69 	PTHREAD_REQUIRE(pthread_condattr_init(&cattr));
70 	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
71 	if (!rp->clockwait)
72 		PTHREAD_REQUIRE(pthread_condattr_setclock(&cattr, rp->clck));
73 	if (rp->pshared) {
74 		PTHREAD_REQUIRE(pthread_condattr_setpshared(&cattr,
75 		    PTHREAD_PROCESS_SHARED));
76 		PTHREAD_REQUIRE(pthread_mutexattr_setpshared(&mattr,
77 		    PTHREAD_PROCESS_SHARED));
78 	}
79 	pthread_mutex_init(&m, &mattr);
80 	pthread_cond_init(&cond, &cattr);
81 
82 	ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
83 
84 	ATF_REQUIRE_EQ(clock_gettime(rp->clck, &ts), 0);
85 	to = ts;
86 
87 	if (debug)
88 		printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
89 		    to.tv_nsec);
90 
91 	ts.tv_sec += WAITTIME;	/* Timeout wait */
92 
93 	if (rp->clockwait)
94 		ret = pthread_cond_clockwait(&cond, &m, rp->clck, &ts);
95 	else
96 		ret = pthread_cond_timedwait(&cond, &m, &ts);
97 	switch (ret) {
98 	case ETIMEDOUT:
99 		/* Timeout */
100 		ATF_REQUIRE_EQ(clock_gettime(rp->clck, &te), 0);
101 		timespecsub(&te, &to, &to);
102 		if (debug) {
103 			printf("timeout: %lld.%09ld sec\n",
104 			    (long long)te.tv_sec, te.tv_nsec);
105 			printf("elapsed: %lld.%09ld sec\n",
106 			    (long long)to.tv_sec, to.tv_nsec);
107 		}
108 		twmin.tv_sec = WAITTIME;
109 		twmin.tv_nsec = 0;
110 		if (isQEMU()) {
111 			struct timespec td, t;
112 			// td.tv_sec = 0;
113 			// td.tv_nsec = 900000000;
114 			t = twmin;
115 			// timespecsub(&t, &td, &twmin);
116 			td.tv_sec = 2;
117 			td.tv_nsec = 500000000;
118 			timespecadd(&t, &td, &twmax);
119 		} else {
120 			twmax = twmin;
121 			twmax.tv_sec++;
122 		}
123 		ATF_REQUIRE(timespeccmp(&to, &twmin, >=));
124 		ATF_REQUIRE(timespeccmp(&to, &twmax, <=));
125 		break;
126 	default:
127 		ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
128 	}
129 
130 	ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
131 	    "pthread_mutex_unlock: %s", strerror(ret));
132 	pthread_exit(&ret);
133 }
134 
135 static void
136 cond_wait(clockid_t clck, bool clockwait, bool pshared, const char *msg)
137 {
138 	pthread_t child;
139 	struct run_param rp;
140 
141 	rp.clck = clck;
142 	rp.clockwait = clockwait;
143 	rp.pshared = pshared;
144 
145 	if (debug)
146 		printf( "**** %s clock wait starting\n", msg);
147 	ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &rp), 0);
148 	ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
149 	if (debug)
150 		printf( "**** %s clock wait ended\n", msg);
151 }
152 
153 ATF_TC(cond_wait_real);
154 ATF_TC_HEAD(cond_wait_real, tc)
155 {
156 	atf_tc_set_md_var(tc, "descr",
157 	    "Checks pthread_cond_timedwait with CLOCK_REALTIME");
158 }
159 
160 ATF_TC_BODY(cond_wait_real, tc) {
161 	cond_wait(CLOCK_REALTIME, false, false, "CLOCK_REALTIME");
162 }
163 
164 ATF_TC(cond_wait_mono);
165 ATF_TC_HEAD(cond_wait_mono, tc)
166 {
167 	atf_tc_set_md_var(tc, "descr",
168 	    "Checks pthread_cond_timedwait with CLOCK_MONOTONIC");
169 }
170 
171 ATF_TC_BODY(cond_wait_mono, tc) {
172 	cond_wait(CLOCK_MONOTONIC, false, false, "CLOCK_MONOTONIC");
173 }
174 
175 ATF_TC(cond_clockwait_real);
176 ATF_TC_HEAD(cond_clockwait_real, tc)
177 {
178 	atf_tc_set_md_var(tc, "descr",
179 	    "Checks pthread_cond_clockwait with CLOCK_REALTIME");
180 }
181 
182 ATF_TC_BODY(cond_clockwait_real, tc) {
183 	cond_wait(CLOCK_REALTIME, true, false, "CLOCK_REALTIME");
184 }
185 
186 ATF_TC(cond_clockwait_mono);
187 ATF_TC_HEAD(cond_clockwait_mono, tc)
188 {
189 	atf_tc_set_md_var(tc, "descr",
190 	    "Checks pthread_cond_clockwait with CLOCK_MONOTONIC");
191 }
192 
193 ATF_TC_BODY(cond_clockwait_mono, tc) {
194 	cond_wait(CLOCK_MONOTONIC, true, false, "CLOCK_MONOTONIC");
195 }
196 
197 ATF_TC(cond_klockwait_real);
198 ATF_TC_HEAD(cond_klockwait_real, tc)
199 {
200 	atf_tc_set_md_var(tc, "descr",
201     "Checks pthread_cond_clockwait with CLOCK_REALTIME calling kernel");
202 }
203 
204 ATF_TC_BODY(cond_klockwait_real, tc) {
205 	cond_wait(CLOCK_REALTIME, true, true, "CLOCK_REALTIME");
206 }
207 
208 ATF_TC(cond_klockwait_mono);
209 ATF_TC_HEAD(cond_klockwait_mono, tc)
210 {
211 	atf_tc_set_md_var(tc, "descr",
212     "Checks pthread_cond_clockwait with CLOCK_MONOTONIC calling kernel");
213 }
214 
215 ATF_TC_BODY(cond_klockwait_mono, tc) {
216 	cond_wait(CLOCK_MONOTONIC, true, true, "CLOCK_MONOTONIC");
217 }
218 
219 ATF_TP_ADD_TCS(tp)
220 {
221 	ATF_TP_ADD_TC(tp, cond_wait_real);
222 	ATF_TP_ADD_TC(tp, cond_wait_mono);
223 	ATF_TP_ADD_TC(tp, cond_clockwait_real);
224 	ATF_TP_ADD_TC(tp, cond_clockwait_mono);
225 	ATF_TP_ADD_TC(tp, cond_klockwait_real);
226 	ATF_TP_ADD_TC(tp, cond_klockwait_mono);
227 	return atf_no_error();
228 }
229