1 /* $NetBSD: t_mutex.c,v 1.6 2014/02/09 21:26:07 jmmv Exp $ */ 2 3 /* 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __COPYRIGHT("@(#) Copyright (c) 2008\ 31 The NetBSD Foundation, inc. All rights reserved."); 32 __RCSID("$NetBSD: t_mutex.c,v 1.6 2014/02/09 21:26:07 jmmv Exp $"); 33 34 #include <pthread.h> 35 #include <stdio.h> 36 #include <string.h> 37 #include <unistd.h> 38 39 #include <atf-c.h> 40 #include <atf-c/config.h> 41 42 #include "h_common.h" 43 44 static pthread_mutex_t mutex; 45 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; 46 static int global_x; 47 48 static void * 49 mutex1_threadfunc(void *arg) 50 { 51 int *param; 52 53 printf("2: Second thread.\n"); 54 55 param = arg; 56 printf("2: Locking mutex\n"); 57 pthread_mutex_lock(&mutex); 58 printf("2: Got mutex. *param = %d\n", *param); 59 ATF_REQUIRE_EQ(*param, 20); 60 (*param)++; 61 62 pthread_mutex_unlock(&mutex); 63 64 return param; 65 } 66 67 ATF_TC(mutex1); 68 ATF_TC_HEAD(mutex1, tc) 69 { 70 atf_tc_set_md_var(tc, "descr", "Checks mutexes"); 71 } 72 ATF_TC_BODY(mutex1, tc) 73 { 74 int x; 75 pthread_t new; 76 void *joinval; 77 78 printf("1: Mutex-test 1\n"); 79 80 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 81 x = 1; 82 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 83 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x)); 84 printf("1: Before changing the value.\n"); 85 sleep(2); 86 x = 20; 87 printf("1: Before releasing the mutex.\n"); 88 sleep(2); 89 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 90 printf("1: After releasing the mutex.\n"); 91 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 92 93 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 94 printf("1: Thread joined. X was %d. Return value (int) was %d\n", 95 x, *(int *)joinval); 96 ATF_REQUIRE_EQ(x, 21); 97 ATF_REQUIRE_EQ(*(int *)joinval, 21); 98 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 99 } 100 101 static void * 102 mutex2_threadfunc(void *arg) 103 { 104 long count = *(int *)arg; 105 106 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); 107 108 while (count--) { 109 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 110 global_x++; 111 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 112 } 113 114 return (void *)count; 115 } 116 117 ATF_TC(mutex2); 118 ATF_TC_HEAD(mutex2, tc) 119 { 120 atf_tc_set_md_var(tc, "descr", "Checks mutexes"); 121 #if defined(__powerpc__) 122 atf_tc_set_md_var(tc, "timeout", "40"); 123 #endif 124 } 125 ATF_TC_BODY(mutex2, tc) 126 { 127 int count, count2; 128 pthread_t new; 129 void *joinval; 130 131 printf("1: Mutex-test 2\n"); 132 133 #if defined(__powerpc__) 134 atf_tc_expect_timeout("PR port-powerpc/44387"); 135 #endif 136 137 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 138 139 global_x = 0; 140 count = count2 = 10000000; 141 142 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 143 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2)); 144 145 printf("1: Thread %p\n", pthread_self()); 146 147 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 148 149 while (count--) { 150 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 151 global_x++; 152 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 153 } 154 155 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 156 157 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 158 printf("1: Thread joined. X was %d. Return value (long) was %ld\n", 159 global_x, (long)joinval); 160 ATF_REQUIRE_EQ(global_x, 20000000); 161 162 #if defined(__powerpc__) 163 /* XXX force a timeout in ppc case since an un-triggered race 164 otherwise looks like a "failure" */ 165 /* We sleep for longer than the timeout to make ATF not 166 complain about unexpected success */ 167 sleep(41); 168 #endif 169 } 170 171 static void * 172 mutex3_threadfunc(void *arg) 173 { 174 long count = *(int *)arg; 175 176 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); 177 178 while (count--) { 179 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); 180 global_x++; 181 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 182 } 183 184 return (void *)count; 185 } 186 187 ATF_TC(mutex3); 188 ATF_TC_HEAD(mutex3, tc) 189 { 190 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static " 191 "initializer"); 192 #if defined(__powerpc__) 193 atf_tc_set_md_var(tc, "timeout", "40"); 194 #endif 195 } 196 ATF_TC_BODY(mutex3, tc) 197 { 198 int count, count2; 199 pthread_t new; 200 void *joinval; 201 202 printf("1: Mutex-test 3\n"); 203 204 #if defined(__powerpc__) 205 atf_tc_expect_timeout("PR port-powerpc/44387"); 206 #endif 207 208 global_x = 0; 209 count = count2 = 10000000; 210 211 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); 212 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2)); 213 214 printf("1: Thread %p\n", pthread_self()); 215 216 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 217 218 while (count--) { 219 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); 220 global_x++; 221 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 222 } 223 224 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 225 226 PTHREAD_REQUIRE(pthread_mutex_lock(&static_mutex)); 227 printf("1: Thread joined. X was %d. Return value (long) was %ld\n", 228 global_x, (long)joinval); 229 ATF_REQUIRE_EQ(global_x, 20000000); 230 231 #if defined(__powerpc__) 232 /* XXX force a timeout in ppc case since an un-triggered race 233 otherwise looks like a "failure" */ 234 /* We sleep for longer than the timeout to make ATF not 235 complain about unexpected success */ 236 sleep(41); 237 #endif 238 } 239 240 static void * 241 mutex4_threadfunc(void *arg) 242 { 243 int *param; 244 245 printf("2: Second thread.\n"); 246 247 param = arg; 248 printf("2: Locking mutex\n"); 249 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 250 printf("2: Got mutex. *param = %d\n", *param); 251 (*param)++; 252 253 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 254 255 return param; 256 } 257 258 ATF_TC(mutex4); 259 ATF_TC_HEAD(mutex4, tc) 260 { 261 atf_tc_set_md_var(tc, "descr", "Checks mutexes"); 262 } 263 ATF_TC_BODY(mutex4, tc) 264 { 265 int x; 266 pthread_t new; 267 pthread_mutexattr_t mattr; 268 void *joinval; 269 270 printf("1: Mutex-test 4\n"); 271 272 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr)); 273 PTHREAD_REQUIRE(pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_RECURSIVE)); 274 275 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, &mattr)); 276 277 PTHREAD_REQUIRE(pthread_mutexattr_destroy(&mattr)); 278 279 x = 1; 280 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 281 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x)); 282 283 printf("1: Before recursively acquiring the mutex.\n"); 284 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 285 286 printf("1: Before releasing the mutex once.\n"); 287 sleep(2); 288 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 289 printf("1: After releasing the mutex once.\n"); 290 291 x = 20; 292 293 printf("1: Before releasing the mutex twice.\n"); 294 sleep(2); 295 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 296 printf("1: After releasing the mutex twice.\n"); 297 298 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 299 300 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 301 printf("1: Thread joined. X was %d. Return value (int) was %d\n", 302 x, *(int *)joinval); 303 ATF_REQUIRE_EQ(x, 21); 304 ATF_REQUIRE_EQ(*(int *)joinval, 21); 305 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 306 } 307 308 ATF_TP_ADD_TCS(tp) 309 { 310 ATF_TP_ADD_TC(tp, mutex1); 311 ATF_TP_ADD_TC(tp, mutex2); 312 ATF_TP_ADD_TC(tp, mutex3); 313 ATF_TP_ADD_TC(tp, mutex4); 314 315 return atf_no_error(); 316 } 317