xref: /openbsd-src/regress/lib/libpthread/pthread_mutex/pthread_mutex.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: pthread_mutex.c,v 1.1.1.1 2001/08/15 14:37:12 fgsch 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 "test.h"
48 
49 int contention_variable;
50 
51 void *
52 thread_contention(arg)
53 	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 void
67 test_contention_lock(mutex)
68 	pthread_mutex_t *mutex;
69 {
70 	pthread_t thread;
71 
72 	printf("  test_contention_lock()\n");
73 	CHECKr(pthread_mutex_lock(mutex));
74 	contention_variable = 0;
75 	CHECKr(pthread_create(&thread, NULL, thread_contention, mutex));
76 	pthread_yield();
77 	contention_variable = 1;
78 	CHECKr(pthread_mutex_unlock(mutex));
79 	CHECKr(pthread_mutex_lock(mutex));
80 	ASSERT(contention_variable == 2);
81 	CHECKr(pthread_mutex_unlock(mutex));
82 }
83 
84 void
85 test_nocontention_lock(mutex)
86 	pthread_mutex_t *mutex;
87 {
88 	printf("  test_nocontention_lock()\n");
89 	CHECKr(pthread_mutex_lock(mutex));
90 	CHECKr(pthread_mutex_unlock(mutex));
91 }
92 
93 void
94 test_debug_double_lock(mutex)
95 	pthread_mutex_t *mutex;
96 {
97 	printf("  test_debug_double_lock()\n");
98 	CHECKr(pthread_mutex_lock(mutex));
99 	ASSERTe(pthread_mutex_lock(mutex), == EDEADLK);
100 	CHECKr(pthread_mutex_unlock(mutex));
101 }
102 
103 void
104 test_debug_double_unlock(mutex)
105 	pthread_mutex_t *mutex;
106 {
107 	printf("  test_debug_double_unlock()\n");
108 	CHECKr(pthread_mutex_lock(mutex));
109 	CHECKr(pthread_mutex_unlock(mutex));
110 	/* Posix D10 says undefined behaviour? */
111 	ASSERTe(pthread_mutex_unlock(mutex), != EPERM);
112 }
113 
114 void
115 test_nocontention_trylock(mutex)
116 	pthread_mutex_t *mutex;
117 {
118 	printf("  test_nocontention_trylock()\n");
119 	CHECKr(pthread_mutex_trylock(mutex));
120 	CHECKr(pthread_mutex_unlock(mutex));
121 }
122 
123 void
124 test_mutex_static()
125 {
126 	pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER;
127 
128 	printf("test_mutex_static()\n");
129 	test_nocontention_lock(&mutex_static);
130 	test_contention_lock(&mutex_static);
131 }
132 
133 void
134 test_mutex_fast(void)
135 {
136 	pthread_mutex_t mutex_fast;
137 
138 	printf("test_mutex_fast()\n");
139 	CHECKr(pthread_mutex_init(&mutex_fast, NULL));
140 	test_nocontention_lock(&mutex_fast);
141 	test_contention_lock(&mutex_fast);
142 	CHECKr(pthread_mutex_destroy(&mutex_fast));
143 }
144 
145 void
146 test_mutex_debug()
147 {
148 	pthread_mutexattr_t mutex_debug_attr;
149 	pthread_mutex_t mutex_debug;
150 
151 	printf("test_mutex_debug()\n");
152 	CHECKr(pthread_mutexattr_init(&mutex_debug_attr));
153 	CHECKr(pthread_mutexattr_settype(&mutex_debug_attr,
154 	    PTHREAD_MUTEX_ERRORCHECK));
155 	CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr));
156 	test_nocontention_lock(&mutex_debug);
157 	test_contention_lock(&mutex_debug);
158 	test_debug_double_lock(&mutex_debug);
159 	test_debug_double_unlock(&mutex_debug);
160 	CHECKr(pthread_mutex_destroy(&mutex_debug));
161 }
162 
163 void
164 test_mutex_recursive()
165 {
166 	pthread_mutexattr_t mutex_recursive_attr;
167 	pthread_mutex_t mutex_recursive;
168 	int i;
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 < 9; i++)
178 		CHECKr(pthread_mutex_lock(&mutex_recursive));
179 	for (i = 0; i < 9; 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 }
185 
186 int
187 main()
188 {
189 	test_mutex_static();
190 	test_mutex_fast();
191 	test_mutex_debug();
192 	test_mutex_recursive();
193 	SUCCEED;
194 }
195