1272343Sngie/* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2013 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie#include <sys/cdefs.h>
29272343Sngie__RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $");
30272343Sngie
31272343Sngie#include <errno.h>
32272343Sngie#include <pthread.h>
33272343Sngie#include <stdio.h>
34272343Sngie#include <stdlib.h>
35272343Sngie#include <string.h>
36272343Sngie#include <time.h>
37272343Sngie#include <unistd.h>
38272343Sngie
39272343Sngie#include <atf-c.h>
40272343Sngie
41272343Sngie#include "isqemu.h"
42272343Sngie
43276478Sngie#ifdef __FreeBSD__
44276478Sngie#include <sys/time.h>
45276478Sngie#endif
46276478Sngie
47272343Sngie#define WAITTIME 2	/* Timeout wait secound */
48272343Sngie
49272343Sngiestatic const int debug = 1;
50272343Sngie
51272343Sngiestatic void *
52272343Sngierun(void *param)
53272343Sngie{
54272343Sngie	struct timespec ts, to, te;
55272343Sngie	clockid_t clck;
56272343Sngie	pthread_condattr_t attr;
57272343Sngie	pthread_cond_t cond;
58272343Sngie	pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
59272343Sngie	int ret = 0;
60272343Sngie
61272343Sngie
62272343Sngie	clck = *(clockid_t *)param;
63272343Sngie	pthread_condattr_init(&attr);
64272343Sngie	pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */
65272343Sngie	pthread_cond_init(&cond, &attr);
66272343Sngie
67272343Sngie	ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
68272343Sngie
69272343Sngie	ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
70272343Sngie	to = ts;
71272343Sngie
72272343Sngie	if (debug)
73272343Sngie		printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
74272343Sngie		    to.tv_nsec);
75272343Sngie
76272343Sngie	ts.tv_sec += WAITTIME;	/* Timeout wait */
77272343Sngie
78272343Sngie	switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
79272343Sngie	case ETIMEDOUT:
80272343Sngie		/* Timeout */
81272343Sngie		ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
82272343Sngie		timespecsub(&te, &to, &to);
83272343Sngie		if (debug) {
84272343Sngie			printf("timeout: %lld.%09ld sec\n",
85272343Sngie			    (long long)te.tv_sec, te.tv_nsec);
86272343Sngie			printf("elapsed: %lld.%09ld sec\n",
87272343Sngie			    (long long)to.tv_sec, to.tv_nsec);
88272343Sngie		}
89272343Sngie		if (isQEMU()) {
90272343Sngie			double to_seconds = to.tv_sec + 1e-9 * to.tv_nsec;
91272343Sngie			ATF_REQUIRE(to_seconds >= WAITTIME * 0.9);
92272343Sngie			/* Loose upper limit because of qemu timing bugs */
93272343Sngie			ATF_REQUIRE(to_seconds < WAITTIME * 2.5);
94272343Sngie		} else {
95272343Sngie			ATF_REQUIRE_EQ(to.tv_sec, WAITTIME);
96272343Sngie		}
97272343Sngie		break;
98272343Sngie	default:
99272343Sngie		ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
100272343Sngie	}
101272343Sngie
102272343Sngie	ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
103272343Sngie	    "pthread_mutex_unlock: %s", strerror(ret));
104272343Sngie	pthread_exit(&ret);
105272343Sngie}
106272343Sngie
107272343Sngiestatic void
108272343Sngiecond_wait(clockid_t clck, const char *msg) {
109272343Sngie	pthread_t child;
110272343Sngie
111272343Sngie	if (debug)
112272343Sngie		printf( "**** %s clock wait starting\n", msg);
113272343Sngie	ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
114272343Sngie	ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
115272343Sngie	if (debug)
116272343Sngie		printf( "**** %s clock wait ended\n", msg);
117272343Sngie}
118272343Sngie
119272343SngieATF_TC(cond_wait_real);
120272343SngieATF_TC_HEAD(cond_wait_real, tc)
121272343Sngie{
122272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
123272343Sngie	    "with CLOCK_REALTIME");
124272343Sngie}
125272343Sngie
126272343SngieATF_TC_BODY(cond_wait_real, tc) {
127272343Sngie	cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
128272343Sngie}
129272343Sngie
130272343SngieATF_TC(cond_wait_mono);
131272343SngieATF_TC_HEAD(cond_wait_mono, tc)
132272343Sngie{
133272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
134272343Sngie	    "with CLOCK_MONOTONIC");
135272343Sngie}
136272343Sngie
137272343SngieATF_TC_BODY(cond_wait_mono, tc) {
138272343Sngie	cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
139272343Sngie}
140272343Sngie
141272343SngieATF_TP_ADD_TCS(tp)
142272343Sngie{
143272343Sngie	ATF_TP_ADD_TC(tp, cond_wait_real);
144272343Sngie	ATF_TP_ADD_TC(tp, cond_wait_mono);
145272343Sngie	return 0;
146272343Sngie}
147