[BZ #5995]
[glibc.git] / nptl / pthread_mutex_unlock.c
blobd33d0593d8d9c441435aebcdb441b67bde7f4d0b
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
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include "pthreadP.h"
23 #include <lowlevellock.h>
26 int
27 internal_function attribute_hidden
28 __pthread_mutex_unlock_usercnt (mutex, decr)
29 pthread_mutex_t *mutex;
30 int decr;
32 int newowner = 0;
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))
40 return EPERM;
42 if (--mutex->__data.__count != 0)
43 /* We still hold the mutex. */
44 return 0;
45 goto normal;
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))
51 return EPERM;
52 /* FALLTHROUGH */
54 case PTHREAD_MUTEX_TIMED_NP:
55 case PTHREAD_MUTEX_ADAPTIVE_NP:
56 /* Always reset the owner field. */
57 normal:
58 mutex->__data.__owner = 0;
59 if (decr)
60 /* One less user. */
61 --mutex->__data.__nusers;
63 /* Unlock. */
64 lll_unlock (mutex->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex));
65 break;
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;
78 goto notrecoverable;
81 if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
82 return EPERM;
84 if (--mutex->__data.__count != 0)
85 /* We still hold the mutex. */
86 return 0;
88 goto robust;
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))
96 return EPERM;
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))
103 notrecoverable:
104 newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
106 robust:
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;
113 if (decr)
114 /* One less user. */
115 --mutex->__data.__nusers;
117 /* Unlock. */
118 lll_robust_unlock (mutex->__data.__lock,
119 PTHREAD_ROBUST_MUTEX_PSHARED (mutex));
121 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
122 break;
124 case PTHREAD_MUTEX_PI_RECURSIVE_NP:
125 /* Recursive mutex. */
126 if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
127 return EPERM;
129 if (--mutex->__data.__count != 0)
130 /* We still hold the mutex. */
131 return 0;
132 goto continue_pi;
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))
149 return EPERM;
151 if (--mutex->__data.__count != 0)
152 /* We still hold the mutex. */
153 return 0;
155 goto continue_pi;
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))
166 return EPERM;
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))
174 pi_notrecoverable:
175 newowner = PTHREAD_MUTEX_NOTRECOVERABLE;
177 continue_pi:
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)
184 | 1));
185 DEQUEUE_MUTEX (mutex);
188 mutex->__data.__owner = newowner;
189 if (decr)
190 /* One less user. */
191 --mutex->__data.__nusers;
193 /* Unlock. */
194 if ((mutex->__data.__lock & FUTEX_WAITERS) != 0
195 || atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock, 0,
196 THREAD_GETMEM (THREAD_SELF,
197 tid)))
199 INTERNAL_SYSCALL_DECL (__err);
200 INTERNAL_SYSCALL (futex, __err, 2, &mutex->__data.__lock,
201 FUTEX_UNLOCK_PI);
204 THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL);
205 break;
207 case PTHREAD_MUTEX_PP_RECURSIVE_NP:
208 /* Recursive mutex. */
209 if (mutex->__data.__owner != THREAD_GETMEM (THREAD_SELF, tid))
210 return EPERM;
212 if (--mutex->__data.__count != 0)
213 /* We still hold the mutex. */
214 return 0;
215 goto pp;
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)
221 return EPERM;
222 /* FALLTHROUGH */
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;
230 if (decr)
231 /* One less user. */
232 --mutex->__data.__nusers;
234 /* Unlock. */
235 int newval, oldval;
238 oldval = mutex->__data.__lock;
239 newval = oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK;
241 while (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock,
242 newval, oldval));
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);
251 default:
252 /* Correct code cannot set any other type. */
253 return EINVAL;
256 return 0;
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)