tools/llvm: Do not build with symbols
[minix3.git] / tests / lib / libpthread / t_mutex.c
blob862923179a72826a3f2521c37879e319ca9abc4c
1 /* $NetBSD: t_mutex.c,v 1.5 2011/04/04 14:47:22 martin 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.5 2011/04/04 14:47:22 martin Exp $");
34 #include <pthread.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <unistd.h>
39 #include <atf-c.h>
40 #include <atf-c/config.h>
42 #include "h_common.h"
44 static pthread_mutex_t mutex;
45 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static int global_x;
48 static void *
49 mutex1_threadfunc(void *arg)
51 int *param;
53 printf("2: Second thread.\n");
55 param = arg;
56 printf("2: Locking mutex\n");
57 pthread_mutex_lock(&mutex);
58 printf("2: Got mutex. *param = %d\n", *param);
59 ATF_REQUIRE_EQ(*param, 20);
60 (*param)++;
62 pthread_mutex_unlock(&mutex);
64 return param;
67 ATF_TC(mutex1);
68 ATF_TC_HEAD(mutex1, tc)
70 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
72 ATF_TC_BODY(mutex1, tc)
74 int x;
75 pthread_t new;
76 void *joinval;
78 printf("1: Mutex-test 1\n");
80 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
81 x = 1;
82 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
83 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x));
84 printf("1: Before changing the value.\n");
85 sleep(2);
86 x = 20;
87 printf("1: Before releasing the mutex.\n");
88 sleep(2);
89 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
90 printf("1: After releasing the mutex.\n");
91 PTHREAD_REQUIRE(pthread_join(new, &joinval));
93 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
94 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
95 x, *(int *)joinval);
96 ATF_REQUIRE_EQ(x, 21);
97 ATF_REQUIRE_EQ(*(int *)joinval, 21);
98 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
101 static void *
102 mutex2_threadfunc(void *arg)
104 long count = *(int *)arg;
106 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
108 while (count--) {
109 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
110 global_x++;
111 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
114 return (void *)count;
117 ATF_TC(mutex2);
118 ATF_TC_HEAD(mutex2, tc)
120 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
121 if (strcmp(atf_config_get("atf_arch"), "powerpc") == 0)
122 atf_tc_set_md_var(tc, "timeout", "40");
124 ATF_TC_BODY(mutex2, tc)
126 const char *m_arch;
127 int count, count2;
128 pthread_t new;
129 void *joinval;
131 printf("1: Mutex-test 2\n");
133 m_arch = atf_config_get("atf_arch");
134 if (strcmp(m_arch, "powerpc") == 0) {
135 atf_tc_expect_timeout("PR port-powerpc/44387");
138 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL));
140 global_x = 0;
141 count = count2 = 10000000;
143 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
144 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2));
146 printf("1: Thread %p\n", pthread_self());
148 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
150 while (count--) {
151 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
152 global_x++;
153 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
156 PTHREAD_REQUIRE(pthread_join(new, &joinval));
158 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
159 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
160 global_x, (long)joinval);
161 ATF_REQUIRE_EQ(global_x, 20000000);
163 /* XXX force a timeout in ppc case since an un-triggered race
164 otherwise looks like a "failure" */
165 if (strcmp(m_arch, "powerpc") == 0) {
166 /* We sleep for longer than the timeout to make ATF not
167 complain about unexpected success */
168 sleep(41);
172 static void *
173 mutex3_threadfunc(void *arg)
175 long count = *(int *)arg;
177 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count);
179 while (count--) {
180 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
181 global_x++;
182 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
185 return (void *)count;
188 ATF_TC(mutex3);
189 ATF_TC_HEAD(mutex3, tc)
191 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static "
192 "initializer");
193 if (strcmp(atf_config_get("atf_arch"), "powerpc") == 0)
194 atf_tc_set_md_var(tc, "timeout", "40");
196 ATF_TC_BODY(mutex3, tc)
198 const char *m_arch;
199 int count, count2;
200 pthread_t new;
201 void *joinval;
203 printf("1: Mutex-test 3\n");
205 m_arch = atf_config_get("atf_arch");
206 if (strcmp(m_arch, "powerpc") == 0) {
207 atf_tc_expect_timeout("PR port-powerpc/44387");
210 global_x = 0;
211 count = count2 = 10000000;
213 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
214 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2));
216 printf("1: Thread %p\n", pthread_self());
218 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
220 while (count--) {
221 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
222 global_x++;
223 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex));
226 PTHREAD_REQUIRE(pthread_join(new, &joinval));
228 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex));
229 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
230 global_x, (long)joinval);
231 ATF_REQUIRE_EQ(global_x, 20000000);
233 /* XXX force a timeout in ppc case since an un-triggered race
234 otherwise looks like a "failure" */
235 if (strcmp(m_arch, "powerpc") == 0) {
236 /* We sleep for longer than the timeout to make ATF not
237 complain about unexpected success */
238 sleep(41);
242 static void *
243 mutex4_threadfunc(void *arg)
245 int *param;
247 printf("2: Second thread.\n");
249 param = arg;
250 printf("2: Locking mutex\n");
251 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
252 printf("2: Got mutex. *param = %d\n", *param);
253 (*param)++;
255 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
257 return param;
260 ATF_TC(mutex4);
261 ATF_TC_HEAD(mutex4, tc)
263 atf_tc_set_md_var(tc, "descr", "Checks mutexes");
265 ATF_TC_BODY(mutex4, tc)
267 int x;
268 pthread_t new;
269 pthread_mutexattr_t mattr;
270 void *joinval;
272 printf("1: Mutex-test 4\n");
274 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr));
275 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE));
277 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr));
279 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr));
281 x = 1;
282 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
283 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x));
285 printf("1: Before recursively acquiring the mutex.\n");
286 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
288 printf("1: Before releasing the mutex once.\n");
289 sleep(2);
290 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
291 printf("1: After releasing the mutex once.\n");
293 x = 20;
295 printf("1: Before releasing the mutex twice.\n");
296 sleep(2);
297 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
298 printf("1: After releasing the mutex twice.\n");
300 PTHREAD_REQUIRE(pthread_join(new, &joinval));
302 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex));
303 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
304 x, *(int *)joinval);
305 ATF_REQUIRE_EQ(x, 21);
306 ATF_REQUIRE_EQ(*(int *)joinval, 21);
307 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex));
310 ATF_TP_ADD_TCS(tp)
312 ATF_TP_ADD_TC(tp, mutex1);
313 ATF_TP_ADD_TC(tp, mutex2);
314 ATF_TP_ADD_TC(tp, mutex3);
315 ATF_TP_ADD_TC(tp, mutex4);
317 return atf_no_error();