1*dc26651aSEric van Gyzen /* $NetBSD: t_condwait.c,v 1.8 2019/08/11 11:42:23 martin Exp $ */
257718be8SEnji Cooper
357718be8SEnji Cooper /*
457718be8SEnji Cooper * Copyright (c) 2013 The NetBSD Foundation, Inc.
557718be8SEnji Cooper * All rights reserved.
657718be8SEnji Cooper *
757718be8SEnji Cooper * Redistribution and use in source and binary forms, with or without
857718be8SEnji Cooper * modification, are permitted provided that the following conditions
957718be8SEnji Cooper * are met:
1057718be8SEnji Cooper * 1. Redistributions of source code must retain the above copyright
1157718be8SEnji Cooper * notice, this list of conditions and the following disclaimer.
1257718be8SEnji Cooper * 2. Redistributions in binary form must reproduce the above copyright
1357718be8SEnji Cooper * notice, this list of conditions and the following disclaimer in the
1457718be8SEnji Cooper * documentation and/or other materials provided with the distribution.
1557718be8SEnji Cooper *
1657718be8SEnji Cooper * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1757718be8SEnji Cooper * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1857718be8SEnji Cooper * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1957718be8SEnji Cooper * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2057718be8SEnji Cooper * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2157718be8SEnji Cooper * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2257718be8SEnji Cooper * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2357718be8SEnji Cooper * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2457718be8SEnji Cooper * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2557718be8SEnji Cooper * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2657718be8SEnji Cooper * POSSIBILITY OF SUCH DAMAGE.
2757718be8SEnji Cooper */
2857718be8SEnji Cooper #include <sys/cdefs.h>
29*dc26651aSEric van Gyzen __RCSID("$NetBSD: t_condwait.c,v 1.8 2019/08/11 11:42:23 martin Exp $");
3057718be8SEnji Cooper
313b0a9131SEnji Cooper #include <sys/time.h>
3257718be8SEnji Cooper #include <errno.h>
3357718be8SEnji Cooper #include <pthread.h>
3457718be8SEnji Cooper #include <stdio.h>
3557718be8SEnji Cooper #include <stdlib.h>
3657718be8SEnji Cooper #include <string.h>
3757718be8SEnji Cooper #include <time.h>
3857718be8SEnji Cooper #include <unistd.h>
3957718be8SEnji Cooper
4057718be8SEnji Cooper #include <atf-c.h>
4157718be8SEnji Cooper
4257718be8SEnji Cooper #include "isqemu.h"
4357718be8SEnji Cooper
44d6766132SEnji Cooper #include "h_common.h"
45a20294deSEnji Cooper
4657718be8SEnji Cooper #define WAITTIME 2 /* Timeout wait secound */
4757718be8SEnji Cooper
4857718be8SEnji Cooper static const int debug = 1;
4957718be8SEnji Cooper
5057718be8SEnji Cooper static void *
run(void * param)5157718be8SEnji Cooper run(void *param)
5257718be8SEnji Cooper {
53*dc26651aSEric van Gyzen struct timespec ts, to, te, twmin, twmax;
5457718be8SEnji Cooper clockid_t clck;
5557718be8SEnji Cooper pthread_condattr_t attr;
5657718be8SEnji Cooper pthread_cond_t cond;
5757718be8SEnji Cooper pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
5857718be8SEnji Cooper int ret = 0;
5957718be8SEnji Cooper
6057718be8SEnji Cooper
6157718be8SEnji Cooper clck = *(clockid_t *)param;
62d6766132SEnji Cooper PTHREAD_REQUIRE(pthread_condattr_init(&attr));
63d6766132SEnji Cooper PTHREAD_REQUIRE(pthread_condattr_setclock(&attr, clck));
6457718be8SEnji Cooper pthread_cond_init(&cond, &attr);
6557718be8SEnji Cooper
6657718be8SEnji Cooper ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
6757718be8SEnji Cooper
6857718be8SEnji Cooper ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
6957718be8SEnji Cooper to = ts;
7057718be8SEnji Cooper
7157718be8SEnji Cooper if (debug)
7257718be8SEnji Cooper printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
7357718be8SEnji Cooper to.tv_nsec);
7457718be8SEnji Cooper
7557718be8SEnji Cooper ts.tv_sec += WAITTIME; /* Timeout wait */
7657718be8SEnji Cooper
7757718be8SEnji Cooper switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
7857718be8SEnji Cooper case ETIMEDOUT:
7957718be8SEnji Cooper /* Timeout */
8057718be8SEnji Cooper ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
8157718be8SEnji Cooper timespecsub(&te, &to, &to);
8257718be8SEnji Cooper if (debug) {
8357718be8SEnji Cooper printf("timeout: %lld.%09ld sec\n",
8457718be8SEnji Cooper (long long)te.tv_sec, te.tv_nsec);
8557718be8SEnji Cooper printf("elapsed: %lld.%09ld sec\n",
8657718be8SEnji Cooper (long long)to.tv_sec, to.tv_nsec);
8757718be8SEnji Cooper }
88*dc26651aSEric van Gyzen twmin.tv_sec = WAITTIME;
89*dc26651aSEric van Gyzen twmin.tv_nsec = 0;
9057718be8SEnji Cooper if (isQEMU()) {
91*dc26651aSEric van Gyzen struct timespec td, t;
92*dc26651aSEric van Gyzen // td.tv_sec = 0;
93*dc26651aSEric van Gyzen // td.tv_nsec = 900000000;
94*dc26651aSEric van Gyzen t = twmin;
95*dc26651aSEric van Gyzen // timespecsub(&t, &td, &twmin);
96*dc26651aSEric van Gyzen td.tv_sec = 2;
97*dc26651aSEric van Gyzen td.tv_nsec = 500000000;
98*dc26651aSEric van Gyzen timespecadd(&t, &td, &twmax);
9957718be8SEnji Cooper } else {
100*dc26651aSEric van Gyzen twmax = twmin;
101*dc26651aSEric van Gyzen twmax.tv_sec++;
10257718be8SEnji Cooper }
103*dc26651aSEric van Gyzen ATF_REQUIRE(timespeccmp(&to, &twmin, >=));
104*dc26651aSEric van Gyzen ATF_REQUIRE(timespeccmp(&to, &twmax, <=));
10557718be8SEnji Cooper break;
10657718be8SEnji Cooper default:
10757718be8SEnji Cooper ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
10857718be8SEnji Cooper }
10957718be8SEnji Cooper
11057718be8SEnji Cooper ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
11157718be8SEnji Cooper "pthread_mutex_unlock: %s", strerror(ret));
11257718be8SEnji Cooper pthread_exit(&ret);
11357718be8SEnji Cooper }
11457718be8SEnji Cooper
11557718be8SEnji Cooper static void
cond_wait(clockid_t clck,const char * msg)11657718be8SEnji Cooper cond_wait(clockid_t clck, const char *msg) {
11757718be8SEnji Cooper pthread_t child;
11857718be8SEnji Cooper
11957718be8SEnji Cooper if (debug)
12057718be8SEnji Cooper printf( "**** %s clock wait starting\n", msg);
12157718be8SEnji Cooper ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
12257718be8SEnji Cooper ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
12357718be8SEnji Cooper if (debug)
12457718be8SEnji Cooper printf( "**** %s clock wait ended\n", msg);
12557718be8SEnji Cooper }
12657718be8SEnji Cooper
12757718be8SEnji Cooper ATF_TC(cond_wait_real);
ATF_TC_HEAD(cond_wait_real,tc)12857718be8SEnji Cooper ATF_TC_HEAD(cond_wait_real, tc)
12957718be8SEnji Cooper {
13057718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
13157718be8SEnji Cooper "with CLOCK_REALTIME");
13257718be8SEnji Cooper }
13357718be8SEnji Cooper
ATF_TC_BODY(cond_wait_real,tc)13457718be8SEnji Cooper ATF_TC_BODY(cond_wait_real, tc) {
13557718be8SEnji Cooper cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
13657718be8SEnji Cooper }
13757718be8SEnji Cooper
13857718be8SEnji Cooper ATF_TC(cond_wait_mono);
ATF_TC_HEAD(cond_wait_mono,tc)13957718be8SEnji Cooper ATF_TC_HEAD(cond_wait_mono, tc)
14057718be8SEnji Cooper {
14157718be8SEnji Cooper atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
14257718be8SEnji Cooper "with CLOCK_MONOTONIC");
14357718be8SEnji Cooper }
14457718be8SEnji Cooper
ATF_TC_BODY(cond_wait_mono,tc)14557718be8SEnji Cooper ATF_TC_BODY(cond_wait_mono, tc) {
14657718be8SEnji Cooper cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
14757718be8SEnji Cooper }
14857718be8SEnji Cooper
ATF_TP_ADD_TCS(tp)14957718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
15057718be8SEnji Cooper {
15157718be8SEnji Cooper ATF_TP_ADD_TC(tp, cond_wait_real);
15257718be8SEnji Cooper ATF_TP_ADD_TC(tp, cond_wait_mono);
153*dc26651aSEric van Gyzen return atf_no_error();
15457718be8SEnji Cooper }
155