1 /* $NetBSD: pthread_mutex.c,v 1.59 2014/02/03 15:51:01 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Nathan J. Williams, by Jason R. Thorpe, and by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * To track threads waiting for mutexes to be released, we use lockless 34 * lists built on atomic operations and memory barriers. 35 * 36 * A simple spinlock would be faster and make the code easier to 37 * follow, but spinlocks are problematic in userspace. If a thread is 38 * preempted by the kernel while holding a spinlock, any other thread 39 * attempting to acquire that spinlock will needlessly busy wait. 40 * 41 * There is no good way to know that the holding thread is no longer 42 * running, nor to request a wake-up once it has begun running again. 43 * Of more concern, threads in the SCHED_FIFO class do not have a 44 * limited time quantum and so could spin forever, preventing the 45 * thread holding the spinlock from getting CPU time: it would never 46 * be released. 47 */ 48 49 #include <sys/cdefs.h> 50 __RCSID("$NetBSD: pthread_mutex.c,v 1.59 2014/02/03 15:51:01 rmind Exp $"); 51 52 #include <sys/types.h> 53 #include <sys/lwpctl.h> 54 #include <sys/lock.h> 55 56 #include <errno.h> 57 #include <limits.h> 58 #include <stdlib.h> 59 #include <time.h> 60 #include <string.h> 61 #include <stdio.h> 62 63 #include "pthread.h" 64 #include "pthread_int.h" 65 #include "reentrant.h" 66 67 #define MUTEX_WAITERS_BIT ((uintptr_t)0x01) 68 #define MUTEX_RECURSIVE_BIT ((uintptr_t)0x02) 69 #define MUTEX_DEFERRED_BIT ((uintptr_t)0x04) 70 #define MUTEX_THREAD ((uintptr_t)-16L) 71 72 #define MUTEX_HAS_WAITERS(x) ((uintptr_t)(x) & MUTEX_WAITERS_BIT) 73 #define MUTEX_RECURSIVE(x) ((uintptr_t)(x) & MUTEX_RECURSIVE_BIT) 74 #define MUTEX_OWNER(x) ((uintptr_t)(x) & MUTEX_THREAD) 75 76 #if __GNUC_PREREQ__(3, 0) 77 #define NOINLINE __attribute ((noinline)) 78 #else 79 #define NOINLINE /* nothing */ 80 #endif 81 82 static void pthread__mutex_wakeup(pthread_t, pthread_mutex_t *); 83 static int pthread__mutex_lock_slow(pthread_mutex_t *); 84 static int pthread__mutex_unlock_slow(pthread_mutex_t *); 85 static void pthread__mutex_pause(void); 86 87 int _pthread_mutex_held_np(pthread_mutex_t *); 88 pthread_t _pthread_mutex_owner_np(pthread_mutex_t *); 89 90 __weak_alias(pthread_mutex_held_np,_pthread_mutex_held_np) 91 __weak_alias(pthread_mutex_owner_np,_pthread_mutex_owner_np) 92 93 __strong_alias(__libc_mutex_init,pthread_mutex_init) 94 __strong_alias(__libc_mutex_lock,pthread_mutex_lock) 95 __strong_alias(__libc_mutex_trylock,pthread_mutex_trylock) 96 __strong_alias(__libc_mutex_unlock,pthread_mutex_unlock) 97 __strong_alias(__libc_mutex_destroy,pthread_mutex_destroy) 98 99 __strong_alias(__libc_mutexattr_init,pthread_mutexattr_init) 100 __strong_alias(__libc_mutexattr_destroy,pthread_mutexattr_destroy) 101 __strong_alias(__libc_mutexattr_settype,pthread_mutexattr_settype) 102 103 int 104 pthread_mutex_init(pthread_mutex_t *ptm, const pthread_mutexattr_t *attr) 105 { 106 intptr_t type; 107 108 if (__predict_false(__uselibcstub)) 109 return __libc_mutex_init_stub(ptm, attr); 110 111 if (attr == NULL) 112 type = PTHREAD_MUTEX_NORMAL; 113 else 114 type = (intptr_t)attr->ptma_private; 115 116 switch (type) { 117 case PTHREAD_MUTEX_ERRORCHECK: 118 __cpu_simple_lock_set(&ptm->ptm_errorcheck); 119 ptm->ptm_owner = NULL; 120 break; 121 case PTHREAD_MUTEX_RECURSIVE: 122 __cpu_simple_lock_clear(&ptm->ptm_errorcheck); 123 ptm->ptm_owner = (void *)MUTEX_RECURSIVE_BIT; 124 break; 125 default: 126 __cpu_simple_lock_clear(&ptm->ptm_errorcheck); 127 ptm->ptm_owner = NULL; 128 break; 129 } 130 131 ptm->ptm_magic = _PT_MUTEX_MAGIC; 132 ptm->ptm_waiters = NULL; 133 ptm->ptm_recursed = 0; 134 135 return 0; 136 } 137 138 int 139 pthread_mutex_destroy(pthread_mutex_t *ptm) 140 { 141 142 if (__predict_false(__uselibcstub)) 143 return __libc_mutex_destroy_stub(ptm); 144 145 pthread__error(EINVAL, "Invalid mutex", 146 ptm->ptm_magic == _PT_MUTEX_MAGIC); 147 pthread__error(EBUSY, "Destroying locked mutex", 148 MUTEX_OWNER(ptm->ptm_owner) == 0); 149 150 ptm->ptm_magic = _PT_MUTEX_DEAD; 151 return 0; 152 } 153 154 int 155 pthread_mutex_lock(pthread_mutex_t *ptm) 156 { 157 pthread_t self; 158 void *val; 159 160 if (__predict_false(__uselibcstub)) 161 return __libc_mutex_lock_stub(ptm); 162 163 self = pthread__self(); 164 val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self); 165 if (__predict_true(val == NULL)) { 166 #ifndef PTHREAD__ATOMIC_IS_MEMBAR 167 membar_enter(); 168 #endif 169 return 0; 170 } 171 return pthread__mutex_lock_slow(ptm); 172 } 173 174 /* We want function call overhead. */ 175 NOINLINE static void 176 pthread__mutex_pause(void) 177 { 178 179 pthread__smt_pause(); 180 } 181 182 /* 183 * Spin while the holder is running. 'lwpctl' gives us the true 184 * status of the thread. pt_blocking is set by libpthread in order 185 * to cut out system call and kernel spinlock overhead on remote CPUs 186 * (could represent many thousands of clock cycles). pt_blocking also 187 * makes this thread yield if the target is calling sched_yield(). 188 */ 189 NOINLINE static void * 190 pthread__mutex_spin(pthread_mutex_t *ptm, pthread_t owner) 191 { 192 pthread_t thread; 193 unsigned int count, i; 194 195 for (count = 2;; owner = ptm->ptm_owner) { 196 thread = (pthread_t)MUTEX_OWNER(owner); 197 if (thread == NULL) 198 break; 199 if (thread->pt_lwpctl->lc_curcpu == LWPCTL_CPU_NONE || 200 thread->pt_blocking) 201 break; 202 if (count < 128) 203 count += count; 204 for (i = count; i != 0; i--) 205 pthread__mutex_pause(); 206 } 207 208 return owner; 209 } 210 211 NOINLINE static void 212 pthread__mutex_setwaiters(pthread_t self, pthread_mutex_t *ptm) 213 { 214 void *new, *owner; 215 216 /* 217 * Note that the mutex can become unlocked before we set 218 * the waiters bit. If that happens it's not safe to sleep 219 * as we may never be awoken: we must remove the current 220 * thread from the waiters list and try again. 221 * 222 * Because we are doing this atomically, we can't remove 223 * one waiter: we must remove all waiters and awken them, 224 * then sleep in _lwp_park() until we have been awoken. 225 * 226 * Issue a memory barrier to ensure that we are reading 227 * the value of ptm_owner/pt_mutexwait after we have entered 228 * the waiters list (the CAS itself must be atomic). 229 */ 230 again: 231 membar_consumer(); 232 owner = ptm->ptm_owner; 233 234 if (MUTEX_OWNER(owner) == 0) { 235 pthread__mutex_wakeup(self, ptm); 236 return; 237 } 238 if (!MUTEX_HAS_WAITERS(owner)) { 239 new = (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT); 240 if (atomic_cas_ptr(&ptm->ptm_owner, owner, new) != owner) { 241 goto again; 242 } 243 } 244 245 /* 246 * Note that pthread_mutex_unlock() can do a non-interlocked CAS. 247 * We cannot know if the presence of the waiters bit is stable 248 * while the holding thread is running. There are many assumptions; 249 * see sys/kern/kern_mutex.c for details. In short, we must spin if 250 * we see that the holder is running again. 251 */ 252 membar_sync(); 253 pthread__mutex_spin(ptm, owner); 254 255 if (membar_consumer(), !MUTEX_HAS_WAITERS(ptm->ptm_owner)) { 256 goto again; 257 } 258 } 259 260 NOINLINE static int 261 pthread__mutex_lock_slow(pthread_mutex_t *ptm) 262 { 263 void *waiters, *new, *owner, *next; 264 pthread_t self; 265 int serrno; 266 267 pthread__error(EINVAL, "Invalid mutex", 268 ptm->ptm_magic == _PT_MUTEX_MAGIC); 269 270 owner = ptm->ptm_owner; 271 self = pthread__self(); 272 273 /* Recursive or errorcheck? */ 274 if (MUTEX_OWNER(owner) == (uintptr_t)self) { 275 if (MUTEX_RECURSIVE(owner)) { 276 if (ptm->ptm_recursed == INT_MAX) 277 return EAGAIN; 278 ptm->ptm_recursed++; 279 return 0; 280 } 281 if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) 282 return EDEADLK; 283 } 284 285 serrno = errno; 286 for (;; owner = ptm->ptm_owner) { 287 /* Spin while the owner is running. */ 288 owner = pthread__mutex_spin(ptm, owner); 289 290 /* If it has become free, try to acquire it again. */ 291 if (MUTEX_OWNER(owner) == 0) { 292 do { 293 new = (void *) 294 ((uintptr_t)self | (uintptr_t)owner); 295 next = atomic_cas_ptr(&ptm->ptm_owner, owner, 296 new); 297 if (next == owner) { 298 errno = serrno; 299 #ifndef PTHREAD__ATOMIC_IS_MEMBAR 300 membar_enter(); 301 #endif 302 return 0; 303 } 304 owner = next; 305 } while (MUTEX_OWNER(owner) == 0); 306 /* 307 * We have lost the race to acquire the mutex. 308 * The new owner could be running on another 309 * CPU, in which case we should spin and avoid 310 * the overhead of blocking. 311 */ 312 continue; 313 } 314 315 /* 316 * Nope, still held. Add thread to the list of waiters. 317 * Issue a memory barrier to ensure mutexwait/mutexnext 318 * are visible before we enter the waiters list. 319 */ 320 self->pt_mutexwait = 1; 321 for (waiters = ptm->ptm_waiters;; waiters = next) { 322 self->pt_mutexnext = waiters; 323 membar_producer(); 324 next = atomic_cas_ptr(&ptm->ptm_waiters, waiters, self); 325 if (next == waiters) 326 break; 327 } 328 329 /* Set the waiters bit and block. */ 330 pthread__mutex_setwaiters(self, ptm); 331 332 /* 333 * We may have been awoken by the current thread above, 334 * or will be awoken by the current holder of the mutex. 335 * The key requirement is that we must not proceed until 336 * told that we are no longer waiting (via pt_mutexwait 337 * being set to zero). Otherwise it is unsafe to re-enter 338 * the thread onto the waiters list. 339 */ 340 while (self->pt_mutexwait) { 341 self->pt_blocking++; 342 (void)_lwp_park(CLOCK_REALTIME, TIMER_ABSTIME, NULL, 343 self->pt_unpark, __UNVOLATILE(&ptm->ptm_waiters), 344 __UNVOLATILE(&ptm->ptm_waiters)); 345 self->pt_unpark = 0; 346 self->pt_blocking--; 347 membar_sync(); 348 } 349 } 350 } 351 352 int 353 pthread_mutex_trylock(pthread_mutex_t *ptm) 354 { 355 pthread_t self; 356 void *val, *new, *next; 357 358 if (__predict_false(__uselibcstub)) 359 return __libc_mutex_trylock_stub(ptm); 360 361 self = pthread__self(); 362 val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self); 363 if (__predict_true(val == NULL)) { 364 #ifndef PTHREAD__ATOMIC_IS_MEMBAR 365 membar_enter(); 366 #endif 367 return 0; 368 } 369 370 if (MUTEX_RECURSIVE(val)) { 371 if (MUTEX_OWNER(val) == 0) { 372 new = (void *)((uintptr_t)self | (uintptr_t)val); 373 next = atomic_cas_ptr(&ptm->ptm_owner, val, new); 374 if (__predict_true(next == val)) { 375 #ifndef PTHREAD__ATOMIC_IS_MEMBAR 376 membar_enter(); 377 #endif 378 return 0; 379 } 380 } 381 if (MUTEX_OWNER(val) == (uintptr_t)self) { 382 if (ptm->ptm_recursed == INT_MAX) 383 return EAGAIN; 384 ptm->ptm_recursed++; 385 return 0; 386 } 387 } 388 389 return EBUSY; 390 } 391 392 int 393 pthread_mutex_unlock(pthread_mutex_t *ptm) 394 { 395 pthread_t self; 396 void *value; 397 398 if (__predict_false(__uselibcstub)) 399 return __libc_mutex_unlock_stub(ptm); 400 401 /* 402 * Note this may be a non-interlocked CAS. See lock_slow() 403 * above and sys/kern/kern_mutex.c for details. 404 */ 405 #ifndef PTHREAD__ATOMIC_IS_MEMBAR 406 membar_exit(); 407 #endif 408 self = pthread__self(); 409 value = atomic_cas_ptr_ni(&ptm->ptm_owner, self, NULL); 410 if (__predict_true(value == self)) { 411 pthread__smt_wake(); 412 return 0; 413 } 414 return pthread__mutex_unlock_slow(ptm); 415 } 416 417 NOINLINE static int 418 pthread__mutex_unlock_slow(pthread_mutex_t *ptm) 419 { 420 pthread_t self, owner, new; 421 int weown, error, deferred; 422 423 pthread__error(EINVAL, "Invalid mutex", 424 ptm->ptm_magic == _PT_MUTEX_MAGIC); 425 426 self = pthread__self(); 427 owner = ptm->ptm_owner; 428 weown = (MUTEX_OWNER(owner) == (uintptr_t)self); 429 deferred = (int)((uintptr_t)owner & MUTEX_DEFERRED_BIT); 430 error = 0; 431 432 if (__SIMPLELOCK_LOCKED_P(&ptm->ptm_errorcheck)) { 433 if (!weown) { 434 error = EPERM; 435 new = owner; 436 } else { 437 new = NULL; 438 } 439 } else if (MUTEX_RECURSIVE(owner)) { 440 if (!weown) { 441 error = EPERM; 442 new = owner; 443 } else if (ptm->ptm_recursed) { 444 ptm->ptm_recursed--; 445 new = owner; 446 } else { 447 new = (pthread_t)MUTEX_RECURSIVE_BIT; 448 } 449 } else { 450 pthread__error(EPERM, 451 "Unlocking unlocked mutex", (owner != NULL)); 452 pthread__error(EPERM, 453 "Unlocking mutex owned by another thread", weown); 454 new = NULL; 455 } 456 457 /* 458 * Release the mutex. If there appear to be waiters, then 459 * wake them up. 460 */ 461 if (new != owner) { 462 owner = atomic_swap_ptr(&ptm->ptm_owner, new); 463 if (MUTEX_HAS_WAITERS(owner) != 0) { 464 pthread__mutex_wakeup(self, ptm); 465 return 0; 466 } 467 } 468 469 /* 470 * There were no waiters, but we may have deferred waking 471 * other threads until mutex unlock - we must wake them now. 472 */ 473 if (!deferred) 474 return error; 475 476 if (self->pt_nwaiters == 1) { 477 /* 478 * If the calling thread is about to block, defer 479 * unparking the target until _lwp_park() is called. 480 */ 481 if (self->pt_willpark && self->pt_unpark == 0) { 482 self->pt_unpark = self->pt_waiters[0]; 483 } else { 484 (void)_lwp_unpark(self->pt_waiters[0], 485 __UNVOLATILE(&ptm->ptm_waiters)); 486 } 487 } else { 488 (void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters, 489 __UNVOLATILE(&ptm->ptm_waiters)); 490 } 491 self->pt_nwaiters = 0; 492 493 return error; 494 } 495 496 /* 497 * pthread__mutex_wakeup: unpark threads waiting for us 498 * 499 * unpark threads on the ptm->ptm_waiters list and self->pt_waiters. 500 */ 501 502 static void 503 pthread__mutex_wakeup(pthread_t self, pthread_mutex_t *ptm) 504 { 505 pthread_t thread, next; 506 ssize_t n, rv; 507 508 /* 509 * Take ownership of the current set of waiters. No 510 * need for a memory barrier following this, all loads 511 * are dependent upon 'thread'. 512 */ 513 thread = atomic_swap_ptr(&ptm->ptm_waiters, NULL); 514 pthread__smt_wake(); 515 516 for (;;) { 517 /* 518 * Pull waiters from the queue and add to our list. 519 * Use a memory barrier to ensure that we safely 520 * read the value of pt_mutexnext before 'thread' 521 * sees pt_mutexwait being cleared. 522 */ 523 for (n = self->pt_nwaiters, self->pt_nwaiters = 0; 524 n < pthread__unpark_max && thread != NULL; 525 thread = next) { 526 next = thread->pt_mutexnext; 527 if (thread != self) { 528 self->pt_waiters[n++] = thread->pt_lid; 529 membar_sync(); 530 } 531 thread->pt_mutexwait = 0; 532 /* No longer safe to touch 'thread' */ 533 } 534 535 switch (n) { 536 case 0: 537 return; 538 case 1: 539 /* 540 * If the calling thread is about to block, 541 * defer unparking the target until _lwp_park() 542 * is called. 543 */ 544 if (self->pt_willpark && self->pt_unpark == 0) { 545 self->pt_unpark = self->pt_waiters[0]; 546 return; 547 } 548 rv = (ssize_t)_lwp_unpark(self->pt_waiters[0], 549 __UNVOLATILE(&ptm->ptm_waiters)); 550 if (rv != 0 && errno != EALREADY && errno != EINTR && 551 errno != ESRCH) { 552 pthread__errorfunc(__FILE__, __LINE__, 553 __func__, "_lwp_unpark failed"); 554 } 555 return; 556 default: 557 rv = _lwp_unpark_all(self->pt_waiters, (size_t)n, 558 __UNVOLATILE(&ptm->ptm_waiters)); 559 if (rv != 0 && errno != EINTR) { 560 pthread__errorfunc(__FILE__, __LINE__, 561 __func__, "_lwp_unpark_all failed"); 562 } 563 break; 564 } 565 } 566 } 567 568 int 569 pthread_mutexattr_init(pthread_mutexattr_t *attr) 570 { 571 if (__predict_false(__uselibcstub)) 572 return __libc_mutexattr_init_stub(attr); 573 574 attr->ptma_magic = _PT_MUTEXATTR_MAGIC; 575 attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT; 576 return 0; 577 } 578 579 int 580 pthread_mutexattr_destroy(pthread_mutexattr_t *attr) 581 { 582 if (__predict_false(__uselibcstub)) 583 return __libc_mutexattr_destroy_stub(attr); 584 585 pthread__error(EINVAL, "Invalid mutex attribute", 586 attr->ptma_magic == _PT_MUTEXATTR_MAGIC); 587 588 return 0; 589 } 590 591 int 592 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep) 593 { 594 pthread__error(EINVAL, "Invalid mutex attribute", 595 attr->ptma_magic == _PT_MUTEXATTR_MAGIC); 596 597 *typep = (int)(intptr_t)attr->ptma_private; 598 return 0; 599 } 600 601 int 602 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) 603 { 604 if (__predict_false(__uselibcstub)) 605 return __libc_mutexattr_settype_stub(attr, type); 606 607 pthread__error(EINVAL, "Invalid mutex attribute", 608 attr->ptma_magic == _PT_MUTEXATTR_MAGIC); 609 610 switch (type) { 611 case PTHREAD_MUTEX_NORMAL: 612 case PTHREAD_MUTEX_ERRORCHECK: 613 case PTHREAD_MUTEX_RECURSIVE: 614 attr->ptma_private = (void *)(intptr_t)type; 615 return 0; 616 default: 617 return EINVAL; 618 } 619 } 620 621 /* 622 * pthread__mutex_deferwake: try to defer unparking threads in self->pt_waiters 623 * 624 * In order to avoid unnecessary contention on the interlocking mutex, 625 * we defer waking up threads until we unlock the mutex. The threads will 626 * be woken up when the calling thread (self) releases the first mutex with 627 * MUTEX_DEFERRED_BIT set. It likely be the mutex 'ptm', but no problem 628 * even if it isn't. 629 */ 630 631 void 632 pthread__mutex_deferwake(pthread_t self, pthread_mutex_t *ptm) 633 { 634 635 if (__predict_false(ptm == NULL || 636 MUTEX_OWNER(ptm->ptm_owner) != (uintptr_t)self)) { 637 (void)_lwp_unpark_all(self->pt_waiters, self->pt_nwaiters, 638 __UNVOLATILE(&ptm->ptm_waiters)); 639 self->pt_nwaiters = 0; 640 } else { 641 atomic_or_ulong((volatile unsigned long *) 642 (uintptr_t)&ptm->ptm_owner, 643 (unsigned long)MUTEX_DEFERRED_BIT); 644 } 645 } 646 647 int 648 _pthread_mutex_held_np(pthread_mutex_t *ptm) 649 { 650 651 return MUTEX_OWNER(ptm->ptm_owner) == (uintptr_t)pthread__self(); 652 } 653 654 pthread_t 655 _pthread_mutex_owner_np(pthread_mutex_t *ptm) 656 { 657 658 return (pthread_t)MUTEX_OWNER(ptm->ptm_owner); 659 } 660