blob: 40f1ed2bb6c67d079ecfde418360b5221be2e154 [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>
Elliott Hughes84114c82013-07-17 13:33:19 -070033#include <sys/mman.h>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010034#include <unistd.h>
35
Pierre Peifferd0c884d2012-02-22 16:40:15 +010036#include "pthread_internal.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070037
38#include "private/bionic_atomic_inline.h"
Elliott Hughes04303f52014-09-18 16:11:59 -070039#include "private/bionic_constants.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070040#include "private/bionic_futex.h"
Elliott Hughes04303f52014-09-18 16:11:59 -070041#include "private/bionic_time_conversions.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070042#include "private/bionic_tls.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043
Brigid Smitha406ee62014-07-21 15:38:06 -070044#include "private/bionic_systrace.h"
45
Mathias Agopian7c0c3792011-09-05 23:54:55 -070046extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
47extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
48
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049/* a mutex is implemented as a 32-bit integer holding the following fields
50 *
51 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -080052 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -070054 * 13 shared process-shared flag
55 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056 * 1-0 state lock state (0, 1 or 2)
57 */
58
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010059/* Convenience macro, creates a mask of 'bits' bits that starts from
60 * the 'shift'-th least significant bit in a 32-bit word.
61 *
62 * Examples: FIELD_MASK(0,4) -> 0xf
63 * FIELD_MASK(16,9) -> 0x1ff0000
64 */
65#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010067/* This one is used to create a bit pattern from a given field value */
68#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +010069
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010070/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
71#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010073/* Mutex state:
74 *
75 * 0 for unlocked
76 * 1 for locked, no waiters
77 * 2 for locked, maybe waiters
78 */
79#define MUTEX_STATE_SHIFT 0
80#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +010082#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
83#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
84#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
85
86#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
87#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
88#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
89
90#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
91#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
92
93#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
94#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
95#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
96
97/* return true iff the mutex if locked with no waiters */
98#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
99
100/* return true iff the mutex if locked with maybe waiters */
101#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
102
103/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
104#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
105
106/* Mutex counter:
107 *
108 * We need to check for overflow before incrementing, and we also need to
109 * detect when the counter is 0
110 */
111#define MUTEX_COUNTER_SHIFT 2
112#define MUTEX_COUNTER_LEN 11
113#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
114
115#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
116#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
117
118/* Used to increment the counter directly after overflow has been checked */
119#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
120
121/* Returns true iff the counter is 0 */
122#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
123
124/* Mutex shared bit flag
125 *
126 * This flag is set to indicate that the mutex is shared among processes.
127 * This changes the futex opcode we use for futex wait/wake operations
128 * (non-shared operations are much faster).
129 */
130#define MUTEX_SHARED_SHIFT 13
131#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
132
133/* Mutex type:
134 *
135 * We support normal, recursive and errorcheck mutexes.
136 *
137 * The constants defined here *cannot* be changed because they must match
138 * the C library ABI which defines the following initialization values in
139 * <pthread.h>:
140 *
141 * __PTHREAD_MUTEX_INIT_VALUE
142 * __PTHREAD_RECURSIVE_MUTEX_VALUE
143 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
144 */
145#define MUTEX_TYPE_SHIFT 14
146#define MUTEX_TYPE_LEN 2
147#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
148
149#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
150#define MUTEX_TYPE_RECURSIVE 1
151#define MUTEX_TYPE_ERRORCHECK 2
152
153#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
154
155#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
156#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
157#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
158
159/* Mutex owner field:
160 *
161 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800162 * tid of the owning thread. Note that this works because the Linux
163 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100164 *
165 * More specifically, it will wrap to 10000 when it reaches over 32768 for
166 * application processes. You can check this by running the following inside
167 * an adb shell session:
168 *
169 OLDPID=$$;
170 while true; do
171 NEWPID=$(sh -c 'echo $$')
172 if [ "$NEWPID" -gt 32768 ]; then
173 echo "AARGH: new PID $NEWPID is too high!"
174 exit 1
175 fi
176 if [ "$NEWPID" -lt "$OLDPID" ]; then
177 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
178 else
179 echo -n "$NEWPID!"
180 fi
181 OLDPID=$NEWPID
182 done
183
184 * Note that you can run the same example on a desktop Linux system,
185 * the wrapping will also happen at 32768, but will go back to 300 instead.
186 */
187#define MUTEX_OWNER_SHIFT 16
188#define MUTEX_OWNER_LEN 16
189
190#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
191#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
192
193/* Convenience macros.
194 *
195 * These are used to form or modify the bit pattern of a given mutex value
196 */
197
198
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700200/* a mutex attribute holds the following fields
201 *
202 * bits: name description
203 * 0-3 type type of mutex
204 * 4 shared process-shared flag
205 */
206#define MUTEXATTR_TYPE_MASK 0x000f
207#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208
209
210int pthread_mutexattr_init(pthread_mutexattr_t *attr)
211{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800212 *attr = PTHREAD_MUTEX_DEFAULT;
213 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214}
215
216int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
217{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800218 *attr = -1;
219 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220}
221
Elliott Hughes39b644a2014-03-04 10:55:39 -0800222int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type_p)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800224 int type = (*attr & MUTEXATTR_TYPE_MASK);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700225
Elliott Hughes39b644a2014-03-04 10:55:39 -0800226 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK) {
227 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228 }
Elliott Hughes39b644a2014-03-04 10:55:39 -0800229
230 *type_p = type;
231 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800232}
233
234int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
235{
Elliott Hughes39b644a2014-03-04 10:55:39 -0800236 if (type < PTHREAD_MUTEX_NORMAL || type > PTHREAD_MUTEX_ERRORCHECK ) {
237 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238 }
Elliott Hughes39b644a2014-03-04 10:55:39 -0800239
240 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
241 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242}
243
244/* process-shared mutexes are not supported at the moment */
245
246int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
247{
Mathias Agopianb7681162009-07-13 22:00:33 -0700248 switch (pshared) {
249 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700250 *attr &= ~MUTEXATTR_SHARED_MASK;
251 return 0;
252
Mathias Agopianb7681162009-07-13 22:00:33 -0700253 case PTHREAD_PROCESS_SHARED:
254 /* our current implementation of pthread actually supports shared
255 * mutexes but won't cleanup if a process dies with the mutex held.
256 * Nevertheless, it's better than nothing. Shared mutexes are used
257 * by surfaceflinger and audioflinger.
258 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700259 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700260 return 0;
261 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700262 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263}
264
Elliott Hughesc3f11402013-10-30 14:40:09 -0700265int pthread_mutexattr_getpshared(const pthread_mutexattr_t* attr, int* pshared) {
Elliott Hughes39b644a2014-03-04 10:55:39 -0800266 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267 return 0;
268}
269
Elliott Hughesdff72032013-12-11 14:54:00 -0800270int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) {
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700271 if (__predict_true(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100272 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700273 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800274 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700275
Elliott Hughesdff72032013-12-11 14:54:00 -0800276 int value = 0;
277 if ((*attr & MUTEXATTR_SHARED_MASK) != 0) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700278 value |= MUTEX_SHARED_MASK;
Elliott Hughesdff72032013-12-11 14:54:00 -0800279 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700280
281 switch (*attr & MUTEXATTR_TYPE_MASK) {
282 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100283 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700284 break;
285 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100286 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700287 break;
288 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100289 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700290 break;
291 default:
292 return EINVAL;
293 }
294
295 mutex->value = value;
296 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297}
298
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800299
300/*
301 * Lock a non-recursive mutex.
302 *
303 * As noted above, there are three states:
304 * 0 (unlocked, no contention)
305 * 1 (locked, no contention)
306 * 2 (locked, contention)
307 *
308 * Non-recursive mutexes don't use the thread-id or counter fields, and the
309 * "type" value is zero, so the only bits that will be set are the ones in
310 * the lock state field.
311 */
Elliott Hughes62499602014-05-28 20:30:40 -0700312static inline void _normal_lock(pthread_mutex_t* mutex, int shared) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100313 /* convenience shortcuts */
314 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
315 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800316 /*
317 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100318 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
319 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
320 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800321 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100322 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
323 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800325 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100326 * requires promoting it to state 2 (CONTENDED). We need to
327 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800328 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100329 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800330 * see if we got zero back; if so, we have acquired the lock. If
331 * not, another thread still holds the lock and we wait again.
332 *
333 * The second argument to the __futex_wait() call is compared
334 * against the current value. If it doesn't match, __futex_wait()
335 * returns immediately (otherwise, it sleeps for a time specified
336 * by the third argument; 0 means sleep forever). This ensures
337 * that the mutex is in state 2 when we go to sleep on it, which
338 * guarantees a wake-up call.
339 */
Brigid Smitha406ee62014-07-21 15:38:06 -0700340
341 ScopedTrace trace("Contending for pthread mutex");
342
343
Elliott Hughes62499602014-05-28 20:30:40 -0700344 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
345 __futex_wait_ex(&mutex->value, shared, locked_contended, NULL);
346 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700348 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349}
350
351/*
352 * Release a non-recursive mutex. The caller is responsible for determining
353 * that we are in fact the owner of this lock.
354 */
Elliott Hughes62499602014-05-28 20:30:40 -0700355static inline void _normal_unlock(pthread_mutex_t* mutex, int shared) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700356 ANDROID_MEMBAR_FULL();
357
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800358 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700359 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100360 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800361 * if it wasn't 1 we have to do some additional work.
362 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100363 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800365 * Start by releasing the lock. The decrement changed it from
366 * "contended lock" to "uncontended lock", which means we still
367 * hold it, and anybody who tries to sneak in will push it back
368 * to state 2.
369 *
370 * Once we set it to zero the lock is up for grabs. We follow
371 * this with a __futex_wake() to ensure that one of the waiting
372 * threads has a chance to grab it.
373 *
374 * This doesn't cause a race with the swap/wait pair in
375 * _normal_lock(), because the __futex_wait() call there will
376 * return immediately if the mutex value isn't 2.
377 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700378 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800379
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800380 /*
381 * Wake up one waiting thread. We don't know which thread will be
382 * woken or when it'll start executing -- futexes make no guarantees
383 * here. There may not even be a thread waiting.
384 *
385 * The newly-woken thread will replace the 0 we just set above
386 * with 2, which means that when it eventually releases the mutex
387 * it will also call FUTEX_WAKE. This results in one extra wake
388 * call whenever a lock is contended, but lets us avoid forgetting
389 * anyone without requiring us to track the number of sleepers.
390 *
391 * It's possible for another thread to sneak in and grab the lock
392 * between the zero assignment above and the wake call below. If
393 * the new thread is "slow" and holds the lock for a while, we'll
394 * wake up a sleeper, which will swap in a 2 and then go back to
395 * sleep since the lock is still held. If the new thread is "fast",
396 * running to completion before we call wake, the thread we
397 * eventually wake will find an unlocked mutex and will execute.
398 * Either way we have correct behavior and nobody is orphaned on
399 * the wait queue.
400 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700401 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402 }
403}
404
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100405/* This common inlined function is used to increment the counter of an
406 * errorcheck or recursive mutex.
407 *
408 * For errorcheck mutexes, it will return EDEADLK
409 * If the counter overflows, it will return EAGAIN
410 * Otherwise, it atomically increments the counter and returns 0
411 * after providing an acquire barrier.
412 *
413 * mtype is the current mutex type
414 * mvalue is the current mutex value (already loaded)
415 * mutex pointers to the mutex.
416 */
Elliott Hughes62499602014-05-28 20:30:40 -0700417static inline __always_inline int _recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100418 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100419 /* trying to re-lock a mutex we already acquired */
420 return EDEADLK;
421 }
422
423 /* Detect recursive lock overflow and return EAGAIN.
424 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100425 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100426 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100427 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100428 return EAGAIN;
429 }
430
431 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100432 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100433 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100434 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100435 for (;;) {
436 /* increment counter, overflow was already checked */
437 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700438 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100439 /* mutex is still locked, not need for a memory barrier */
440 return 0;
441 }
442 /* the value was changed, this happens when another thread changes
443 * the lower state bits from 1 to 2 to indicate contention. This
444 * cannot change the counter, so simply reload and try again.
445 */
446 mvalue = mutex->value;
447 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448}
449
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700450int pthread_mutex_lock(pthread_mutex_t* mutex) {
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
Brigid Smithff03a7a2014-05-28 11:45:48 -0700457 /* Handle non-recursive 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
Brigid Smitha406ee62014-07-21 15:38:06 -0700484 ScopedTrace trace("Contending for pthread mutex");
485
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100486 for (;;) {
487 int newval;
488
489 /* if the mutex is unlocked, its value should be 'mtype' and
490 * we try to acquire it by setting its owner and state atomically.
491 * NOTE: We put the state to 2 since we _know_ there is contention
492 * when we are in this loop. This ensures all waiters will be
493 * unlocked.
494 */
495 if (mvalue == mtype) {
496 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
497 /* TODO: Change this to __bionic_cmpxchg_acquire when we
498 * implement it to get rid of the explicit memory
499 * barrier below.
500 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700501 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100502 mvalue = mutex->value;
503 continue;
504 }
505 ANDROID_MEMBAR_FULL();
506 return 0;
507 }
508
509 /* the mutex is already locked by another thread, if its state is 1
510 * we will change it to 2 to indicate contention. */
511 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
512 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700513 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100514 mvalue = mutex->value;
515 continue;
516 }
517 mvalue = newval;
518 }
519
520 /* wait until the mutex is unlocked */
521 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
522
523 mvalue = mutex->value;
524 }
525 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800526}
527
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700528int pthread_mutex_unlock(pthread_mutex_t* mutex) {
Wink Savillea12c5442013-01-08 15:15:45 -0800529 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800530
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100531 mvalue = mutex->value;
532 mtype = (mvalue & MUTEX_TYPE_MASK);
533 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700535 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700536 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100537 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800538 return 0;
539 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700540
541 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800542 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100543 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700544 return EPERM;
545
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100546 /* If the counter is > 0, we can simply decrement it atomically.
547 * Since other threads can mutate the lower state bits (and only the
548 * lower state bits), use a cmpxchg to do it.
549 */
550 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
551 for (;;) {
552 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700553 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100554 /* success: we still own the mutex, so no memory barrier */
555 return 0;
556 }
557 /* the value changed, so reload and loop */
558 mvalue = mutex->value;
559 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700560 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100561
562 /* the counter is 0, so we're going to unlock the mutex by resetting
563 * its value to 'unlocked'. We need to perform a swap in order
564 * to read the current state, which will be 2 if there are waiters
565 * to awake.
566 *
567 * TODO: Change this to __bionic_swap_release when we implement it
568 * to get rid of the explicit memory barrier below.
569 */
570 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
571 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700572
573 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100574 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700575 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700576 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700577 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800578}
579
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700580int pthread_mutex_trylock(pthread_mutex_t* mutex) {
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700581 int mvalue = mutex->value;
582 int mtype = (mvalue & MUTEX_TYPE_MASK);
583 int shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700584
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700585 // Handle common case first.
586 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100587 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
588 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
589 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700590 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800591 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700592 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700593
594 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800595 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700596
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700597 // Do we already own this recursive or error-check mutex?
598 pid_t tid = __get_thread()->tid;
599 if (tid == MUTEX_OWNER_FROM_BITS(mvalue)) {
600 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
601 return EBUSY;
602 }
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100603 return _recursive_increment(mutex, mvalue, mtype);
Elliott Hughes5b1111a2014-10-24 19:33:11 -0700604 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700605
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100606 /* Same as pthread_mutex_lock, except that we don't want to wait, and
607 * the only operation that can succeed is a single cmpxchg to acquire the
608 * lock if it is released / not owned by anyone. No need for a complex loop.
609 */
610 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
611 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700612
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700613 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100614 ANDROID_MEMBAR_FULL();
615 return 0;
616 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700617
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100618 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800619}
620
Elliott Hughes04303f52014-09-18 16:11:59 -0700621static int __pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_ts, clockid_t clock) {
Elliott Hughes0e714a52014-03-03 16:42:47 -0800622 timespec ts;
623
624 int mvalue = mutex->value;
625 int mtype = (mvalue & MUTEX_TYPE_MASK);
626 int shared = (mvalue & MUTEX_SHARED_MASK);
627
628 // Handle common case first.
629 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
630 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
631 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
632 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
633
634 // Fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0.
635 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
636 ANDROID_MEMBAR_FULL();
637 return 0;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700638 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700639
Brigid Smitha406ee62014-07-21 15:38:06 -0700640 ScopedTrace trace("Contending for timed pthread mutex");
641
Elliott Hughes0e714a52014-03-03 16:42:47 -0800642 // Loop while needed.
643 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
Elliott Hughes04303f52014-09-18 16:11:59 -0700644 if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
Elliott Hughes0e714a52014-03-03 16:42:47 -0800645 return ETIMEDOUT;
646 }
647 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
648 }
649 ANDROID_MEMBAR_FULL();
650 return 0;
651 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700652
Elliott Hughes0e714a52014-03-03 16:42:47 -0800653 // Do we already own this recursive or error-check mutex?
654 pid_t tid = __get_thread()->tid;
655 if (tid == MUTEX_OWNER_FROM_BITS(mvalue)) {
656 return _recursive_increment(mutex, mvalue, mtype);
657 }
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700658
Elliott Hughes0e714a52014-03-03 16:42:47 -0800659 // The following implements the same loop as pthread_mutex_lock_impl
660 // but adds checks to ensure that the operation never exceeds the
661 // absolute expiration time.
662 mtype |= shared;
663
664 // First try a quick lock.
665 if (mvalue == mtype) {
666 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
667 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
668 ANDROID_MEMBAR_FULL();
669 return 0;
670 }
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100671 mvalue = mutex->value;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800672 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700673
Brigid Smitha406ee62014-07-21 15:38:06 -0700674 ScopedTrace trace("Contending for timed pthread mutex");
675
Elliott Hughes0e714a52014-03-03 16:42:47 -0800676 while (true) {
677 // If the value is 'unlocked', try to acquire it directly.
678 // NOTE: put state to 2 since we know there is contention.
679 if (mvalue == mtype) { // Unlocked.
680 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
681 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700682 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700683 return 0;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800684 }
685 // The value changed before we could lock it. We need to check
686 // the time to avoid livelocks, reload the value, then loop again.
Elliott Hughes04303f52014-09-18 16:11:59 -0700687 if (!timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
Elliott Hughes0e714a52014-03-03 16:42:47 -0800688 return ETIMEDOUT;
689 }
690
691 mvalue = mutex->value;
692 continue;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800693 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700694
Elliott Hughes0e714a52014-03-03 16:42:47 -0800695 // The value is locked. If 'uncontended', try to switch its state
696 // to 'contented' to ensure we get woken up later.
697 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
698 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
699 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
700 // This failed because the value changed, reload it.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100701 mvalue = mutex->value;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800702 } else {
703 // This succeeded, update mvalue.
704 mvalue = newval;
705 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100706 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700707
Elliott Hughes0e714a52014-03-03 16:42:47 -0800708 // Check time and update 'ts'.
Elliott Hughes04303f52014-09-18 16:11:59 -0700709 if (timespec_from_absolute_timespec(ts, *abs_ts, clock)) {
Elliott Hughes0e714a52014-03-03 16:42:47 -0800710 return ETIMEDOUT;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700711 }
Elliott Hughes0e714a52014-03-03 16:42:47 -0800712
713 // Only wait to be woken up if the state is '2', otherwise we'll
714 // simply loop right now. This can happen when the second cmpxchg
715 // in our loop failed because the mutex was unlocked by another thread.
716 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
717 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == -ETIMEDOUT) {
718 return ETIMEDOUT;
719 }
720 mvalue = mutex->value;
721 }
722 }
723 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700724}
725
Elliott Hughes0e714a52014-03-03 16:42:47 -0800726#if !defined(__LP64__)
727extern "C" int pthread_mutex_lock_timeout_np(pthread_mutex_t* mutex, unsigned ms) {
728 timespec abs_timeout;
729 clock_gettime(CLOCK_MONOTONIC, &abs_timeout);
730 abs_timeout.tv_sec += ms / 1000;
731 abs_timeout.tv_nsec += (ms % 1000) * 1000000;
Elliott Hughes04303f52014-09-18 16:11:59 -0700732 if (abs_timeout.tv_nsec >= NS_PER_S) {
Elliott Hughes0e714a52014-03-03 16:42:47 -0800733 abs_timeout.tv_sec++;
Elliott Hughes04303f52014-09-18 16:11:59 -0700734 abs_timeout.tv_nsec -= NS_PER_S;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800735 }
736
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700737 int error = __pthread_mutex_timedlock(mutex, &abs_timeout, CLOCK_MONOTONIC);
738 if (error == ETIMEDOUT) {
739 error = EBUSY;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800740 }
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700741 return error;
Elliott Hughes0e714a52014-03-03 16:42:47 -0800742}
743#endif
744
745int pthread_mutex_timedlock(pthread_mutex_t* mutex, const timespec* abs_timeout) {
746 return __pthread_mutex_timedlock(mutex, abs_timeout, CLOCK_REALTIME);
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700747}
748
Elliott Hughes07f1ded2014-05-14 11:35:49 -0700749int pthread_mutex_destroy(pthread_mutex_t* mutex) {
750 // Use trylock to ensure that the mutex is valid and not already locked.
751 int error = pthread_mutex_trylock(mutex);
752 if (error != 0) {
753 return error;
754 }
755 mutex->value = 0xdead10cc;
756 return 0;
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700757}