blob: 1010f11d4cc09ba6f7042ad0ea7d9612b9b4cbaf [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"
42#include "private/thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
Mathias Agopian7c0c3792011-09-05 23:54:55 -070044extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
45extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
46
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047/* a mutex is implemented as a 32-bit integer holding the following fields
48 *
49 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -080050 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -070052 * 13 shared process-shared flag
53 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054 * 1-0 state lock state (0, 1 or 2)
55 */
56
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010057/* Convenience macro, creates a mask of 'bits' bits that starts from
58 * the 'shift'-th least significant bit in a 32-bit word.
59 *
60 * Examples: FIELD_MASK(0,4) -> 0xf
61 * FIELD_MASK(16,9) -> 0x1ff0000
62 */
63#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010065/* This one is used to create a bit pattern from a given field value */
66#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +010067
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010068/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
69#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010071/* Mutex state:
72 *
73 * 0 for unlocked
74 * 1 for locked, no waiters
75 * 2 for locked, maybe waiters
76 */
77#define MUTEX_STATE_SHIFT 0
78#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010080#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
81#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
82#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
83
84#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
85#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
86#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
87
88#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
89#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
90
91#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
92#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
93#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
94
95/* return true iff the mutex if locked with no waiters */
96#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
97
98/* return true iff the mutex if locked with maybe waiters */
99#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
100
101/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
102#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
103
104/* Mutex counter:
105 *
106 * We need to check for overflow before incrementing, and we also need to
107 * detect when the counter is 0
108 */
109#define MUTEX_COUNTER_SHIFT 2
110#define MUTEX_COUNTER_LEN 11
111#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
112
113#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
114#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
115
116/* Used to increment the counter directly after overflow has been checked */
117#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
118
119/* Returns true iff the counter is 0 */
120#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
121
122/* Mutex shared bit flag
123 *
124 * This flag is set to indicate that the mutex is shared among processes.
125 * This changes the futex opcode we use for futex wait/wake operations
126 * (non-shared operations are much faster).
127 */
128#define MUTEX_SHARED_SHIFT 13
129#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
130
131/* Mutex type:
132 *
133 * We support normal, recursive and errorcheck mutexes.
134 *
135 * The constants defined here *cannot* be changed because they must match
136 * the C library ABI which defines the following initialization values in
137 * <pthread.h>:
138 *
139 * __PTHREAD_MUTEX_INIT_VALUE
140 * __PTHREAD_RECURSIVE_MUTEX_VALUE
141 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
142 */
143#define MUTEX_TYPE_SHIFT 14
144#define MUTEX_TYPE_LEN 2
145#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
146
147#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
148#define MUTEX_TYPE_RECURSIVE 1
149#define MUTEX_TYPE_ERRORCHECK 2
150
151#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
152
153#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
154#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
155#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
156
157/* Mutex owner field:
158 *
159 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800160 * tid of the owning thread. Note that this works because the Linux
161 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100162 *
163 * More specifically, it will wrap to 10000 when it reaches over 32768 for
164 * application processes. You can check this by running the following inside
165 * an adb shell session:
166 *
167 OLDPID=$$;
168 while true; do
169 NEWPID=$(sh -c 'echo $$')
170 if [ "$NEWPID" -gt 32768 ]; then
171 echo "AARGH: new PID $NEWPID is too high!"
172 exit 1
173 fi
174 if [ "$NEWPID" -lt "$OLDPID" ]; then
175 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
176 else
177 echo -n "$NEWPID!"
178 fi
179 OLDPID=$NEWPID
180 done
181
182 * Note that you can run the same example on a desktop Linux system,
183 * the wrapping will also happen at 32768, but will go back to 300 instead.
184 */
185#define MUTEX_OWNER_SHIFT 16
186#define MUTEX_OWNER_LEN 16
187
188#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
189#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
190
191/* Convenience macros.
192 *
193 * These are used to form or modify the bit pattern of a given mutex value
194 */
195
196
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700198/* a mutex attribute holds the following fields
199 *
200 * bits: name description
201 * 0-3 type type of mutex
202 * 4 shared process-shared flag
203 */
204#define MUTEXATTR_TYPE_MASK 0x000f
205#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
207
208int pthread_mutexattr_init(pthread_mutexattr_t *attr)
209{
210 if (attr) {
211 *attr = PTHREAD_MUTEX_DEFAULT;
212 return 0;
213 } else {
214 return EINVAL;
215 }
216}
217
218int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
219{
220 if (attr) {
221 *attr = -1;
222 return 0;
223 } else {
224 return EINVAL;
225 }
226}
227
228int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
229{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700230 if (attr) {
231 int atype = (*attr & MUTEXATTR_TYPE_MASK);
232
233 if (atype >= PTHREAD_MUTEX_NORMAL &&
234 atype <= PTHREAD_MUTEX_ERRORCHECK) {
235 *type = atype;
236 return 0;
237 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238 }
239 return EINVAL;
240}
241
242int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
243{
244 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
245 type <= PTHREAD_MUTEX_ERRORCHECK ) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700246 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800247 return 0;
248 }
249 return EINVAL;
250}
251
252/* process-shared mutexes are not supported at the moment */
253
254int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
255{
256 if (!attr)
257 return EINVAL;
258
Mathias Agopianb7681162009-07-13 22:00:33 -0700259 switch (pshared) {
260 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700261 *attr &= ~MUTEXATTR_SHARED_MASK;
262 return 0;
263
Mathias Agopianb7681162009-07-13 22:00:33 -0700264 case PTHREAD_PROCESS_SHARED:
265 /* our current implementation of pthread actually supports shared
266 * mutexes but won't cleanup if a process dies with the mutex held.
267 * Nevertheless, it's better than nothing. Shared mutexes are used
268 * by surfaceflinger and audioflinger.
269 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700270 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700271 return 0;
272 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700273 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274}
275
Elliott Hughesc3f11402013-10-30 14:40:09 -0700276int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700277 if (!attr || !pshared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800278 return EINVAL;
279
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700280 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
281 : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282 return 0;
283}
284
285int pthread_mutex_init(pthread_mutex_t *mutex,
286 const pthread_mutexattr_t *attr)
287{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700288 int value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700290 if (mutex == NULL)
291 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700293 if (__predict_true(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100294 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700295 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800296 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700297
298 if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
299 value |= MUTEX_SHARED_MASK;
300
301 switch (*attr & MUTEXATTR_TYPE_MASK) {
302 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100303 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700304 break;
305 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100306 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700307 break;
308 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100309 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700310 break;
311 default:
312 return EINVAL;
313 }
314
315 mutex->value = value;
316 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800317}
318
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319
320/*
321 * Lock a non-recursive mutex.
322 *
323 * As noted above, there are three states:
324 * 0 (unlocked, no contention)
325 * 1 (locked, no contention)
326 * 2 (locked, contention)
327 *
328 * Non-recursive mutexes don't use the thread-id or counter fields, and the
329 * "type" value is zero, so the only bits that will be set are the ones in
330 * the lock state field.
331 */
332static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100333_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100335 /* convenience shortcuts */
336 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
337 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800338 /*
339 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100340 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
341 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
342 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800343 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100344 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
345 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800346 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800347 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100348 * requires promoting it to state 2 (CONTENDED). We need to
349 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800350 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100351 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800352 * see if we got zero back; if so, we have acquired the lock. If
353 * not, another thread still holds the lock and we wait again.
354 *
355 * The second argument to the __futex_wait() call is compared
356 * against the current value. If it doesn't match, __futex_wait()
357 * returns immediately (otherwise, it sleeps for a time specified
358 * by the third argument; 0 means sleep forever). This ensures
359 * that the mutex is in state 2 when we go to sleep on it, which
360 * guarantees a wake-up call.
361 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100362 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
363 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700365 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366}
367
368/*
369 * Release a non-recursive mutex. The caller is responsible for determining
370 * that we are in fact the owner of this lock.
371 */
372static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100373_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700375 ANDROID_MEMBAR_FULL();
376
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800377 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700378 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100379 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800380 * if it wasn't 1 we have to do some additional work.
381 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100382 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800383 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800384 * Start by releasing the lock. The decrement changed it from
385 * "contended lock" to "uncontended lock", which means we still
386 * hold it, and anybody who tries to sneak in will push it back
387 * to state 2.
388 *
389 * Once we set it to zero the lock is up for grabs. We follow
390 * this with a __futex_wake() to ensure that one of the waiting
391 * threads has a chance to grab it.
392 *
393 * This doesn't cause a race with the swap/wait pair in
394 * _normal_lock(), because the __futex_wait() call there will
395 * return immediately if the mutex value isn't 2.
396 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700397 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800399 /*
400 * Wake up one waiting thread. We don't know which thread will be
401 * woken or when it'll start executing -- futexes make no guarantees
402 * here. There may not even be a thread waiting.
403 *
404 * The newly-woken thread will replace the 0 we just set above
405 * with 2, which means that when it eventually releases the mutex
406 * it will also call FUTEX_WAKE. This results in one extra wake
407 * call whenever a lock is contended, but lets us avoid forgetting
408 * anyone without requiring us to track the number of sleepers.
409 *
410 * It's possible for another thread to sneak in and grab the lock
411 * between the zero assignment above and the wake call below. If
412 * the new thread is "slow" and holds the lock for a while, we'll
413 * wake up a sleeper, which will swap in a 2 and then go back to
414 * sleep since the lock is still held. If the new thread is "fast",
415 * running to completion before we call wake, the thread we
416 * eventually wake will find an unlocked mutex and will execute.
417 * Either way we have correct behavior and nobody is orphaned on
418 * the wait queue.
419 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700420 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800421 }
422}
423
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100424/* This common inlined function is used to increment the counter of an
425 * errorcheck or recursive mutex.
426 *
427 * For errorcheck mutexes, it will return EDEADLK
428 * If the counter overflows, it will return EAGAIN
429 * Otherwise, it atomically increments the counter and returns 0
430 * after providing an acquire barrier.
431 *
432 * mtype is the current mutex type
433 * mvalue is the current mutex value (already loaded)
434 * mutex pointers to the mutex.
435 */
436static __inline__ __attribute__((always_inline)) int
437_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100439 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100440 /* trying to re-lock a mutex we already acquired */
441 return EDEADLK;
442 }
443
444 /* Detect recursive lock overflow and return EAGAIN.
445 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100446 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100447 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100448 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100449 return EAGAIN;
450 }
451
452 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100453 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100454 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100455 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100456 for (;;) {
457 /* increment counter, overflow was already checked */
458 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700459 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100460 /* mutex is still locked, not need for a memory barrier */
461 return 0;
462 }
463 /* the value was changed, this happens when another thread changes
464 * the lower state bits from 1 to 2 to indicate contention. This
465 * cannot change the counter, so simply reload and try again.
466 */
467 mvalue = mutex->value;
468 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800469}
470
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700471__LIBC_HIDDEN__
472int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800473{
Wink Savillea12c5442013-01-08 15:15:45 -0800474 int mvalue, mtype, tid, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800475
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700476 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700477 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800478
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100479 mvalue = mutex->value;
480 mtype = (mvalue & MUTEX_TYPE_MASK);
481 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800482
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700483 /* Handle normal case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700484 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100485 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800486 return 0;
487 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700488
489 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800490 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100491 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100492 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700493
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100494 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700495 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700496
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100497 /* First, if the mutex is unlocked, try to quickly acquire it.
498 * In the optimistic case where this works, set the state to 1 to
499 * indicate locked with no contention */
500 if (mvalue == mtype) {
501 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
502 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
503 ANDROID_MEMBAR_FULL();
504 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700505 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100506 /* argh, the value changed, reload before entering the loop */
507 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700508 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100509
510 for (;;) {
511 int newval;
512
513 /* if the mutex is unlocked, its value should be 'mtype' and
514 * we try to acquire it by setting its owner and state atomically.
515 * NOTE: We put the state to 2 since we _know_ there is contention
516 * when we are in this loop. This ensures all waiters will be
517 * unlocked.
518 */
519 if (mvalue == mtype) {
520 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
521 /* TODO: Change this to __bionic_cmpxchg_acquire when we
522 * implement it to get rid of the explicit memory
523 * barrier below.
524 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700525 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100526 mvalue = mutex->value;
527 continue;
528 }
529 ANDROID_MEMBAR_FULL();
530 return 0;
531 }
532
533 /* the mutex is already locked by another thread, if its state is 1
534 * we will change it to 2 to indicate contention. */
535 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
536 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700537 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100538 mvalue = mutex->value;
539 continue;
540 }
541 mvalue = newval;
542 }
543
544 /* wait until the mutex is unlocked */
545 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
546
547 mvalue = mutex->value;
548 }
549 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800550}
551
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700552int pthread_mutex_lock(pthread_mutex_t *mutex)
553{
554 int err = pthread_mutex_lock_impl(mutex);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700555 if (PTHREAD_DEBUG_ENABLED) {
556 if (!err) {
557 pthread_debug_mutex_lock_check(mutex);
558 }
559 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700560 return err;
561}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800562
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700563__LIBC_HIDDEN__
564int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565{
Wink Savillea12c5442013-01-08 15:15:45 -0800566 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800567
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700568 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700569 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800570
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100571 mvalue = mutex->value;
572 mtype = (mvalue & MUTEX_TYPE_MASK);
573 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800574
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700575 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700576 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100577 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578 return 0;
579 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700580
581 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800582 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100583 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700584 return EPERM;
585
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100586 /* If the counter is > 0, we can simply decrement it atomically.
587 * Since other threads can mutate the lower state bits (and only the
588 * lower state bits), use a cmpxchg to do it.
589 */
590 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
591 for (;;) {
592 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700593 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100594 /* success: we still own the mutex, so no memory barrier */
595 return 0;
596 }
597 /* the value changed, so reload and loop */
598 mvalue = mutex->value;
599 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700600 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100601
602 /* the counter is 0, so we're going to unlock the mutex by resetting
603 * its value to 'unlocked'. We need to perform a swap in order
604 * to read the current state, which will be 2 if there are waiters
605 * to awake.
606 *
607 * TODO: Change this to __bionic_swap_release when we implement it
608 * to get rid of the explicit memory barrier below.
609 */
610 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
611 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700612
613 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100614 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700615 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700616 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700617 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800618}
619
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700620int pthread_mutex_unlock(pthread_mutex_t *mutex)
621{
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700622 if (PTHREAD_DEBUG_ENABLED) {
623 pthread_debug_mutex_unlock_check(mutex);
624 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700625 return pthread_mutex_unlock_impl(mutex);
626}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700628__LIBC_HIDDEN__
629int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800630{
Wink Savillea12c5442013-01-08 15:15:45 -0800631 int mvalue, mtype, tid, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700632
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700633 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700634 return EINVAL;
635
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100636 mvalue = mutex->value;
637 mtype = (mvalue & MUTEX_TYPE_MASK);
638 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700639
640 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700641 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800642 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100643 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
644 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
645 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700646 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800647 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700648 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700649
650 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800651 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700652
653 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800654 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100655 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100656 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700657
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100658 /* Same as pthread_mutex_lock, except that we don't want to wait, and
659 * the only operation that can succeed is a single cmpxchg to acquire the
660 * lock if it is released / not owned by anyone. No need for a complex loop.
661 */
662 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
663 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700664
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700665 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100666 ANDROID_MEMBAR_FULL();
667 return 0;
668 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700669
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100670 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800671}
672
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700673int pthread_mutex_trylock(pthread_mutex_t *mutex)
674{
675 int err = pthread_mutex_trylock_impl(mutex);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700676 if (PTHREAD_DEBUG_ENABLED) {
677 if (!err) {
678 pthread_debug_mutex_lock_check(mutex);
679 }
680 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700681 return err;
682}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700684/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
685 * milliseconds.
686 */
Elliott Hughesc3f11402013-10-30 14:40:09 -0700687static void __timespec_to_relative_msec(timespec* abstime, unsigned msecs, clockid_t clock) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700688 clock_gettime(clock, abstime);
689 abstime->tv_sec += msecs/1000;
690 abstime->tv_nsec += (msecs%1000)*1000000;
691 if (abstime->tv_nsec >= 1000000000) {
692 abstime->tv_sec++;
693 abstime->tv_nsec -= 1000000000;
694 }
695}
696
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700697__LIBC_HIDDEN__
698int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700699{
700 clockid_t clock = CLOCK_MONOTONIC;
Elliott Hughesc3f11402013-10-30 14:40:09 -0700701 timespec abstime;
702 timespec ts;
Wink Savillea12c5442013-01-08 15:15:45 -0800703 int mvalue, mtype, tid, shared;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700704
705 /* compute absolute expiration time */
706 __timespec_to_relative_msec(&abstime, msecs, clock);
707
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700708 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700709 return EINVAL;
710
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100711 mvalue = mutex->value;
712 mtype = (mvalue & MUTEX_TYPE_MASK);
713 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700714
715 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700716 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800717 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100718 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
719 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
720 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
721
722 /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */
723 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700724 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800725 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700726 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700727
728 /* loop while needed */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100729 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700730 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
731 return EBUSY;
732
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100733 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800734 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700735 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700736 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800737 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700738
739 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800740 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100741 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100742 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700743
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100744 /* the following implements the same loop than pthread_mutex_lock_impl
745 * but adds checks to ensure that the operation never exceeds the
746 * absolute expiration time.
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700747 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100748 mtype |= shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700749
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100750 /* first try a quick lock */
751 if (mvalue == mtype) {
752 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700753 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100754 ANDROID_MEMBAR_FULL();
755 return 0;
756 }
757 mvalue = mutex->value;
758 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700759
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700760 for (;;) {
Elliott Hughesc3f11402013-10-30 14:40:09 -0700761 timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700762
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100763 /* if the value is 'unlocked', try to acquire it directly */
764 /* NOTE: put state to 2 since we know there is contention */
765 if (mvalue == mtype) /* unlocked */ {
766 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
767 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
768 ANDROID_MEMBAR_FULL();
769 return 0;
770 }
771 /* the value changed before we could lock it. We need to check
772 * the time to avoid livelocks, reload the value, then loop again. */
773 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
774 return EBUSY;
775
776 mvalue = mutex->value;
777 continue;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700778 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700779
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100780 /* The value is locked. If 'uncontended', try to switch its state
781 * to 'contented' to ensure we get woken up later. */
782 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
783 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
784 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
785 /* this failed because the value changed, reload it */
786 mvalue = mutex->value;
787 } else {
788 /* this succeeded, update mvalue */
789 mvalue = newval;
790 }
791 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700792
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100793 /* check time and update 'ts' */
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700794 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
795 return EBUSY;
796
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100797 /* Only wait to be woken up if the state is '2', otherwise we'll
798 * simply loop right now. This can happen when the second cmpxchg
799 * in our loop failed because the mutex was unlocked by another
800 * thread.
801 */
802 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
803 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) {
804 return EBUSY;
805 }
806 mvalue = mutex->value;
807 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700808 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100809 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700810}
811
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700812int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
813{
814 int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700815 if (PTHREAD_DEBUG_ENABLED) {
816 if (!err) {
817 pthread_debug_mutex_lock_check(mutex);
818 }
819 }
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700820 return err;
821}
822
823int pthread_mutex_destroy(pthread_mutex_t *mutex)
824{
825 int ret;
826
827 /* use trylock to ensure that the mutex value is
828 * valid and is not already locked. */
829 ret = pthread_mutex_trylock_impl(mutex);
830 if (ret != 0)
831 return ret;
832
833 mutex->value = 0xdead10cc;
834 return 0;
835}