1 /* $NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $ */
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __RCSID("$NetBSD: t_condwait.c,v 1.4 2013/04/12 17:18:11 christos Exp $");
43 #define WAITTIME 2 /* Timeout wait secound */
45 static const int debug
= 1;
50 struct timespec ts
, to
, te
;
52 pthread_condattr_t attr
;
54 pthread_mutex_t m
= PTHREAD_MUTEX_INITIALIZER
;
58 clck
= *(clockid_t
*)param
;
59 pthread_condattr_init(&attr
);
60 pthread_condattr_setclock(&attr
, clck
); /* MONOTONIC or MONOTONIC */
61 pthread_cond_init(&cond
, &attr
);
63 ATF_REQUIRE_EQ((ret
= pthread_mutex_lock(&m
)), 0);
65 ATF_REQUIRE_EQ(clock_gettime(clck
, &ts
), 0);
69 printf("started: %lld.%09ld sec\n", (long long)to
.tv_sec
,
72 ts
.tv_sec
+= WAITTIME
; /* Timeout wait */
74 switch (ret
= pthread_cond_timedwait(&cond
, &m
, &ts
)) {
77 ATF_REQUIRE_EQ(clock_gettime(clck
, &te
), 0);
78 timespecsub(&te
, &to
, &to
);
80 printf("timeout: %lld.%09ld sec\n",
81 (long long)te
.tv_sec
, te
.tv_nsec
);
82 printf("elapsed: %lld.%09ld sec\n",
83 (long long)to
.tv_sec
, to
.tv_nsec
);
86 double to_seconds
= to
.tv_sec
+ 1e-9 * to
.tv_nsec
;
87 ATF_REQUIRE(to_seconds
>= WAITTIME
* 0.9);
88 /* Loose upper limit because of qemu timing bugs */
89 ATF_REQUIRE(to_seconds
< WAITTIME
* 2.5);
91 ATF_REQUIRE_EQ(to
.tv_sec
, WAITTIME
);
95 ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret
));
98 ATF_REQUIRE_MSG(!(ret
= pthread_mutex_unlock(&m
)),
99 "pthread_mutex_unlock: %s", strerror(ret
));
104 cond_wait(clockid_t clck
, const char *msg
) {
108 printf( "**** %s clock wait starting\n", msg
);
109 ATF_REQUIRE_EQ(pthread_create(&child
, NULL
, run
, &clck
), 0);
110 ATF_REQUIRE_EQ(pthread_join(child
, NULL
), 0); /* wait for terminate */
112 printf( "**** %s clock wait ended\n", msg
);
115 ATF_TC(cond_wait_real
);
116 ATF_TC_HEAD(cond_wait_real
, tc
)
118 atf_tc_set_md_var(tc
, "descr", "Checks pthread_cond_timedwait "
119 "with CLOCK_REALTIME");
122 ATF_TC_BODY(cond_wait_real
, tc
) {
123 cond_wait(CLOCK_REALTIME
, "CLOCK_REALTIME");
126 ATF_TC(cond_wait_mono
);
127 ATF_TC_HEAD(cond_wait_mono
, tc
)
129 atf_tc_set_md_var(tc
, "descr", "Checks pthread_cond_timedwait "
130 "with CLOCK_MONOTONIC");
133 ATF_TC_BODY(cond_wait_mono
, tc
) {
134 cond_wait(CLOCK_MONOTONIC
, "CLOCK_MONOTONIC");
139 ATF_TP_ADD_TC(tp
, cond_wait_real
);
140 ATF_TP_ADD_TC(tp
, cond_wait_mono
);