tpp.c 4.4 KB

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