1*db3296cfSderaadt /*	$OpenBSD: pthread_mutex.c,v 1.4 2003/07/31 21:48:05 deraadt 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 
44b2ea75c1Sfgsch #include <pthread.h>
45b2ea75c1Sfgsch #include <pthread_np.h>
46b2ea75c1Sfgsch #include <stdio.h>
478445c537Stodd #include <stdlib.h>
48b2ea75c1Sfgsch #include "test.h"
49b2ea75c1Sfgsch 
50b2ea75c1Sfgsch int contention_variable;
51b2ea75c1Sfgsch 
52*db3296cfSderaadt static void *
53*db3296cfSderaadt thread_contention(void *arg)
54b2ea75c1Sfgsch {
55b2ea75c1Sfgsch 	pthread_mutex_t *mutex = arg;
56b2ea75c1Sfgsch 
57b2ea75c1Sfgsch 	SET_NAME("cntntn");
58b2ea75c1Sfgsch 
59b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
60b2ea75c1Sfgsch 	ASSERT(contention_variable == 1);
61b2ea75c1Sfgsch 	contention_variable = 2;
62b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
63b2ea75c1Sfgsch 	pthread_exit(NULL);
64b2ea75c1Sfgsch }
65b2ea75c1Sfgsch 
66*db3296cfSderaadt static void
67*db3296cfSderaadt test_contention_lock(pthread_mutex_t *mutex)
68b2ea75c1Sfgsch {
69b2ea75c1Sfgsch 	pthread_t thread;
70b2ea75c1Sfgsch 
71b2ea75c1Sfgsch 	printf("  test_contention_lock()\n");
72b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
73b2ea75c1Sfgsch 	contention_variable = 0;
74b2ea75c1Sfgsch 	CHECKr(pthread_create(&thread, NULL, thread_contention, mutex));
75b2ea75c1Sfgsch 	pthread_yield();
76b2ea75c1Sfgsch 	contention_variable = 1;
77b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
78b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
79b2ea75c1Sfgsch 	ASSERT(contention_variable == 2);
80b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
81b2ea75c1Sfgsch }
82b2ea75c1Sfgsch 
83*db3296cfSderaadt static void
84*db3296cfSderaadt test_nocontention_lock(pthread_mutex_t *mutex)
85b2ea75c1Sfgsch {
86b2ea75c1Sfgsch 	printf("  test_nocontention_lock()\n");
87b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
88b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
89b2ea75c1Sfgsch }
90b2ea75c1Sfgsch 
91*db3296cfSderaadt static void
92*db3296cfSderaadt test_debug_double_lock(pthread_mutex_t *mutex)
93b2ea75c1Sfgsch {
94b2ea75c1Sfgsch 	printf("  test_debug_double_lock()\n");
95b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
96b2ea75c1Sfgsch 	ASSERTe(pthread_mutex_lock(mutex), == EDEADLK);
97b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
98b2ea75c1Sfgsch }
99b2ea75c1Sfgsch 
100*db3296cfSderaadt static void
101*db3296cfSderaadt test_debug_double_unlock(pthread_mutex_t *mutex)
102b2ea75c1Sfgsch {
103b2ea75c1Sfgsch 	printf("  test_debug_double_unlock()\n");
104b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(mutex));
105b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
106b2ea75c1Sfgsch 	/* Posix D10 says undefined behaviour? */
107523ac096Smarc 	ASSERTe(pthread_mutex_unlock(mutex), != 0);
108b2ea75c1Sfgsch }
109b2ea75c1Sfgsch 
110*db3296cfSderaadt static void
111*db3296cfSderaadt test_nocontention_trylock(pthread_mutex_t *mutex)
112b2ea75c1Sfgsch {
113b2ea75c1Sfgsch 	printf("  test_nocontention_trylock()\n");
114b2ea75c1Sfgsch 	CHECKr(pthread_mutex_trylock(mutex));
115b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(mutex));
116b2ea75c1Sfgsch }
117b2ea75c1Sfgsch 
118*db3296cfSderaadt static void
119*db3296cfSderaadt test_mutex_static(void)
120b2ea75c1Sfgsch {
121b2ea75c1Sfgsch 	pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
122b2ea75c1Sfgsch 
123b2ea75c1Sfgsch 	printf("test_mutex_static()\n");
124b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_static);
125b2ea75c1Sfgsch 	test_contention_lock(&mutex_static);
126b2ea75c1Sfgsch }
127b2ea75c1Sfgsch 
128*db3296cfSderaadt static void
129b2ea75c1Sfgsch test_mutex_fast(void)
130b2ea75c1Sfgsch {
131b2ea75c1Sfgsch 	pthread_mutex_t mutex_fast;
132b2ea75c1Sfgsch 
133b2ea75c1Sfgsch 	printf("test_mutex_fast()\n");
134b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_fast, NULL));
135b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_fast);
136b2ea75c1Sfgsch 	test_contention_lock(&mutex_fast);
137b2ea75c1Sfgsch 	CHECKr(pthread_mutex_destroy(&mutex_fast));
138b2ea75c1Sfgsch }
139b2ea75c1Sfgsch 
140*db3296cfSderaadt static void
141*db3296cfSderaadt test_mutex_debug(void)
142b2ea75c1Sfgsch {
143b2ea75c1Sfgsch 	pthread_mutexattr_t mutex_debug_attr;
144b2ea75c1Sfgsch 	pthread_mutex_t mutex_debug;
145b2ea75c1Sfgsch 
146b2ea75c1Sfgsch 	printf("test_mutex_debug()\n");
147b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_init(&mutex_debug_attr));
148b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_settype(&mutex_debug_attr,
149b2ea75c1Sfgsch 	    PTHREAD_MUTEX_ERRORCHECK));
150b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr));
151b2ea75c1Sfgsch 	test_nocontention_lock(&mutex_debug);
152b2ea75c1Sfgsch 	test_contention_lock(&mutex_debug);
153b2ea75c1Sfgsch 	test_debug_double_lock(&mutex_debug);
154b2ea75c1Sfgsch 	test_debug_double_unlock(&mutex_debug);
155b2ea75c1Sfgsch 	CHECKr(pthread_mutex_destroy(&mutex_debug));
156b2ea75c1Sfgsch }
157b2ea75c1Sfgsch 
158*db3296cfSderaadt static void
159*db3296cfSderaadt test_mutex_recursive(void)
160b2ea75c1Sfgsch {
161b2ea75c1Sfgsch 	pthread_mutexattr_t mutex_recursive_attr;
162b2ea75c1Sfgsch 	pthread_mutex_t mutex_recursive;
163b2ea75c1Sfgsch 	int i;
164523ac096Smarc 	int j = 9;
165b2ea75c1Sfgsch 
166b2ea75c1Sfgsch 	printf("test_mutex_recursive()\n");
167b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_init(&mutex_recursive_attr));
168b2ea75c1Sfgsch 	CHECKr(pthread_mutexattr_settype(&mutex_recursive_attr,
169b2ea75c1Sfgsch 	    PTHREAD_MUTEX_RECURSIVE));
170b2ea75c1Sfgsch 	CHECKr(pthread_mutex_init(&mutex_recursive, &mutex_recursive_attr));
171b2ea75c1Sfgsch 
172b2ea75c1Sfgsch 	CHECKr(pthread_mutex_lock(&mutex_recursive));
173523ac096Smarc 	for (i = 0; i < j; i++)
174b2ea75c1Sfgsch 		CHECKr(pthread_mutex_lock(&mutex_recursive));
175523ac096Smarc 	for (i = 0; i < j; i++)
176b2ea75c1Sfgsch 		CHECKr(pthread_mutex_unlock(&mutex_recursive));
177b2ea75c1Sfgsch 	CHECKr(pthread_mutex_unlock(&mutex_recursive));
178b2ea75c1Sfgsch 	/* Posix D10 says undefined behaviour? */
179b2ea75c1Sfgsch 	ASSERTe(pthread_mutex_unlock(&mutex_recursive), != 0);
180523ac096Smarc 	CHECKr(pthread_mutex_destroy(&mutex_recursive));
181b2ea75c1Sfgsch }
182b2ea75c1Sfgsch 
183b2ea75c1Sfgsch int
184*db3296cfSderaadt main(int argc, char *argv[])
185b2ea75c1Sfgsch {
186b2ea75c1Sfgsch 	test_mutex_static();
187b2ea75c1Sfgsch 	test_mutex_fast();
188b2ea75c1Sfgsch 	test_mutex_debug();
189b2ea75c1Sfgsch 	test_mutex_recursive();
190b2ea75c1Sfgsch 	SUCCEED;
191b2ea75c1Sfgsch }
192