blob: c4cd9e0e46c7bc62630d31e901fdaf653b3b9a7c [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Pierre Peifferd0c884d2012-02-22 16:40:15 +010028
Elliott Hughes6f94de32013-02-12 06:06:22 +000029#include <pthread.h>
Elliott Hughes3e898472013-02-12 16:40:24 +000030
31#include <errno.h>
32#include <limits.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033#include <sys/atomics.h>
Elliott Hughes84114c82013-07-17 13:33:19 -070034#include <sys/mman.h>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010035#include <unistd.h>
36
Pierre Peifferd0c884d2012-02-22 16:40:15 +010037#include "pthread_internal.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070038
39#include "private/bionic_atomic_inline.h"
40#include "private/bionic_futex.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070041#include "private/bionic_tls.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Mathias Agopian7c0c3792011-09-05 23:54:55 -070043extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
44extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
45
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046/* a mutex is implemented as a 32-bit integer holding the following fields
47 *
48 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -080049 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -070051 * 13 shared process-shared flag
52 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053 * 1-0 state lock state (0, 1 or 2)
54 */
55
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010056/* Convenience macro, creates a mask of 'bits' bits that starts from
57 * the 'shift'-th least significant bit in a 32-bit word.
58 *
59 * Examples: FIELD_MASK(0,4) -> 0xf
60 * FIELD_MASK(16,9) -> 0x1ff0000
61 */
62#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010064/* This one is used to create a bit pattern from a given field value */
65#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +010066
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010067/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
68#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010070/* Mutex state:
71 *
72 * 0 for unlocked
73 * 1 for locked, no waiters
74 * 2 for locked, maybe waiters
75 */
76#define MUTEX_STATE_SHIFT 0
77#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010079#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
80#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
81#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
82
83#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
84#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
85#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
86
87#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
88#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
89
90#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
91#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
92#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
93
94/* return true iff the mutex if locked with no waiters */
95#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
96
97/* return true iff the mutex if locked with maybe waiters */
98#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
99
100/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
101#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
102
103/* Mutex counter:
104 *
105 * We need to check for overflow before incrementing, and we also need to
106 * detect when the counter is 0
107 */
108#define MUTEX_COUNTER_SHIFT 2
109#define MUTEX_COUNTER_LEN 11
110#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
111
112#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
113#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
114
115/* Used to increment the counter directly after overflow has been checked */
116#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
117
118/* Returns true iff the counter is 0 */
119#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
120
121/* Mutex shared bit flag
122 *
123 * This flag is set to indicate that the mutex is shared among processes.
124 * This changes the futex opcode we use for futex wait/wake operations
125 * (non-shared operations are much faster).
126 */
127#define MUTEX_SHARED_SHIFT 13
128#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
129
130/* Mutex type:
131 *
132 * We support normal, recursive and errorcheck mutexes.
133 *
134 * The constants defined here *cannot* be changed because they must match
135 * the C library ABI which defines the following initialization values in
136 * <pthread.h>:
137 *
138 * __PTHREAD_MUTEX_INIT_VALUE
139 * __PTHREAD_RECURSIVE_MUTEX_VALUE
140 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
141 */
142#define MUTEX_TYPE_SHIFT 14
143#define MUTEX_TYPE_LEN 2
144#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
145
146#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
147#define MUTEX_TYPE_RECURSIVE 1
148#define MUTEX_TYPE_ERRORCHECK 2
149
150#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
151
152#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
153#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
154#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
155
156/* Mutex owner field:
157 *
158 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800159 * tid of the owning thread. Note that this works because the Linux
160 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100161 *
162 * More specifically, it will wrap to 10000 when it reaches over 32768 for
163 * application processes. You can check this by running the following inside
164 * an adb shell session:
165 *
166 OLDPID=$$;
167 while true; do
168 NEWPID=$(sh -c 'echo $$')
169 if [ "$NEWPID" -gt 32768 ]; then
170 echo "AARGH: new PID $NEWPID is too high!"
171 exit 1
172 fi
173 if [ "$NEWPID" -lt "$OLDPID" ]; then
174 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
175 else
176 echo -n "$NEWPID!"
177 fi
178 OLDPID=$NEWPID
179 done
180
181 * Note that you can run the same example on a desktop Linux system,
182 * the wrapping will also happen at 32768, but will go back to 300 instead.
183 */
184#define MUTEX_OWNER_SHIFT 16
185#define MUTEX_OWNER_LEN 16
186
187#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
188#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
189
190/* Convenience macros.
191 *
192 * These are used to form or modify the bit pattern of a given mutex value
193 */
194
195
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700197/* a mutex attribute holds the following fields
198 *
199 * bits: name description
200 * 0-3 type type of mutex
201 * 4 shared process-shared flag
202 */
203#define MUTEXATTR_TYPE_MASK 0x000f
204#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
206
207int pthread_mutexattr_init(pthread_mutexattr_t *attr)
208{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800209 *attr = PTHREAD_MUTEX_DEFAULT;
210 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211}
212
213int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
214{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800215 *attr = -1;
216 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217}
218
Elliott Hughes39b644a2014-03-04 10:55:39 -0800219int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800221 int type = (*attr & MUTEXATTR_TYPE_MASK);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700222
Elliott Hughes39b644a2014-03-04 10:55:39 -0800223 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) {
224 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225 }
Elliott Hughes39b644a2014-03-04 10:55:39 -0800226
227 *type_p = type;
228 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229}
230
231int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
232{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800233 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) {
234 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235 }
Elliott Hughes39b644a2014-03-04 10:55:39 -0800236
237 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
238 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239}
240
241/* process-shared mutexes are not supported at the moment */
242
243int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
244{
Mathias Agopianb7681162009-07-13 22:00:33 -0700245 switch (pshared) {
246 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700247 *attr &= ~MUTEXATTR_SHARED_MASK;
248 return 0;
249
Mathias Agopianb7681162009-07-13 22:00:33 -0700250 case PTHREAD_PROCESS_SHARED:
251 /* our current implementation of pthread actually supports shared
252 * mutexes but won't cleanup if a process dies with the mutex held.
253 * Nevertheless, it's better than nothing. Shared mutexes are used
254 * by surfaceflinger and audioflinger.
255 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700256 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700257 return 0;
258 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700259 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260}
261
Elliott Hughesc3f11402013-10-30 14:40:09 -0700262int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) {
Elliott Hughes39b644a2014-03-04 10:55:39 -0800263 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264 return 0;
265}
266
Elliott Hughesdff72032013-12-11 14:54:00 -0800267int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) {
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700268 if (__predict_true(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100269 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700270 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800271 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700272
Elliott Hughesdff72032013-12-11 14:54:00 -0800273 int value = 0;
274 if ((*attr & MUTEXATTR_SHARED_MASK) != 0) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700275 value |= MUTEX_SHARED_MASK;
Elliott Hughesdff72032013-12-11 14:54:00 -0800276 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700277
278 switch (*attr & MUTEXATTR_TYPE_MASK) {
279 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100280 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700281 break;
282 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100283 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700284 break;
285 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100286 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700287 break;
288 default:
289 return EINVAL;
290 }
291
292 mutex->value = value;
293 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800294}
295
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296
297/*
298 * Lock a non-recursive mutex.
299 *
300 * As noted above, there are three states:
301 * 0 (unlocked, no contention)
302 * 1 (locked, no contention)
303 * 2 (locked, contention)
304 *
305 * Non-recursive mutexes don't use the thread-id or counter fields, and the
306 * "type" value is zero, so the only bits that will be set are the ones in
307 * the lock state field.
308 */
309static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100310_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100312 /* convenience shortcuts */
313 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
314 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800315 /*
316 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100317 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
318 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
319 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800320 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100321 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
322 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800324 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100325 * requires promoting it to state 2 (CONTENDED). We need to
326 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800327 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100328 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800329 * see if we got zero back; if so, we have acquired the lock. If
330 * not, another thread still holds the lock and we wait again.
331 *
332 * The second argument to the __futex_wait() call is compared
333 * against the current value. If it doesn't match, __futex_wait()
334 * returns immediately (otherwise, it sleeps for a time specified
335 * by the third argument; 0 means sleep forever). This ensures
336 * that the mutex is in state 2 when we go to sleep on it, which
337 * guarantees a wake-up call.
338 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100339 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
340 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800341 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700342 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800343}
344
345/*
346 * Release a non-recursive mutex. The caller is responsible for determining
347 * that we are in fact the owner of this lock.
348 */
349static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100350_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700352 ANDROID_MEMBAR_FULL();
353
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800354 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700355 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100356 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800357 * if it wasn't 1 we have to do some additional work.
358 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100359 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800360 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800361 * Start by releasing the lock. The decrement changed it from
362 * "contended lock" to "uncontended lock", which means we still
363 * hold it, and anybody who tries to sneak in will push it back
364 * to state 2.
365 *
366 * Once we set it to zero the lock is up for grabs. We follow
367 * this with a __futex_wake() to ensure that one of the waiting
368 * threads has a chance to grab it.
369 *
370 * This doesn't cause a race with the swap/wait pair in
371 * _normal_lock(), because the __futex_wait() call there will
372 * return immediately if the mutex value isn't 2.
373 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700374 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800375
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800376 /*
377 * Wake up one waiting thread. We don't know which thread will be
378 * woken or when it'll start executing -- futexes make no guarantees
379 * here. There may not even be a thread waiting.
380 *
381 * The newly-woken thread will replace the 0 we just set above
382 * with 2, which means that when it eventually releases the mutex
383 * it will also call FUTEX_WAKE. This results in one extra wake
384 * call whenever a lock is contended, but lets us avoid forgetting
385 * anyone without requiring us to track the number of sleepers.
386 *
387 * It's possible for another thread to sneak in and grab the lock
388 * between the zero assignment above and the wake call below. If
389 * the new thread is "slow" and holds the lock for a while, we'll
390 * wake up a sleeper, which will swap in a 2 and then go back to
391 * sleep since the lock is still held. If the new thread is "fast",
392 * running to completion before we call wake, the thread we
393 * eventually wake will find an unlocked mutex and will execute.
394 * Either way we have correct behavior and nobody is orphaned on
395 * the wait queue.
396 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700397 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 }
399}
400
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100401/* This common inlined function is used to increment the counter of an
402 * errorcheck or recursive mutex.
403 *
404 * For errorcheck mutexes, it will return EDEADLK
405 * If the counter overflows, it will return EAGAIN
406 * Otherwise, it atomically increments the counter and returns 0
407 * after providing an acquire barrier.
408 *
409 * mtype is the current mutex type
410 * mvalue is the current mutex value (already loaded)
411 * mutex pointers to the mutex.
412 */
413static __inline__ __attribute__((always_inline)) int
414_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100416 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100417 /* trying to re-lock a mutex we already acquired */
418 return EDEADLK;
419 }
420
421 /* Detect recursive lock overflow and return EAGAIN.
422 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100423 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100424 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100425 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100426 return EAGAIN;
427 }
428
429 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100430 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100431 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100432 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100433 for (;;) {
434 /* increment counter, overflow was already checked */
435 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700436 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100437 /* mutex is still locked, not need for a memory barrier */
438 return 0;
439 }
440 /* the value was changed, this happens when another thread changes
441 * the lower state bits from 1 to 2 to indicate contention. This
442 * cannot change the counter, so simply reload and try again.
443 */
444 mvalue = mutex->value;
445 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446}
447
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700448__LIBC_HIDDEN__
449int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450{
Wink Savillea12c5442013-01-08 15:15:45 -0800451 int mvalue, mtype, tid, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800452
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100453 mvalue = mutex->value;
454 mtype = (mvalue & MUTEX_TYPE_MASK);
455 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800456
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700457 /* Handle normal case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700458 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100459 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800460 return 0;
461 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700462
463 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800464 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100465 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100466 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700467
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100468 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700469 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700470
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100471 /* First, if the mutex is unlocked, try to quickly acquire it.
472 * In the optimistic case where this works, set the state to 1 to
473 * indicate locked with no contention */
474 if (mvalue == mtype) {
475 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
476 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
477 ANDROID_MEMBAR_FULL();
478 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700479 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100480 /* argh, the value changed, reload before entering the loop */
481 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700482 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100483
484 for (;;) {
485 int newval;
486
487 /* if the mutex is unlocked, its value should be 'mtype' and
488 * we try to acquire it by setting its owner and state atomically.
489 * NOTE: We put the state to 2 since we _know_ there is contention
490 * when we are in this loop. This ensures all waiters will be
491 * unlocked.
492 */
493 if (mvalue == mtype) {
494 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
495 /* TODO: Change this to __bionic_cmpxchg_acquire when we
496 * implement it to get rid of the explicit memory
497 * barrier below.
498 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700499 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100500 mvalue = mutex->value;
501 continue;
502 }
503 ANDROID_MEMBAR_FULL();
504 return 0;
505 }
506
507 /* the mutex is already locked by another thread, if its state is 1
508 * we will change it to 2 to indicate contention. */
509 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
510 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700511 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100512 mvalue = mutex->value;
513 continue;
514 }
515 mvalue = newval;
516 }
517
518 /* wait until the mutex is unlocked */
519 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
520
521 mvalue = mutex->value;
522 }
523 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800524}
525
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700526int pthread_mutex_lock(pthread_mutex_t *mutex)
527{
528 int err = pthread_mutex_lock_impl(mutex);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700529 if (PTHREAD_DEBUG_ENABLED) {
530 if (!err) {
531 pthread_debug_mutex_lock_check(mutex);
532 }
533 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700534 return err;
535}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800536
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700537__LIBC_HIDDEN__
538int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800539{
Wink Savillea12c5442013-01-08 15:15:45 -0800540 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800541
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100542 mvalue = mutex->value;
543 mtype = (mvalue & MUTEX_TYPE_MASK);
544 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800545
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700546 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700547 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100548 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800549 return 0;
550 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700551
552 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800553 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100554 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700555 return EPERM;
556
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100557 /* If the counter is > 0, we can simply decrement it atomically.
558 * Since other threads can mutate the lower state bits (and only the
559 * lower state bits), use a cmpxchg to do it.
560 */
561 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
562 for (;;) {
563 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700564 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100565 /* success: we still own the mutex, so no memory barrier */
566 return 0;
567 }
568 /* the value changed, so reload and loop */
569 mvalue = mutex->value;
570 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700571 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100572
573 /* the counter is 0, so we're going to unlock the mutex by resetting
574 * its value to 'unlocked'. We need to perform a swap in order
575 * to read the current state, which will be 2 if there are waiters
576 * to awake.
577 *
578 * TODO: Change this to __bionic_swap_release when we implement it
579 * to get rid of the explicit memory barrier below.
580 */
581 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
582 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700583
584 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100585 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700586 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700587 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700588 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800589}
590
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700591int pthread_mutex_unlock(pthread_mutex_t *mutex)
592{
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700593 if (PTHREAD_DEBUG_ENABLED) {
594 pthread_debug_mutex_unlock_check(mutex);
595 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700596 return pthread_mutex_unlock_impl(mutex);
597}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800598
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700599__LIBC_HIDDEN__
600int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800601{
Wink Savillea12c5442013-01-08 15:15:45 -0800602 int mvalue, mtype, tid, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700603
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100604 mvalue = mutex->value;
605 mtype = (mvalue & MUTEX_TYPE_MASK);
606 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700607
608 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700609 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800610 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100611 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
612 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
613 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700614 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800615 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700616 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700617
618 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800619 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700620
621 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800622 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100623 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100624 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700625
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100626 /* Same as pthread_mutex_lock, except that we don't want to wait, and
627 * the only operation that can succeed is a single cmpxchg to acquire the
628 * lock if it is released / not owned by anyone. No need for a complex loop.
629 */
630 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
631 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700632
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700633 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100634 ANDROID_MEMBAR_FULL();
635 return 0;
636 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700637
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100638 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800639}
640
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700641int pthread_mutex_trylock(pthread_mutex_t *mutex)
642{
643 int err = pthread_mutex_trylock_impl(mutex);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700644 if (PTHREAD_DEBUG_ENABLED) {
645 if (!err) {
646 pthread_debug_mutex_lock_check(mutex);
647 }
648 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700649 return err;
650}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800651
Elliott Hughes0e714a52014-03-03 16:42:47 -0800652static int __pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout, clockid_t clock) {
653 timespec ts;
654
655 int mvalue = mutex->value;
656 int mtype = (mvalue & MUTEX_TYPE_MASK);
657 int shared = (mvalue & MUTEX_SHARED_MASK);
658
659 // Handle common case first.
660 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
661 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
662 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
663 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
664
665 // Fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0.
666 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
667 ANDROID_MEMBAR_FULL();
668 return 0;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700669 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700670
Elliott Hughes0e714a52014-03-03 16:42:47 -0800671 // Loop while needed.
672 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
673 if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) {
674 return ETIMEDOUT;
675 }
676 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
677 }
678 ANDROID_MEMBAR_FULL();
679 return 0;
680 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700681
Elliott Hughes0e714a52014-03-03 16:42:47 -0800682 // Do we already own this recursive or error-check mutex?
683 pid_t tid = __get_thread()->tid;
684 if (tid == MUTEX_OWNER_FROM_BITS(mvalue)) {
685 return _recursive_increment(mutex, mvalue, mtype);
686 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700687
Elliott Hughes0e714a52014-03-03 16:42:47 -0800688 // The following implements the same loop as pthread_mutex_lock_impl
689 // but adds checks to ensure that the operation never exceeds the
690 // absolute expiration time.
691 mtype |= shared;
692
693 // First try a quick lock.
694 if (mvalue == mtype) {
695 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
696 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
697 ANDROID_MEMBAR_FULL();
698 return 0;
699 }
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100700 mvalue = mutex->value;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800701 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700702
Elliott Hughes0e714a52014-03-03 16:42:47 -0800703 while (true) {
704 // If the value is 'unlocked', try to acquire it directly.
705 // NOTE: put state to 2 since we know there is contention.
706 if (mvalue == mtype) { // Unlocked.
707 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
708 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700709 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700710 return 0;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800711 }
712 // The value changed before we could lock it. We need to check
713 // the time to avoid livelocks, reload the value, then loop again.
714 if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) {
715 return ETIMEDOUT;
716 }
717
718 mvalue = mutex->value;
719 continue;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800720 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700721
Elliott Hughes0e714a52014-03-03 16:42:47 -0800722 // The value is locked. If 'uncontended', try to switch its state
723 // to 'contented' to ensure we get woken up later.
724 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
725 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
726 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
727 // This failed because the value changed, reload it.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100728 mvalue = mutex->value;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800729 } else {
730 // This succeeded, update mvalue.
731 mvalue = newval;
732 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100733 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700734
Elliott Hughes0e714a52014-03-03 16:42:47 -0800735 // Check time and update 'ts'.
736 if (__timespec_from_absolute(&ts, abs_timeout, clock) < 0) {
737 return ETIMEDOUT;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700738 }
Elliott Hughes0e714a52014-03-03 16:42:47 -0800739
740 // Only wait to be woken up if the state is '2', otherwise we'll
741 // simply loop right now. This can happen when the second cmpxchg
742 // in our loop failed because the mutex was unlocked by another thread.
743 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
744 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == -ETIMEDOUT) {
745 return ETIMEDOUT;
746 }
747 mvalue = mutex->value;
748 }
749 }
750 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700751}
752
Elliott Hughes0e714a52014-03-03 16:42:47 -0800753#if !defined(__LP64__)
754extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex, unsigned ms) {
755 timespec abs_timeout;
756 clock_gettime(CLOCK_MONOTONIC, &abs_timeout);
757 abs_timeout.tv_sec += ms / 1000;
758 abs_timeout.tv_nsec += (ms % 1000) * 1000000;
759 if (abs_timeout.tv_nsec >= 1000000000) {
760 abs_timeout.tv_sec++;
761 abs_timeout.tv_nsec -= 1000000000;
762 }
763
764 int err = __pthread_mutex_timedlock(mutex, &abs_timeout, CLOCK_MONOTONIC);
765 if (err == ETIMEDOUT) {
766 err = EBUSY;
767 }
768 if (PTHREAD_DEBUG_ENABLED) {
769 if (!err) {
770 pthread_debug_mutex_lock_check(mutex);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700771 }
Elliott Hughes0e714a52014-03-03 16:42:47 -0800772 }
773 return err;
774}
775#endif
776
777int pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout) {
778 return __pthread_mutex_timedlock(mutex, abs_timeout, CLOCK_REALTIME);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700779}
780
781int pthread_mutex_destroy(pthread_mutex_t *mutex)
782{
783 int ret;
784
785 /* use trylock to ensure that the mutex value is
786 * valid and is not already locked. */
787 ret = pthread_mutex_trylock_impl(mutex);
788 if (ret != 0)
789 return ret;
790
791 mutex->value = 0xdead10cc;
792 return 0;
793}