pthread_mutex_setprioceiling.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Set current priority ceiling of pthread_mutex_t.
  2. Copyright (C) 2006, 2007 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <stdbool.h>
  17. #include <errno.h>
  18. #include <pthreadP.h>
  19. int
  20. pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling)
  21. pthread_mutex_t *mutex;
  22. int prioceiling;
  23. int *old_ceiling;
  24. {
  25. /* The low bits of __kind aren't ever changed after pthread_mutex_init,
  26. so we don't need a lock yet. */
  27. if ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
  28. return EINVAL;
  29. if (__sched_fifo_min_prio == -1)
  30. __init_sched_fifo_prio ();
  31. if (__builtin_expect (prioceiling < __sched_fifo_min_prio, 0)
  32. || __builtin_expect (prioceiling > __sched_fifo_max_prio, 0)
  33. || __builtin_expect ((prioceiling
  34. & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
  35. >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
  36. != prioceiling, 0))
  37. return EINVAL;
  38. /* Check whether we already hold the mutex. */
  39. bool locked = false;
  40. int kind = PTHREAD_MUTEX_TYPE (mutex);
  41. if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
  42. {
  43. if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
  44. return EDEADLK;
  45. if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
  46. locked = true;
  47. }
  48. int oldval = mutex->__data.__lock;
  49. if (! locked)
  50. do
  51. {
  52. /* Need to lock the mutex, but without obeying the priority
  53. protect protocol. */
  54. int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
  55. oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  56. ceilval | 1, ceilval);
  57. if (oldval == ceilval)
  58. break;
  59. do
  60. {
  61. oldval
  62. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  63. ceilval | 2,
  64. ceilval | 1);
  65. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  66. break;
  67. if (oldval != ceilval)
  68. lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
  69. PTHREAD_MUTEX_PSHARED (mutex));
  70. }
  71. while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  72. ceilval | 2, ceilval)
  73. != ceilval);
  74. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  75. continue;
  76. }
  77. while (0);
  78. int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  79. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  80. if (locked)
  81. {
  82. int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
  83. if (ret)
  84. return ret;
  85. }
  86. if (old_ceiling != NULL)
  87. *old_ceiling = oldprio;
  88. int newlock = 0;
  89. if (locked)
  90. newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
  91. mutex->__data.__lock = newlock
  92. | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
  93. atomic_full_barrier ();
  94. lll_futex_wake (&mutex->__data.__lock, INT_MAX,
  95. PTHREAD_MUTEX_PSHARED (mutex));
  96. return 0;
  97. }