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