]> git.ipfire.org Git - thirdparty/glibc.git/blame - nptl/pthread_mutex_setprioceiling.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / nptl / pthread_mutex_setprioceiling.c
CommitLineData
a5f2bd86 1/* Set current priority ceiling of pthread_mutex_t.
2b778ceb 2 Copyright (C) 2006-2021 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 17 License along with the GNU C Library; if not, see
5a82c748 18 <https://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>
878fe624 24#include <futex-internal.h>
a5f2bd86
RM
25
26
27int
f63f2bfd
JM
28pthread_mutex_setprioceiling (pthread_mutex_t *mutex, int prioceiling,
29 int *old_ceiling)
a5f2bd86 30{
403b4feb
SL
31 /* See concurrency notes regarding __kind in struct __pthread_mutex_s
32 in sysdeps/nptl/bits/thread-shared-types.h. */
33 if ((atomic_load_relaxed (&(mutex->__data.__kind))
34 & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
a5f2bd86
RM
35 return EINVAL;
36
cdcb42d7
TR
37 /* See __init_sched_fifo_prio. */
38 if (atomic_load_relaxed (&__sched_fifo_min_prio) == -1
39 || atomic_load_relaxed (&__sched_fifo_max_prio) == -1)
f17efcb4
UD
40 __init_sched_fifo_prio ();
41
cdcb42d7
TR
42 if (__glibc_unlikely (prioceiling
43 < atomic_load_relaxed (&__sched_fifo_min_prio))
44 || __glibc_unlikely (prioceiling
45 > atomic_load_relaxed (&__sched_fifo_max_prio))
46 || __glibc_unlikely ((prioceiling
f17efcb4
UD
47 & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
48 >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
cdcb42d7 49 != prioceiling))
a5f2bd86
RM
50 return EINVAL;
51
f17efcb4
UD
52 /* Check whether we already hold the mutex. */
53 bool locked = false;
5bd8a249 54 int kind = PTHREAD_MUTEX_TYPE (mutex);
f17efcb4
UD
55 if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
56 {
5bd8a249 57 if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
f17efcb4
UD
58 return EDEADLK;
59
5bd8a249 60 if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
f17efcb4
UD
61 locked = true;
62 }
63
64 int oldval = mutex->__data.__lock;
65 if (! locked)
66 do
67 {
68 /* Need to lock the mutex, but without obeying the priority
69 protect protocol. */
70 int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
71
72 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
73 ceilval | 1, ceilval);
74 if (oldval == ceilval)
75 break;
76
77 do
78 {
79 oldval
80 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
81 ceilval | 2,
82 ceilval | 1);
83
84 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
85 break;
86
87 if (oldval != ceilval)
878fe624
AZ
88 futex_wait ((unsigned int *) &mutex->__data.__lock, ceilval | 2,
89 PTHREAD_MUTEX_PSHARED (mutex));
f17efcb4
UD
90 }
91 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
92 ceilval | 2, ceilval)
93 != ceilval);
94
95 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
96 continue;
97 }
98 while (0);
99
100 int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
101 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
102 if (locked)
103 {
104 int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
105 if (ret)
106 return ret;
107 }
a5f2bd86
RM
108
109 if (old_ceiling != NULL)
f17efcb4 110 *old_ceiling = oldprio;
a5f2bd86 111
f17efcb4
UD
112 int newlock = 0;
113 if (locked)
114 newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
115 mutex->__data.__lock = newlock
a5f2bd86 116 | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
f17efcb4 117 atomic_full_barrier ();
a5f2bd86 118
b45b1c5b
AZ
119 futex_wake ((unsigned int *)&mutex->__data.__lock, INT_MAX,
120 PTHREAD_MUTEX_PSHARED (mutex));
a5f2bd86
RM
121
122 return 0;
123}