t_condwait.c revision 1.9 1 1.9 andvar /* $NetBSD: t_condwait.c,v 1.9 2022/04/16 18:15:22 andvar 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.9 andvar __RCSID("$NetBSD: t_condwait.c,v 1.9 2022/04/16 18:15:22 andvar Exp $");
30 1.1 christos
31 1.5 christos #include <sys/time.h>
32 1.1 christos #include <errno.h>
33 1.1 christos #include <pthread.h>
34 1.1 christos #include <stdio.h>
35 1.1 christos #include <stdlib.h>
36 1.1 christos #include <string.h>
37 1.1 christos #include <time.h>
38 1.1 christos #include <unistd.h>
39 1.1 christos
40 1.1 christos #include <atf-c.h>
41 1.1 christos
42 1.4 christos #include "isqemu.h"
43 1.4 christos
44 1.5 christos #include "h_common.h"
45 1.5 christos
46 1.9 andvar #define WAITTIME 2 /* Timeout wait in seconds */
47 1.1 christos
48 1.1 christos static const int debug = 1;
49 1.1 christos
50 1.1 christos static void *
51 1.1 christos run(void *param)
52 1.1 christos {
53 1.7 martin struct timespec ts, to, te, twmin, twmax;
54 1.1 christos clockid_t clck;
55 1.1 christos pthread_condattr_t attr;
56 1.1 christos pthread_cond_t cond;
57 1.1 christos pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
58 1.1 christos int ret = 0;
59 1.1 christos
60 1.1 christos
61 1.1 christos clck = *(clockid_t *)param;
62 1.5 christos PTHREAD_REQUIRE(pthread_condattr_init(&attr));
63 1.5 christos PTHREAD_REQUIRE(pthread_condattr_setclock(&attr, clck));
64 1.1 christos pthread_cond_init(&cond, &attr);
65 1.1 christos
66 1.1 christos ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
67 1.1 christos
68 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
69 1.1 christos to = ts;
70 1.1 christos
71 1.1 christos if (debug)
72 1.2 christos printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
73 1.2 christos to.tv_nsec);
74 1.1 christos
75 1.1 christos ts.tv_sec += WAITTIME; /* Timeout wait */
76 1.1 christos
77 1.1 christos switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
78 1.1 christos case ETIMEDOUT:
79 1.1 christos /* Timeout */
80 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
81 1.1 christos timespecsub(&te, &to, &to);
82 1.1 christos if (debug) {
83 1.2 christos printf("timeout: %lld.%09ld sec\n",
84 1.2 christos (long long)te.tv_sec, te.tv_nsec);
85 1.2 christos printf("elapsed: %lld.%09ld sec\n",
86 1.2 christos (long long)to.tv_sec, to.tv_nsec);
87 1.1 christos }
88 1.7 martin twmin.tv_sec = WAITTIME;
89 1.7 martin twmin.tv_nsec = 0;
90 1.8 martin if (isQEMU()) {
91 1.7 martin struct timespec td, t;
92 1.8 martin // td.tv_sec = 0;
93 1.8 martin // td.tv_nsec = 900000000;
94 1.7 martin t = twmin;
95 1.8 martin // timespecsub(&t, &td, &twmin);
96 1.7 martin td.tv_sec = 2;
97 1.7 martin td.tv_nsec = 500000000;
98 1.7 martin timespecadd(&t, &td, &twmax);
99 1.4 christos } else {
100 1.7 martin twmax = twmin;
101 1.7 martin twmax.tv_sec++;
102 1.4 christos }
103 1.7 martin ATF_REQUIRE(timespeccmp(&to, &twmin, >=));
104 1.7 martin ATF_REQUIRE(timespeccmp(&to, &twmax, <=));
105 1.1 christos break;
106 1.1 christos default:
107 1.1 christos ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
108 1.1 christos }
109 1.1 christos
110 1.1 christos ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
111 1.1 christos "pthread_mutex_unlock: %s", strerror(ret));
112 1.1 christos pthread_exit(&ret);
113 1.1 christos }
114 1.1 christos
115 1.1 christos static void
116 1.1 christos cond_wait(clockid_t clck, const char *msg) {
117 1.1 christos pthread_t child;
118 1.1 christos
119 1.1 christos if (debug)
120 1.1 christos printf( "**** %s clock wait starting\n", msg);
121 1.1 christos ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
122 1.1 christos ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
123 1.1 christos if (debug)
124 1.1 christos printf( "**** %s clock wait ended\n", msg);
125 1.1 christos }
126 1.1 christos
127 1.1 christos ATF_TC(cond_wait_real);
128 1.1 christos ATF_TC_HEAD(cond_wait_real, tc)
129 1.1 christos {
130 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
131 1.1 christos "with CLOCK_REALTIME");
132 1.1 christos }
133 1.1 christos
134 1.1 christos ATF_TC_BODY(cond_wait_real, tc) {
135 1.1 christos cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
136 1.1 christos }
137 1.1 christos
138 1.1 christos ATF_TC(cond_wait_mono);
139 1.1 christos ATF_TC_HEAD(cond_wait_mono, tc)
140 1.1 christos {
141 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
142 1.1 christos "with CLOCK_MONOTONIC");
143 1.1 christos }
144 1.1 christos
145 1.1 christos ATF_TC_BODY(cond_wait_mono, tc) {
146 1.1 christos cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
147 1.1 christos }
148 1.1 christos
149 1.1 christos ATF_TP_ADD_TCS(tp)
150 1.1 christos {
151 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_real);
152 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_mono);
153 1.6 maya return atf_no_error();
154 1.1 christos }
155