1 /* $OpenBSD: pthread_mutex.c,v 1.5 2005/10/30 23:59:43 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 <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 CHECKr(pthread_mutex_lock(mutex)); 79 ASSERT(contention_variable == 2); 80 CHECKr(pthread_mutex_unlock(mutex)); 81 } 82 83 static void 84 test_nocontention_lock(pthread_mutex_t *mutex) 85 { 86 printf(" test_nocontention_lock()\n"); 87 CHECKr(pthread_mutex_lock(mutex)); 88 CHECKr(pthread_mutex_unlock(mutex)); 89 } 90 91 static void 92 test_debug_double_lock(pthread_mutex_t *mutex) 93 { 94 printf(" test_debug_double_lock()\n"); 95 CHECKr(pthread_mutex_lock(mutex)); 96 ASSERTe(pthread_mutex_lock(mutex), == EDEADLK); 97 CHECKr(pthread_mutex_unlock(mutex)); 98 } 99 100 static void 101 test_debug_double_unlock(pthread_mutex_t *mutex) 102 { 103 printf(" test_debug_double_unlock()\n"); 104 CHECKr(pthread_mutex_lock(mutex)); 105 CHECKr(pthread_mutex_unlock(mutex)); 106 /* Posix D10 says undefined behaviour? */ 107 ASSERTe(pthread_mutex_unlock(mutex), != 0); 108 } 109 110 static void 111 test_nocontention_trylock(pthread_mutex_t *mutex) 112 { 113 printf(" test_nocontention_trylock()\n"); 114 CHECKr(pthread_mutex_trylock(mutex)); 115 CHECKr(pthread_mutex_unlock(mutex)); 116 } 117 118 static void 119 test_mutex_static(void) 120 { 121 pthread_mutex_t mutex_static = PTHREAD_MUTEX_INITIALIZER; 122 123 printf("test_mutex_static()\n"); 124 test_nocontention_lock(&mutex_static); 125 test_nocontention_trylock(&mutex_static); 126 test_contention_lock(&mutex_static); 127 } 128 129 static void 130 test_mutex_fast(void) 131 { 132 pthread_mutex_t mutex_fast; 133 134 printf("test_mutex_fast()\n"); 135 CHECKr(pthread_mutex_init(&mutex_fast, NULL)); 136 test_nocontention_lock(&mutex_fast); 137 test_nocontention_trylock(&mutex_fast); 138 test_contention_lock(&mutex_fast); 139 CHECKr(pthread_mutex_destroy(&mutex_fast)); 140 } 141 142 static void 143 test_mutex_debug(void) 144 { 145 pthread_mutexattr_t mutex_debug_attr; 146 pthread_mutex_t mutex_debug; 147 148 printf("test_mutex_debug()\n"); 149 CHECKr(pthread_mutexattr_init(&mutex_debug_attr)); 150 CHECKr(pthread_mutexattr_settype(&mutex_debug_attr, 151 PTHREAD_MUTEX_ERRORCHECK)); 152 CHECKr(pthread_mutex_init(&mutex_debug, &mutex_debug_attr)); 153 test_nocontention_lock(&mutex_debug); 154 test_nocontention_trylock(&mutex_debug); 155 test_contention_lock(&mutex_debug); 156 test_debug_double_lock(&mutex_debug); 157 test_debug_double_unlock(&mutex_debug); 158 CHECKr(pthread_mutex_destroy(&mutex_debug)); 159 } 160 161 static void 162 test_mutex_recursive(void) 163 { 164 pthread_mutexattr_t mutex_recursive_attr; 165 pthread_mutex_t mutex_recursive; 166 int i; 167 int j = 9; 168 169 printf("test_mutex_recursive()\n"); 170 CHECKr(pthread_mutexattr_init(&mutex_recursive_attr)); 171 CHECKr(pthread_mutexattr_settype(&mutex_recursive_attr, 172 PTHREAD_MUTEX_RECURSIVE)); 173 CHECKr(pthread_mutex_init(&mutex_recursive, &mutex_recursive_attr)); 174 175 CHECKr(pthread_mutex_lock(&mutex_recursive)); 176 for (i = 0; i < j; i++) 177 CHECKr(pthread_mutex_lock(&mutex_recursive)); 178 for (i = 0; i < j; i++) 179 CHECKr(pthread_mutex_unlock(&mutex_recursive)); 180 CHECKr(pthread_mutex_unlock(&mutex_recursive)); 181 /* Posix D10 says undefined behaviour? */ 182 ASSERTe(pthread_mutex_unlock(&mutex_recursive), != 0); 183 CHECKr(pthread_mutex_destroy(&mutex_recursive)); 184 } 185 186 int 187 main(int argc, char *argv[]) 188 { 189 test_mutex_static(); 190 test_mutex_fast(); 191 test_mutex_debug(); 192 test_mutex_recursive(); 193 SUCCEED; 194 } 195