]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_mutex_setprioceiling.c
Add per-thread cache to malloc
[thirdparty/glibc.git] / nptl / pthread_mutex_setprioceiling.c
CommitLineData
a5f2bd86 1/* Set current priority ceiling of pthread_mutex_t.
bfff8b1b 2 Copyright (C) 2006-2017 Free Software Foundation, Inc.
a5f2bd86
RM
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
PE
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
a5f2bd86 19
f17efcb4 20#include <stdbool.h>
a5f2bd86
RM
21#include <errno.h>
22#include <pthreadP.h>
cdcb42d7 23#include <atomic.h>
a5f2bd86
RM
24
25
26int
f63f2bfd
JM
27pthread_mutex_setprioceiling (pthread_mutex_t *mutex, int prioceiling,
28 int *old_ceiling)
a5f2bd86
RM
29{
30 /* The low bits of __kind aren't ever changed after pthread_mutex_init,
31 so we don't need a lock yet. */
df47504c 32 if ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
a5f2bd86
RM
33 return EINVAL;
34
cdcb42d7
TR
35 /* See __init_sched_fifo_prio. */
36 if (atomic_load_relaxed (&__sched_fifo_min_prio) == -1
37 || atomic_load_relaxed (&__sched_fifo_max_prio) == -1)
f17efcb4
UD
38 __init_sched_fifo_prio ();
39
cdcb42d7
TR
40 if (__glibc_unlikely (prioceiling
41 < atomic_load_relaxed (&__sched_fifo_min_prio))
42 || __glibc_unlikely (prioceiling
43 > atomic_load_relaxed (&__sched_fifo_max_prio))
44 || __glibc_unlikely ((prioceiling
f17efcb4
UD
45 & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
46 >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
cdcb42d7 47 != prioceiling))
a5f2bd86
RM
48 return EINVAL;
49
f17efcb4
UD
50 /* Check whether we already hold the mutex. */
51 bool locked = false;
5bd8a249 52 int kind = PTHREAD_MUTEX_TYPE (mutex);
f17efcb4
UD
53 if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
54 {
5bd8a249 55 if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
f17efcb4
UD
56 return EDEADLK;
57
5bd8a249 58 if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
f17efcb4
UD
59 locked = true;
60 }
61
62 int oldval = mutex->__data.__lock;
63 if (! locked)
64 do
65 {
66 /* Need to lock the mutex, but without obeying the priority
67 protect protocol. */
68 int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
69
70 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
71 ceilval | 1, ceilval);
72 if (oldval == ceilval)
73 break;
74
75 do
76 {
77 oldval
78 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
79 ceilval | 2,
80 ceilval | 1);
81
82 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
83 break;
84
85 if (oldval != ceilval)
835abc5c 86 lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
5bd8a249 87 PTHREAD_MUTEX_PSHARED (mutex));
f17efcb4
UD
88 }
89 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
90 ceilval | 2, ceilval)
91 != ceilval);
92
93 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
94 continue;
95 }
96 while (0);
97
98 int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
99 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
100 if (locked)
101 {
102 int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
103 if (ret)
104 return ret;
105 }
a5f2bd86
RM
106
107 if (old_ceiling != NULL)
f17efcb4 108 *old_ceiling = oldprio;
a5f2bd86 109
f17efcb4
UD
110 int newlock = 0;
111 if (locked)
112 newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
113 mutex->__data.__lock = newlock
a5f2bd86 114 | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
f17efcb4 115 atomic_full_barrier ();
a5f2bd86 116
835abc5c 117 lll_futex_wake (&mutex->__data.__lock, INT_MAX,
5bd8a249 118 PTHREAD_MUTEX_PSHARED (mutex));
a5f2bd86
RM
119
120 return 0;
121}