t_condwait.c revision 1.2 1 1.2 christos /* $NetBSD: t_condwait.c,v 1.2 2013/03/29 02:32:38 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.2 christos __RCSID("$NetBSD: t_condwait.c,v 1.2 2013/03/29 02:32:38 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.1 christos #define WAITTIME 2 /* Timeout wait secound */
42 1.1 christos
43 1.1 christos static const int debug = 1;
44 1.1 christos
45 1.1 christos static void *
46 1.1 christos run(void *param)
47 1.1 christos {
48 1.1 christos struct timespec ts, to, te;
49 1.1 christos clockid_t clck;
50 1.1 christos pthread_condattr_t attr;
51 1.1 christos pthread_cond_t cond;
52 1.1 christos pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
53 1.1 christos int ret = 0;
54 1.1 christos
55 1.1 christos
56 1.1 christos clck = *(clockid_t *)param;
57 1.1 christos pthread_condattr_init(&attr);
58 1.1 christos pthread_condattr_setclock(&attr, clck); /* MONOTONIC or MONOTONIC */
59 1.1 christos pthread_cond_init(&cond, &attr);
60 1.1 christos
61 1.1 christos ATF_REQUIRE_EQ((ret = pthread_mutex_lock(&m)), 0);
62 1.1 christos
63 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &ts), 0);
64 1.1 christos to = ts;
65 1.1 christos
66 1.1 christos if (debug)
67 1.2 christos printf("started: %lld.%09ld sec\n", (long long)to.tv_sec,
68 1.2 christos to.tv_nsec);
69 1.1 christos
70 1.1 christos ts.tv_sec += WAITTIME; /* Timeout wait */
71 1.1 christos
72 1.1 christos switch (ret = pthread_cond_timedwait(&cond, &m, &ts)) {
73 1.1 christos case ETIMEDOUT:
74 1.1 christos /* Timeout */
75 1.1 christos ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0);
76 1.1 christos timespecsub(&te, &to, &to);
77 1.1 christos if (debug) {
78 1.2 christos printf("timeout: %lld.%09ld sec\n",
79 1.2 christos (long long)te.tv_sec, te.tv_nsec);
80 1.2 christos printf("elapsed: %lld.%09ld sec\n",
81 1.2 christos (long long)to.tv_sec, to.tv_nsec);
82 1.1 christos }
83 1.1 christos ATF_REQUIRE_EQ(to.tv_sec, WAITTIME);
84 1.1 christos break;
85 1.1 christos default:
86 1.1 christos ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));
87 1.1 christos }
88 1.1 christos
89 1.1 christos ATF_REQUIRE_MSG(!(ret = pthread_mutex_unlock(&m)),
90 1.1 christos "pthread_mutex_unlock: %s", strerror(ret));
91 1.1 christos pthread_exit(&ret);
92 1.1 christos }
93 1.1 christos
94 1.1 christos static void
95 1.1 christos cond_wait(clockid_t clck, const char *msg) {
96 1.1 christos pthread_t child;
97 1.1 christos
98 1.1 christos if (debug)
99 1.1 christos printf( "**** %s clock wait starting\n", msg);
100 1.1 christos ATF_REQUIRE_EQ(pthread_create(&child, NULL, run, &clck), 0);
101 1.1 christos ATF_REQUIRE_EQ(pthread_join(child, NULL), 0); /* wait for terminate */
102 1.1 christos if (debug)
103 1.1 christos printf( "**** %s clock wait ended\n", msg);
104 1.1 christos }
105 1.1 christos
106 1.1 christos ATF_TC(cond_wait_real);
107 1.1 christos ATF_TC_HEAD(cond_wait_real, tc)
108 1.1 christos {
109 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
110 1.1 christos "with CLOCK_REALTIME");
111 1.1 christos }
112 1.1 christos
113 1.1 christos ATF_TC_BODY(cond_wait_real, tc) {
114 1.1 christos cond_wait(CLOCK_REALTIME, "CLOCK_REALTIME");
115 1.1 christos }
116 1.1 christos
117 1.1 christos ATF_TC(cond_wait_mono);
118 1.1 christos ATF_TC_HEAD(cond_wait_mono, tc)
119 1.1 christos {
120 1.1 christos atf_tc_set_md_var(tc, "descr", "Checks pthread_cond_timedwait "
121 1.1 christos "with CLOCK_MONOTONIC");
122 1.1 christos }
123 1.1 christos
124 1.1 christos ATF_TC_BODY(cond_wait_mono, tc) {
125 1.1 christos cond_wait(CLOCK_MONOTONIC, "CLOCK_MONOTONIC");
126 1.1 christos }
127 1.1 christos
128 1.1 christos ATF_TP_ADD_TCS(tp)
129 1.1 christos {
130 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_real);
131 1.1 christos ATF_TP_ADD_TC(tp, cond_wait_mono);
132 1.1 christos return 0;
133 1.1 christos }
134