tpp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Thread Priority Protect helpers.
  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 <assert.h>
  17. #include <atomic.h>
  18. #include <errno.h>
  19. #include <pthreadP.h>
  20. #include <sched.h>
  21. #include <stdlib.h>
  22. #include "uClibc-glue.h"
  23. int __sched_fifo_min_prio = -1;
  24. int __sched_fifo_max_prio = -1;
  25. void
  26. __init_sched_fifo_prio (void)
  27. {
  28. __sched_fifo_max_prio = sched_get_priority_max (SCHED_FIFO);
  29. atomic_write_barrier ();
  30. __sched_fifo_min_prio = sched_get_priority_min (SCHED_FIFO);
  31. }
  32. int
  33. __pthread_tpp_change_priority (int previous_prio, int new_prio)
  34. {
  35. struct pthread *self = THREAD_SELF;
  36. struct priority_protection_data *tpp = THREAD_GETMEM (self, tpp);
  37. if (tpp == NULL)
  38. {
  39. if (__sched_fifo_min_prio == -1)
  40. __init_sched_fifo_prio ();
  41. size_t size = sizeof *tpp;
  42. size += (__sched_fifo_max_prio - __sched_fifo_min_prio + 1)
  43. * sizeof (tpp->priomap[0]);
  44. tpp = calloc (size, 1);
  45. if (tpp == NULL)
  46. return ENOMEM;
  47. tpp->priomax = __sched_fifo_min_prio - 1;
  48. THREAD_SETMEM (self, tpp, tpp);
  49. }
  50. assert (new_prio == -1
  51. || (new_prio >= __sched_fifo_min_prio
  52. && new_prio <= __sched_fifo_max_prio));
  53. assert (previous_prio == -1
  54. || (previous_prio >= __sched_fifo_min_prio
  55. && previous_prio <= __sched_fifo_max_prio));
  56. int priomax = tpp->priomax;
  57. int newpriomax = priomax;
  58. if (new_prio != -1)
  59. {
  60. if (tpp->priomap[new_prio - __sched_fifo_min_prio] + 1 == 0)
  61. return EAGAIN;
  62. ++tpp->priomap[new_prio - __sched_fifo_min_prio];
  63. if (new_prio > priomax)
  64. newpriomax = new_prio;
  65. }
  66. if (previous_prio != -1)
  67. {
  68. if (--tpp->priomap[previous_prio - __sched_fifo_min_prio] == 0
  69. && priomax == previous_prio
  70. && previous_prio > new_prio)
  71. {
  72. int i;
  73. for (i = previous_prio - 1; i >= __sched_fifo_min_prio; --i)
  74. if (tpp->priomap[i - __sched_fifo_min_prio])
  75. break;
  76. newpriomax = i;
  77. }
  78. }
  79. if (priomax == newpriomax)
  80. return 0;
  81. lll_lock (self->lock, LLL_PRIVATE);
  82. tpp->priomax = newpriomax;
  83. int result = 0;
  84. if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
  85. {
  86. if (__sched_getparam (self->tid, &self->schedparam) != 0)
  87. result = errno;
  88. else
  89. self->flags |= ATTR_FLAG_SCHED_SET;
  90. }
  91. if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
  92. {
  93. self->schedpolicy = __sched_getscheduler (self->tid);
  94. if (self->schedpolicy == -1)
  95. result = errno;
  96. else
  97. self->flags |= ATTR_FLAG_POLICY_SET;
  98. }
  99. if (result == 0)
  100. {
  101. struct sched_param sp = self->schedparam;
  102. if (sp.sched_priority < newpriomax || sp.sched_priority < priomax)
  103. {
  104. if (sp.sched_priority < newpriomax)
  105. sp.sched_priority = newpriomax;
  106. if (__sched_setscheduler (self->tid, self->schedpolicy, &sp) < 0)
  107. result = errno;
  108. }
  109. }
  110. lll_unlock (self->lock, LLL_PRIVATE);
  111. return result;
  112. }
  113. int
  114. __pthread_current_priority (void)
  115. {
  116. struct pthread *self = THREAD_SELF;
  117. if ((self->flags & (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
  118. == (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
  119. return self->schedparam.sched_priority;
  120. int result = 0;
  121. lll_lock (self->lock, LLL_PRIVATE);
  122. if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
  123. {
  124. if (__sched_getparam (self->tid, &self->schedparam) != 0)
  125. result = -1;
  126. else
  127. self->flags |= ATTR_FLAG_SCHED_SET;
  128. }
  129. if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
  130. {
  131. self->schedpolicy = __sched_getscheduler (self->tid);
  132. if (self->schedpolicy == -1)
  133. result = -1;
  134. else
  135. self->flags |= ATTR_FLAG_POLICY_SET;
  136. }
  137. if (result != -1)
  138. result = self->schedparam.sched_priority;
  139. lll_unlock (self->lock, LLL_PRIVATE);
  140. return result;
  141. }