stdlib: Undo post review change to 16adc58e73f3 [BZ #27749]
[glibc.git] / nptl / tpp.c
blob50c3fb27d0cf764c9a608a2a284d185722333cd4
1 /* Thread Priority Protect helpers.
2 Copyright (C) 2006-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <atomic.h>
21 #include <errno.h>
22 #include <pthreadP.h>
23 #include <sched.h>
24 #include <stdlib.h>
25 #include <atomic.h>
27 int __sched_fifo_min_prio = -1;
28 libc_hidden_data_def (__sched_fifo_min_prio)
29 int __sched_fifo_max_prio = -1;
30 libc_hidden_data_def (__sched_fifo_max_prio)
32 /* We only want to initialize __sched_fifo_min_prio and __sched_fifo_max_prio
33 once. The standard solution would be similar to pthread_once, but then
34 readers would need to use an acquire fence. In this specific case,
35 initialization is comprised of just idempotent writes to two variables
36 that have an initial value of -1. Therefore, we can treat each variable as
37 a separate, at-least-once initialized value. This enables using just
38 relaxed MO loads and stores, but requires that consumers check for
39 initialization of each value that is to be used; see
40 __pthread_tpp_change_priority for an example.
42 void
43 __init_sched_fifo_prio (void)
45 atomic_store_relaxed (&__sched_fifo_max_prio,
46 __sched_get_priority_max (SCHED_FIFO));
47 atomic_store_relaxed (&__sched_fifo_min_prio,
48 __sched_get_priority_min (SCHED_FIFO));
50 libc_hidden_def (__init_sched_fifo_prio)
52 int
53 __pthread_tpp_change_priority (int previous_prio, int new_prio)
55 struct pthread *self = THREAD_SELF;
56 struct priority_protection_data *tpp = THREAD_GETMEM (self, tpp);
57 int fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio);
58 int fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio);
60 if (tpp == NULL)
62 /* See __init_sched_fifo_prio. We need both the min and max prio,
63 so need to check both, and run initialization if either one is
64 not initialized. The memory model's write-read coherence rule
65 makes this work. */
66 if (fifo_min_prio == -1 || fifo_max_prio == -1)
68 __init_sched_fifo_prio ();
69 fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio);
70 fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio);
73 size_t size = sizeof *tpp;
74 size += (fifo_max_prio - fifo_min_prio + 1)
75 * sizeof (tpp->priomap[0]);
76 tpp = calloc (size, 1);
77 if (tpp == NULL)
78 return ENOMEM;
79 tpp->priomax = fifo_min_prio - 1;
80 THREAD_SETMEM (self, tpp, tpp);
83 assert (new_prio == -1
84 || (new_prio >= fifo_min_prio
85 && new_prio <= fifo_max_prio));
86 assert (previous_prio == -1
87 || (previous_prio >= fifo_min_prio
88 && previous_prio <= fifo_max_prio));
90 int priomax = tpp->priomax;
91 int newpriomax = priomax;
92 if (new_prio != -1)
94 if (tpp->priomap[new_prio - fifo_min_prio] + 1 == 0)
95 return EAGAIN;
96 ++tpp->priomap[new_prio - fifo_min_prio];
97 if (new_prio > priomax)
98 newpriomax = new_prio;
101 if (previous_prio != -1)
103 if (--tpp->priomap[previous_prio - fifo_min_prio] == 0
104 && priomax == previous_prio
105 && previous_prio > new_prio)
107 int i;
108 for (i = previous_prio - 1; i >= fifo_min_prio; --i)
109 if (tpp->priomap[i - fifo_min_prio])
110 break;
111 newpriomax = i;
115 if (priomax == newpriomax)
116 return 0;
118 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
119 lll_lock (self->lock, LLL_PRIVATE);
121 tpp->priomax = newpriomax;
123 int result = 0;
125 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
127 if (__sched_getparam (self->tid, &self->schedparam) != 0)
128 result = errno;
129 else
130 self->flags |= ATTR_FLAG_SCHED_SET;
133 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
135 self->schedpolicy = __sched_getscheduler (self->tid);
136 if (self->schedpolicy == -1)
137 result = errno;
138 else
139 self->flags |= ATTR_FLAG_POLICY_SET;
142 if (result == 0)
144 struct sched_param sp = self->schedparam;
145 if (sp.sched_priority < newpriomax || sp.sched_priority < priomax)
147 if (sp.sched_priority < newpriomax)
148 sp.sched_priority = newpriomax;
150 if (__sched_setscheduler (self->tid, self->schedpolicy, &sp) < 0)
151 result = errno;
155 lll_unlock (self->lock, LLL_PRIVATE);
157 return result;
159 libc_hidden_def (__pthread_tpp_change_priority)
162 __pthread_current_priority (void)
164 struct pthread *self = THREAD_SELF;
165 if ((self->flags & (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
166 == (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
167 return self->schedparam.sched_priority;
169 int result = 0;
171 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
172 lll_lock (self->lock, LLL_PRIVATE);
174 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
176 if (__sched_getparam (self->tid, &self->schedparam) != 0)
177 result = -1;
178 else
179 self->flags |= ATTR_FLAG_SCHED_SET;
182 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
184 self->schedpolicy = __sched_getscheduler (self->tid);
185 if (self->schedpolicy == -1)
186 result = -1;
187 else
188 self->flags |= ATTR_FLAG_POLICY_SET;
191 if (result != -1)
192 result = self->schedparam.sched_priority;
194 lll_unlock (self->lock, LLL_PRIVATE);
196 return result;
198 libc_hidden_def (__pthread_current_priority)