1 /* $NetBSD: t_mutex.c,v 1.19 2017/12/01 13:25:29 kre 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.19 2017/12/01 13:25:29 kre Exp $"); 33 34 #include <sys/time.h> /* For timespecadd */ 35 #include <inttypes.h> /* For UINT16_MAX */ 36 #include <pthread.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <errno.h> 40 #include <time.h> 41 #include <unistd.h> 42 #include <sys/sched.h> 43 #include <sys/param.h> 44 45 #include <atf-c.h> 46 47 #include "h_common.h" 48 49 static pthread_mutex_t mutex; 50 static pthread_mutex_t static_mutex = PTHREAD_MUTEX_INITIALIZER; 51 static int global_x; 52 53 #ifdef TIMEDMUTEX 54 /* This code is used for verifying non-timed specific code */ 55 static struct timespec ts_lengthy = { 56 .tv_sec = UINT16_MAX, 57 .tv_nsec = 0 58 }; 59 /* This code is used for verifying timed-only specific code */ 60 static struct timespec ts_shortlived = { 61 .tv_sec = 0, 62 .tv_nsec = 120 63 }; 64 65 static int 66 mutex_lock(pthread_mutex_t *m, const struct timespec *ts) 67 { 68 struct timespec ts_wait; 69 ATF_REQUIRE(clock_gettime(CLOCK_REALTIME, &ts_wait) != -1); 70 timespecadd(&ts_wait, ts, &ts_wait); 71 72 return pthread_mutex_timedlock(m, &ts_wait); 73 } 74 #else 75 #define mutex_lock(a, b) pthread_mutex_lock(a) 76 #endif 77 78 static void * 79 mutex1_threadfunc(void *arg) 80 { 81 int *param; 82 83 printf("2: Second thread.\n"); 84 85 param = arg; 86 printf("2: Locking mutex\n"); 87 mutex_lock(&mutex, &ts_lengthy); 88 printf("2: Got mutex. *param = %d\n", *param); 89 ATF_REQUIRE_EQ(*param, 20); 90 (*param)++; 91 92 pthread_mutex_unlock(&mutex); 93 94 return param; 95 } 96 97 ATF_TC(mutex1); 98 ATF_TC_HEAD(mutex1, tc) 99 { 100 atf_tc_set_md_var(tc, "descr", "Checks mutexes"); 101 } 102 ATF_TC_BODY(mutex1, tc) 103 { 104 int x; 105 pthread_t new; 106 void *joinval; 107 108 printf("1: Mutex-test 1\n"); 109 110 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 111 x = 1; 112 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 113 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex1_threadfunc, &x)); 114 printf("1: Before changing the value.\n"); 115 sleep(2); 116 x = 20; 117 printf("1: Before releasing the mutex.\n"); 118 sleep(2); 119 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 120 printf("1: After releasing the mutex.\n"); 121 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 122 123 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 124 printf("1: Thread joined. X was %d. Return value (int) was %d\n", 125 x, *(int *)joinval); 126 ATF_REQUIRE_EQ(x, 21); 127 ATF_REQUIRE_EQ(*(int *)joinval, 21); 128 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 129 } 130 131 static void * 132 mutex2_threadfunc(void *arg) 133 { 134 long count = *(int *)arg; 135 136 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); 137 138 while (count--) { 139 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 140 global_x++; 141 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 142 } 143 144 return (void *)count; 145 } 146 147 ATF_TC(mutex2); 148 ATF_TC_HEAD(mutex2, tc) 149 { 150 atf_tc_set_md_var(tc, "descr", "Checks mutexes"); 151 } 152 ATF_TC_BODY(mutex2, tc) 153 { 154 int count, count2; 155 pthread_t new; 156 void *joinval; 157 158 printf("1: Mutex-test 2\n"); 159 160 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 161 162 global_x = 0; 163 count = count2 = 10000000; 164 165 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 166 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex2_threadfunc, &count2)); 167 168 printf("1: Thread %p\n", pthread_self()); 169 170 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 171 172 while (count--) { 173 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 174 global_x++; 175 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 176 } 177 178 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 179 180 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 181 printf("1: Thread joined. X was %d. Return value (long) was %ld\n", 182 global_x, (long)joinval); 183 ATF_REQUIRE_EQ(global_x, 20000000); 184 } 185 186 static void * 187 mutex3_threadfunc(void *arg) 188 { 189 long count = *(int *)arg; 190 191 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count); 192 193 while (count--) { 194 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy)); 195 global_x++; 196 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 197 } 198 199 return (void *)count; 200 } 201 202 ATF_TC(mutex3); 203 ATF_TC_HEAD(mutex3, tc) 204 { 205 atf_tc_set_md_var(tc, "descr", "Checks mutexes using a static " 206 "initializer"); 207 } 208 ATF_TC_BODY(mutex3, tc) 209 { 210 int count, count2; 211 pthread_t new; 212 void *joinval; 213 214 printf("1: Mutex-test 3\n"); 215 216 global_x = 0; 217 count = count2 = 10000000; 218 219 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy)); 220 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex3_threadfunc, &count2)); 221 222 printf("1: Thread %p\n", pthread_self()); 223 224 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 225 226 while (count--) { 227 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy)); 228 global_x++; 229 PTHREAD_REQUIRE(pthread_mutex_unlock(&static_mutex)); 230 } 231 232 PTHREAD_REQUIRE(pthread_join(new, &joinval)); 233 234 PTHREAD_REQUIRE(mutex_lock(&static_mutex, &ts_lengthy)); 235 printf("1: Thread joined. X was %d. Return value (long) was %ld\n", 236 global_x, (long)joinval); 237 ATF_REQUIRE_EQ(global_x, 20000000); 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(mutex_lock(&mutex, &ts_lengthy)); 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(mutex_lock(&mutex, &ts_lengthy)); 281 PTHREAD_REQUIRE(pthread_create(&new, NULL, mutex4_threadfunc, &x)); 282 283 printf("1: Before recursively acquiring the mutex.\n"); 284 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 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(mutex_lock(&mutex, &ts_lengthy)); 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 static pthread_mutexattr_t attr5; 309 static pthread_mutex_t mutex5; 310 static int min_fifo_prio, max_fifo_prio; 311 312 static void * 313 child_func(void* arg) 314 { 315 int res; 316 317 printf("child is waiting\n"); 318 res = _sched_protect(-2); 319 ATF_REQUIRE_EQ_MSG(res, -1, "sched_protect returned %d", res); 320 ATF_REQUIRE_EQ(errno, ENOENT); 321 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy)); 322 printf("child is owning resource\n"); 323 res = _sched_protect(-2); 324 ATF_REQUIRE_EQ(res, max_fifo_prio); 325 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5)); 326 printf("child is done\n"); 327 328 return 0; 329 } 330 331 ATF_TC(mutex5); 332 ATF_TC_HEAD(mutex5, tc) 333 { 334 atf_tc_set_md_var(tc, "descr", "Checks mutexes for priority setting"); 335 atf_tc_set_md_var(tc, "require.user", "root"); 336 } 337 338 ATF_TC_BODY(mutex5, tc) 339 { 340 int res; 341 struct sched_param param; 342 pthread_t child; 343 344 min_fifo_prio = sched_get_priority_min(SCHED_FIFO); 345 max_fifo_prio = sched_get_priority_max(SCHED_FIFO); 346 printf("min prio for FIFO = %d\n", min_fifo_prio); 347 param.sched_priority = min_fifo_prio; 348 349 /* = 0 OTHER, 1 FIFO, 2 RR, -1 NONE */ 350 res = sched_setscheduler(getpid(), SCHED_FIFO, ¶m); 351 printf("previous policy used = %d\n", res); 352 353 res = sched_getscheduler(getpid()); 354 ATF_REQUIRE_EQ_MSG(res, SCHED_FIFO, "sched %d != FIFO %d", res, 355 SCHED_FIFO); 356 357 PTHREAD_REQUIRE(pthread_mutexattr_init(&attr5)); 358 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&attr5, 359 PTHREAD_PRIO_PROTECT)); 360 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&attr5, 361 max_fifo_prio)); 362 363 PTHREAD_REQUIRE(pthread_mutex_init(&mutex5, &attr5)); 364 PTHREAD_REQUIRE(mutex_lock(&mutex5, &ts_lengthy)); 365 printf("enter critical section for main\n"); 366 PTHREAD_REQUIRE(pthread_create(&child, NULL, child_func, NULL)); 367 printf("main starts to sleep\n"); 368 sleep(10); 369 printf("main completes\n"); 370 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex5)); 371 PTHREAD_REQUIRE(pthread_join(child, NULL)); 372 } 373 374 ATF_TC(mutexattr1); 375 ATF_TC_HEAD(mutexattr1, tc) 376 { 377 atf_tc_set_md_var(tc, "descr", "Checks mutexattr"); 378 } 379 380 ATF_TC_BODY(mutexattr1, tc) 381 { 382 pthread_mutexattr_t mattr; 383 int protocol, target; 384 385 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr)); 386 387 target = PTHREAD_PRIO_NONE; 388 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target)); 389 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol)); 390 ATF_REQUIRE_EQ(protocol, target); 391 392 /* 393 target = PTHREAD_PRIO_INHERIT; 394 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target)); 395 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol)); 396 ATF_REQUIRE_EQ(protocol, target); 397 */ 398 399 target = PTHREAD_PRIO_PROTECT; 400 PTHREAD_REQUIRE(pthread_mutexattr_setprotocol(&mattr, target)); 401 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, &protocol)); 402 ATF_REQUIRE_EQ(protocol, target); 403 } 404 405 ATF_TC(mutexattr2); 406 ATF_TC_HEAD(mutexattr2, tc) 407 { 408 atf_tc_set_md_var(tc, "descr", "Checks mutexattr"); 409 } 410 411 ATF_TC_BODY(mutexattr2, tc) 412 { 413 pthread_mutexattr_t mattr; 414 415 PTHREAD_REQUIRE(pthread_mutexattr_init(&mattr)); 416 int max_prio = sched_get_priority_max(SCHED_FIFO); 417 int min_prio = sched_get_priority_min(SCHED_FIFO); 418 for (int i = min_prio; i <= max_prio; i++) { 419 int prioceiling; 420 int protocol; 421 422 PTHREAD_REQUIRE(pthread_mutexattr_getprotocol(&mattr, 423 &protocol)); 424 425 printf("priority: %d\nprotocol: %d\n", i, protocol); 426 PTHREAD_REQUIRE(pthread_mutexattr_setprioceiling(&mattr, i)); 427 PTHREAD_REQUIRE(pthread_mutexattr_getprioceiling(&mattr, 428 &prioceiling)); 429 printf("prioceiling: %d\n", prioceiling); 430 ATF_REQUIRE_EQ(i, prioceiling); 431 } 432 } 433 434 #ifdef TIMEDMUTEX 435 ATF_TC(timedmutex1); 436 ATF_TC_HEAD(timedmutex1, tc) 437 { 438 atf_tc_set_md_var(tc, "descr", "Checks timeout on selflock"); 439 } 440 441 ATF_TC_BODY(timedmutex1, tc) 442 { 443 444 printf("Timed mutex-test 1\n"); 445 446 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 447 448 printf("Before acquiring mutex\n"); 449 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 450 451 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n"); 452 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived), 453 ETIMEDOUT); 454 455 printf("Unlocking mutex\n"); 456 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 457 } 458 459 ATF_TC(timedmutex2); 460 ATF_TC_HEAD(timedmutex2, tc) 461 { 462 atf_tc_set_md_var(tc, "descr", 463 "Checks timeout on selflock with timedlock"); 464 } 465 466 ATF_TC_BODY(timedmutex2, tc) 467 { 468 469 printf("Timed mutex-test 2\n"); 470 471 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 472 473 printf("Before acquiring mutex with timedlock\n"); 474 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 475 476 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n"); 477 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived), 478 ETIMEDOUT); 479 480 printf("Unlocking mutex\n"); 481 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 482 } 483 484 ATF_TC(timedmutex3); 485 ATF_TC_HEAD(timedmutex3, tc) 486 { 487 atf_tc_set_md_var(tc, "descr", 488 "Checks timeout on selflock in a new thread"); 489 } 490 491 static void * 492 timedmtx_thrdfunc(void *arg) 493 { 494 printf("Before endeavor to reacquire timed-mutex (timeout expected)\n"); 495 PTHREAD_REQUIRE_STATUS(mutex_lock(&mutex, &ts_shortlived), 496 ETIMEDOUT); 497 498 return NULL; 499 } 500 501 ATF_TC_BODY(timedmutex3, tc) 502 { 503 pthread_t new; 504 505 printf("Timed mutex-test 3\n"); 506 507 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 508 509 printf("Before acquiring mutex with timedlock\n"); 510 PTHREAD_REQUIRE(pthread_mutex_lock(&mutex)); 511 512 printf("Before creating new thread\n"); 513 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL)); 514 515 printf("Before joining the mutex\n"); 516 PTHREAD_REQUIRE(pthread_join(new, NULL)); 517 518 printf("Unlocking mutex\n"); 519 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 520 } 521 522 ATF_TC(timedmutex4); 523 ATF_TC_HEAD(timedmutex4, tc) 524 { 525 atf_tc_set_md_var(tc, "descr", 526 "Checks timeout on selflock with timedlock in a new thread"); 527 } 528 529 ATF_TC_BODY(timedmutex4, tc) 530 { 531 pthread_t new; 532 533 printf("Timed mutex-test 4\n"); 534 535 PTHREAD_REQUIRE(pthread_mutex_init(&mutex, NULL)); 536 537 printf("Before acquiring mutex with timedlock\n"); 538 PTHREAD_REQUIRE(mutex_lock(&mutex, &ts_lengthy)); 539 540 printf("Before creating new thread\n"); 541 PTHREAD_REQUIRE(pthread_create(&new, NULL, timedmtx_thrdfunc, NULL)); 542 543 printf("Before joining the mutex\n"); 544 PTHREAD_REQUIRE(pthread_join(new, NULL)); 545 546 printf("Unlocking mutex\n"); 547 PTHREAD_REQUIRE(pthread_mutex_unlock(&mutex)); 548 } 549 #endif 550 551 ATF_TP_ADD_TCS(tp) 552 { 553 ATF_TP_ADD_TC(tp, mutex1); 554 ATF_TP_ADD_TC(tp, mutex2); 555 ATF_TP_ADD_TC(tp, mutex3); 556 ATF_TP_ADD_TC(tp, mutex4); 557 ATF_TP_ADD_TC(tp, mutex5); 558 ATF_TP_ADD_TC(tp, mutexattr1); 559 ATF_TP_ADD_TC(tp, mutexattr2); 560 561 #ifdef TIMEDMUTEX 562 ATF_TP_ADD_TC(tp, timedmutex1); 563 ATF_TP_ADD_TC(tp, timedmutex2); 564 ATF_TP_ADD_TC(tp, timedmutex3); 565 ATF_TP_ADD_TC(tp, timedmutex4); 566 #endif 567 568 return atf_no_error(); 569 } 570