1 /* 2 * Copyright (c) 1995 John Birrell <jb@cimlogic.com.au>. 3 * Copyright (c) 2006 David Xu <davidxu@freebsd.org>. 4 * 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 John Birrell. 17 * 4. Neither the name of the author nor the names of any co-contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * $FreeBSD$ 34 */ 35 36 #include "namespace.h" 37 #include <stdlib.h> 38 #include <errno.h> 39 #include <string.h> 40 #include <sys/param.h> 41 #include <sys/queue.h> 42 #include <pthread.h> 43 #include "un-namespace.h" 44 45 #include "thr_private.h" 46 47 #if defined(_PTHREADS_INVARIANTS) 48 #define MUTEX_INIT_LINK(m) do { \ 49 (m)->m_qe.tqe_prev = NULL; \ 50 (m)->m_qe.tqe_next = NULL; \ 51 } while (0) 52 #define MUTEX_ASSERT_IS_OWNED(m) do { \ 53 if ((m)->m_qe.tqe_prev == NULL) \ 54 PANIC("mutex is not on list"); \ 55 } while (0) 56 #define MUTEX_ASSERT_NOT_OWNED(m) do { \ 57 if (((m)->m_qe.tqe_prev != NULL) || \ 58 ((m)->m_qe.tqe_next != NULL)) \ 59 PANIC("mutex is on list"); \ 60 } while (0) 61 #else 62 #define MUTEX_INIT_LINK(m) 63 #define MUTEX_ASSERT_IS_OWNED(m) 64 #define MUTEX_ASSERT_NOT_OWNED(m) 65 #endif 66 67 /* 68 * For adaptive mutexes, how many times to spin doing trylock2 69 * before entering the kernel to block 70 */ 71 #define MUTEX_ADAPTIVE_SPINS 200 72 73 /* 74 * Prototypes 75 */ 76 int __pthread_mutex_init(pthread_mutex_t *mutex, 77 const pthread_mutexattr_t *mutex_attr); 78 int __pthread_mutex_trylock(pthread_mutex_t *mutex); 79 int __pthread_mutex_lock(pthread_mutex_t *mutex); 80 int __pthread_mutex_timedlock(pthread_mutex_t *mutex, 81 const struct timespec *abstime); 82 83 static int mutex_self_trylock(pthread_mutex_t); 84 static int mutex_self_lock(pthread_mutex_t, 85 const struct timespec *abstime); 86 static int mutex_unlock_common(pthread_mutex_t *); 87 88 __weak_reference(__pthread_mutex_init, pthread_mutex_init); 89 __weak_reference(__pthread_mutex_lock, pthread_mutex_lock); 90 __weak_reference(__pthread_mutex_timedlock, pthread_mutex_timedlock); 91 __weak_reference(__pthread_mutex_trylock, pthread_mutex_trylock); 92 93 /* Single underscore versions provided for libc internal usage: */ 94 /* No difference between libc and application usage of these: */ 95 __weak_reference(_pthread_mutex_destroy, pthread_mutex_destroy); 96 __weak_reference(_pthread_mutex_unlock, pthread_mutex_unlock); 97 98 __weak_reference(_pthread_mutex_getprioceiling, pthread_mutex_getprioceiling); 99 __weak_reference(_pthread_mutex_setprioceiling, pthread_mutex_setprioceiling); 100 101 static int 102 mutex_init(pthread_mutex_t *mutex, 103 const pthread_mutexattr_t *mutex_attr, int private) 104 { 105 const struct pthread_mutex_attr *attr; 106 struct pthread_mutex *pmutex; 107 108 if (mutex_attr == NULL) { 109 attr = &_pthread_mutexattr_default; 110 } else { 111 attr = *mutex_attr; 112 if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK || 113 attr->m_type >= PTHREAD_MUTEX_TYPE_MAX) 114 return (EINVAL); 115 if (attr->m_protocol < PTHREAD_PRIO_NONE || 116 attr->m_protocol > PTHREAD_PRIO_PROTECT) 117 return (EINVAL); 118 } 119 if ((pmutex = (pthread_mutex_t) 120 calloc(1, sizeof(struct pthread_mutex))) == NULL) 121 return (ENOMEM); 122 123 pmutex->m_type = attr->m_type; 124 pmutex->m_owner = NULL; 125 pmutex->m_flags = attr->m_flags | MUTEX_FLAGS_INITED; 126 if (private) 127 pmutex->m_flags |= MUTEX_FLAGS_PRIVATE; 128 pmutex->m_count = 0; 129 pmutex->m_refcount = 0; 130 MUTEX_INIT_LINK(pmutex); 131 switch(attr->m_protocol) { 132 case PTHREAD_PRIO_INHERIT: 133 pmutex->m_lock.m_owner = UMUTEX_UNOWNED; 134 pmutex->m_lock.m_flags = UMUTEX_PRIO_INHERIT; 135 break; 136 case PTHREAD_PRIO_PROTECT: 137 pmutex->m_lock.m_owner = UMUTEX_CONTESTED; 138 pmutex->m_lock.m_flags = UMUTEX_PRIO_PROTECT; 139 pmutex->m_lock.m_ceilings[0] = attr->m_ceiling; 140 break; 141 case PTHREAD_PRIO_NONE: 142 pmutex->m_lock.m_owner = UMUTEX_UNOWNED; 143 pmutex->m_lock.m_flags = 0; 144 } 145 *mutex = pmutex; 146 return (0); 147 } 148 149 static int 150 init_static(struct pthread *thread, pthread_mutex_t *mutex) 151 { 152 int ret; 153 154 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); 155 156 if (*mutex == NULL) 157 ret = mutex_init(mutex, NULL, 0); 158 else 159 ret = 0; 160 161 THR_LOCK_RELEASE(thread, &_mutex_static_lock); 162 163 return (ret); 164 } 165 166 static int 167 init_static_private(struct pthread *thread, pthread_mutex_t *mutex) 168 { 169 int ret; 170 171 THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); 172 173 if (*mutex == NULL) 174 ret = mutex_init(mutex, NULL, 1); 175 else 176 ret = 0; 177 178 THR_LOCK_RELEASE(thread, &_mutex_static_lock); 179 180 return (ret); 181 } 182 183 static void 184 set_inherited_priority(struct pthread *curthread, struct pthread_mutex *m) 185 { 186 struct pthread_mutex *m2; 187 188 m2 = TAILQ_LAST(&curthread->pp_mutexq, mutex_queue); 189 if (m2 != NULL) 190 m->m_lock.m_ceilings[1] = m2->m_lock.m_ceilings[0]; 191 else 192 m->m_lock.m_ceilings[1] = -1; 193 } 194 195 int 196 _pthread_mutex_init(pthread_mutex_t *mutex, 197 const pthread_mutexattr_t *mutex_attr) 198 { 199 return mutex_init(mutex, mutex_attr, 1); 200 } 201 202 int 203 __pthread_mutex_init(pthread_mutex_t *mutex, 204 const pthread_mutexattr_t *mutex_attr) 205 { 206 return mutex_init(mutex, mutex_attr, 0); 207 } 208 209 void 210 _mutex_fork(struct pthread *curthread) 211 { 212 struct pthread_mutex *m; 213 214 /* 215 * Fix mutex ownership for child process. 216 * note that process shared mutex should not 217 * be inherited because owner is forking thread 218 * which is in parent process, they should be 219 * removed from the owned mutex list, current, 220 * process shared mutex is not supported, so I 221 * am not worried. 222 */ 223 224 TAILQ_FOREACH(m, &curthread->mutexq, m_qe) 225 m->m_lock.m_owner = TID(curthread); 226 TAILQ_FOREACH(m, &curthread->pp_mutexq, m_qe) 227 m->m_lock.m_owner = TID(curthread) | UMUTEX_CONTESTED; 228 } 229 230 int 231 _pthread_mutex_destroy(pthread_mutex_t *mutex) 232 { 233 struct pthread *curthread = _get_curthread(); 234 pthread_mutex_t m; 235 uint32_t id; 236 int ret = 0; 237 238 if (__predict_false(*mutex == NULL)) 239 ret = EINVAL; 240 else { 241 id = TID(curthread); 242 243 /* 244 * Try to lock the mutex structure, we only need to 245 * try once, if failed, the mutex is in used. 246 */ 247 ret = _thr_umutex_trylock(&(*mutex)->m_lock, id); 248 if (ret) 249 return (ret); 250 m = *mutex; 251 /* 252 * Check mutex other fields to see if this mutex is 253 * in use. Mostly for prority mutex types, or there 254 * are condition variables referencing it. 255 */ 256 if (m->m_owner != NULL || m->m_refcount != 0) { 257 if (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) 258 set_inherited_priority(curthread, m); 259 _thr_umutex_unlock(&m->m_lock, id); 260 ret = EBUSY; 261 } else { 262 /* 263 * Save a pointer to the mutex so it can be free'd 264 * and set the caller's pointer to NULL. 265 */ 266 *mutex = NULL; 267 268 if (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) 269 set_inherited_priority(curthread, m); 270 _thr_umutex_unlock(&m->m_lock, id); 271 272 MUTEX_ASSERT_NOT_OWNED(m); 273 free(m); 274 } 275 } 276 277 return (ret); 278 } 279 280 281 #define ENQUEUE_MUTEX(curthread, m) \ 282 m->m_owner = curthread; \ 283 /* Add to the list of owned mutexes: */ \ 284 MUTEX_ASSERT_NOT_OWNED(m); \ 285 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) \ 286 TAILQ_INSERT_TAIL(&curthread->mutexq, m, m_qe); \ 287 else \ 288 TAILQ_INSERT_TAIL(&curthread->pp_mutexq, m, m_qe) 289 290 static int 291 mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex) 292 { 293 struct pthread_mutex *m; 294 uint32_t id; 295 int ret; 296 297 id = TID(curthread); 298 m = *mutex; 299 ret = _thr_umutex_trylock(&m->m_lock, id); 300 if (ret == 0) { 301 ENQUEUE_MUTEX(curthread, m); 302 } else if (m->m_owner == curthread) { 303 ret = mutex_self_trylock(m); 304 } /* else {} */ 305 306 return (ret); 307 } 308 309 int 310 __pthread_mutex_trylock(pthread_mutex_t *mutex) 311 { 312 struct pthread *curthread = _get_curthread(); 313 int ret; 314 315 /* 316 * If the mutex is statically initialized, perform the dynamic 317 * initialization: 318 */ 319 if (__predict_false(*mutex == NULL)) { 320 ret = init_static(curthread, mutex); 321 if (__predict_false(ret)) 322 return (ret); 323 } 324 return (mutex_trylock_common(curthread, mutex)); 325 } 326 327 int 328 _pthread_mutex_trylock(pthread_mutex_t *mutex) 329 { 330 struct pthread *curthread = _get_curthread(); 331 int ret; 332 333 /* 334 * If the mutex is statically initialized, perform the dynamic 335 * initialization marking the mutex private (delete safe): 336 */ 337 if (__predict_false(*mutex == NULL)) { 338 ret = init_static_private(curthread, mutex); 339 if (__predict_false(ret)) 340 return (ret); 341 } 342 return (mutex_trylock_common(curthread, mutex)); 343 } 344 345 static int 346 mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex, 347 const struct timespec * abstime) 348 { 349 struct timespec ts, ts2; 350 struct pthread_mutex *m; 351 uint32_t id; 352 int ret; 353 int count; 354 355 id = TID(curthread); 356 m = *mutex; 357 ret = _thr_umutex_trylock2(&m->m_lock, id); 358 if (ret == 0) { 359 ENQUEUE_MUTEX(curthread, m); 360 } else if (m->m_owner == curthread) { 361 ret = mutex_self_lock(m, abstime); 362 } else { 363 /* 364 * For adaptive mutexes, spin for a bit in the expectation 365 * that if the application requests this mutex type then 366 * the lock is likely to be released quickly and it is 367 * faster than entering the kernel 368 */ 369 if (!_thr_is_smp) 370 goto yield_loop; 371 372 if (m->m_type == PTHREAD_MUTEX_ADAPTIVE_NP) { 373 count = MUTEX_ADAPTIVE_SPINS; 374 375 while (count--) { 376 ret = _thr_umutex_trylock2(&m->m_lock, id); 377 if (ret == 0) 378 break; 379 CPU_SPINWAIT; 380 } 381 if (ret == 0) 382 goto done; 383 } else { 384 if (_thr_spinloops != 0 && 385 !(m->m_lock.m_flags & UMUTEX_PRIO_PROTECT)) { 386 count = _thr_spinloops; 387 while (count) { 388 if (m->m_lock.m_owner == UMUTEX_UNOWNED) { 389 ret = _thr_umutex_trylock2(&m->m_lock, id); 390 if (ret == 0) 391 goto done; 392 } 393 CPU_SPINWAIT; 394 count--; 395 } 396 } 397 } 398 399 yield_loop: 400 if (_thr_yieldloops != 0) { 401 count = _thr_yieldloops; 402 while (count--) { 403 _sched_yield(); 404 ret = _thr_umutex_trylock2(&m->m_lock, id); 405 if (ret == 0) 406 goto done; 407 } 408 } 409 410 if (abstime == NULL) { 411 ret = __thr_umutex_lock(&m->m_lock); 412 } else if (__predict_false( 413 abstime->tv_sec < 0 || abstime->tv_nsec < 0 || 414 abstime->tv_nsec >= 1000000000)) { 415 ret = EINVAL; 416 } else { 417 clock_gettime(CLOCK_REALTIME, &ts); 418 TIMESPEC_SUB(&ts2, abstime, &ts); 419 ret = __thr_umutex_timedlock(&m->m_lock, &ts2); 420 /* 421 * Timed out wait is not restarted if 422 * it was interrupted, not worth to do it. 423 */ 424 if (ret == EINTR) 425 ret = ETIMEDOUT; 426 } 427 done: 428 if (ret == 0) 429 ENQUEUE_MUTEX(curthread, m); 430 } 431 return (ret); 432 } 433 434 int 435 __pthread_mutex_lock(pthread_mutex_t *m) 436 { 437 struct pthread *curthread; 438 int ret; 439 440 _thr_check_init(); 441 442 curthread = _get_curthread(); 443 444 /* 445 * If the mutex is statically initialized, perform the dynamic 446 * initialization: 447 */ 448 if (__predict_false(*m == NULL)) { 449 ret = init_static(curthread, m); 450 if (__predict_false(ret)) 451 return (ret); 452 } 453 return (mutex_lock_common(curthread, m, NULL)); 454 } 455 456 int 457 _pthread_mutex_lock(pthread_mutex_t *m) 458 { 459 struct pthread *curthread; 460 int ret; 461 462 _thr_check_init(); 463 464 curthread = _get_curthread(); 465 466 /* 467 * If the mutex is statically initialized, perform the dynamic 468 * initialization marking it private (delete safe): 469 */ 470 if (__predict_false(*m == NULL)) { 471 ret = init_static_private(curthread, m); 472 if (__predict_false(ret)) 473 return (ret); 474 } 475 return (mutex_lock_common(curthread, m, NULL)); 476 } 477 478 int 479 __pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime) 480 { 481 struct pthread *curthread; 482 int ret; 483 484 _thr_check_init(); 485 486 curthread = _get_curthread(); 487 488 /* 489 * If the mutex is statically initialized, perform the dynamic 490 * initialization: 491 */ 492 if (__predict_false(*m == NULL)) { 493 ret = init_static(curthread, m); 494 if (__predict_false(ret)) 495 return (ret); 496 } 497 return (mutex_lock_common(curthread, m, abstime)); 498 } 499 500 int 501 _pthread_mutex_timedlock(pthread_mutex_t *m, const struct timespec *abstime) 502 { 503 struct pthread *curthread; 504 int ret; 505 506 _thr_check_init(); 507 508 curthread = _get_curthread(); 509 510 /* 511 * If the mutex is statically initialized, perform the dynamic 512 * initialization marking it private (delete safe): 513 */ 514 if (__predict_false(*m == NULL)) { 515 ret = init_static_private(curthread, m); 516 if (__predict_false(ret)) 517 return (ret); 518 } 519 return (mutex_lock_common(curthread, m, abstime)); 520 } 521 522 int 523 _pthread_mutex_unlock(pthread_mutex_t *m) 524 { 525 return (mutex_unlock_common(m)); 526 } 527 528 int 529 _mutex_cv_lock(pthread_mutex_t *m, int count) 530 { 531 int ret; 532 533 ret = mutex_lock_common(_get_curthread(), m, NULL); 534 if (ret == 0) { 535 (*m)->m_refcount--; 536 (*m)->m_count += count; 537 } 538 return (ret); 539 } 540 541 static int 542 mutex_self_trylock(pthread_mutex_t m) 543 { 544 int ret; 545 546 switch (m->m_type) { 547 case PTHREAD_MUTEX_ERRORCHECK: 548 case PTHREAD_MUTEX_NORMAL: 549 ret = EBUSY; 550 break; 551 552 case PTHREAD_MUTEX_RECURSIVE: 553 /* Increment the lock count: */ 554 if (m->m_count + 1 > 0) { 555 m->m_count++; 556 ret = 0; 557 } else 558 ret = EAGAIN; 559 break; 560 561 default: 562 /* Trap invalid mutex types; */ 563 ret = EINVAL; 564 } 565 566 return (ret); 567 } 568 569 static int 570 mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime) 571 { 572 struct timespec ts1, ts2; 573 int ret; 574 575 switch (m->m_type) { 576 case PTHREAD_MUTEX_ERRORCHECK: 577 case PTHREAD_MUTEX_ADAPTIVE_NP: 578 if (abstime) { 579 clock_gettime(CLOCK_REALTIME, &ts1); 580 TIMESPEC_SUB(&ts2, abstime, &ts1); 581 __sys_nanosleep(&ts2, NULL); 582 ret = ETIMEDOUT; 583 } else { 584 /* 585 * POSIX specifies that mutexes should return 586 * EDEADLK if a recursive lock is detected. 587 */ 588 ret = EDEADLK; 589 } 590 break; 591 592 case PTHREAD_MUTEX_NORMAL: 593 /* 594 * What SS2 define as a 'normal' mutex. Intentionally 595 * deadlock on attempts to get a lock you already own. 596 */ 597 ret = 0; 598 if (abstime) { 599 clock_gettime(CLOCK_REALTIME, &ts1); 600 TIMESPEC_SUB(&ts2, abstime, &ts1); 601 __sys_nanosleep(&ts2, NULL); 602 ret = ETIMEDOUT; 603 } else { 604 ts1.tv_sec = 30; 605 ts1.tv_nsec = 0; 606 for (;;) 607 __sys_nanosleep(&ts1, NULL); 608 } 609 break; 610 611 case PTHREAD_MUTEX_RECURSIVE: 612 /* Increment the lock count: */ 613 if (m->m_count + 1 > 0) { 614 m->m_count++; 615 ret = 0; 616 } else 617 ret = EAGAIN; 618 break; 619 620 default: 621 /* Trap invalid mutex types; */ 622 ret = EINVAL; 623 } 624 625 return (ret); 626 } 627 628 static int 629 mutex_unlock_common(pthread_mutex_t *mutex) 630 { 631 struct pthread *curthread = _get_curthread(); 632 struct pthread_mutex *m; 633 uint32_t id; 634 635 if (__predict_false((m = *mutex) == NULL)) 636 return (EINVAL); 637 638 /* 639 * Check if the running thread is not the owner of the mutex. 640 */ 641 if (__predict_false(m->m_owner != curthread)) 642 return (EPERM); 643 644 id = TID(curthread); 645 if (__predict_false( 646 m->m_type == PTHREAD_MUTEX_RECURSIVE && 647 m->m_count > 0)) { 648 m->m_count--; 649 } else { 650 m->m_owner = NULL; 651 /* Remove the mutex from the threads queue. */ 652 MUTEX_ASSERT_IS_OWNED(m); 653 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) 654 TAILQ_REMOVE(&curthread->mutexq, m, m_qe); 655 else { 656 TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe); 657 set_inherited_priority(curthread, m); 658 } 659 MUTEX_INIT_LINK(m); 660 _thr_umutex_unlock(&m->m_lock, id); 661 } 662 return (0); 663 } 664 665 int 666 _mutex_cv_unlock(pthread_mutex_t *mutex, int *count) 667 { 668 struct pthread *curthread = _get_curthread(); 669 struct pthread_mutex *m; 670 671 if (__predict_false((m = *mutex) == NULL)) 672 return (EINVAL); 673 674 /* 675 * Check if the running thread is not the owner of the mutex. 676 */ 677 if (__predict_false(m->m_owner != curthread)) 678 return (EPERM); 679 680 /* 681 * Clear the count in case this is a recursive mutex. 682 */ 683 *count = m->m_count; 684 m->m_refcount++; 685 m->m_count = 0; 686 m->m_owner = NULL; 687 /* Remove the mutex from the threads queue. */ 688 MUTEX_ASSERT_IS_OWNED(m); 689 if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) 690 TAILQ_REMOVE(&curthread->mutexq, m, m_qe); 691 else { 692 TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe); 693 set_inherited_priority(curthread, m); 694 } 695 MUTEX_INIT_LINK(m); 696 _thr_umutex_unlock(&m->m_lock, TID(curthread)); 697 return (0); 698 } 699 700 void 701 _mutex_unlock_private(pthread_t pthread) 702 { 703 struct pthread_mutex *m, *m_next; 704 705 TAILQ_FOREACH_SAFE(m, &pthread->mutexq, m_qe, m_next) { 706 if ((m->m_flags & MUTEX_FLAGS_PRIVATE) != 0) 707 _pthread_mutex_unlock(&m); 708 } 709 } 710 711 int 712 _pthread_mutex_getprioceiling(pthread_mutex_t *mutex, 713 int *prioceiling) 714 { 715 int ret; 716 717 if (*mutex == NULL) 718 ret = EINVAL; 719 else if (((*mutex)->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) 720 ret = EINVAL; 721 else { 722 *prioceiling = (*mutex)->m_lock.m_ceilings[0]; 723 ret = 0; 724 } 725 726 return(ret); 727 } 728 729 int 730 _pthread_mutex_setprioceiling(pthread_mutex_t *mutex, 731 int ceiling, int *old_ceiling) 732 { 733 struct pthread *curthread = _get_curthread(); 734 struct pthread_mutex *m, *m1, *m2; 735 int ret; 736 737 m = *mutex; 738 if (m == NULL || (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) 739 return (EINVAL); 740 741 ret = __thr_umutex_set_ceiling(&m->m_lock, ceiling, old_ceiling); 742 if (ret != 0) 743 return (ret); 744 745 if (m->m_owner == curthread) { 746 MUTEX_ASSERT_IS_OWNED(m); 747 m1 = TAILQ_PREV(m, mutex_queue, m_qe); 748 m2 = TAILQ_NEXT(m, m_qe); 749 if ((m1 != NULL && m1->m_lock.m_ceilings[0] > ceiling) || 750 (m2 != NULL && m2->m_lock.m_ceilings[0] < ceiling)) { 751 TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe); 752 TAILQ_FOREACH(m2, &curthread->pp_mutexq, m_qe) { 753 if (m2->m_lock.m_ceilings[0] > ceiling) { 754 TAILQ_INSERT_BEFORE(m2, m, m_qe); 755 return (0); 756 } 757 } 758 TAILQ_INSERT_TAIL(&curthread->pp_mutexq, m, m_qe); 759 } 760 } 761 return (0); 762 } 763