]>
Commit | Line | Data |
---|---|---|
688903eb | 1 | /* Copyright (C) 2002-2018 Free Software Foundation, Inc. |
76a50749 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2002. | |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
76a50749 | 18 | |
3892d906 | 19 | #include <assert.h> |
76a50749 | 20 | #include <errno.h> |
1bcfb5a5 | 21 | #include <stdlib.h> |
df47504c | 22 | #include <unistd.h> |
ca06321d | 23 | #include <sys/param.h> |
b894c2ea | 24 | #include <not-cancel.h> |
76a50749 | 25 | #include "pthreadP.h" |
4eb984d3 | 26 | #include <atomic.h> |
76a50749 | 27 | #include <lowlevellock.h> |
5acf7263 | 28 | #include <stap-probe.h> |
76a50749 | 29 | |
e8c659d7 AK |
30 | #ifndef lll_lock_elision |
31 | #define lll_lock_elision(lock, try_lock, private) ({ \ | |
32 | lll_lock (lock, private); 0; }) | |
33 | #endif | |
34 | ||
35 | #ifndef lll_trylock_elision | |
36 | #define lll_trylock_elision(a,t) lll_trylock(a) | |
37 | #endif | |
76a50749 | 38 | |
65810f0e TR |
39 | /* Some of the following definitions differ when pthread_mutex_cond_lock.c |
40 | includes this file. */ | |
69431c9a | 41 | #ifndef LLL_MUTEX_LOCK |
5bd8a249 UD |
42 | # define LLL_MUTEX_LOCK(mutex) \ |
43 | lll_lock ((mutex)->__data.__lock, PTHREAD_MUTEX_PSHARED (mutex)) | |
44 | # define LLL_MUTEX_TRYLOCK(mutex) \ | |
45 | lll_trylock ((mutex)->__data.__lock) | |
65810f0e | 46 | # define LLL_ROBUST_MUTEX_LOCK_MODIFIER 0 |
e8c659d7 AK |
47 | # define LLL_MUTEX_LOCK_ELISION(mutex) \ |
48 | lll_lock_elision ((mutex)->__data.__lock, (mutex)->__data.__elision, \ | |
49 | PTHREAD_MUTEX_PSHARED (mutex)) | |
50 | # define LLL_MUTEX_TRYLOCK_ELISION(mutex) \ | |
51 | lll_trylock_elision((mutex)->__data.__lock, (mutex)->__data.__elision, \ | |
52 | PTHREAD_MUTEX_PSHARED (mutex)) | |
69431c9a UD |
53 | #endif |
54 | ||
e8c659d7 AK |
55 | #ifndef FORCE_ELISION |
56 | #define FORCE_ELISION(m, s) | |
57 | #endif | |
69431c9a | 58 | |
6de79a49 UD |
59 | static int __pthread_mutex_lock_full (pthread_mutex_t *mutex) |
60 | __attribute_noinline__; | |
61 | ||
76a50749 | 62 | int |
9dd346ff | 63 | __pthread_mutex_lock (pthread_mutex_t *mutex) |
76a50749 | 64 | { |
e8c659d7 | 65 | unsigned int type = PTHREAD_MUTEX_TYPE_ELISION (mutex); |
5acf7263 RM |
66 | |
67 | LIBC_PROBE (mutex_entry, 1, mutex); | |
68 | ||
e8c659d7 AK |
69 | if (__builtin_expect (type & ~(PTHREAD_MUTEX_KIND_MASK_NP |
70 | | PTHREAD_MUTEX_ELISION_FLAGS_NP), 0)) | |
6de79a49 UD |
71 | return __pthread_mutex_lock_full (mutex); |
72 | ||
a1ffb40e | 73 | if (__glibc_likely (type == PTHREAD_MUTEX_TIMED_NP)) |
6de79a49 | 74 | { |
e8c659d7 | 75 | FORCE_ELISION (mutex, goto elision); |
6de79a49 UD |
76 | simple: |
77 | /* Normal mutex. */ | |
78 | LLL_MUTEX_LOCK (mutex); | |
79 | assert (mutex->__data.__owner == 0); | |
80 | } | |
e8c659d7 | 81 | #ifdef HAVE_ELISION |
a1ffb40e | 82 | else if (__glibc_likely (type == PTHREAD_MUTEX_TIMED_ELISION_NP)) |
e8c659d7 AK |
83 | { |
84 | elision: __attribute__((unused)) | |
85 | /* This case can never happen on a system without elision, | |
86 | as the mutex type initialization functions will not | |
87 | allow to set the elision flags. */ | |
075b9322 | 88 | /* Don't record owner or users for elision case. This is a |
e8c659d7 AK |
89 | tail call. */ |
90 | return LLL_MUTEX_LOCK_ELISION (mutex); | |
91 | } | |
92 | #endif | |
93 | else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex) | |
94 | == PTHREAD_MUTEX_RECURSIVE_NP, 1)) | |
76a50749 UD |
95 | { |
96 | /* Recursive mutex. */ | |
e8c659d7 | 97 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); |
6de79a49 | 98 | |
76a50749 | 99 | /* Check whether we already hold the mutex. */ |
9a7178d6 | 100 | if (mutex->__data.__owner == id) |
76a50749 UD |
101 | { |
102 | /* Just bump the counter. */ | |
a1ffb40e | 103 | if (__glibc_unlikely (mutex->__data.__count + 1 == 0)) |
76a50749 UD |
104 | /* Overflow of the counter. */ |
105 | return EAGAIN; | |
106 | ||
107 | ++mutex->__data.__count; | |
76a50749 | 108 | |
3892d906 | 109 | return 0; |
76a50749 | 110 | } |
3892d906 UD |
111 | |
112 | /* We have to get the mutex. */ | |
5bd8a249 | 113 | LLL_MUTEX_LOCK (mutex); |
3892d906 | 114 | |
1bcfb5a5 | 115 | assert (mutex->__data.__owner == 0); |
3892d906 | 116 | mutex->__data.__count = 1; |
6de79a49 | 117 | } |
e8c659d7 AK |
118 | else if (__builtin_expect (PTHREAD_MUTEX_TYPE (mutex) |
119 | == PTHREAD_MUTEX_ADAPTIVE_NP, 1)) | |
6de79a49 | 120 | { |
2c0b891a UD |
121 | if (! __is_smp) |
122 | goto simple; | |
123 | ||
5bd8a249 | 124 | if (LLL_MUTEX_TRYLOCK (mutex) != 0) |
2c0b891a UD |
125 | { |
126 | int cnt = 0; | |
127 | int max_cnt = MIN (MAX_ADAPTIVE_COUNT, | |
128 | mutex->__data.__spins * 2 + 10); | |
129 | do | |
130 | { | |
131 | if (cnt++ >= max_cnt) | |
132 | { | |
5bd8a249 | 133 | LLL_MUTEX_LOCK (mutex); |
2c0b891a UD |
134 | break; |
135 | } | |
4eb984d3 | 136 | atomic_spin_nop (); |
2c0b891a | 137 | } |
5bd8a249 | 138 | while (LLL_MUTEX_TRYLOCK (mutex) != 0); |
2c0b891a UD |
139 | |
140 | mutex->__data.__spins += (cnt - mutex->__data.__spins) / 8; | |
141 | } | |
1bcfb5a5 | 142 | assert (mutex->__data.__owner == 0); |
6de79a49 UD |
143 | } |
144 | else | |
145 | { | |
e8c659d7 AK |
146 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); |
147 | assert (PTHREAD_MUTEX_TYPE (mutex) == PTHREAD_MUTEX_ERRORCHECK_NP); | |
6de79a49 | 148 | /* Check whether we already hold the mutex. */ |
a1ffb40e | 149 | if (__glibc_unlikely (mutex->__data.__owner == id)) |
6de79a49 UD |
150 | return EDEADLK; |
151 | goto simple; | |
152 | } | |
1bcfb5a5 | 153 | |
e8c659d7 AK |
154 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); |
155 | ||
6de79a49 UD |
156 | /* Record the ownership. */ |
157 | mutex->__data.__owner = id; | |
158 | #ifndef NO_INCR | |
159 | ++mutex->__data.__nusers; | |
160 | #endif | |
161 | ||
5acf7263 RM |
162 | LIBC_PROBE (mutex_acquired, 1, mutex); |
163 | ||
6de79a49 UD |
164 | return 0; |
165 | } | |
166 | ||
167 | static int | |
168 | __pthread_mutex_lock_full (pthread_mutex_t *mutex) | |
169 | { | |
170 | int oldval; | |
171 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); | |
172 | ||
173 | switch (PTHREAD_MUTEX_TYPE (mutex)) | |
174 | { | |
0f6699ea UD |
175 | case PTHREAD_MUTEX_ROBUST_RECURSIVE_NP: |
176 | case PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP: | |
177 | case PTHREAD_MUTEX_ROBUST_NORMAL_NP: | |
178 | case PTHREAD_MUTEX_ROBUST_ADAPTIVE_NP: | |
179 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, | |
180 | &mutex->__data.__list.__next); | |
8f9450a0 TR |
181 | /* We need to set op_pending before starting the operation. Also |
182 | see comments at ENQUEUE_MUTEX. */ | |
183 | __asm ("" ::: "memory"); | |
0f6699ea | 184 | |
683040c3 | 185 | oldval = mutex->__data.__lock; |
353683a2 TR |
186 | /* This is set to FUTEX_WAITERS iff we might have shared the |
187 | FUTEX_WAITERS flag with other threads, and therefore need to keep it | |
188 | set to avoid lost wake-ups. We have the same requirement in the | |
65810f0e TR |
189 | simple mutex algorithm. |
190 | We start with value zero for a normal mutex, and FUTEX_WAITERS if we | |
191 | are building the special case mutexes for use from within condition | |
192 | variables. */ | |
193 | unsigned int assume_other_futex_waiters = LLL_ROBUST_MUTEX_LOCK_MODIFIER; | |
194 | while (1) | |
1bcfb5a5 | 195 | { |
65810f0e TR |
196 | /* Try to acquire the lock through a CAS from 0 (not acquired) to |
197 | our TID | assume_other_futex_waiters. */ | |
5920a4a6 CD |
198 | if (__glibc_likely (oldval == 0)) |
199 | { | |
200 | oldval | |
201 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, | |
202 | id | assume_other_futex_waiters, 0); | |
203 | if (__glibc_likely (oldval == 0)) | |
204 | break; | |
205 | } | |
65810f0e | 206 | |
683040c3 UD |
207 | if ((oldval & FUTEX_OWNER_DIED) != 0) |
208 | { | |
209 | /* The previous owner died. Try locking the mutex. */ | |
0f6699ea UD |
210 | int newval = id; |
211 | #ifdef NO_INCR | |
353683a2 TR |
212 | /* We are not taking assume_other_futex_waiters into accoount |
213 | here simply because we'll set FUTEX_WAITERS anyway. */ | |
0f6699ea | 214 | newval |= FUTEX_WAITERS; |
113ad5fc | 215 | #else |
353683a2 | 216 | newval |= (oldval & FUTEX_WAITERS) | assume_other_futex_waiters; |
0f6699ea UD |
217 | #endif |
218 | ||
219 | newval | |
052757bf | 220 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
0f6699ea UD |
221 | newval, oldval); |
222 | ||
223 | if (newval != oldval) | |
683040c3 | 224 | { |
683040c3 | 225 | oldval = newval; |
65810f0e | 226 | continue; |
683040c3 | 227 | } |
1bcfb5a5 | 228 | |
683040c3 UD |
229 | /* We got the mutex. */ |
230 | mutex->__data.__count = 1; | |
231 | /* But it is inconsistent unless marked otherwise. */ | |
232 | mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT; | |
233 | ||
8f9450a0 TR |
234 | /* We must not enqueue the mutex before we have acquired it. |
235 | Also see comments at ENQUEUE_MUTEX. */ | |
236 | __asm ("" ::: "memory"); | |
683040c3 | 237 | ENQUEUE_MUTEX (mutex); |
8f9450a0 TR |
238 | /* We need to clear op_pending after we enqueue the mutex. */ |
239 | __asm ("" ::: "memory"); | |
0f6699ea | 240 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
683040c3 UD |
241 | |
242 | /* Note that we deliberately exit here. If we fall | |
243 | through to the end of the function __nusers would be | |
244 | incremented which is not correct because the old | |
245 | owner has to be discounted. If we are not supposed | |
246 | to increment __nusers we actually have to decrement | |
247 | it here. */ | |
248 | #ifdef NO_INCR | |
249 | --mutex->__data.__nusers; | |
250 | #endif | |
1bcfb5a5 | 251 | |
683040c3 UD |
252 | return EOWNERDEAD; |
253 | } | |
1bcfb5a5 | 254 | |
683040c3 | 255 | /* Check whether we already hold the mutex. */ |
a1ffb40e | 256 | if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id)) |
683040c3 | 257 | { |
5bd8a249 UD |
258 | int kind = PTHREAD_MUTEX_TYPE (mutex); |
259 | if (kind == PTHREAD_MUTEX_ROBUST_ERRORCHECK_NP) | |
0f6699ea | 260 | { |
8f9450a0 TR |
261 | /* We do not need to ensure ordering wrt another memory |
262 | access. Also see comments at ENQUEUE_MUTEX. */ | |
0f6699ea UD |
263 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, |
264 | NULL); | |
265 | return EDEADLK; | |
266 | } | |
1bcfb5a5 | 267 | |
5bd8a249 | 268 | if (kind == PTHREAD_MUTEX_ROBUST_RECURSIVE_NP) |
683040c3 | 269 | { |
8f9450a0 TR |
270 | /* We do not need to ensure ordering wrt another memory |
271 | access. */ | |
0f6699ea UD |
272 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, |
273 | NULL); | |
274 | ||
683040c3 | 275 | /* Just bump the counter. */ |
a1ffb40e | 276 | if (__glibc_unlikely (mutex->__data.__count + 1 == 0)) |
683040c3 UD |
277 | /* Overflow of the counter. */ |
278 | return EAGAIN; | |
1bcfb5a5 | 279 | |
683040c3 | 280 | ++mutex->__data.__count; |
1bcfb5a5 | 281 | |
683040c3 UD |
282 | return 0; |
283 | } | |
284 | } | |
1bcfb5a5 | 285 | |
65810f0e TR |
286 | /* We cannot acquire the mutex nor has its owner died. Thus, try |
287 | to block using futexes. Set FUTEX_WAITERS if necessary so that | |
288 | other threads are aware that there are potentially threads | |
289 | blocked on the futex. Restart if oldval changed in the | |
290 | meantime. */ | |
291 | if ((oldval & FUTEX_WAITERS) == 0) | |
683040c3 | 292 | { |
65810f0e TR |
293 | if (atomic_compare_and_exchange_bool_acq (&mutex->__data.__lock, |
294 | oldval | FUTEX_WAITERS, | |
295 | oldval) | |
296 | != 0) | |
297 | { | |
298 | oldval = mutex->__data.__lock; | |
299 | continue; | |
300 | } | |
301 | oldval |= FUTEX_WAITERS; | |
683040c3 | 302 | } |
65810f0e TR |
303 | |
304 | /* It is now possible that we share the FUTEX_WAITERS flag with | |
305 | another thread; therefore, update assume_other_futex_waiters so | |
306 | that we do not forget about this when handling other cases | |
307 | above and thus do not cause lost wake-ups. */ | |
308 | assume_other_futex_waiters |= FUTEX_WAITERS; | |
309 | ||
310 | /* Block using the futex and reload current lock value. */ | |
311 | lll_futex_wait (&mutex->__data.__lock, oldval, | |
312 | PTHREAD_ROBUST_MUTEX_PSHARED (mutex)); | |
313 | oldval = mutex->__data.__lock; | |
314 | } | |
315 | ||
316 | /* We have acquired the mutex; check if it is still consistent. */ | |
317 | if (__builtin_expect (mutex->__data.__owner | |
318 | == PTHREAD_MUTEX_NOTRECOVERABLE, 0)) | |
319 | { | |
320 | /* This mutex is now not recoverable. */ | |
321 | mutex->__data.__count = 0; | |
322 | int private = PTHREAD_ROBUST_MUTEX_PSHARED (mutex); | |
323 | lll_unlock (mutex->__data.__lock, private); | |
8f9450a0 TR |
324 | /* FIXME This violates the mutex destruction requirements. See |
325 | __pthread_mutex_unlock_full. */ | |
65810f0e TR |
326 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
327 | return ENOTRECOVERABLE; | |
1bcfb5a5 UD |
328 | } |
329 | ||
683040c3 | 330 | mutex->__data.__count = 1; |
8f9450a0 TR |
331 | /* We must not enqueue the mutex before we have acquired it. |
332 | Also see comments at ENQUEUE_MUTEX. */ | |
333 | __asm ("" ::: "memory"); | |
1bcfb5a5 | 334 | ENQUEUE_MUTEX (mutex); |
8f9450a0 TR |
335 | /* We need to clear op_pending after we enqueue the mutex. */ |
336 | __asm ("" ::: "memory"); | |
0f6699ea | 337 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
2c0b891a | 338 | break; |
dcc73a8d | 339 | |
184ee940 RM |
340 | /* The PI support requires the Linux futex system call. If that's not |
341 | available, pthread_mutex_init should never have allowed the type to | |
342 | be set. So it will get the default case for an invalid type. */ | |
343 | #ifdef __NR_futex | |
df47504c UD |
344 | case PTHREAD_MUTEX_PI_RECURSIVE_NP: |
345 | case PTHREAD_MUTEX_PI_ERRORCHECK_NP: | |
346 | case PTHREAD_MUTEX_PI_NORMAL_NP: | |
347 | case PTHREAD_MUTEX_PI_ADAPTIVE_NP: | |
348 | case PTHREAD_MUTEX_PI_ROBUST_RECURSIVE_NP: | |
349 | case PTHREAD_MUTEX_PI_ROBUST_ERRORCHECK_NP: | |
350 | case PTHREAD_MUTEX_PI_ROBUST_NORMAL_NP: | |
351 | case PTHREAD_MUTEX_PI_ROBUST_ADAPTIVE_NP: | |
352 | { | |
353 | int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP; | |
354 | int robust = mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP; | |
355 | ||
356 | if (robust) | |
8f9450a0 TR |
357 | { |
358 | /* Note: robust PI futexes are signaled by setting bit 0. */ | |
359 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, | |
360 | (void *) (((uintptr_t) &mutex->__data.__list.__next) | |
361 | | 1)); | |
362 | /* We need to set op_pending before starting the operation. Also | |
363 | see comments at ENQUEUE_MUTEX. */ | |
364 | __asm ("" ::: "memory"); | |
365 | } | |
df47504c UD |
366 | |
367 | oldval = mutex->__data.__lock; | |
368 | ||
369 | /* Check whether we already hold the mutex. */ | |
a1ffb40e | 370 | if (__glibc_unlikely ((oldval & FUTEX_TID_MASK) == id)) |
df47504c UD |
371 | { |
372 | if (kind == PTHREAD_MUTEX_ERRORCHECK_NP) | |
373 | { | |
8f9450a0 TR |
374 | /* We do not need to ensure ordering wrt another memory |
375 | access. */ | |
df47504c UD |
376 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
377 | return EDEADLK; | |
378 | } | |
379 | ||
380 | if (kind == PTHREAD_MUTEX_RECURSIVE_NP) | |
381 | { | |
8f9450a0 TR |
382 | /* We do not need to ensure ordering wrt another memory |
383 | access. */ | |
df47504c UD |
384 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
385 | ||
386 | /* Just bump the counter. */ | |
a1ffb40e | 387 | if (__glibc_unlikely (mutex->__data.__count + 1 == 0)) |
df47504c UD |
388 | /* Overflow of the counter. */ |
389 | return EAGAIN; | |
390 | ||
391 | ++mutex->__data.__count; | |
392 | ||
393 | return 0; | |
394 | } | |
395 | } | |
396 | ||
397 | int newval = id; | |
184ee940 | 398 | # ifdef NO_INCR |
df47504c | 399 | newval |= FUTEX_WAITERS; |
184ee940 | 400 | # endif |
052757bf | 401 | oldval = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
df47504c UD |
402 | newval, 0); |
403 | ||
404 | if (oldval != 0) | |
405 | { | |
406 | /* The mutex is locked. The kernel will now take care of | |
407 | everything. */ | |
efac1fce UD |
408 | int private = (robust |
409 | ? PTHREAD_ROBUST_MUTEX_PSHARED (mutex) | |
410 | : PTHREAD_MUTEX_PSHARED (mutex)); | |
df47504c UD |
411 | INTERNAL_SYSCALL_DECL (__err); |
412 | int e = INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, | |
efac1fce UD |
413 | __lll_private_flag (FUTEX_LOCK_PI, |
414 | private), 1, 0); | |
df47504c UD |
415 | |
416 | if (INTERNAL_SYSCALL_ERROR_P (e, __err) | |
417 | && (INTERNAL_SYSCALL_ERRNO (e, __err) == ESRCH | |
418 | || INTERNAL_SYSCALL_ERRNO (e, __err) == EDEADLK)) | |
419 | { | |
420 | assert (INTERNAL_SYSCALL_ERRNO (e, __err) != EDEADLK | |
421 | || (kind != PTHREAD_MUTEX_ERRORCHECK_NP | |
422 | && kind != PTHREAD_MUTEX_RECURSIVE_NP)); | |
423 | /* ESRCH can happen only for non-robust PI mutexes where | |
424 | the owner of the lock died. */ | |
425 | assert (INTERNAL_SYSCALL_ERRNO (e, __err) != ESRCH || !robust); | |
426 | ||
427 | /* Delay the thread indefinitely. */ | |
428 | while (1) | |
08d6eb46 | 429 | __pause_nocancel (); |
df47504c UD |
430 | } |
431 | ||
432 | oldval = mutex->__data.__lock; | |
433 | ||
434 | assert (robust || (oldval & FUTEX_OWNER_DIED) == 0); | |
435 | } | |
436 | ||
a1ffb40e | 437 | if (__glibc_unlikely (oldval & FUTEX_OWNER_DIED)) |
df47504c UD |
438 | { |
439 | atomic_and (&mutex->__data.__lock, ~FUTEX_OWNER_DIED); | |
440 | ||
441 | /* We got the mutex. */ | |
442 | mutex->__data.__count = 1; | |
443 | /* But it is inconsistent unless marked otherwise. */ | |
444 | mutex->__data.__owner = PTHREAD_MUTEX_INCONSISTENT; | |
445 | ||
8f9450a0 TR |
446 | /* We must not enqueue the mutex before we have acquired it. |
447 | Also see comments at ENQUEUE_MUTEX. */ | |
448 | __asm ("" ::: "memory"); | |
df47504c | 449 | ENQUEUE_MUTEX_PI (mutex); |
8f9450a0 TR |
450 | /* We need to clear op_pending after we enqueue the mutex. */ |
451 | __asm ("" ::: "memory"); | |
df47504c UD |
452 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
453 | ||
454 | /* Note that we deliberately exit here. If we fall | |
455 | through to the end of the function __nusers would be | |
456 | incremented which is not correct because the old owner | |
457 | has to be discounted. If we are not supposed to | |
458 | increment __nusers we actually have to decrement it here. */ | |
184ee940 | 459 | # ifdef NO_INCR |
df47504c | 460 | --mutex->__data.__nusers; |
184ee940 | 461 | # endif |
df47504c UD |
462 | |
463 | return EOWNERDEAD; | |
464 | } | |
465 | ||
466 | if (robust | |
467 | && __builtin_expect (mutex->__data.__owner | |
468 | == PTHREAD_MUTEX_NOTRECOVERABLE, 0)) | |
469 | { | |
470 | /* This mutex is now not recoverable. */ | |
471 | mutex->__data.__count = 0; | |
472 | ||
473 | INTERNAL_SYSCALL_DECL (__err); | |
474 | INTERNAL_SYSCALL (futex, __err, 4, &mutex->__data.__lock, | |
efac1fce | 475 | __lll_private_flag (FUTEX_UNLOCK_PI, |
6de79a49 | 476 | PTHREAD_ROBUST_MUTEX_PSHARED (mutex)), |
efac1fce | 477 | 0, 0); |
df47504c | 478 | |
8f9450a0 TR |
479 | /* To the kernel, this will be visible after the kernel has |
480 | acquired the mutex in the syscall. */ | |
df47504c UD |
481 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
482 | return ENOTRECOVERABLE; | |
483 | } | |
484 | ||
485 | mutex->__data.__count = 1; | |
486 | if (robust) | |
487 | { | |
8f9450a0 TR |
488 | /* We must not enqueue the mutex before we have acquired it. |
489 | Also see comments at ENQUEUE_MUTEX. */ | |
490 | __asm ("" ::: "memory"); | |
df47504c | 491 | ENQUEUE_MUTEX_PI (mutex); |
8f9450a0 TR |
492 | /* We need to clear op_pending after we enqueue the mutex. */ |
493 | __asm ("" ::: "memory"); | |
df47504c UD |
494 | THREAD_SETMEM (THREAD_SELF, robust_head.list_op_pending, NULL); |
495 | } | |
496 | } | |
497 | break; | |
184ee940 | 498 | #endif /* __NR_futex. */ |
df47504c | 499 | |
f17efcb4 UD |
500 | case PTHREAD_MUTEX_PP_RECURSIVE_NP: |
501 | case PTHREAD_MUTEX_PP_ERRORCHECK_NP: | |
502 | case PTHREAD_MUTEX_PP_NORMAL_NP: | |
503 | case PTHREAD_MUTEX_PP_ADAPTIVE_NP: | |
504 | { | |
505 | int kind = mutex->__data.__kind & PTHREAD_MUTEX_KIND_MASK_NP; | |
506 | ||
507 | oldval = mutex->__data.__lock; | |
508 | ||
509 | /* Check whether we already hold the mutex. */ | |
510 | if (mutex->__data.__owner == id) | |
511 | { | |
512 | if (kind == PTHREAD_MUTEX_ERRORCHECK_NP) | |
513 | return EDEADLK; | |
514 | ||
515 | if (kind == PTHREAD_MUTEX_RECURSIVE_NP) | |
516 | { | |
517 | /* Just bump the counter. */ | |
a1ffb40e | 518 | if (__glibc_unlikely (mutex->__data.__count + 1 == 0)) |
f17efcb4 UD |
519 | /* Overflow of the counter. */ |
520 | return EAGAIN; | |
521 | ||
522 | ++mutex->__data.__count; | |
523 | ||
524 | return 0; | |
525 | } | |
526 | } | |
527 | ||
528 | int oldprio = -1, ceilval; | |
529 | do | |
530 | { | |
531 | int ceiling = (oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) | |
532 | >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT; | |
533 | ||
534 | if (__pthread_current_priority () > ceiling) | |
535 | { | |
536 | if (oldprio != -1) | |
537 | __pthread_tpp_change_priority (oldprio, -1); | |
538 | return EINVAL; | |
539 | } | |
540 | ||
6de79a49 | 541 | int retval = __pthread_tpp_change_priority (oldprio, ceiling); |
f17efcb4 UD |
542 | if (retval) |
543 | return retval; | |
544 | ||
545 | ceilval = ceiling << PTHREAD_MUTEX_PRIO_CEILING_SHIFT; | |
546 | oldprio = ceiling; | |
547 | ||
548 | oldval | |
052757bf | 549 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
f17efcb4 UD |
550 | #ifdef NO_INCR |
551 | ceilval | 2, | |
552 | #else | |
553 | ceilval | 1, | |
554 | #endif | |
555 | ceilval); | |
556 | ||
557 | if (oldval == ceilval) | |
558 | break; | |
559 | ||
560 | do | |
561 | { | |
562 | oldval | |
052757bf | 563 | = atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
f17efcb4 UD |
564 | ceilval | 2, |
565 | ceilval | 1); | |
566 | ||
567 | if ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval) | |
568 | break; | |
569 | ||
570 | if (oldval != ceilval) | |
835abc5c | 571 | lll_futex_wait (&mutex->__data.__lock, ceilval | 2, |
5bd8a249 | 572 | PTHREAD_MUTEX_PSHARED (mutex)); |
f17efcb4 | 573 | } |
052757bf | 574 | while (atomic_compare_and_exchange_val_acq (&mutex->__data.__lock, |
f17efcb4 UD |
575 | ceilval | 2, ceilval) |
576 | != ceilval); | |
577 | } | |
578 | while ((oldval & PTHREAD_MUTEX_PRIO_CEILING_MASK) != ceilval); | |
579 | ||
580 | assert (mutex->__data.__owner == 0); | |
581 | mutex->__data.__count = 1; | |
582 | } | |
583 | break; | |
584 | ||
dcc73a8d UD |
585 | default: |
586 | /* Correct code cannot set any other type. */ | |
587 | return EINVAL; | |
76a50749 UD |
588 | } |
589 | ||
3892d906 | 590 | /* Record the ownership. */ |
3892d906 UD |
591 | mutex->__data.__owner = id; |
592 | #ifndef NO_INCR | |
593 | ++mutex->__data.__nusers; | |
594 | #endif | |
595 | ||
5acf7263 RM |
596 | LIBC_PROBE (mutex_acquired, 1, mutex); |
597 | ||
6de79a49 | 598 | return 0; |
76a50749 | 599 | } |
69431c9a | 600 | #ifndef __pthread_mutex_lock |
fa872e1b | 601 | weak_alias (__pthread_mutex_lock, pthread_mutex_lock) |
4d17e683 | 602 | hidden_def (__pthread_mutex_lock) |
69431c9a | 603 | #endif |
b0948ffd UD |
604 | |
605 | ||
606 | #ifdef NO_INCR | |
607 | void | |
9dd346ff | 608 | __pthread_mutex_cond_lock_adjust (pthread_mutex_t *mutex) |
b0948ffd UD |
609 | { |
610 | assert ((mutex->__data.__kind & PTHREAD_MUTEX_PRIO_INHERIT_NP) != 0); | |
611 | assert ((mutex->__data.__kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP) == 0); | |
612 | assert ((mutex->__data.__kind & PTHREAD_MUTEX_PSHARED_BIT) == 0); | |
613 | ||
614 | /* Record the ownership. */ | |
615 | pid_t id = THREAD_GETMEM (THREAD_SELF, tid); | |
616 | mutex->__data.__owner = id; | |
617 | ||
618 | if (mutex->__data.__kind == PTHREAD_MUTEX_PI_RECURSIVE_NP) | |
619 | ++mutex->__data.__count; | |
620 | } | |
621 | #endif |