]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/tpp.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / nptl / tpp.c
CommitLineData
f17efcb4 1/* Thread Priority Protect helpers.
d614a753 2 Copyright (C) 2006-2020 Free Software Foundation, Inc.
f17efcb4
UD
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
59ba27a6 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://www.gnu.org/licenses/>. */
f17efcb4
UD
19
20#include <assert.h>
21#include <atomic.h>
22#include <errno.h>
23#include <pthreadP.h>
24#include <sched.h>
25#include <stdlib.h>
cdcb42d7 26#include <atomic.h>
f17efcb4
UD
27
28
29int __sched_fifo_min_prio = -1;
30int __sched_fifo_max_prio = -1;
31
cdcb42d7
TR
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.
41 */
f17efcb4
UD
42void
43__init_sched_fifo_prio (void)
44{
cdcb42d7 45 atomic_store_relaxed (&__sched_fifo_max_prio,
fa872e1b 46 __sched_get_priority_max (SCHED_FIFO));
cdcb42d7 47 atomic_store_relaxed (&__sched_fifo_min_prio,
fa872e1b 48 __sched_get_priority_min (SCHED_FIFO));
f17efcb4
UD
49}
50
51int
52__pthread_tpp_change_priority (int previous_prio, int new_prio)
53{
54 struct pthread *self = THREAD_SELF;
55 struct priority_protection_data *tpp = THREAD_GETMEM (self, tpp);
cdcb42d7
TR
56 int fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio);
57 int fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio);
f17efcb4
UD
58
59 if (tpp == NULL)
60 {
cdcb42d7
TR
61 /* See __init_sched_fifo_prio. We need both the min and max prio,
62 so need to check both, and run initialization if either one is
63 not initialized. The memory model's write-read coherence rule
64 makes this work. */
65 if (fifo_min_prio == -1 || fifo_max_prio == -1)
66 {
67 __init_sched_fifo_prio ();
68 fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio);
69 fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio);
70 }
f17efcb4
UD
71
72 size_t size = sizeof *tpp;
cdcb42d7 73 size += (fifo_max_prio - fifo_min_prio + 1)
f17efcb4
UD
74 * sizeof (tpp->priomap[0]);
75 tpp = calloc (size, 1);
76 if (tpp == NULL)
77 return ENOMEM;
cdcb42d7 78 tpp->priomax = fifo_min_prio - 1;
f17efcb4
UD
79 THREAD_SETMEM (self, tpp, tpp);
80 }
81
82 assert (new_prio == -1
cdcb42d7
TR
83 || (new_prio >= fifo_min_prio
84 && new_prio <= fifo_max_prio));
f17efcb4 85 assert (previous_prio == -1
cdcb42d7
TR
86 || (previous_prio >= fifo_min_prio
87 && previous_prio <= fifo_max_prio));
f17efcb4
UD
88
89 int priomax = tpp->priomax;
90 int newpriomax = priomax;
91 if (new_prio != -1)
92 {
cdcb42d7 93 if (tpp->priomap[new_prio - fifo_min_prio] + 1 == 0)
f17efcb4 94 return EAGAIN;
cdcb42d7 95 ++tpp->priomap[new_prio - fifo_min_prio];
f17efcb4
UD
96 if (new_prio > priomax)
97 newpriomax = new_prio;
98 }
99
100 if (previous_prio != -1)
101 {
cdcb42d7 102 if (--tpp->priomap[previous_prio - fifo_min_prio] == 0
f17efcb4
UD
103 && priomax == previous_prio
104 && previous_prio > new_prio)
105 {
106 int i;
cdcb42d7
TR
107 for (i = previous_prio - 1; i >= fifo_min_prio; --i)
108 if (tpp->priomap[i - fifo_min_prio])
f17efcb4
UD
109 break;
110 newpriomax = i;
111 }
112 }
113
114 if (priomax == newpriomax)
115 return 0;
116
f8bf15fe 117 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
e51deae7 118 lll_lock (self->lock, LLL_PRIVATE);
f17efcb4
UD
119
120 tpp->priomax = newpriomax;
121
122 int result = 0;
123
124 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
125 {
126 if (__sched_getparam (self->tid, &self->schedparam) != 0)
127 result = errno;
128 else
129 self->flags |= ATTR_FLAG_SCHED_SET;
130 }
131
132 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
133 {
134 self->schedpolicy = __sched_getscheduler (self->tid);
135 if (self->schedpolicy == -1)
136 result = errno;
137 else
138 self->flags |= ATTR_FLAG_POLICY_SET;
139 }
140
141 if (result == 0)
142 {
143 struct sched_param sp = self->schedparam;
144 if (sp.sched_priority < newpriomax || sp.sched_priority < priomax)
145 {
146 if (sp.sched_priority < newpriomax)
147 sp.sched_priority = newpriomax;
148
149 if (__sched_setscheduler (self->tid, self->schedpolicy, &sp) < 0)
150 result = errno;
151 }
152 }
153
e51deae7 154 lll_unlock (self->lock, LLL_PRIVATE);
f17efcb4
UD
155
156 return result;
157}
158
159int
160__pthread_current_priority (void)
161{
162 struct pthread *self = THREAD_SELF;
163 if ((self->flags & (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
164 == (ATTR_FLAG_POLICY_SET | ATTR_FLAG_SCHED_SET))
165 return self->schedparam.sched_priority;
166
167 int result = 0;
168
f8bf15fe 169 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
e51deae7 170 lll_lock (self->lock, LLL_PRIVATE);
f17efcb4
UD
171
172 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
173 {
174 if (__sched_getparam (self->tid, &self->schedparam) != 0)
175 result = -1;
176 else
177 self->flags |= ATTR_FLAG_SCHED_SET;
178 }
179
180 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
181 {
182 self->schedpolicy = __sched_getscheduler (self->tid);
183 if (self->schedpolicy == -1)
184 result = -1;
185 else
186 self->flags |= ATTR_FLAG_POLICY_SET;
187 }
188
189 if (result != -1)
190 result = self->schedparam.sched_priority;
191
e51deae7 192 lll_unlock (self->lock, LLL_PRIVATE);
f17efcb4
UD
193
194 return result;
195}