t_cnd.c revision 1.1.2.2 1 1.1.2.2 christos /* $NetBSD: t_cnd.c,v 1.1.2.2 2019/06/10 22:10:07 christos Exp $ */
2 1.1.2.2 christos
3 1.1.2.2 christos /*-
4 1.1.2.2 christos * Copyright (c) 2019 The NetBSD Foundation, Inc.
5 1.1.2.2 christos * All rights reserved.
6 1.1.2.2 christos *
7 1.1.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.2 christos * by Kamil Rytarowski.
9 1.1.2.2 christos *
10 1.1.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 christos * modification, are permitted provided that the following conditions
12 1.1.2.2 christos * are met:
13 1.1.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.1.2.2 christos *
19 1.1.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.2 christos */
31 1.1.2.2 christos
32 1.1.2.2 christos #include <sys/cdefs.h>
33 1.1.2.2 christos __COPYRIGHT("@(#) Copyright (c) 2019\
34 1.1.2.2 christos The NetBSD Foundation, inc. All rights reserved.");
35 1.1.2.2 christos __RCSID("$NetBSD: t_cnd.c,v 1.1.2.2 2019/06/10 22:10:07 christos Exp $");
36 1.1.2.2 christos
37 1.1.2.2 christos #include <stdbool.h>
38 1.1.2.2 christos #include <threads.h>
39 1.1.2.2 christos #include <time.h>
40 1.1.2.2 christos
41 1.1.2.2 christos #include <atf-c.h>
42 1.1.2.2 christos
43 1.1.2.2 christos ATF_TC(cnd_init);
44 1.1.2.2 christos ATF_TC_HEAD(cnd_init, tc)
45 1.1.2.2 christos {
46 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 cnd_init(3)");
47 1.1.2.2 christos }
48 1.1.2.2 christos
49 1.1.2.2 christos ATF_TC_BODY(cnd_init, tc)
50 1.1.2.2 christos {
51 1.1.2.2 christos cnd_t c;
52 1.1.2.2 christos
53 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_init(&c), thrd_success);
54 1.1.2.2 christos cnd_destroy(&c);
55 1.1.2.2 christos }
56 1.1.2.2 christos
57 1.1.2.2 christos #define B_THREADS 8
58 1.1.2.2 christos
59 1.1.2.2 christos static mtx_t b_m;
60 1.1.2.2 christos static cnd_t b_c;
61 1.1.2.2 christos static bool b_finished;
62 1.1.2.2 christos
63 1.1.2.2 christos static int
64 1.1.2.2 christos b_func(void *arg)
65 1.1.2.2 christos {
66 1.1.2.2 christos bool signal;
67 1.1.2.2 christos
68 1.1.2.2 christos signal = (bool)(intptr_t)arg;
69 1.1.2.2 christos
70 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_lock(&b_m), thrd_success);
71 1.1.2.2 christos while (!b_finished) {
72 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_wait(&b_c, &b_m), thrd_success);
73 1.1.2.2 christos }
74 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_unlock(&b_m), thrd_success);
75 1.1.2.2 christos
76 1.1.2.2 christos if (signal) {
77 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_signal(&b_c), thrd_success);
78 1.1.2.2 christos }
79 1.1.2.2 christos
80 1.1.2.2 christos return 0;
81 1.1.2.2 christos }
82 1.1.2.2 christos
83 1.1.2.2 christos static void
84 1.1.2.2 christos cnd_notify(bool signal)
85 1.1.2.2 christos {
86 1.1.2.2 christos size_t i;
87 1.1.2.2 christos thrd_t t[B_THREADS];
88 1.1.2.2 christos void *n;
89 1.1.2.2 christos
90 1.1.2.2 christos n = (void *)(intptr_t)signal;
91 1.1.2.2 christos
92 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_init(&b_m, mtx_plain), thrd_success);
93 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_init(&b_c), thrd_success);
94 1.1.2.2 christos
95 1.1.2.2 christos for (i = 0; i < B_THREADS; i++) {
96 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_create(&t[i], b_func, n), thrd_success);
97 1.1.2.2 christos }
98 1.1.2.2 christos
99 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_lock(&b_m), thrd_success);
100 1.1.2.2 christos b_finished = true;
101 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_unlock(&b_m), thrd_success);
102 1.1.2.2 christos
103 1.1.2.2 christos if (signal) {
104 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_signal(&b_c), thrd_success);
105 1.1.2.2 christos } else {
106 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_broadcast(&b_c), thrd_success);
107 1.1.2.2 christos }
108 1.1.2.2 christos
109 1.1.2.2 christos for (i = 0; i < B_THREADS; i++) {
110 1.1.2.2 christos ATF_REQUIRE_EQ(thrd_join(t[i], NULL), thrd_success);
111 1.1.2.2 christos }
112 1.1.2.2 christos
113 1.1.2.2 christos mtx_destroy(&b_m);
114 1.1.2.2 christos cnd_destroy(&b_c);
115 1.1.2.2 christos }
116 1.1.2.2 christos
117 1.1.2.2 christos ATF_TC(cnd_broadcast);
118 1.1.2.2 christos ATF_TC_HEAD(cnd_broadcast, tc)
119 1.1.2.2 christos {
120 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 cnd_broadcast(3)");
121 1.1.2.2 christos }
122 1.1.2.2 christos
123 1.1.2.2 christos ATF_TC_BODY(cnd_broadcast, tc)
124 1.1.2.2 christos {
125 1.1.2.2 christos
126 1.1.2.2 christos cnd_notify(false);
127 1.1.2.2 christos }
128 1.1.2.2 christos
129 1.1.2.2 christos ATF_TC(cnd_signal);
130 1.1.2.2 christos ATF_TC_HEAD(cnd_signal, tc)
131 1.1.2.2 christos {
132 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 cnd_signal(3)");
133 1.1.2.2 christos }
134 1.1.2.2 christos
135 1.1.2.2 christos ATF_TC_BODY(cnd_signal, tc)
136 1.1.2.2 christos {
137 1.1.2.2 christos
138 1.1.2.2 christos cnd_notify(true);
139 1.1.2.2 christos }
140 1.1.2.2 christos
141 1.1.2.2 christos ATF_TC(cnd_timedwait);
142 1.1.2.2 christos ATF_TC_HEAD(cnd_timedwait, tc)
143 1.1.2.2 christos {
144 1.1.2.2 christos atf_tc_set_md_var(tc, "descr", "Test C11 cnd_timedwait(3)");
145 1.1.2.2 christos }
146 1.1.2.2 christos
147 1.1.2.2 christos ATF_TC_BODY(cnd_timedwait, tc)
148 1.1.2.2 christos {
149 1.1.2.2 christos mtx_t m;
150 1.1.2.2 christos cnd_t c;
151 1.1.2.2 christos struct timespec ts;
152 1.1.2.2 christos
153 1.1.2.2 christos ts.tv_sec = 0;
154 1.1.2.2 christos ts.tv_nsec = 1;
155 1.1.2.2 christos
156 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
157 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_init(&c), thrd_success);
158 1.1.2.2 christos
159 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
160 1.1.2.2 christos ATF_REQUIRE_EQ(cnd_timedwait(&c, &m, &ts), thrd_timedout);
161 1.1.2.2 christos ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
162 1.1.2.2 christos
163 1.1.2.2 christos mtx_destroy(&m);
164 1.1.2.2 christos cnd_destroy(&c);
165 1.1.2.2 christos }
166 1.1.2.2 christos
167 1.1.2.2 christos ATF_TP_ADD_TCS(tp)
168 1.1.2.2 christos {
169 1.1.2.2 christos ATF_TP_ADD_TC(tp, cnd_init);
170 1.1.2.2 christos ATF_TP_ADD_TC(tp, cnd_broadcast);
171 1.1.2.2 christos ATF_TP_ADD_TC(tp, cnd_signal);
172 1.1.2.2 christos ATF_TP_ADD_TC(tp, cnd_timedwait);
173 1.1.2.2 christos
174 1.1.2.2 christos return atf_no_error();
175 1.1.2.2 christos }
176