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