Update copyright dates with scripts/update-copyrights.
[glibc.git] / nptl / pthread_mutex_setprioceiling.c
blob1be7471c919a123c4ad6749865db2b28e40a3f43
1 /* Set current priority ceiling of pthread_mutex_t.
2 Copyright (C) 2006-2015 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 <stdbool.h>
21 #include <errno.h>
22 #include <pthreadP.h>
23 #include <atomic.h>
26 int
27 pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling)
28 pthread_mutex_t *mutex;
29 int prioceiling;
30 int *old_ceiling;
32 /* The low bits of __kind aren't ever changed after pthread_mutex_init,
33 so we don't need a lock yet. */
34 if ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_PROTECT_NP) == 0)
35 return EINVAL;
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)
40 __init_sched_fifo_prio ();
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
47 & (PTHREAD_MUTEXATTR_PRIO_CEILING_MASK
48 >> PTHREAD_MUTEXATTR_PRIO_CEILING_SHIFT))
49 != prioceiling))
50 return EINVAL;
52 /* Check whether we already hold the mutex. */
53 bool locked = false;
54 int kind = PTHREAD_MUTEX_TYPE (mutex);
55 if (mutex->__data.__owner == THREAD_GETMEM (THREAD_SELF, tid))
57 if (kind == PTHREAD_MUTEX_PP_ERRORCHECK_NP)
58 return EDEADLK;
60 if (kind == PTHREAD_MUTEX_PP_RECURSIVE_NP)
61 locked = true;
64 int oldval = mutex->__data.__lock;
65 if (! locked)
68 /* Need to lock the mutex, but without obeying the priority
69 protect protocol. */
70 int ceilval = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK);
72 oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
73 ceilval | 1, ceilval);
74 if (oldval == ceilval)
75 break;
79 oldval
80 = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
81 ceilval | 2,
82 ceilval | 1);
84 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
85 break;
87 if (oldval != ceilval)
88 lll_futex_wait (&mutex->__data.__lock, ceilval | 2,
89 PTHREAD_MUTEX_PSHARED (mutex));
91 while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock,
92 ceilval | 2, ceilval)
93 != ceilval);
95 if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval)
96 continue;
98 while (0);
100 int oldprio = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK)
101 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT;
102 if (locked)
104 int ret = __pthread_tpp_change_priority (oldprio, prioceiling);
105 if (ret)
106 return ret;
109 if (old_ceiling != NULL)
110 *old_ceiling = oldprio;
112 int newlock = 0;
113 if (locked)
114 newlock = (mutex->__data.__lock & ~PTHREAD_MUTEX_PRIO_CEILING_MASK);
115 mutex->__data.__lock = newlock
116 | (prioceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT);
117 atomic_full_barrier ();
119 lll_futex_wake (&mutex->__data.__lock, INT_MAX,
120 PTHREAD_MUTEX_PSHARED (mutex));
122 return 0;