blob: 32812b4c99f7cc800e775ad4d054ac575b00b831 [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"
41#include "private/bionic_pthread.h"
42#include "private/bionic_tls.h"
43#include "private/thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044
Mathias Agopian7c0c3792011-09-05 23:54:55 -070045extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
46extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
47
Elliott Hughesc4c6e192013-10-08 14:48:05 -070048extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int status);
49extern void _exit_thread(int status);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -070051int __futex_wake_ex(volatile void *ftx, int pshared, int val)
52{
53 return __futex_syscall3(ftx, pshared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, val);
54}
55
56int __futex_wait_ex(volatile void *ftx, int pshared, int val, const struct timespec *timeout)
57{
58 return __futex_syscall4(ftx, pshared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, val, timeout);
59}
60
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
62 * and thread cancelation
63 */
64
65void __pthread_cleanup_push( __pthread_cleanup_t* c,
66 __pthread_cleanup_func_t routine,
67 void* arg )
68{
69 pthread_internal_t* thread = __get_thread();
70
71 c->__cleanup_routine = routine;
72 c->__cleanup_arg = arg;
73 c->__cleanup_prev = thread->cleanup_stack;
74 thread->cleanup_stack = c;
75}
76
77void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
78{
79 pthread_internal_t* thread = __get_thread();
80
81 thread->cleanup_stack = c->__cleanup_prev;
82 if (execute)
83 c->__cleanup_routine(c->__cleanup_arg);
84}
85
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086void pthread_exit(void * retval)
87{
88 pthread_internal_t* thread = __get_thread();
89 void* stack_base = thread->attr.stack_base;
90 int stack_size = thread->attr.stack_size;
91 int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
Jack Rene480fc82011-09-21 12:44:11 +020092 sigset_t mask;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
94 // call the cleanup handlers first
95 while (thread->cleanup_stack) {
96 __pthread_cleanup_t* c = thread->cleanup_stack;
97 thread->cleanup_stack = c->__cleanup_prev;
98 c->__cleanup_routine(c->__cleanup_arg);
99 }
100
101 // call the TLS destructors, it is important to do that before removing this
102 // thread from the global list. this will ensure that if someone else deletes
103 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
104 // space (see pthread_key_delete)
105 pthread_key_clean_all();
106
Elliott Hughes84114c82013-07-17 13:33:19 -0700107 if (thread->alternate_signal_stack != NULL) {
108 // Tell the kernel to stop using the alternate signal stack.
109 stack_t ss;
110 ss.ss_sp = NULL;
111 ss.ss_flags = SS_DISABLE;
112 sigaltstack(&ss, NULL);
113
114 // Free it.
115 munmap(thread->alternate_signal_stack, SIGSTKSZ);
116 thread->alternate_signal_stack = NULL;
117 }
118
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119 // if the thread is detached, destroy the pthread_internal_t
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400120 // otherwise, keep it in memory and signal any joiners.
Elliott Hughes9d23e042013-02-15 19:21:51 -0800121 pthread_mutex_lock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800123 _pthread_internal_remove_locked(thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124 } else {
Bjorn Andersson0753dc62012-05-03 17:12:39 -0700125 /* make sure that the thread struct doesn't have stale pointers to a stack that
126 * will be unmapped after the exit call below.
127 */
128 if (!user_stack) {
129 thread->attr.stack_base = NULL;
130 thread->attr.stack_size = 0;
131 thread->tls = NULL;
132 }
133
msg5550f020d12013-06-06 14:59:28 -0400134 /* Indicate that the thread has exited for joining threads. */
135 thread->attr.flags |= PTHREAD_ATTR_FLAG_ZOMBIE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800136 thread->return_value = retval;
msg5550f020d12013-06-06 14:59:28 -0400137
138 /* Signal the joining thread if present. */
139 if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
140 pthread_cond_signal(&thread->join_cond);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142 }
Elliott Hughes9d23e042013-02-15 19:21:51 -0800143 pthread_mutex_unlock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144
Jack Rene480fc82011-09-21 12:44:11 +0200145 sigfillset(&mask);
146 sigdelset(&mask, SIGSEGV);
Elliott Hughesc4c6e192013-10-08 14:48:05 -0700147 sigprocmask(SIG_SETMASK, &mask, NULL);
Jack Rene480fc82011-09-21 12:44:11 +0200148
Elliott Hughesa97cc5b2013-10-07 10:20:24 -0700149 if (user_stack) {
Elliott Hughesc4c6e192013-10-08 14:48:05 -0700150 // Cleaning up this thread's stack is the creator's responsibility, not ours.
151 _exit_thread(0);
Elliott Hughesa97cc5b2013-10-07 10:20:24 -0700152 } else {
153 // We need to munmap the stack we're running on before calling exit.
154 // That's not something we can do in C.
Elliott Hughesc4c6e192013-10-08 14:48:05 -0700155 _exit_with_stack_teardown(stack_base, stack_size, 0);
Elliott Hughesa97cc5b2013-10-07 10:20:24 -0700156 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157}
158
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159/* a mutex is implemented as a 32-bit integer holding the following fields
160 *
161 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -0800162 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700164 * 13 shared process-shared flag
165 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166 * 1-0 state lock state (0, 1 or 2)
167 */
168
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100169/* Convenience macro, creates a mask of 'bits' bits that starts from
170 * the 'shift'-th least significant bit in a 32-bit word.
171 *
172 * Examples: FIELD_MASK(0,4) -> 0xf
173 * FIELD_MASK(16,9) -> 0x1ff0000
174 */
175#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100177/* This one is used to create a bit pattern from a given field value */
178#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100179
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100180/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
181#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100183/* Mutex state:
184 *
185 * 0 for unlocked
186 * 1 for locked, no waiters
187 * 2 for locked, maybe waiters
188 */
189#define MUTEX_STATE_SHIFT 0
190#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100192#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
193#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
194#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
195
196#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
197#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
198#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
199
200#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
201#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
202
203#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
204#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
205#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
206
207/* return true iff the mutex if locked with no waiters */
208#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
209
210/* return true iff the mutex if locked with maybe waiters */
211#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
212
213/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
214#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
215
216/* Mutex counter:
217 *
218 * We need to check for overflow before incrementing, and we also need to
219 * detect when the counter is 0
220 */
221#define MUTEX_COUNTER_SHIFT 2
222#define MUTEX_COUNTER_LEN 11
223#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
224
225#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
226#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
227
228/* Used to increment the counter directly after overflow has been checked */
229#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
230
231/* Returns true iff the counter is 0 */
232#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
233
234/* Mutex shared bit flag
235 *
236 * This flag is set to indicate that the mutex is shared among processes.
237 * This changes the futex opcode we use for futex wait/wake operations
238 * (non-shared operations are much faster).
239 */
240#define MUTEX_SHARED_SHIFT 13
241#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
242
243/* Mutex type:
244 *
245 * We support normal, recursive and errorcheck mutexes.
246 *
247 * The constants defined here *cannot* be changed because they must match
248 * the C library ABI which defines the following initialization values in
249 * <pthread.h>:
250 *
251 * __PTHREAD_MUTEX_INIT_VALUE
252 * __PTHREAD_RECURSIVE_MUTEX_VALUE
253 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
254 */
255#define MUTEX_TYPE_SHIFT 14
256#define MUTEX_TYPE_LEN 2
257#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
258
259#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
260#define MUTEX_TYPE_RECURSIVE 1
261#define MUTEX_TYPE_ERRORCHECK 2
262
263#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
264
265#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
266#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
267#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
268
269/* Mutex owner field:
270 *
271 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800272 * tid of the owning thread. Note that this works because the Linux
273 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100274 *
275 * More specifically, it will wrap to 10000 when it reaches over 32768 for
276 * application processes. You can check this by running the following inside
277 * an adb shell session:
278 *
279 OLDPID=$$;
280 while true; do
281 NEWPID=$(sh -c 'echo $$')
282 if [ "$NEWPID" -gt 32768 ]; then
283 echo "AARGH: new PID $NEWPID is too high!"
284 exit 1
285 fi
286 if [ "$NEWPID" -lt "$OLDPID" ]; then
287 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
288 else
289 echo -n "$NEWPID!"
290 fi
291 OLDPID=$NEWPID
292 done
293
294 * Note that you can run the same example on a desktop Linux system,
295 * the wrapping will also happen at 32768, but will go back to 300 instead.
296 */
297#define MUTEX_OWNER_SHIFT 16
298#define MUTEX_OWNER_LEN 16
299
300#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
301#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
302
303/* Convenience macros.
304 *
305 * These are used to form or modify the bit pattern of a given mutex value
306 */
307
308
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800309
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700310/* a mutex attribute holds the following fields
311 *
312 * bits: name description
313 * 0-3 type type of mutex
314 * 4 shared process-shared flag
315 */
316#define MUTEXATTR_TYPE_MASK 0x000f
317#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800318
319
320int pthread_mutexattr_init(pthread_mutexattr_t *attr)
321{
322 if (attr) {
323 *attr = PTHREAD_MUTEX_DEFAULT;
324 return 0;
325 } else {
326 return EINVAL;
327 }
328}
329
330int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
331{
332 if (attr) {
333 *attr = -1;
334 return 0;
335 } else {
336 return EINVAL;
337 }
338}
339
340int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
341{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700342 if (attr) {
343 int atype = (*attr & MUTEXATTR_TYPE_MASK);
344
345 if (atype >= PTHREAD_MUTEX_NORMAL &&
346 atype <= PTHREAD_MUTEX_ERRORCHECK) {
347 *type = atype;
348 return 0;
349 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350 }
351 return EINVAL;
352}
353
354int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
355{
356 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
357 type <= PTHREAD_MUTEX_ERRORCHECK ) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700358 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 return 0;
360 }
361 return EINVAL;
362}
363
364/* process-shared mutexes are not supported at the moment */
365
366int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
367{
368 if (!attr)
369 return EINVAL;
370
Mathias Agopianb7681162009-07-13 22:00:33 -0700371 switch (pshared) {
372 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700373 *attr &= ~MUTEXATTR_SHARED_MASK;
374 return 0;
375
Mathias Agopianb7681162009-07-13 22:00:33 -0700376 case PTHREAD_PROCESS_SHARED:
377 /* our current implementation of pthread actually supports shared
378 * mutexes but won't cleanup if a process dies with the mutex held.
379 * Nevertheless, it's better than nothing. Shared mutexes are used
380 * by surfaceflinger and audioflinger.
381 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700382 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700383 return 0;
384 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700385 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386}
387
388int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
389{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700390 if (!attr || !pshared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800391 return EINVAL;
392
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700393 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
394 : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800395 return 0;
396}
397
398int pthread_mutex_init(pthread_mutex_t *mutex,
399 const pthread_mutexattr_t *attr)
400{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700401 int value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700403 if (mutex == NULL)
404 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700406 if (__predict_true(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100407 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700408 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700410
411 if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
412 value |= MUTEX_SHARED_MASK;
413
414 switch (*attr & MUTEXATTR_TYPE_MASK) {
415 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100416 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700417 break;
418 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100419 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700420 break;
421 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100422 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700423 break;
424 default:
425 return EINVAL;
426 }
427
428 mutex->value = value;
429 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430}
431
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432
433/*
434 * Lock a non-recursive mutex.
435 *
436 * As noted above, there are three states:
437 * 0 (unlocked, no contention)
438 * 1 (locked, no contention)
439 * 2 (locked, contention)
440 *
441 * Non-recursive mutexes don't use the thread-id or counter fields, and the
442 * "type" value is zero, so the only bits that will be set are the ones in
443 * the lock state field.
444 */
445static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100446_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100448 /* convenience shortcuts */
449 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
450 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800451 /*
452 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100453 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
454 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
455 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800456 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100457 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
458 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800460 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100461 * requires promoting it to state 2 (CONTENDED). We need to
462 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800463 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100464 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800465 * see if we got zero back; if so, we have acquired the lock. If
466 * not, another thread still holds the lock and we wait again.
467 *
468 * The second argument to the __futex_wait() call is compared
469 * against the current value. If it doesn't match, __futex_wait()
470 * returns immediately (otherwise, it sleeps for a time specified
471 * by the third argument; 0 means sleep forever). This ensures
472 * that the mutex is in state 2 when we go to sleep on it, which
473 * guarantees a wake-up call.
474 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100475 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
476 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800477 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700478 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800479}
480
481/*
482 * Release a non-recursive mutex. The caller is responsible for determining
483 * that we are in fact the owner of this lock.
484 */
485static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100486_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800487{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700488 ANDROID_MEMBAR_FULL();
489
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800490 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700491 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100492 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800493 * if it wasn't 1 we have to do some additional work.
494 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100495 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800496 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800497 * Start by releasing the lock. The decrement changed it from
498 * "contended lock" to "uncontended lock", which means we still
499 * hold it, and anybody who tries to sneak in will push it back
500 * to state 2.
501 *
502 * Once we set it to zero the lock is up for grabs. We follow
503 * this with a __futex_wake() to ensure that one of the waiting
504 * threads has a chance to grab it.
505 *
506 * This doesn't cause a race with the swap/wait pair in
507 * _normal_lock(), because the __futex_wait() call there will
508 * return immediately if the mutex value isn't 2.
509 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700510 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800511
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800512 /*
513 * Wake up one waiting thread. We don't know which thread will be
514 * woken or when it'll start executing -- futexes make no guarantees
515 * here. There may not even be a thread waiting.
516 *
517 * The newly-woken thread will replace the 0 we just set above
518 * with 2, which means that when it eventually releases the mutex
519 * it will also call FUTEX_WAKE. This results in one extra wake
520 * call whenever a lock is contended, but lets us avoid forgetting
521 * anyone without requiring us to track the number of sleepers.
522 *
523 * It's possible for another thread to sneak in and grab the lock
524 * between the zero assignment above and the wake call below. If
525 * the new thread is "slow" and holds the lock for a while, we'll
526 * wake up a sleeper, which will swap in a 2 and then go back to
527 * sleep since the lock is still held. If the new thread is "fast",
528 * running to completion before we call wake, the thread we
529 * eventually wake will find an unlocked mutex and will execute.
530 * Either way we have correct behavior and nobody is orphaned on
531 * the wait queue.
532 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700533 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534 }
535}
536
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100537/* This common inlined function is used to increment the counter of an
538 * errorcheck or recursive mutex.
539 *
540 * For errorcheck mutexes, it will return EDEADLK
541 * If the counter overflows, it will return EAGAIN
542 * Otherwise, it atomically increments the counter and returns 0
543 * after providing an acquire barrier.
544 *
545 * mtype is the current mutex type
546 * mvalue is the current mutex value (already loaded)
547 * mutex pointers to the mutex.
548 */
549static __inline__ __attribute__((always_inline)) int
550_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800551{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100552 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100553 /* trying to re-lock a mutex we already acquired */
554 return EDEADLK;
555 }
556
557 /* Detect recursive lock overflow and return EAGAIN.
558 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100559 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100560 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100561 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100562 return EAGAIN;
563 }
564
565 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100566 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100567 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100568 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100569 for (;;) {
570 /* increment counter, overflow was already checked */
571 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700572 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100573 /* mutex is still locked, not need for a memory barrier */
574 return 0;
575 }
576 /* the value was changed, this happens when another thread changes
577 * the lower state bits from 1 to 2 to indicate contention. This
578 * cannot change the counter, so simply reload and try again.
579 */
580 mvalue = mutex->value;
581 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582}
583
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700584__LIBC_HIDDEN__
585int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800586{
Wink Savillea12c5442013-01-08 15:15:45 -0800587 int mvalue, mtype, tid, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800588
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700589 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700590 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800591
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100592 mvalue = mutex->value;
593 mtype = (mvalue & MUTEX_TYPE_MASK);
594 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800595
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700596 /* Handle normal case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700597 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100598 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800599 return 0;
600 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700601
602 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800603 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100604 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100605 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700606
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100607 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700608 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700609
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100610 /* First, if the mutex is unlocked, try to quickly acquire it.
611 * In the optimistic case where this works, set the state to 1 to
612 * indicate locked with no contention */
613 if (mvalue == mtype) {
614 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
615 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
616 ANDROID_MEMBAR_FULL();
617 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700618 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100619 /* argh, the value changed, reload before entering the loop */
620 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700621 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100622
623 for (;;) {
624 int newval;
625
626 /* if the mutex is unlocked, its value should be 'mtype' and
627 * we try to acquire it by setting its owner and state atomically.
628 * NOTE: We put the state to 2 since we _know_ there is contention
629 * when we are in this loop. This ensures all waiters will be
630 * unlocked.
631 */
632 if (mvalue == mtype) {
633 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
634 /* TODO: Change this to __bionic_cmpxchg_acquire when we
635 * implement it to get rid of the explicit memory
636 * barrier below.
637 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700638 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100639 mvalue = mutex->value;
640 continue;
641 }
642 ANDROID_MEMBAR_FULL();
643 return 0;
644 }
645
646 /* the mutex is already locked by another thread, if its state is 1
647 * we will change it to 2 to indicate contention. */
648 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
649 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700650 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100651 mvalue = mutex->value;
652 continue;
653 }
654 mvalue = newval;
655 }
656
657 /* wait until the mutex is unlocked */
658 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
659
660 mvalue = mutex->value;
661 }
662 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663}
664
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700665int pthread_mutex_lock(pthread_mutex_t *mutex)
666{
667 int err = pthread_mutex_lock_impl(mutex);
668#ifdef PTHREAD_DEBUG
669 if (PTHREAD_DEBUG_ENABLED) {
670 if (!err) {
671 pthread_debug_mutex_lock_check(mutex);
672 }
673 }
674#endif
675 return err;
676}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800677
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700678__LIBC_HIDDEN__
679int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800680{
Wink Savillea12c5442013-01-08 15:15:45 -0800681 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800682
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700683 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700684 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800685
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100686 mvalue = mutex->value;
687 mtype = (mvalue & MUTEX_TYPE_MASK);
688 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800689
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700690 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700691 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100692 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800693 return 0;
694 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700695
696 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800697 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100698 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700699 return EPERM;
700
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100701 /* If the counter is > 0, we can simply decrement it atomically.
702 * Since other threads can mutate the lower state bits (and only the
703 * lower state bits), use a cmpxchg to do it.
704 */
705 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
706 for (;;) {
707 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700708 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100709 /* success: we still own the mutex, so no memory barrier */
710 return 0;
711 }
712 /* the value changed, so reload and loop */
713 mvalue = mutex->value;
714 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700715 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100716
717 /* the counter is 0, so we're going to unlock the mutex by resetting
718 * its value to 'unlocked'. We need to perform a swap in order
719 * to read the current state, which will be 2 if there are waiters
720 * to awake.
721 *
722 * TODO: Change this to __bionic_swap_release when we implement it
723 * to get rid of the explicit memory barrier below.
724 */
725 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
726 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700727
728 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100729 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700730 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700731 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700732 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800733}
734
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700735int pthread_mutex_unlock(pthread_mutex_t *mutex)
736{
737#ifdef PTHREAD_DEBUG
738 if (PTHREAD_DEBUG_ENABLED) {
739 pthread_debug_mutex_unlock_check(mutex);
740 }
741#endif
742 return pthread_mutex_unlock_impl(mutex);
743}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800744
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700745__LIBC_HIDDEN__
746int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800747{
Wink Savillea12c5442013-01-08 15:15:45 -0800748 int mvalue, mtype, tid, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700749
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700750 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700751 return EINVAL;
752
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100753 mvalue = mutex->value;
754 mtype = (mvalue & MUTEX_TYPE_MASK);
755 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700756
757 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700758 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800759 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100760 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
761 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
762 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700763 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800764 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700765 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700766
767 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800768 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700769
770 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800771 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100772 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100773 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700774
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100775 /* Same as pthread_mutex_lock, except that we don't want to wait, and
776 * the only operation that can succeed is a single cmpxchg to acquire the
777 * lock if it is released / not owned by anyone. No need for a complex loop.
778 */
779 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
780 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700781
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700782 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100783 ANDROID_MEMBAR_FULL();
784 return 0;
785 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700786
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100787 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800788}
789
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700790int pthread_mutex_trylock(pthread_mutex_t *mutex)
791{
792 int err = pthread_mutex_trylock_impl(mutex);
793#ifdef PTHREAD_DEBUG
794 if (PTHREAD_DEBUG_ENABLED) {
795 if (!err) {
796 pthread_debug_mutex_lock_check(mutex);
797 }
798 }
799#endif
800 return err;
801}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800802
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700803/* initialize 'ts' with the difference between 'abstime' and the current time
804 * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
805 */
806static int
807__timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock)
808{
809 clock_gettime(clock, ts);
810 ts->tv_sec = abstime->tv_sec - ts->tv_sec;
811 ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec;
812 if (ts->tv_nsec < 0) {
813 ts->tv_sec--;
814 ts->tv_nsec += 1000000000;
815 }
David 'Digit' Turnerbc10cd22009-09-23 15:56:50 -0700816 if ((ts->tv_nsec < 0) || (ts->tv_sec < 0))
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700817 return -1;
818
819 return 0;
820}
821
822/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
823 * milliseconds.
824 */
825static void
826__timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock)
827{
828 clock_gettime(clock, abstime);
829 abstime->tv_sec += msecs/1000;
830 abstime->tv_nsec += (msecs%1000)*1000000;
831 if (abstime->tv_nsec >= 1000000000) {
832 abstime->tv_sec++;
833 abstime->tv_nsec -= 1000000000;
834 }
835}
836
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700837__LIBC_HIDDEN__
838int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700839{
840 clockid_t clock = CLOCK_MONOTONIC;
841 struct timespec abstime;
842 struct timespec ts;
Wink Savillea12c5442013-01-08 15:15:45 -0800843 int mvalue, mtype, tid, shared;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700844
845 /* compute absolute expiration time */
846 __timespec_to_relative_msec(&abstime, msecs, clock);
847
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700848 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700849 return EINVAL;
850
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100851 mvalue = mutex->value;
852 mtype = (mvalue & MUTEX_TYPE_MASK);
853 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700854
855 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700856 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800857 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100858 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
859 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
860 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
861
862 /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */
863 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700864 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800865 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700866 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700867
868 /* loop while needed */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100869 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700870 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
871 return EBUSY;
872
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100873 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800874 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700875 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700876 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800877 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700878
879 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800880 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100881 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100882 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700883
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100884 /* the following implements the same loop than pthread_mutex_lock_impl
885 * but adds checks to ensure that the operation never exceeds the
886 * absolute expiration time.
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700887 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100888 mtype |= shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700889
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100890 /* first try a quick lock */
891 if (mvalue == mtype) {
892 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700893 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100894 ANDROID_MEMBAR_FULL();
895 return 0;
896 }
897 mvalue = mutex->value;
898 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700899
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700900 for (;;) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100901 struct timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700902
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100903 /* if the value is 'unlocked', try to acquire it directly */
904 /* NOTE: put state to 2 since we know there is contention */
905 if (mvalue == mtype) /* unlocked */ {
906 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
907 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
908 ANDROID_MEMBAR_FULL();
909 return 0;
910 }
911 /* the value changed before we could lock it. We need to check
912 * the time to avoid livelocks, reload the value, then loop again. */
913 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
914 return EBUSY;
915
916 mvalue = mutex->value;
917 continue;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700918 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700919
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100920 /* The value is locked. If 'uncontended', try to switch its state
921 * to 'contented' to ensure we get woken up later. */
922 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
923 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
924 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
925 /* this failed because the value changed, reload it */
926 mvalue = mutex->value;
927 } else {
928 /* this succeeded, update mvalue */
929 mvalue = newval;
930 }
931 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700932
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100933 /* check time and update 'ts' */
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700934 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
935 return EBUSY;
936
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100937 /* Only wait to be woken up if the state is '2', otherwise we'll
938 * simply loop right now. This can happen when the second cmpxchg
939 * in our loop failed because the mutex was unlocked by another
940 * thread.
941 */
942 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
943 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) {
944 return EBUSY;
945 }
946 mvalue = mutex->value;
947 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700948 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100949 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700950}
951
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700952int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
953{
954 int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs);
955#ifdef PTHREAD_DEBUG
956 if (PTHREAD_DEBUG_ENABLED) {
957 if (!err) {
958 pthread_debug_mutex_lock_check(mutex);
959 }
960 }
961#endif
962 return err;
963}
964
965int pthread_mutex_destroy(pthread_mutex_t *mutex)
966{
967 int ret;
968
969 /* use trylock to ensure that the mutex value is
970 * valid and is not already locked. */
971 ret = pthread_mutex_trylock_impl(mutex);
972 if (ret != 0)
973 return ret;
974
975 mutex->value = 0xdead10cc;
976 return 0;
977}
978
979
980
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700981int pthread_condattr_init(pthread_condattr_t *attr)
982{
983 if (attr == NULL)
984 return EINVAL;
985
986 *attr = PTHREAD_PROCESS_PRIVATE;
987 return 0;
988}
989
990int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared)
991{
992 if (attr == NULL || pshared == NULL)
993 return EINVAL;
994
995 *pshared = *attr;
996 return 0;
997}
998
999int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1000{
1001 if (attr == NULL)
1002 return EINVAL;
1003
1004 if (pshared != PTHREAD_PROCESS_SHARED &&
1005 pshared != PTHREAD_PROCESS_PRIVATE)
1006 return EINVAL;
1007
1008 *attr = pshared;
1009 return 0;
1010}
1011
1012int pthread_condattr_destroy(pthread_condattr_t *attr)
1013{
1014 if (attr == NULL)
1015 return EINVAL;
1016
1017 *attr = 0xdeada11d;
1018 return 0;
1019}
1020
1021/* We use one bit in condition variable values as the 'shared' flag
1022 * The rest is a counter.
1023 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001024#define COND_SHARED_MASK 0x0001
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001025#define COND_COUNTER_INCREMENT 0x0002
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001026#define COND_COUNTER_MASK (~COND_SHARED_MASK)
1027
1028#define COND_IS_SHARED(c) (((c)->value & COND_SHARED_MASK) != 0)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001029
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001030/* XXX *technically* there is a race condition that could allow
1031 * XXX a signal to be missed. If thread A is preempted in _wait()
1032 * XXX after unlocking the mutex and before waiting, and if other
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001033 * XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034 * XXX before thread A is scheduled again and calls futex_wait(),
1035 * XXX then the signal will be lost.
1036 */
1037
1038int pthread_cond_init(pthread_cond_t *cond,
1039 const pthread_condattr_t *attr)
1040{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001041 if (cond == NULL)
1042 return EINVAL;
1043
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001044 cond->value = 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001045
1046 if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED)
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001047 cond->value |= COND_SHARED_MASK;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001048
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001049 return 0;
1050}
1051
1052int pthread_cond_destroy(pthread_cond_t *cond)
1053{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001054 if (cond == NULL)
1055 return EINVAL;
1056
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001057 cond->value = 0xdeadc04d;
1058 return 0;
1059}
1060
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001061/* This function is used by pthread_cond_broadcast and
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001062 * pthread_cond_signal to atomically decrement the counter
1063 * then wake-up 'counter' threads.
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001064 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001065static int
1066__pthread_cond_pulse(pthread_cond_t *cond, int counter)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001067{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001068 long flags;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001069
Elliott Hughesd4e753f2013-07-16 12:45:46 -07001070 if (__predict_false(cond == NULL))
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001071 return EINVAL;
1072
1073 flags = (cond->value & ~COND_COUNTER_MASK);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001074 for (;;) {
1075 long oldval = cond->value;
1076 long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK)
1077 | flags;
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001078 if (__bionic_cmpxchg(oldval, newval, &cond->value) == 0)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001079 break;
1080 }
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001081
Andy McFaddene2ac8982010-09-02 13:34:53 -07001082 /*
1083 * Ensure that all memory accesses previously made by this thread are
1084 * visible to the woken thread(s). On the other side, the "wait"
1085 * code will issue any necessary barriers when locking the mutex.
1086 *
1087 * This may not strictly be necessary -- if the caller follows
1088 * recommended practice and holds the mutex before signaling the cond
1089 * var, the mutex ops will provide correct semantics. If they don't
1090 * hold the mutex, they're subject to race conditions anyway.
1091 */
1092 ANDROID_MEMBAR_FULL();
1093
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001094 __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter);
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001095 return 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001096}
1097
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001098int pthread_cond_broadcast(pthread_cond_t *cond)
1099{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001100 return __pthread_cond_pulse(cond, INT_MAX);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001101}
1102
1103int pthread_cond_signal(pthread_cond_t *cond)
1104{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001105 return __pthread_cond_pulse(cond, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001106}
1107
1108int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1109{
1110 return pthread_cond_timedwait(cond, mutex, NULL);
1111}
1112
1113int __pthread_cond_timedwait_relative(pthread_cond_t *cond,
1114 pthread_mutex_t * mutex,
1115 const struct timespec *reltime)
1116{
1117 int status;
1118 int oldvalue = cond->value;
1119
1120 pthread_mutex_unlock(mutex);
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001121 status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond), oldvalue, reltime);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001122 pthread_mutex_lock(mutex);
1123
1124 if (status == (-ETIMEDOUT)) return ETIMEDOUT;
1125 return 0;
1126}
1127
1128int __pthread_cond_timedwait(pthread_cond_t *cond,
1129 pthread_mutex_t * mutex,
1130 const struct timespec *abstime,
1131 clockid_t clock)
1132{
1133 struct timespec ts;
1134 struct timespec * tsp;
1135
1136 if (abstime != NULL) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001137 if (__timespec_to_absolute(&ts, abstime, clock) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001138 return ETIMEDOUT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001139 tsp = &ts;
1140 } else {
1141 tsp = NULL;
1142 }
1143
1144 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
1145}
1146
1147int pthread_cond_timedwait(pthread_cond_t *cond,
1148 pthread_mutex_t * mutex,
1149 const struct timespec *abstime)
1150{
1151 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME);
1152}
1153
1154
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001155/* this one exists only for backward binary compatibility */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001156int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
1157 pthread_mutex_t * mutex,
1158 const struct timespec *abstime)
1159{
1160 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1161}
1162
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001163int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
1164 pthread_mutex_t * mutex,
1165 const struct timespec *abstime)
1166{
1167 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1168}
1169
1170int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
1171 pthread_mutex_t * mutex,
1172 const struct timespec *reltime)
1173{
1174 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
1175}
1176
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001177int pthread_cond_timeout_np(pthread_cond_t *cond,
1178 pthread_mutex_t * mutex,
1179 unsigned msecs)
1180{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001181 struct timespec ts;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001182
1183 ts.tv_sec = msecs / 1000;
1184 ts.tv_nsec = (msecs % 1000) * 1000000;
1185
Matthieu CASTETa4e67f42008-12-27 00:04:10 +01001186 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001187}
1188
1189
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001190/* NOTE: this implementation doesn't support a init function that throws a C++ exception
1191 * or calls fork()
1192 */
Elliott Hughes9d23e042013-02-15 19:21:51 -08001193int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001194{
Andy McFaddenb1c9cc22010-09-23 12:30:12 -07001195 volatile pthread_once_t* ocptr = once_control;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001197 /* PTHREAD_ONCE_INIT is 0, we use the following bit flags
1198 *
1199 * bit 0 set -> initialization is under way
1200 * bit 1 set -> initialization is complete
1201 */
1202#define ONCE_INITIALIZING (1 << 0)
1203#define ONCE_COMPLETED (1 << 1)
1204
1205 /* First check if the once is already initialized. This will be the common
1206 * case and we want to make this as fast as possible. Note that this still
1207 * requires a load_acquire operation here to ensure that all the
1208 * stores performed by the initialization function are observable on
1209 * this CPU after we exit.
1210 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -07001211 if (__predict_true((*ocptr & ONCE_COMPLETED) != 0)) {
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001212 ANDROID_MEMBAR_FULL();
1213 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001214 }
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001215
1216 for (;;) {
1217 /* Try to atomically set the INITIALIZING flag.
1218 * This requires a cmpxchg loop, and we may need
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001219 * to exit prematurely if we detect that
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001220 * COMPLETED is now set.
1221 */
1222 int32_t oldval, newval;
1223
1224 do {
1225 oldval = *ocptr;
1226 if ((oldval & ONCE_COMPLETED) != 0)
1227 break;
1228
1229 newval = oldval | ONCE_INITIALIZING;
1230 } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0);
1231
1232 if ((oldval & ONCE_COMPLETED) != 0) {
1233 /* We detected that COMPLETED was set while in our loop */
1234 ANDROID_MEMBAR_FULL();
1235 return 0;
1236 }
1237
1238 if ((oldval & ONCE_INITIALIZING) == 0) {
1239 /* We got there first, we can jump out of the loop to
1240 * handle the initialization */
1241 break;
1242 }
1243
1244 /* Another thread is running the initialization and hasn't completed
1245 * yet, so wait for it, then try again. */
1246 __futex_wait_ex(ocptr, 0, oldval, NULL);
1247 }
1248
1249 /* call the initialization function. */
1250 (*init_routine)();
1251
1252 /* Do a store_release indicating that initialization is complete */
1253 ANDROID_MEMBAR_FULL();
1254 *ocptr = ONCE_COMPLETED;
1255
1256 /* Wake up any waiters, if any */
1257 __futex_wake_ex(ocptr, 0, INT_MAX);
1258
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001259 return 0;
1260}
André Goddard Rosa78c1c042010-05-19 23:17:16 -03001261
Elliott Hughes40eabe22013-02-14 18:59:37 -08001262pid_t __pthread_gettid(pthread_t thid) {
1263 pthread_internal_t* thread = (pthread_internal_t*) thid;
1264 return thread->tid;
Glenn Kastend53cae02011-07-11 15:41:28 -07001265}
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001266
Elliott Hughes40eabe22013-02-14 18:59:37 -08001267int __pthread_settid(pthread_t thid, pid_t tid) {
1268 if (thid == 0) {
1269 return EINVAL;
1270 }
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001271
Elliott Hughes40eabe22013-02-14 18:59:37 -08001272 pthread_internal_t* thread = (pthread_internal_t*) thid;
1273 thread->tid = tid;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001274
Elliott Hughes40eabe22013-02-14 18:59:37 -08001275 return 0;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001276}