1*a86e1260Sguenther /*	$OpenBSD: pthread_mutex.c,v 1.10 2012/03/24 21:39:10 guenther Exp $	*/
2b2ea75c1Sfgsch /*
3b2ea75c1Sfgsch  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4b2ea75c1Sfgsch  * proven@mit.edu All rights reserved.
5b2ea75c1Sfgsch  *
6b2ea75c1Sfgsch  * Redistribution and use in source and binary forms, with or without
7b2ea75c1Sfgsch  * modification, are permitted provided that the following conditions
8b2ea75c1Sfgsch  * are met:
9b2ea75c1Sfgsch  * 1. Redistributions of source code must retain the above copyright
10b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer.
11b2ea75c1Sfgsch  * 2. Redistributions in binary form must reproduce the above copyright
12b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer in the
13b2ea75c1Sfgsch  *    documentation and/or other materials provided with the distribution.
14b2ea75c1Sfgsch  * 3. All advertising materials mentioning features or use of this software
15b2ea75c1Sfgsch  *    must display the following acknowledgement:
16b2ea75c1Sfgsch  *	This product includes software developed by Chris Provenzano,
17b2ea75c1Sfgsch  *	the University of California, Berkeley, and contributors.
18b2ea75c1Sfgsch  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19b2ea75c1Sfgsch  *   contributors may be used to endorse or promote products derived
20b2ea75c1Sfgsch  *   from this software without specific prior written permission.
21b2ea75c1Sfgsch  *
22b2ea75c1Sfgsch  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23b2ea75c1Sfgsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24b2ea75c1Sfgsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25b2ea75c1Sfgsch  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26b2ea75c1Sfgsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27b2ea75c1Sfgsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28b2ea75c1Sfgsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29b2ea75c1Sfgsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30b2ea75c1Sfgsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31b2ea75c1Sfgsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32b2ea75c1Sfgsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33b2ea75c1Sfgsch  */
34b2ea75c1Sfgsch 
35b2ea75c1Sfgsch /* ==== test_pthread_cond.c =========================================
36b2ea75c1Sfgsch  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37b2ea75c1Sfgsch  *
38b2ea75c1Sfgsch  * Description : Test pthread_mutex(). Run this after test_create()
39b2ea75c1Sfgsch  *
40b2ea75c1Sfgsch  *  1.23 94/05/04 proven
41b2ea75c1Sfgsch  *      -Started coding this file.
42b2ea75c1Sfgsch  */
43b2ea75c1Sfgsch 
44078d8877Sguenther #include <sys/time.h>
45078d8877Sguenther #include <errno.h>
46b2ea75c1Sfgsch #include <pthread.h>
47b2ea75c1Sfgsch #include <pthread_np.h>
48b2ea75c1Sfgsch #include <stdio.h>
498445c537Stodd #include <stdlib.h>
50c9c91f29Sguenther #include <unistd.h>
51b2ea75c1Sfgsch #include "test.h"
52b2ea75c1Sfgsch 
53b2ea75c1Sfgsch int contention_variable;
54b2ea75c1Sfgsch 
55db3296cfSderaadt static void *
thread_contention(void * arg)56db3296cfSderaadt thread_contention(void *arg)
57b2ea75c1Sfgsch {
58b2ea75c1Sfgsch 	pthread_mutex_t *mutex = arg;
59b2ea75c1Sfgsch 
60b2ea75c1Sfgsch 	SET_NAME("cntntn");
61b2ea75c1Sfgsch 
62b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
63b2ea75c1Sfgsch 	ASSERT(contention_variable == 1);
64b2ea75c1Sfgsch 	contention_variable = 2;
65b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
66b2ea75c1Sfgsch 	pthread_exit(NULL);
67b2ea75c1Sfgsch }
68b2ea75c1Sfgsch 
69db3296cfSderaadt static void
test_contention_lock(pthread_mutex_t * mutex)70db3296cfSderaadt test_contention_lock(pthread_mutex_t *mutex)
71b2ea75c1Sfgsch {
72b2ea75c1Sfgsch 	pthread_t thread;
73b2ea75c1Sfgsch 
74b2ea75c1Sfgsch 	printf("  test_contention_lock()\n");
75b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
76b2ea75c1Sfgsch 	contention_variable = 0;
77b2ea75c1Sfgsch 	CHECKr(pthread_create(&thread, NULL, thread_contention, mutex));
78b2ea75c1Sfgsch 	pthread_yield();
79b2ea75c1Sfgsch 	contention_variable = 1;
80b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
81c9c91f29Sguenther 	sleep(1);
82b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
83b2ea75c1Sfgsch 	ASSERT(contention_variable == 2);
84b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
85078d8877Sguenther 	CHECKr(pthread_join(thread, NULL));
86b2ea75c1Sfgsch }
87b2ea75c1Sfgsch 
88db3296cfSderaadt static void
test_nocontention_lock(pthread_mutex_t * mutex)89db3296cfSderaadt test_nocontention_lock(pthread_mutex_t *mutex)
90b2ea75c1Sfgsch {
91b2ea75c1Sfgsch 	printf("  test_nocontention_lock()\n");
92b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
93b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
94b2ea75c1Sfgsch }
95b2ea75c1Sfgsch 
96db3296cfSderaadt static void
test_debug_double_lock(pthread_mutex_t * mutex)97db3296cfSderaadt test_debug_double_lock(pthread_mutex_t *mutex)
98b2ea75c1Sfgsch {
99b2ea75c1Sfgsch 	printf("  test_debug_double_lock()\n");
100b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
101b2ea75c1Sfgsch 	ASSERTe(pthread_mutex_lock(mutex), == EDEADLK);
102b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
103b2ea75c1Sfgsch }
104b2ea75c1Sfgsch 
105db3296cfSderaadt static void
test_debug_double_unlock(pthread_mutex_t * mutex)106db3296cfSderaadt test_debug_double_unlock(pthread_mutex_t *mutex)
107b2ea75c1Sfgsch {
108b2ea75c1Sfgsch 	printf("  test_debug_double_unlock()\n");
109b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
110b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
111b2ea75c1Sfgsch 	/* Posix D10 says undefined behaviour? */
112523ac096Smarc 	ASSERTe(pthread_mutex_unlock(mutex), != 0);
113b2ea75c1Sfgsch }
114b2ea75c1Sfgsch 
115db3296cfSderaadt static void
test_nocontention_trylock(pthread_mutex_t * mutex)116db3296cfSderaadt test_nocontention_trylock(pthread_mutex_t *mutex)
117b2ea75c1Sfgsch {
118b2ea75c1Sfgsch 	printf("  test_nocontention_trylock()\n");
119b2ea75c1Sfgsch 	CHECKr(pthread_mutex_trylock(mutex));
120b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
121b2ea75c1Sfgsch }
122b2ea75c1Sfgsch 
123db3296cfSderaadt static void
test_mutex_static(void)124db3296cfSderaadt test_mutex_static(void)
125b2ea75c1Sfgsch {
126b2ea75c1Sfgsch 	pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
127b2ea75c1Sfgsch 
128b2ea75c1Sfgsch 	printf("test_mutex_static()\n");
129b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_static);
130d0774bb2Sfgsch 	test_nocontention_trylock(&mutex_static);
131b2ea75c1Sfgsch 	test_contention_lock(&mutex_static);
132b2ea75c1Sfgsch }
133b2ea75c1Sfgsch 
134db3296cfSderaadt static void
test_mutex_fast(void)135b2ea75c1Sfgsch test_mutex_fast(void)
136b2ea75c1Sfgsch {
137b2ea75c1Sfgsch 	pthread_mutex_t mutex_fast;
138b2ea75c1Sfgsch 
139b2ea75c1Sfgsch 	printf("test_mutex_fast()\n");
140b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_fast, NULL));
141b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_fast);
142d0774bb2Sfgsch 	test_nocontention_trylock(&mutex_fast);
143b2ea75c1Sfgsch 	test_contention_lock(&mutex_fast);
144b2ea75c1Sfgsch 	CHECKr(pthread_mutex_destroy(&mutex_fast));
145b2ea75c1Sfgsch }
146b2ea75c1Sfgsch 
147db3296cfSderaadt static void
test_mutex_debug(void)148db3296cfSderaadt test_mutex_debug(void)
149b2ea75c1Sfgsch {
150b2ea75c1Sfgsch 	pthread_mutexattr_t mutex_debug_attr;
151b2ea75c1Sfgsch 	pthread_mutex_t mutex_debug;
152b2ea75c1Sfgsch 
153b2ea75c1Sfgsch 	printf("test_mutex_debug()\n");
154b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_init(&mutex_debug_attr));
155b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_settype(&mutex_debug_attr,
156b2ea75c1Sfgsch 	    PTHREAD_MUTEX_ERRORCHECK));
157b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr));
158078d8877Sguenther 	CHECKr(pthread_mutexattr_destroy(&mutex_debug_attr));
159b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_debug);
160d0774bb2Sfgsch 	test_nocontention_trylock(&mutex_debug);
161b2ea75c1Sfgsch 	test_contention_lock(&mutex_debug);
162b2ea75c1Sfgsch 	test_debug_double_lock(&mutex_debug);
163b2ea75c1Sfgsch 	test_debug_double_unlock(&mutex_debug);
164b2ea75c1Sfgsch 	CHECKr(pthread_mutex_destroy(&mutex_debug));
165b2ea75c1Sfgsch }
166b2ea75c1Sfgsch 
167db3296cfSderaadt static void
test_mutex_recursive_lock(pthread_mutex_t * mutex)16818bdda7bSuwe test_mutex_recursive_lock(pthread_mutex_t *mutex)
16918bdda7bSuwe {
17018bdda7bSuwe 	int i;
17118bdda7bSuwe 	int j = 9;
17218bdda7bSuwe 
17318bdda7bSuwe 	printf("  %s()\n", __func__);
17418bdda7bSuwe 	CHECKr(pthread_mutex_lock(mutex));
17518bdda7bSuwe 	for (i = 0; i < j; i++)
17618bdda7bSuwe 		CHECKr(pthread_mutex_lock(mutex));
17718bdda7bSuwe 	for (i = 0; i < j; i++)
17818bdda7bSuwe 		CHECKr(pthread_mutex_unlock(mutex));
17918bdda7bSuwe 	CHECKr(pthread_mutex_unlock(mutex));
18018bdda7bSuwe }
18118bdda7bSuwe 
18218bdda7bSuwe static void
test_mutex_recursive_trylock(pthread_mutex_t * mutex)18318bdda7bSuwe test_mutex_recursive_trylock(pthread_mutex_t *mutex)
18418bdda7bSuwe {
18518bdda7bSuwe 	int i;
18618bdda7bSuwe 	int j = 9;
18718bdda7bSuwe 
18818bdda7bSuwe 	printf("  %s()\n", __func__);
18918bdda7bSuwe 	CHECKr(pthread_mutex_trylock(mutex));
19018bdda7bSuwe 	for (i = 0; i < j; i++)
19118bdda7bSuwe 		CHECKr(pthread_mutex_trylock(mutex));
19218bdda7bSuwe 	for (i = 0; i < j; i++)
19318bdda7bSuwe 		CHECKr(pthread_mutex_unlock(mutex));
19418bdda7bSuwe 	CHECKr(pthread_mutex_unlock(mutex));
19518bdda7bSuwe }
19618bdda7bSuwe 
19718bdda7bSuwe static void
test_mutex_recursive(void)198db3296cfSderaadt test_mutex_recursive(void)
199b2ea75c1Sfgsch {
200b2ea75c1Sfgsch 	pthread_mutexattr_t mutex_recursive_attr;
201b2ea75c1Sfgsch 	pthread_mutex_t mutex_recursive;
202b2ea75c1Sfgsch 
203b2ea75c1Sfgsch 	printf("test_mutex_recursive()\n");
204b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_init(&mutex_recursive_attr));
205b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_settype(&mutex_recursive_attr,
206b2ea75c1Sfgsch 	    PTHREAD_MUTEX_RECURSIVE));
207b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_recursive, &mutex_recursive_attr));
208078d8877Sguenther 	CHECKr(pthread_mutexattr_destroy(&mutex_recursive_attr));
20918bdda7bSuwe 	test_mutex_recursive_lock(&mutex_recursive);
21018bdda7bSuwe 	test_mutex_recursive_trylock(&mutex_recursive);
211b2ea75c1Sfgsch 	/* Posix D10 says undefined behaviour? */
212b2ea75c1Sfgsch 	ASSERTe(pthread_mutex_unlock(&mutex_recursive), != 0);
213523ac096Smarc 	CHECKr(pthread_mutex_destroy(&mutex_recursive));
214b2ea75c1Sfgsch }
215b2ea75c1Sfgsch 
216078d8877Sguenther static void *
thread_deadlock(void * arg)217078d8877Sguenther thread_deadlock(void *arg)
218078d8877Sguenther {
219078d8877Sguenther 	pthread_mutex_t *mutex = arg;;
220078d8877Sguenther 
221078d8877Sguenther 	/* intentionally deadlock this thread */
222078d8877Sguenther 	CHECKr(pthread_mutex_lock(mutex));
223078d8877Sguenther 	CHECKr(pthread_mutex_lock(mutex));
224078d8877Sguenther 
225078d8877Sguenther 	/* never reached */
226078d8877Sguenther 	abort();
227078d8877Sguenther }
228078d8877Sguenther 
229078d8877Sguenther static void
test_mutex_normal(void)230078d8877Sguenther test_mutex_normal(void)
231078d8877Sguenther {
232078d8877Sguenther 	pthread_mutexattr_t mutex_normal_attr;
233078d8877Sguenther 	pthread_mutex_t mutex_normal;
234078d8877Sguenther 	pthread_t thread;
235078d8877Sguenther 	struct timespec ts;
236078d8877Sguenther 
237078d8877Sguenther 	printf("test_mutex_normal()\n");
238078d8877Sguenther 	CHECKr(pthread_mutexattr_init(&mutex_normal_attr));
239078d8877Sguenther 	CHECKr(pthread_mutexattr_settype(&mutex_normal_attr,
240078d8877Sguenther 	    PTHREAD_MUTEX_NORMAL));
241078d8877Sguenther 	CHECKr(pthread_mutex_init(&mutex_normal, &mutex_normal_attr));
242078d8877Sguenther 	CHECKr(pthread_mutexattr_destroy(&mutex_normal_attr));
243078d8877Sguenther 	test_nocontention_lock(&mutex_normal);
244078d8877Sguenther 	test_nocontention_trylock(&mutex_normal);
245078d8877Sguenther 	test_contention_lock(&mutex_normal);
246078d8877Sguenther 
247078d8877Sguenther 	/* test self-deadlock with timeout */
248078d8877Sguenther 	CHECKr(pthread_mutex_lock(&mutex_normal));
249078d8877Sguenther 	CHECKe(clock_gettime(CLOCK_REALTIME, &ts));
250078d8877Sguenther 	ts.tv_sec += 2;
251078d8877Sguenther 	ASSERTe(pthread_mutex_timedlock(&mutex_normal, &ts), == ETIMEDOUT);
252078d8877Sguenther 	CHECKr(pthread_mutex_unlock(&mutex_normal));
253*a86e1260Sguenther 	/* verify that it can still be locked and unlocked */
254*a86e1260Sguenther 	CHECKr(pthread_mutex_lock(&mutex_normal));
255*a86e1260Sguenther 	CHECKr(pthread_mutex_unlock(&mutex_normal));
256078d8877Sguenther 	CHECKr(pthread_create(&thread, NULL, thread_deadlock, &mutex_normal));
257078d8877Sguenther 	sleep(1);
258078d8877Sguenther }
259078d8877Sguenther 
260b2ea75c1Sfgsch int
main(int argc,char * argv[])261db3296cfSderaadt main(int argc, char *argv[])
262b2ea75c1Sfgsch {
263b2ea75c1Sfgsch 	test_mutex_static();
264b2ea75c1Sfgsch 	test_mutex_fast();
265b2ea75c1Sfgsch 	test_mutex_debug();
266b2ea75c1Sfgsch 	test_mutex_recursive();
267078d8877Sguenther 	test_mutex_normal();
268b2ea75c1Sfgsch 	SUCCEED;
269b2ea75c1Sfgsch }
270