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