1 /* $NetBSD: t_mutex.c,v 1.5 2011/04/04 14:47:22 martin Exp $ */
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 $");
40 #include <atf-c/config.h>
44 static pthread_mutex_t mutex
;
45 static pthread_mutex_t static_mutex
= PTHREAD_MUTEX_INITIALIZER
;
49 mutex1_threadfunc(void *arg
)
53 printf("2: Second thread.\n");
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);
62 pthread_mutex_unlock(&mutex
);
68 ATF_TC_HEAD(mutex1
, tc
)
70 atf_tc_set_md_var(tc
, "descr", "Checks mutexes");
72 ATF_TC_BODY(mutex1
, tc
)
78 printf("1: Mutex-test 1\n");
80 PTHREAD_REQUIRE(pthread_mutex_init(&mutex
, NULL
));
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");
87 printf("1: Before releasing the mutex.\n");
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",
96 ATF_REQUIRE_EQ(x
, 21);
97 ATF_REQUIRE_EQ(*(int *)joinval
, 21);
98 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
102 mutex2_threadfunc(void *arg
)
104 long count
= *(int *)arg
;
106 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count
);
109 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
111 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
114 return (void *)count
;
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
)
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
));
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
));
151 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
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 */
173 mutex3_threadfunc(void *arg
)
175 long count
= *(int *)arg
;
177 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count
);
180 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex
));
182 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex
));
185 return (void *)count
;
189 ATF_TC_HEAD(mutex3
, tc
)
191 atf_tc_set_md_var(tc
, "descr", "Checks mutexes using a static "
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
)
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");
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
));
221 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex
));
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 */
243 mutex4_threadfunc(void *arg
)
247 printf("2: Second thread.\n");
250 printf("2: Locking mutex\n");
251 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
252 printf("2: Got mutex. *param = %d\n", *param
);
255 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
261 ATF_TC_HEAD(mutex4
, tc
)
263 atf_tc_set_md_var(tc
, "descr", "Checks mutexes");
265 ATF_TC_BODY(mutex4
, tc
)
269 pthread_mutexattr_t mattr
;
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
));
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");
290 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
291 printf("1: After releasing the mutex once.\n");
295 printf("1: Before releasing the mutex twice.\n");
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",
305 ATF_REQUIRE_EQ(x
, 21);
306 ATF_REQUIRE_EQ(*(int *)joinval
, 21);
307 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
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();