1 /* $NetBSD: t_mutex.c,v 1.7 2014/11/04 00:20:19 justin 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.7 2014/11/04 00:20:19 justin Exp $");
43 static pthread_mutex_t mutex
;
44 static pthread_mutex_t static_mutex
= PTHREAD_MUTEX_INITIALIZER
;
48 mutex1_threadfunc(void *arg
)
52 printf("2: Second thread.\n");
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);
61 pthread_mutex_unlock(&mutex
);
67 ATF_TC_HEAD(mutex1
, tc
)
69 atf_tc_set_md_var(tc
, "descr", "Checks mutexes");
71 ATF_TC_BODY(mutex1
, tc
)
77 printf("1: Mutex-test 1\n");
79 PTHREAD_REQUIRE(pthread_mutex_init(&mutex
, NULL
));
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");
86 printf("1: Before releasing the mutex.\n");
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",
95 ATF_REQUIRE_EQ(x
, 21);
96 ATF_REQUIRE_EQ(*(int *)joinval
, 21);
97 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
101 mutex2_threadfunc(void *arg
)
103 long count
= *(int *)arg
;
105 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count
);
108 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
110 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
113 return (void *)count
;
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");
124 ATF_TC_BODY(mutex2
, tc
)
130 printf("1: Mutex-test 2\n");
132 #if defined(__powerpc__)
133 atf_tc_expect_timeout("PR port-powerpc/44387");
136 PTHREAD_REQUIRE(pthread_mutex_init(&mutex
, NULL
));
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
));
149 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
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 */
171 mutex3_threadfunc(void *arg
)
173 long count
= *(int *)arg
;
175 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count
);
178 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex
));
180 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex
));
183 return (void *)count
;
187 ATF_TC_HEAD(mutex3
, tc
)
189 atf_tc_set_md_var(tc
, "descr", "Checks mutexes using a static "
191 #if defined(__powerpc__)
192 atf_tc_set_md_var(tc
, "timeout", "40");
195 ATF_TC_BODY(mutex3
, tc
)
201 printf("1: Mutex-test 3\n");
203 #if defined(__powerpc__)
204 atf_tc_expect_timeout("PR port-powerpc/44387");
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
));
218 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex
));
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 */
240 mutex4_threadfunc(void *arg
)
244 printf("2: Second thread.\n");
247 printf("2: Locking mutex\n");
248 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex
));
249 printf("2: Got mutex. *param = %d\n", *param
);
252 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
258 ATF_TC_HEAD(mutex4
, tc
)
260 atf_tc_set_md_var(tc
, "descr", "Checks mutexes");
262 ATF_TC_BODY(mutex4
, tc
)
266 pthread_mutexattr_t mattr
;
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
));
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");
287 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
288 printf("1: After releasing the mutex once.\n");
292 printf("1: Before releasing the mutex twice.\n");
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",
302 ATF_REQUIRE_EQ(x
, 21);
303 ATF_REQUIRE_EQ(*(int *)joinval
, 21);
304 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex
));
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();