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