hurd: Fix getting ssize_t for <sys/socket.h>
[glibc.git] / nptl / tpp.c
blob7eb2b96b99e25e4e8884f5501610a5d56d420b26
1 /* Thread Priority Protect helpers.
2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2006.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
20 #include <assert.h>
21 #include <atomic.h>
22 #include <errno.h>
23 #include <pthreadP.h>
24 #include <sched.h>
25 #include <stdlib.h>
26 #include <atomic.h>
29 int __sched_fifo_min_prio = -1;
30 int __sched_fifo_max_prio = -1;
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));
51 int
52 __pthread_tpp_change_priority (int previous_prio, int new_prio)
54 struct pthread *self = THREAD_SELF;
55 struct priority_protection_data *tpp = THREAD_GETMEM (self, tpp);
56 int fifo_min_prio = atomic_load_relaxed (&__sched_fifo_min_prio);
57 int fifo_max_prio = atomic_load_relaxed (&__sched_fifo_max_prio);
59 if (tpp == NULL)
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)
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);
72 size_t size = sizeof *tpp;
73 size += (fifo_max_prio - fifo_min_prio + 1)
74 * sizeof (tpp->priomap[0]);
75 tpp = calloc (size, 1);
76 if (tpp == NULL)
77 return ENOMEM;
78 tpp->priomax = fifo_min_prio - 1;
79 THREAD_SETMEM (self, tpp, tpp);
82 assert (new_prio == -1
83 || (new_prio >= fifo_min_prio
84 && new_prio <= fifo_max_prio));
85 assert (previous_prio == -1
86 || (previous_prio >= fifo_min_prio
87 && previous_prio <= fifo_max_prio));
89 int priomax = tpp->priomax;
90 int newpriomax = priomax;
91 if (new_prio != -1)
93 if (tpp->priomap[new_prio - fifo_min_prio] + 1 == 0)
94 return EAGAIN;
95 ++tpp->priomap[new_prio - fifo_min_prio];
96 if (new_prio > priomax)
97 newpriomax = new_prio;
100 if (previous_prio != -1)
102 if (--tpp->priomap[previous_prio - fifo_min_prio] == 0
103 && priomax == previous_prio
104 && previous_prio > new_prio)
106 int i;
107 for (i = previous_prio - 1; i >= fifo_min_prio; --i)
108 if (tpp->priomap[i - fifo_min_prio])
109 break;
110 newpriomax = i;
114 if (priomax == newpriomax)
115 return 0;
117 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
118 lll_lock (self->lock, LLL_PRIVATE);
120 tpp->priomax = newpriomax;
122 int result = 0;
124 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
126 if (__sched_getparam (self->tid, &self->schedparam) != 0)
127 result = errno;
128 else
129 self->flags |= ATTR_FLAG_SCHED_SET;
132 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
134 self->schedpolicy = __sched_getscheduler (self->tid);
135 if (self->schedpolicy == -1)
136 result = errno;
137 else
138 self->flags |= ATTR_FLAG_POLICY_SET;
141 if (result == 0)
143 struct sched_param sp = self->schedparam;
144 if (sp.sched_priority < newpriomax || sp.sched_priority < priomax)
146 if (sp.sched_priority < newpriomax)
147 sp.sched_priority = newpriomax;
149 if (__sched_setscheduler (self->tid, self->schedpolicy, &sp) < 0)
150 result = errno;
154 lll_unlock (self->lock, LLL_PRIVATE);
156 return result;
160 __pthread_current_priority (void)
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;
167 int result = 0;
169 /* See CREATE THREAD NOTES in nptl/pthread_create.c. */
170 lll_lock (self->lock, LLL_PRIVATE);
172 if ((self->flags & ATTR_FLAG_SCHED_SET) == 0)
174 if (__sched_getparam (self->tid, &self->schedparam) != 0)
175 result = -1;
176 else
177 self->flags |= ATTR_FLAG_SCHED_SET;
180 if ((self->flags & ATTR_FLAG_POLICY_SET) == 0)
182 self->schedpolicy = __sched_getscheduler (self->tid);
183 if (self->schedpolicy == -1)
184 result = -1;
185 else
186 self->flags |= ATTR_FLAG_POLICY_SET;
189 if (result != -1)
190 result = self->schedparam.sched_priority;
192 lll_unlock (self->lock, LLL_PRIVATE);
194 return result;