1 /* Copyright (C) 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <lowlevellock.h>
27 internal_function attribute_hidden
28 __pthread_mutex_unlock_usercnt (mutex
, decr
)
29 pthread_mutex_t
*mutex
;
34 switch (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex
),
35 PTHREAD_MUTEX_TIMED_NP
))
37 case PTHREAD_MUTEX_RECURSIVE_NP
:
38 /* Recursive mutex. */
39 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
))
42 if (--mutex
->__data
.__count
!= 0)
43 /* We still hold the mutex. */
47 case PTHREAD_MUTEX_ERRORCHECK_NP
:
48 /* Error checking mutex. */
49 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
)
50 || ! lll_islocked (mutex
->__data
.__lock
))
54 case PTHREAD_MUTEX_TIMED_NP
:
55 case PTHREAD_MUTEX_ADAPTIVE_NP
:
56 /* Always reset the owner field. */
58 mutex
->__data
.__owner
= 0;
61 --mutex
->__data
.__nusers
;
64 lll_unlock (mutex
->__data
.__lock
, PTHREAD_MUTEX_PSHARED (mutex
));
67 case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP
:
68 /* Recursive mutex. */
69 if ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
70 == THREAD_GETMEM (THREAD_SELF
, tid
)
71 && __builtin_expect (mutex
->__data
.__owner
72 == PTHREAD_MUTEX_INCONSISTENT
, 0))
74 if (--mutex
->__data
.__count
!= 0)
75 /* We still hold the mutex. */
76 return ENOTRECOVERABLE
;
81 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
))
84 if (--mutex
->__data
.__count
!= 0)
85 /* We still hold the mutex. */
90 case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP
:
91 case PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
92 case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP
:
93 if ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
94 != THREAD_GETMEM (THREAD_SELF
, tid
)
95 || ! lll_islocked (mutex
->__data
.__lock
))
98 /* If the previous owner died and the caller did not succeed in
99 making the state consistent, mark the mutex as unrecoverable
100 and make all waiters. */
101 if (__builtin_expect (mutex
->__data
.__owner
102 == PTHREAD_MUTEX_INCONSISTENT
, 0))
104 newowner
= PTHREAD_MUTEX_NOTRECOVERABLE
;
107 /* Remove mutex from the list. */
108 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
109 &mutex
->__data
.__list
.__next
);
110 DEQUEUE_MUTEX (mutex
);
112 mutex
->__data
.__owner
= newowner
;
115 --mutex
->__data
.__nusers
;
118 lll_robust_unlock (mutex
->__data
.__lock
,
119 PTHREAD_ROBUST_MUTEX_PSHARED (mutex
));
121 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
124 case PTHREAD_MUTEX_PI_RECURSIVE_NP
:
125 /* Recursive mutex. */
126 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
))
129 if (--mutex
->__data
.__count
!= 0)
130 /* We still hold the mutex. */
134 case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP
:
135 /* Recursive mutex. */
136 if ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
137 == THREAD_GETMEM (THREAD_SELF
, tid
)
138 && __builtin_expect (mutex
->__data
.__owner
139 == PTHREAD_MUTEX_INCONSISTENT
, 0))
141 if (--mutex
->__data
.__count
!= 0)
142 /* We still hold the mutex. */
143 return ENOTRECOVERABLE
;
145 goto pi_notrecoverable
;
148 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
))
151 if (--mutex
->__data
.__count
!= 0)
152 /* We still hold the mutex. */
157 case PTHREAD_MUTEX_PI_ERRORCHECK_NP
:
158 case PTHREAD_MUTEX_PI_NORMAL_NP
:
159 case PTHREAD_MUTEX_PI_ADAPTIVE_NP
:
160 case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP
:
161 case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP
:
162 case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP
:
163 if ((mutex
->__data
.__lock
& FUTEX_TID_MASK
)
164 != THREAD_GETMEM (THREAD_SELF
, tid
)
165 || ! lll_islocked (mutex
->__data
.__lock
))
168 /* If the previous owner died and the caller did not succeed in
169 making the state consistent, mark the mutex as unrecoverable
170 and make all waiters. */
171 if ((mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
) != 0
172 && __builtin_expect (mutex
->__data
.__owner
173 == PTHREAD_MUTEX_INCONSISTENT
, 0))
175 newowner
= PTHREAD_MUTEX_NOTRECOVERABLE
;
178 if ((mutex
->__data
.__kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
) != 0)
180 /* Remove mutex from the list.
181 Note: robust PI futexes are signaled by setting bit 0. */
182 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
,
183 (void *) (((uintptr_t) &mutex
->__data
.__list
.__next
)
185 DEQUEUE_MUTEX (mutex
);
188 mutex
->__data
.__owner
= newowner
;
191 --mutex
->__data
.__nusers
;
194 if ((mutex
->__data
.__lock
& FUTEX_WAITERS
) != 0
195 || atomic_compare_and_exchange_bool_acq (&mutex
->__data
.__lock
, 0,
196 THREAD_GETMEM (THREAD_SELF
,
199 INTERNAL_SYSCALL_DECL (__err
);
200 INTERNAL_SYSCALL (futex
, __err
, 2, &mutex
->__data
.__lock
,
204 THREAD_SETMEM (THREAD_SELF
, robust_head
.list_op_pending
, NULL
);
207 case PTHREAD_MUTEX_PP_RECURSIVE_NP
:
208 /* Recursive mutex. */
209 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
))
212 if (--mutex
->__data
.__count
!= 0)
213 /* We still hold the mutex. */
217 case PTHREAD_MUTEX_PP_ERRORCHECK_NP
:
218 /* Error checking mutex. */
219 if (mutex
->__data
.__owner
!= THREAD_GETMEM (THREAD_SELF
, tid
)
220 || (mutex
->__data
.__lock
& ~ PTHREAD_MUTEX_PRIO_CEILING_MASK
) == 0)
224 case PTHREAD_MUTEX_PP_NORMAL_NP
:
225 case PTHREAD_MUTEX_PP_ADAPTIVE_NP
:
226 /* Always reset the owner field. */
228 mutex
->__data
.__owner
= 0;
232 --mutex
->__data
.__nusers
;
238 oldval
= mutex
->__data
.__lock
;
239 newval
= oldval
& PTHREAD_MUTEX_PRIO_CEILING_MASK
;
241 while (atomic_compare_and_exchange_bool_acq (&mutex
->__data
.__lock
,
244 if ((oldval
& ~PTHREAD_MUTEX_PRIO_CEILING_MASK
) > 1)
245 lll_futex_wake (&mutex
->__data
.__lock
, 1,
246 PTHREAD_MUTEX_PSHARED (mutex
));
248 int oldprio
= newval
>> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
;
249 return __pthread_tpp_change_priority (oldprio
, -1);
252 /* Correct code cannot set any other type. */
261 __pthread_mutex_unlock (mutex
)
262 pthread_mutex_t
*mutex
;
264 return __pthread_mutex_unlock_usercnt (mutex
, 1);
266 strong_alias (__pthread_mutex_unlock
, pthread_mutex_unlock
)
267 strong_alias (__pthread_mutex_unlock
, __pthread_mutex_unlock_internal
)