etc/protocols - sync with NetBSD-8
[minix.git] / tests / lib / libpthread / t_mutex.c
blobb5b07b31b4f0e9d5b03fc23400c388b4fbf7b0ff
1 /* $NetBSD: t_mutex.c,v 1.7 2014/11/04 00:20:19 justin Exp $ */
3 /*
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
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.
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.
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 $");
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <atf-c.h>
41 #include "h_common.h"
43 static pthread_mutex_t mutex;
44 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static int global_x;
47 static void *
48 mutex1_threadfunc(void *arg)
50 int *param;
52 printf("2: Second thread.\n");
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)++;
61 pthread_mutex_unlock(&mutex);
63 return param;
66 ATF_TC(mutex1);
67 ATF_TC_HEAD(mutex1, tc)
69 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
71 ATF_TC_BODY(mutex1, tc)
73 int x;
74 pthread_t new;
75 void *joinval;
77 printf("1: Mutex-test 1\n");
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));
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));
100 static void *
101 mutex2_threadfunc(void *arg)
103 long count = *(int *)arg;
105 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
107 while (count--) {
108 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
109 global_x++;
110 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
113 return (void *)count;
116 ATF_TC(mutex2);
117 ATF_TC_HEAD(mutex2, tc)
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
124 ATF_TC_BODY(mutex2, tc)
126 int count, count2;
127 pthread_t new;
128 void *joinval;
130 printf("1: Mutex-test 2\n");
132 #if defined(__powerpc__)
133 atf_tc_expect_timeout("PR port-powerpc/44387");
134 #endif
136 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
138 global_x = 0;
139 count = count2 = 10000000;
141 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
142 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
144 printf("1: Thread %p\n", pthread_self());
146 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
148 while (count--) {
149 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
150 global_x++;
151 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
154 PTHREAD_REQUIRE(pthread_join(new, &joinval));
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);
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
170 static void *
171 mutex3_threadfunc(void *arg)
173 long count = *(int *)arg;
175 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
177 while (count--) {
178 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
179 global_x++;
180 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
183 return (void *)count;
186 ATF_TC(mutex3);
187 ATF_TC_HEAD(mutex3, tc)
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
195 ATF_TC_BODY(mutex3, tc)
197 int count, count2;
198 pthread_t new;
199 void *joinval;
201 printf("1: Mutex-test 3\n");
203 #if defined(__powerpc__)
204 atf_tc_expect_timeout("PR port-powerpc/44387");
205 #endif
207 global_x = 0;
208 count = count2 = 10000000;
210 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
211 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
213 printf("1: Thread %p\n", pthread_self());
215 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
217 while (count--) {
218 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
219 global_x++;
220 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
223 PTHREAD_REQUIRE(pthread_join(new, &joinval));
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);
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
239 static void *
240 mutex4_threadfunc(void *arg)
242 int *param;
244 printf("2: Second thread.\n");
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)++;
252 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
254 return param;
257 ATF_TC(mutex4);
258 ATF_TC_HEAD(mutex4, tc)
260 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
262 ATF_TC_BODY(mutex4, tc)
264 int x;
265 pthread_t new;
266 pthread_mutexattr_t mattr;
267 void *joinval;
269 printf("1: Mutex-test 4\n");
271 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
272 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
274 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
276 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
278 x = 1;
279 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
280 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
282 printf("1: Before recursively acquiring the mutex.\n");
283 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
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");
290 x = 20;
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");
297 PTHREAD_REQUIRE(pthread_join(new, &joinval));
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));
307 ATF_TP_ADD_TCS(tp)
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);
314 return atf_no_error();