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