xref: /openbsd-src/regress/lib/libpthread/pthread_mutex/pthread_mutex.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: pthread_mutex.c,v 1.6 2005/12/19 05:22:57 tedu Exp $	*/
2 /*
3  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4  * proven@mit.edu All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Chris Provenzano,
17  *	the University of California, Berkeley, and contributors.
18  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19  *   contributors may be used to endorse or promote products derived
20  *   from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* ==== test_pthread_cond.c =========================================
36  * Copyright (c) 1993 by Chris Provenzano, proven@athena.mit.edu
37  *
38  * Description : Test pthread_mutex(). Run this after test_create()
39  *
40  *  1.23 94/05/04 proven
41  *      -Started coding this file.
42  */
43 
44 #include <pthread.h>
45 #include <pthread_np.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include "test.h"
49 
50 int contention_variable;
51 
52 static void *
53 thread_contention(void *arg)
54 {
55 	pthread_mutex_t *mutex = arg;
56 
57 	SET_NAME("cntntn");
58 
59 	CHECKr(pthread_mutex_lock(mutex));
60 	ASSERT(contention_variable == 1);
61 	contention_variable = 2;
62 	CHECKr(pthread_mutex_unlock(mutex));
63 	pthread_exit(NULL);
64 }
65 
66 static void
67 test_contention_lock(pthread_mutex_t *mutex)
68 {
69 	pthread_t thread;
70 
71 	printf("  test_contention_lock()\n");
72 	CHECKr(pthread_mutex_lock(mutex));
73 	contention_variable = 0;
74 	CHECKr(pthread_create(&thread, NULL, thread_contention, mutex));
75 	pthread_yield();
76 	contention_variable = 1;
77 	CHECKr(pthread_mutex_unlock(mutex));
78 	pthread_yield();
79 	CHECKr(pthread_mutex_lock(mutex));
80 	ASSERT(contention_variable == 2);
81 	CHECKr(pthread_mutex_unlock(mutex));
82 }
83 
84 static void
85 test_nocontention_lock(pthread_mutex_t *mutex)
86 {
87 	printf("  test_nocontention_lock()\n");
88 	CHECKr(pthread_mutex_lock(mutex));
89 	CHECKr(pthread_mutex_unlock(mutex));
90 }
91 
92 static void
93 test_debug_double_lock(pthread_mutex_t *mutex)
94 {
95 	printf("  test_debug_double_lock()\n");
96 	CHECKr(pthread_mutex_lock(mutex));
97 	ASSERTe(pthread_mutex_lock(mutex), == EDEADLK);
98 	CHECKr(pthread_mutex_unlock(mutex));
99 }
100 
101 static void
102 test_debug_double_unlock(pthread_mutex_t *mutex)
103 {
104 	printf("  test_debug_double_unlock()\n");
105 	CHECKr(pthread_mutex_lock(mutex));
106 	CHECKr(pthread_mutex_unlock(mutex));
107 	/* Posix D10 says undefined behaviour? */
108 	ASSERTe(pthread_mutex_unlock(mutex), != 0);
109 }
110 
111 static void
112 test_nocontention_trylock(pthread_mutex_t *mutex)
113 {
114 	printf("  test_nocontention_trylock()\n");
115 	CHECKr(pthread_mutex_trylock(mutex));
116 	CHECKr(pthread_mutex_unlock(mutex));
117 }
118 
119 static void
120 test_mutex_static(void)
121 {
122 	pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
123 
124 	printf("test_mutex_static()\n");
125 	test_nocontention_lock(&mutex_static);
126 	test_nocontention_trylock(&mutex_static);
127 	test_contention_lock(&mutex_static);
128 }
129 
130 static void
131 test_mutex_fast(void)
132 {
133 	pthread_mutex_t mutex_fast;
134 
135 	printf("test_mutex_fast()\n");
136 	CHECKr(pthread_mutex_init(&mutex_fast, NULL));
137 	test_nocontention_lock(&mutex_fast);
138 	test_nocontention_trylock(&mutex_fast);
139 	test_contention_lock(&mutex_fast);
140 	CHECKr(pthread_mutex_destroy(&mutex_fast));
141 }
142 
143 static void
144 test_mutex_debug(void)
145 {
146 	pthread_mutexattr_t mutex_debug_attr;
147 	pthread_mutex_t mutex_debug;
148 
149 	printf("test_mutex_debug()\n");
150 	CHECKr(pthread_mutexattr_init(&mutex_debug_attr));
151 	CHECKr(pthread_mutexattr_settype(&mutex_debug_attr,
152 	    PTHREAD_MUTEX_ERRORCHECK));
153 	CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr));
154 	test_nocontention_lock(&mutex_debug);
155 	test_nocontention_trylock(&mutex_debug);
156 	test_contention_lock(&mutex_debug);
157 	test_debug_double_lock(&mutex_debug);
158 	test_debug_double_unlock(&mutex_debug);
159 	CHECKr(pthread_mutex_destroy(&mutex_debug));
160 }
161 
162 static void
163 test_mutex_recursive(void)
164 {
165 	pthread_mutexattr_t mutex_recursive_attr;
166 	pthread_mutex_t mutex_recursive;
167 	int i;
168 	int j = 9;
169 
170 	printf("test_mutex_recursive()\n");
171 	CHECKr(pthread_mutexattr_init(&mutex_recursive_attr));
172 	CHECKr(pthread_mutexattr_settype(&mutex_recursive_attr,
173 	    PTHREAD_MUTEX_RECURSIVE));
174 	CHECKr(pthread_mutex_init(&mutex_recursive, &mutex_recursive_attr));
175 
176 	CHECKr(pthread_mutex_lock(&mutex_recursive));
177 	for (i = 0; i < j; i++)
178 		CHECKr(pthread_mutex_lock(&mutex_recursive));
179 	for (i = 0; i < j; i++)
180 		CHECKr(pthread_mutex_unlock(&mutex_recursive));
181 	CHECKr(pthread_mutex_unlock(&mutex_recursive));
182 	/* Posix D10 says undefined behaviour? */
183 	ASSERTe(pthread_mutex_unlock(&mutex_recursive), != 0);
184 	CHECKr(pthread_mutex_destroy(&mutex_recursive));
185 }
186 
187 int
188 main(int argc, char *argv[])
189 {
190 	test_mutex_static();
191 	test_mutex_fast();
192 	test_mutex_debug();
193 	test_mutex_recursive();
194 	SUCCEED;
195 }
196