pthread_mutex_setprioceiling.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <stdbool.h>
  18. #include <errno.h>
  19. #include <pthreadP.h>
  20. int
  21. pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling)
  22. pthread_mutex_t *mutex;
  23. int prioceiling;
  24. int *old_ceiling;
  25. {
  26. /* The low bits of __kind aren't ever changed after pthread_mutex_init,
  27. so we don't need a lock yet. */
  28. if ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
  29. return EINVAL;
  30. if (__sched_fifo_min_prio == -1)
  31. __init_sched_fifo_prio ();
  32. if (__builtin_expect (prioceiling < __sched_fifo_min_prio, 0)
  33. || __builtin_expect (prioceiling > __sched_fifo_max_prio, 0)
  34. || __builtin_expect ((prioceiling
  35. & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
  36. >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
  37. != prioceiling, 0))
  38. return EINVAL;
  39. /* Check whether we already hold the mutex. */
  40. bool locked = false;
  41. int kind = PTHREAD_MUTEX_TYPE (mutex);
  42. if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
  43. {
  44. if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
  45. return EDEADLK;
  46. if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
  47. locked = true;
  48. }
  49. int oldval = mutex->__data.__lock;
  50. if (! locked)
  51. do
  52. {
  53. /* Need to lock the mutex, but without obeying the priority
  54. protect protocol. */
  55. int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
  56. oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  57. ceilval | 1, ceilval);
  58. if (oldval == ceilval)
  59. break;
  60. do
  61. {
  62. oldval
  63. = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  64. ceilval | 2,
  65. ceilval | 1);
  66. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  67. break;
  68. if (oldval != ceilval)
  69. lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
  70. PTHREAD_MUTEX_PSHARED (mutex));
  71. }
  72. while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
  73. ceilval | 2, ceilval)
  74. != ceilval);
  75. if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
  76. continue;
  77. }
  78. while (0);
  79. int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
  80. >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
  81. if (locked)
  82. {
  83. int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
  84. if (ret)
  85. return ret;
  86. }
  87. if (old_ceiling != NULL)
  88. *old_ceiling = oldprio;
  89. int newlock = 0;
  90. if (locked)
  91. newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
  92. mutex->__data.__lock = newlock
  93. | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
  94. atomic_full_barrier ();
  95. lll_futex_wake (&mutex->__data.__lock, INT_MAX,
  96. PTHREAD_MUTEX_PSHARED (mutex));
  97. return 0;
  98. }