t_mtx.c revision 1.1.2.2 1 /* $NetBSD: t_mtx.c,v 1.1.2.2 2019/06/10 22:10:07 christos 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_mtx.c,v 1.1.2.2 2019/06/10 22:10:07 christos Exp $");
36
37 #include <time.h>
38 #include <threads.h>
39
40 #include <atf-c.h>
41
42 ATF_TC(mtx_init);
43 ATF_TC_HEAD(mtx_init, tc)
44 {
45 atf_tc_set_md_var(tc, "descr", "Test C11 mtx_init(3)");
46 }
47
48 ATF_TC_BODY(mtx_init, tc)
49 {
50 mtx_t m;
51
52 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
53 mtx_destroy(&m);
54
55 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
56 mtx_destroy(&m);
57
58 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
59 mtx_destroy(&m);
60
61 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
62 mtx_destroy(&m);
63
64 ATF_REQUIRE_EQ(mtx_init(&m, mtx_recursive), thrd_error);
65 mtx_destroy(&m);
66
67 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_timed), thrd_error);
68 mtx_destroy(&m);
69
70 ATF_REQUIRE_EQ(mtx_init(&m, -1), thrd_error);
71 mtx_destroy(&m);
72 }
73
74 ATF_TC(mtx_lock);
75 ATF_TC_HEAD(mtx_lock, tc)
76 {
77 atf_tc_set_md_var(tc, "descr", "Test C11 mtx_lock(3)");
78 }
79
80 ATF_TC_BODY(mtx_lock, tc)
81 {
82 mtx_t m;
83
84 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
85 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
86 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
87 mtx_destroy(&m);
88
89 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
90 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
91 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
92 mtx_destroy(&m);
93
94 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
95 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
96 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
97 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
98 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
99 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
100 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
101 mtx_destroy(&m);
102
103 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
104 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
105 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
106 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
107 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
108 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
109 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
110 mtx_destroy(&m);
111 }
112
113 ATF_TC(mtx_timedlock);
114 ATF_TC_HEAD(mtx_timedlock, tc)
115 {
116 atf_tc_set_md_var(tc, "descr", "Test C11 mtx_timedlock(3)");
117 }
118
119 ATF_TC_BODY(mtx_timedlock, tc)
120 {
121 mtx_t m;
122 struct timespec ts;
123
124 ts.tv_sec = 0;
125 ts.tv_nsec = 1;
126
127 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
128 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
129 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
130 mtx_destroy(&m);
131
132 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
133 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
134 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_timedout);
135 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
136 mtx_destroy(&m);
137
138 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
139 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
140 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
141 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
142 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
143 mtx_destroy(&m);
144
145 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
146 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
147 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
148 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
149 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
150 mtx_destroy(&m);
151
152 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
153 ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success);
154 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
155 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
156 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
157 mtx_destroy(&m);
158 }
159
160 ATF_TC(mtx_trylock);
161 ATF_TC_HEAD(mtx_trylock, tc)
162 {
163 atf_tc_set_md_var(tc, "descr", "Test C11 mtx_trylock(3)");
164 }
165
166 ATF_TC_BODY(mtx_trylock, tc)
167 {
168 mtx_t m;
169
170 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
171 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
172 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
173 mtx_destroy(&m);
174
175 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success);
176 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
177 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy);
178 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
179 mtx_destroy(&m);
180
181 ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success);
182 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
183 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
184 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
185 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
186 mtx_destroy(&m);
187
188 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
189 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
190 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
191 mtx_destroy(&m);
192
193 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success);
194 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
195 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy);
196 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
197 mtx_destroy(&m);
198
199 ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success);
200 ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success);
201 ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success);
202 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
203 ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success);
204 mtx_destroy(&m);
205 }
206
207 ATF_TP_ADD_TCS(tp)
208 {
209 ATF_TP_ADD_TC(tp, mtx_init);
210 ATF_TP_ADD_TC(tp, mtx_lock);
211 ATF_TP_ADD_TC(tp, mtx_timedlock);
212 ATF_TP_ADD_TC(tp, mtx_trylock);
213
214 return atf_no_error();
215 }
216