xref: /minix/tests/lib/libpthread/t_mutex.c (revision 00b67f09)
1 /* $NetBSD: t_mutex.c,v 1.7 2014/11/04 00:20:19 justin Exp $ */
2 
3 /*
4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __COPYRIGHT("@(#) Copyright (c) 2008\
31  The NetBSD Foundation, inc. All rights reserved.");
32 __RCSID("$NetBSD: t_mutex.c,v 1.7 2014/11/04 00:20:19 justin Exp $");
33 
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
38 
39 #include <atf-c.h>
40 
41 #include "h_common.h"
42 
43 static pthread_mutex_t mutex;
44 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static int global_x;
46 
47 static void *
48 mutex1_threadfunc(void *arg)
49 {
50 	int *param;
51 
52 	printf("2: Second thread.\n");
53 
54 	param = arg;
55 	printf("2: Locking mutex\n");
56 	pthread_mutex_lock(&mutex);
57 	printf("2: Got mutex. *param = %d\n", *param);
58 	ATF_REQUIRE_EQ(*param, 20);
59 	(*param)++;
60 
61 	pthread_mutex_unlock(&mutex);
62 
63 	return param;
64 }
65 
66 ATF_TC(mutex1);
67 ATF_TC_HEAD(mutex1, tc)
68 {
69 	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
70 }
71 ATF_TC_BODY(mutex1, tc)
72 {
73 	int x;
74 	pthread_t new;
75 	void *joinval;
76 
77 	printf("1: Mutex-test 1\n");
78 
79 	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
80 	x = 1;
81 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
82 	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
83 	printf("1: Before changing the value.\n");
84 	sleep(2);
85 	x = 20;
86 	printf("1: Before releasing the mutex.\n");
87 	sleep(2);
88 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
89 	printf("1: After releasing the mutex.\n");
90 	PTHREAD_REQUIRE(pthread_join(new, &joinval));
91 
92 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
93 	printf("1: Thread joined. X was %d. Return value (int) was %d\n",
94 		x, *(int *)joinval);
95 	ATF_REQUIRE_EQ(x, 21);
96 	ATF_REQUIRE_EQ(*(int *)joinval, 21);
97 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
98 }
99 
100 static void *
101 mutex2_threadfunc(void *arg)
102 {
103 	long count = *(int *)arg;
104 
105 	printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
106 
107 	while (count--) {
108 		PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
109 		global_x++;
110 		PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
111 	}
112 
113 	return (void *)count;
114 }
115 
116 ATF_TC(mutex2);
117 ATF_TC_HEAD(mutex2, tc)
118 {
119 	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
120 #if defined(__powerpc__)
121 	atf_tc_set_md_var(tc, "timeout", "40");
122 #endif
123 }
124 ATF_TC_BODY(mutex2, tc)
125 {
126 	int count, count2;
127 	pthread_t new;
128 	void *joinval;
129 
130 	printf("1: Mutex-test 2\n");
131 
132 #if defined(__powerpc__)
133 	atf_tc_expect_timeout("PR port-powerpc/44387");
134 #endif
135 
136 	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
137 
138 	global_x = 0;
139 	count = count2 = 10000000;
140 
141 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
142 	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
143 
144 	printf("1: Thread %p\n", pthread_self());
145 
146 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
147 
148 	while (count--) {
149 		PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
150 		global_x++;
151 		PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
152 	}
153 
154 	PTHREAD_REQUIRE(pthread_join(new, &joinval));
155 
156 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
157 	printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
158 		global_x, (long)joinval);
159 	ATF_REQUIRE_EQ(global_x, 20000000);
160 
161 #if defined(__powerpc__)
162 	/* XXX force a timeout in ppc case since an un-triggered race
163 	   otherwise looks like a "failure" */
164 	/* We sleep for longer than the timeout to make ATF not
165 	   complain about unexpected success */
166 	sleep(41);
167 #endif
168 }
169 
170 static void *
171 mutex3_threadfunc(void *arg)
172 {
173 	long count = *(int *)arg;
174 
175 	printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
176 
177 	while (count--) {
178 		PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
179 		global_x++;
180 		PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
181 	}
182 
183 	return (void *)count;
184 }
185 
186 ATF_TC(mutex3);
187 ATF_TC_HEAD(mutex3, tc)
188 {
189 	atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
190 	    "initializer");
191 #if defined(__powerpc__)
192 	atf_tc_set_md_var(tc, "timeout", "40");
193 #endif
194 }
195 ATF_TC_BODY(mutex3, tc)
196 {
197 	int count, count2;
198 	pthread_t new;
199 	void *joinval;
200 
201 	printf("1: Mutex-test 3\n");
202 
203 #if defined(__powerpc__)
204 	atf_tc_expect_timeout("PR port-powerpc/44387");
205 #endif
206 
207 	global_x = 0;
208 	count = count2 = 10000000;
209 
210 	PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
211 	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
212 
213 	printf("1: Thread %p\n", pthread_self());
214 
215 	PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
216 
217 	while (count--) {
218 		PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
219 		global_x++;
220 		PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
221 	}
222 
223 	PTHREAD_REQUIRE(pthread_join(new, &joinval));
224 
225 	PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
226 	printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
227 		global_x, (long)joinval);
228 	ATF_REQUIRE_EQ(global_x, 20000000);
229 
230 #if defined(__powerpc__)
231 	/* XXX force a timeout in ppc case since an un-triggered race
232 	   otherwise looks like a "failure" */
233 	/* We sleep for longer than the timeout to make ATF not
234 	   complain about unexpected success */
235 	sleep(41);
236 #endif
237 }
238 
239 static void *
240 mutex4_threadfunc(void *arg)
241 {
242 	int *param;
243 
244 	printf("2: Second thread.\n");
245 
246 	param = arg;
247 	printf("2: Locking mutex\n");
248 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
249 	printf("2: Got mutex. *param = %d\n", *param);
250 	(*param)++;
251 
252 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
253 
254 	return param;
255 }
256 
257 ATF_TC(mutex4);
258 ATF_TC_HEAD(mutex4, tc)
259 {
260 	atf_tc_set_md_var(tc, "descr", "Checks mutexes");
261 }
262 ATF_TC_BODY(mutex4, tc)
263 {
264 	int x;
265 	pthread_t new;
266 	pthread_mutexattr_t mattr;
267 	void *joinval;
268 
269 	printf("1: Mutex-test 4\n");
270 
271 	PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
272 	PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
273 
274 	PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
275 
276 	PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
277 
278 	x = 1;
279 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
280 	PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
281 
282 	printf("1: Before recursively acquiring the mutex.\n");
283 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
284 
285 	printf("1: Before releasing the mutex once.\n");
286 	sleep(2);
287 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
288 	printf("1: After releasing the mutex once.\n");
289 
290 	x = 20;
291 
292 	printf("1: Before releasing the mutex twice.\n");
293 	sleep(2);
294 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
295 	printf("1: After releasing the mutex twice.\n");
296 
297 	PTHREAD_REQUIRE(pthread_join(new, &joinval));
298 
299 	PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
300 	printf("1: Thread joined. X was %d. Return value (int) was %d\n",
301 		x, *(int *)joinval);
302 	ATF_REQUIRE_EQ(x, 21);
303 	ATF_REQUIRE_EQ(*(int *)joinval, 21);
304 	PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
305 }
306 
307 ATF_TP_ADD_TCS(tp)
308 {
309 	ATF_TP_ADD_TC(tp, mutex1);
310 	ATF_TP_ADD_TC(tp, mutex2);
311 	ATF_TP_ADD_TC(tp, mutex3);
312 	ATF_TP_ADD_TC(tp, mutex4);
313 
314 	return atf_no_error();
315 }
316