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