blob: e30fa9de90d64bca14f990d02568b03d16aab7a6 [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>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010034#include <unistd.h>
35
36#include "bionic_atomic_inline.h"
37#include "bionic_futex.h"
38#include "bionic_pthread.h"
39#include "bionic_tls.h"
40#include "pthread_internal.h"
41#include "thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042
Mathias Agopian7c0c3792011-09-05 23:54:55 -070043extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
44extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
45
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080046extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
47extern void _exit_thread(int retCode);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -070049int __futex_wake_ex(volatile void *ftx, int pshared, int val)
50{
51 return __futex_syscall3(ftx, pshared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, val);
52}
53
54int __futex_wait_ex(volatile void *ftx, int pshared, int val, const struct timespec *timeout)
55{
56 return __futex_syscall4(ftx, pshared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, val, timeout);
57}
58
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -070059#define __likely(cond) __builtin_expect(!!(cond), 1)
60#define __unlikely(cond) __builtin_expect(!!(cond), 0)
61
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062void*
63__get_stack_base(int *p_stack_size)
64{
65 pthread_internal_t* thread = __get_thread();
66
67 *p_stack_size = thread->attr.stack_size;
68 return thread->attr.stack_base;
69}
70
71
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
73 * and thread cancelation
74 */
75
76void __pthread_cleanup_push( __pthread_cleanup_t* c,
77 __pthread_cleanup_func_t routine,
78 void* arg )
79{
80 pthread_internal_t* thread = __get_thread();
81
82 c->__cleanup_routine = routine;
83 c->__cleanup_arg = arg;
84 c->__cleanup_prev = thread->cleanup_stack;
85 thread->cleanup_stack = c;
86}
87
88void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
89{
90 pthread_internal_t* thread = __get_thread();
91
92 thread->cleanup_stack = c->__cleanup_prev;
93 if (execute)
94 c->__cleanup_routine(c->__cleanup_arg);
95}
96
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097void pthread_exit(void * retval)
98{
99 pthread_internal_t* thread = __get_thread();
100 void* stack_base = thread->attr.stack_base;
101 int stack_size = thread->attr.stack_size;
102 int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
Jack Rene480fc82011-09-21 12:44:11 +0200103 sigset_t mask;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
105 // call the cleanup handlers first
106 while (thread->cleanup_stack) {
107 __pthread_cleanup_t* c = thread->cleanup_stack;
108 thread->cleanup_stack = c->__cleanup_prev;
109 c->__cleanup_routine(c->__cleanup_arg);
110 }
111
112 // call the TLS destructors, it is important to do that before removing this
113 // thread from the global list. this will ensure that if someone else deletes
114 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
115 // space (see pthread_key_delete)
116 pthread_key_clean_all();
117
118 // if the thread is detached, destroy the pthread_internal_t
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400119 // otherwise, keep it in memory and signal any joiners.
Elliott Hughes9d23e042013-02-15 19:21:51 -0800120 pthread_mutex_lock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800122 _pthread_internal_remove_locked(thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800123 } else {
Bjorn Andersson0753dc62012-05-03 17:12:39 -0700124 /* make sure that the thread struct doesn't have stale pointers to a stack that
125 * will be unmapped after the exit call below.
126 */
127 if (!user_stack) {
128 thread->attr.stack_base = NULL;
129 thread->attr.stack_size = 0;
130 thread->tls = NULL;
131 }
132
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133 /* the join_count field is used to store the number of threads waiting for
134 * the termination of this thread with pthread_join(),
135 *
136 * if it is positive we need to signal the waiters, and we do not touch
137 * the count (it will be decremented by the waiters, the last one will
138 * also remove/free the thread structure
139 *
140 * if it is zero, we set the count value to -1 to indicate that the
141 * thread is in 'zombie' state: it has stopped executing, and its stack
142 * is gone (as well as its TLS area). when another thread calls pthread_join()
143 * on it, it will immediately free the thread and return.
144 */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145 thread->return_value = retval;
146 if (thread->join_count > 0) {
147 pthread_cond_broadcast(&thread->join_cond);
148 } else {
149 thread->join_count = -1; /* zombie thread */
150 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151 }
Elliott Hughes9d23e042013-02-15 19:21:51 -0800152 pthread_mutex_unlock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153
Jack Rene480fc82011-09-21 12:44:11 +0200154 sigfillset(&mask);
155 sigdelset(&mask, SIGSEGV);
156 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
157
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158 // destroy the thread stack
159 if (user_stack)
160 _exit_thread((int)retval);
161 else
162 _exit_with_stack_teardown(stack_base, stack_size, (int)retval);
163}
164
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165/* a mutex is implemented as a 32-bit integer holding the following fields
166 *
167 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -0800168 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700170 * 13 shared process-shared flag
171 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172 * 1-0 state lock state (0, 1 or 2)
173 */
174
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100175/* Convenience macro, creates a mask of 'bits' bits that starts from
176 * the 'shift'-th least significant bit in a 32-bit word.
177 *
178 * Examples: FIELD_MASK(0,4) -> 0xf
179 * FIELD_MASK(16,9) -> 0x1ff0000
180 */
181#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100183/* This one is used to create a bit pattern from a given field value */
184#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100185
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100186/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
187#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100189/* Mutex state:
190 *
191 * 0 for unlocked
192 * 1 for locked, no waiters
193 * 2 for locked, maybe waiters
194 */
195#define MUTEX_STATE_SHIFT 0
196#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800197
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100198#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
199#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
200#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
201
202#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
203#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
204#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
205
206#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
207#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
208
209#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
210#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
211#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
212
213/* return true iff the mutex if locked with no waiters */
214#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
215
216/* return true iff the mutex if locked with maybe waiters */
217#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
218
219/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
220#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
221
222/* Mutex counter:
223 *
224 * We need to check for overflow before incrementing, and we also need to
225 * detect when the counter is 0
226 */
227#define MUTEX_COUNTER_SHIFT 2
228#define MUTEX_COUNTER_LEN 11
229#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
230
231#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
232#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
233
234/* Used to increment the counter directly after overflow has been checked */
235#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
236
237/* Returns true iff the counter is 0 */
238#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
239
240/* Mutex shared bit flag
241 *
242 * This flag is set to indicate that the mutex is shared among processes.
243 * This changes the futex opcode we use for futex wait/wake operations
244 * (non-shared operations are much faster).
245 */
246#define MUTEX_SHARED_SHIFT 13
247#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
248
249/* Mutex type:
250 *
251 * We support normal, recursive and errorcheck mutexes.
252 *
253 * The constants defined here *cannot* be changed because they must match
254 * the C library ABI which defines the following initialization values in
255 * <pthread.h>:
256 *
257 * __PTHREAD_MUTEX_INIT_VALUE
258 * __PTHREAD_RECURSIVE_MUTEX_VALUE
259 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
260 */
261#define MUTEX_TYPE_SHIFT 14
262#define MUTEX_TYPE_LEN 2
263#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
264
265#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
266#define MUTEX_TYPE_RECURSIVE 1
267#define MUTEX_TYPE_ERRORCHECK 2
268
269#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
270
271#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
272#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
273#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
274
275/* Mutex owner field:
276 *
277 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800278 * tid of the owning thread. Note that this works because the Linux
279 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100280 *
281 * More specifically, it will wrap to 10000 when it reaches over 32768 for
282 * application processes. You can check this by running the following inside
283 * an adb shell session:
284 *
285 OLDPID=$$;
286 while true; do
287 NEWPID=$(sh -c 'echo $$')
288 if [ "$NEWPID" -gt 32768 ]; then
289 echo "AARGH: new PID $NEWPID is too high!"
290 exit 1
291 fi
292 if [ "$NEWPID" -lt "$OLDPID" ]; then
293 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
294 else
295 echo -n "$NEWPID!"
296 fi
297 OLDPID=$NEWPID
298 done
299
300 * Note that you can run the same example on a desktop Linux system,
301 * the wrapping will also happen at 32768, but will go back to 300 instead.
302 */
303#define MUTEX_OWNER_SHIFT 16
304#define MUTEX_OWNER_LEN 16
305
306#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
307#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
308
309/* Convenience macros.
310 *
311 * These are used to form or modify the bit pattern of a given mutex value
312 */
313
314
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800315
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700316/* a mutex attribute holds the following fields
317 *
318 * bits: name description
319 * 0-3 type type of mutex
320 * 4 shared process-shared flag
321 */
322#define MUTEXATTR_TYPE_MASK 0x000f
323#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324
325
326int pthread_mutexattr_init(pthread_mutexattr_t *attr)
327{
328 if (attr) {
329 *attr = PTHREAD_MUTEX_DEFAULT;
330 return 0;
331 } else {
332 return EINVAL;
333 }
334}
335
336int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
337{
338 if (attr) {
339 *attr = -1;
340 return 0;
341 } else {
342 return EINVAL;
343 }
344}
345
346int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
347{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700348 if (attr) {
349 int atype = (*attr & MUTEXATTR_TYPE_MASK);
350
351 if (atype >= PTHREAD_MUTEX_NORMAL &&
352 atype <= PTHREAD_MUTEX_ERRORCHECK) {
353 *type = atype;
354 return 0;
355 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800356 }
357 return EINVAL;
358}
359
360int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
361{
362 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
363 type <= PTHREAD_MUTEX_ERRORCHECK ) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700364 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365 return 0;
366 }
367 return EINVAL;
368}
369
370/* process-shared mutexes are not supported at the moment */
371
372int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
373{
374 if (!attr)
375 return EINVAL;
376
Mathias Agopianb7681162009-07-13 22:00:33 -0700377 switch (pshared) {
378 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700379 *attr &= ~MUTEXATTR_SHARED_MASK;
380 return 0;
381
Mathias Agopianb7681162009-07-13 22:00:33 -0700382 case PTHREAD_PROCESS_SHARED:
383 /* our current implementation of pthread actually supports shared
384 * mutexes but won't cleanup if a process dies with the mutex held.
385 * Nevertheless, it's better than nothing. Shared mutexes are used
386 * by surfaceflinger and audioflinger.
387 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700388 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700389 return 0;
390 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700391 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392}
393
394int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
395{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700396 if (!attr || !pshared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 return EINVAL;
398
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700399 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
400 : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 return 0;
402}
403
404int pthread_mutex_init(pthread_mutex_t *mutex,
405 const pthread_mutexattr_t *attr)
406{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700407 int value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800408
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700409 if (mutex == NULL)
410 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700412 if (__likely(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100413 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700414 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700416
417 if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
418 value |= MUTEX_SHARED_MASK;
419
420 switch (*attr & MUTEXATTR_TYPE_MASK) {
421 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100422 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700423 break;
424 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100425 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700426 break;
427 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100428 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700429 break;
430 default:
431 return EINVAL;
432 }
433
434 mutex->value = value;
435 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436}
437
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438
439/*
440 * Lock a non-recursive mutex.
441 *
442 * As noted above, there are three states:
443 * 0 (unlocked, no contention)
444 * 1 (locked, no contention)
445 * 2 (locked, contention)
446 *
447 * Non-recursive mutexes don't use the thread-id or counter fields, and the
448 * "type" value is zero, so the only bits that will be set are the ones in
449 * the lock state field.
450 */
451static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100452_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800453{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100454 /* convenience shortcuts */
455 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
456 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800457 /*
458 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100459 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
460 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
461 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800462 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100463 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
464 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800465 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800466 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100467 * requires promoting it to state 2 (CONTENDED). We need to
468 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800469 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100470 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800471 * see if we got zero back; if so, we have acquired the lock. If
472 * not, another thread still holds the lock and we wait again.
473 *
474 * The second argument to the __futex_wait() call is compared
475 * against the current value. If it doesn't match, __futex_wait()
476 * returns immediately (otherwise, it sleeps for a time specified
477 * by the third argument; 0 means sleep forever). This ensures
478 * that the mutex is in state 2 when we go to sleep on it, which
479 * guarantees a wake-up call.
480 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100481 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
482 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800483 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700484 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800485}
486
487/*
488 * Release a non-recursive mutex. The caller is responsible for determining
489 * that we are in fact the owner of this lock.
490 */
491static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100492_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800493{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700494 ANDROID_MEMBAR_FULL();
495
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800496 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700497 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100498 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800499 * if it wasn't 1 we have to do some additional work.
500 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100501 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800502 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800503 * Start by releasing the lock. The decrement changed it from
504 * "contended lock" to "uncontended lock", which means we still
505 * hold it, and anybody who tries to sneak in will push it back
506 * to state 2.
507 *
508 * Once we set it to zero the lock is up for grabs. We follow
509 * this with a __futex_wake() to ensure that one of the waiting
510 * threads has a chance to grab it.
511 *
512 * This doesn't cause a race with the swap/wait pair in
513 * _normal_lock(), because the __futex_wait() call there will
514 * return immediately if the mutex value isn't 2.
515 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700516 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800518 /*
519 * Wake up one waiting thread. We don't know which thread will be
520 * woken or when it'll start executing -- futexes make no guarantees
521 * here. There may not even be a thread waiting.
522 *
523 * The newly-woken thread will replace the 0 we just set above
524 * with 2, which means that when it eventually releases the mutex
525 * it will also call FUTEX_WAKE. This results in one extra wake
526 * call whenever a lock is contended, but lets us avoid forgetting
527 * anyone without requiring us to track the number of sleepers.
528 *
529 * It's possible for another thread to sneak in and grab the lock
530 * between the zero assignment above and the wake call below. If
531 * the new thread is "slow" and holds the lock for a while, we'll
532 * wake up a sleeper, which will swap in a 2 and then go back to
533 * sleep since the lock is still held. If the new thread is "fast",
534 * running to completion before we call wake, the thread we
535 * eventually wake will find an unlocked mutex and will execute.
536 * Either way we have correct behavior and nobody is orphaned on
537 * the wait queue.
538 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700539 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800540 }
541}
542
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100543/* This common inlined function is used to increment the counter of an
544 * errorcheck or recursive mutex.
545 *
546 * For errorcheck mutexes, it will return EDEADLK
547 * If the counter overflows, it will return EAGAIN
548 * Otherwise, it atomically increments the counter and returns 0
549 * after providing an acquire barrier.
550 *
551 * mtype is the current mutex type
552 * mvalue is the current mutex value (already loaded)
553 * mutex pointers to the mutex.
554 */
555static __inline__ __attribute__((always_inline)) int
556_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800557{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100558 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100559 /* trying to re-lock a mutex we already acquired */
560 return EDEADLK;
561 }
562
563 /* Detect recursive lock overflow and return EAGAIN.
564 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100565 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100566 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100567 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100568 return EAGAIN;
569 }
570
571 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100572 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100573 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100574 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100575 for (;;) {
576 /* increment counter, overflow was already checked */
577 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
578 if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
579 /* mutex is still locked, not need for a memory barrier */
580 return 0;
581 }
582 /* the value was changed, this happens when another thread changes
583 * the lower state bits from 1 to 2 to indicate contention. This
584 * cannot change the counter, so simply reload and try again.
585 */
586 mvalue = mutex->value;
587 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800588}
589
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700590__LIBC_HIDDEN__
591int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800592{
Wink Savillea12c5442013-01-08 15:15:45 -0800593 int mvalue, mtype, tid, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800594
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700595 if (__unlikely(mutex == NULL))
596 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800597
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100598 mvalue = mutex->value;
599 mtype = (mvalue & MUTEX_TYPE_MASK);
600 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800601
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700602 /* Handle normal case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100603 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100604 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800605 return 0;
606 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700607
608 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800609 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100610 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100611 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700612
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100613 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700614 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700615
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100616 /* First, if the mutex is unlocked, try to quickly acquire it.
617 * In the optimistic case where this works, set the state to 1 to
618 * indicate locked with no contention */
619 if (mvalue == mtype) {
620 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
621 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
622 ANDROID_MEMBAR_FULL();
623 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700624 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100625 /* argh, the value changed, reload before entering the loop */
626 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700627 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100628
629 for (;;) {
630 int newval;
631
632 /* if the mutex is unlocked, its value should be 'mtype' and
633 * we try to acquire it by setting its owner and state atomically.
634 * NOTE: We put the state to 2 since we _know_ there is contention
635 * when we are in this loop. This ensures all waiters will be
636 * unlocked.
637 */
638 if (mvalue == mtype) {
639 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
640 /* TODO: Change this to __bionic_cmpxchg_acquire when we
641 * implement it to get rid of the explicit memory
642 * barrier below.
643 */
644 if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
645 mvalue = mutex->value;
646 continue;
647 }
648 ANDROID_MEMBAR_FULL();
649 return 0;
650 }
651
652 /* the mutex is already locked by another thread, if its state is 1
653 * we will change it to 2 to indicate contention. */
654 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
655 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
656 if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
657 mvalue = mutex->value;
658 continue;
659 }
660 mvalue = newval;
661 }
662
663 /* wait until the mutex is unlocked */
664 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
665
666 mvalue = mutex->value;
667 }
668 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800669}
670
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700671int pthread_mutex_lock(pthread_mutex_t *mutex)
672{
673 int err = pthread_mutex_lock_impl(mutex);
674#ifdef PTHREAD_DEBUG
675 if (PTHREAD_DEBUG_ENABLED) {
676 if (!err) {
677 pthread_debug_mutex_lock_check(mutex);
678 }
679 }
680#endif
681 return err;
682}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800683
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700684__LIBC_HIDDEN__
685int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686{
Wink Savillea12c5442013-01-08 15:15:45 -0800687 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800688
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700689 if (__unlikely(mutex == NULL))
690 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800691
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100692 mvalue = mutex->value;
693 mtype = (mvalue & MUTEX_TYPE_MASK);
694 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800695
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700696 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100697 if (__likely(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100698 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800699 return 0;
700 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700701
702 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800703 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100704 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700705 return EPERM;
706
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100707 /* If the counter is > 0, we can simply decrement it atomically.
708 * Since other threads can mutate the lower state bits (and only the
709 * lower state bits), use a cmpxchg to do it.
710 */
711 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
712 for (;;) {
713 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
714 if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
715 /* success: we still own the mutex, so no memory barrier */
716 return 0;
717 }
718 /* the value changed, so reload and loop */
719 mvalue = mutex->value;
720 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700721 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100722
723 /* the counter is 0, so we're going to unlock the mutex by resetting
724 * its value to 'unlocked'. We need to perform a swap in order
725 * to read the current state, which will be 2 if there are waiters
726 * to awake.
727 *
728 * TODO: Change this to __bionic_swap_release when we implement it
729 * to get rid of the explicit memory barrier below.
730 */
731 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
732 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700733
734 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100735 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700736 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700737 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700738 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800739}
740
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700741int pthread_mutex_unlock(pthread_mutex_t *mutex)
742{
743#ifdef PTHREAD_DEBUG
744 if (PTHREAD_DEBUG_ENABLED) {
745 pthread_debug_mutex_unlock_check(mutex);
746 }
747#endif
748 return pthread_mutex_unlock_impl(mutex);
749}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800750
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700751__LIBC_HIDDEN__
752int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800753{
Wink Savillea12c5442013-01-08 15:15:45 -0800754 int mvalue, mtype, tid, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700755
756 if (__unlikely(mutex == NULL))
757 return EINVAL;
758
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100759 mvalue = mutex->value;
760 mtype = (mvalue & MUTEX_TYPE_MASK);
761 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700762
763 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100764 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800765 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100766 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
767 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
768 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700769 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800770 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700771 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700772
773 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800774 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700775
776 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800777 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100778 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100779 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700780
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100781 /* Same as pthread_mutex_lock, except that we don't want to wait, and
782 * the only operation that can succeed is a single cmpxchg to acquire the
783 * lock if it is released / not owned by anyone. No need for a complex loop.
784 */
785 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
786 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700787
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100788 if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
789 ANDROID_MEMBAR_FULL();
790 return 0;
791 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700792
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100793 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800794}
795
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700796int pthread_mutex_trylock(pthread_mutex_t *mutex)
797{
798 int err = pthread_mutex_trylock_impl(mutex);
799#ifdef PTHREAD_DEBUG
800 if (PTHREAD_DEBUG_ENABLED) {
801 if (!err) {
802 pthread_debug_mutex_lock_check(mutex);
803 }
804 }
805#endif
806 return err;
807}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800808
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700809/* initialize 'ts' with the difference between 'abstime' and the current time
810 * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
811 */
812static int
813__timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock)
814{
815 clock_gettime(clock, ts);
816 ts->tv_sec = abstime->tv_sec - ts->tv_sec;
817 ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec;
818 if (ts->tv_nsec < 0) {
819 ts->tv_sec--;
820 ts->tv_nsec += 1000000000;
821 }
David 'Digit' Turnerbc10cd22009-09-23 15:56:50 -0700822 if ((ts->tv_nsec < 0) || (ts->tv_sec < 0))
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700823 return -1;
824
825 return 0;
826}
827
828/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
829 * milliseconds.
830 */
831static void
832__timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock)
833{
834 clock_gettime(clock, abstime);
835 abstime->tv_sec += msecs/1000;
836 abstime->tv_nsec += (msecs%1000)*1000000;
837 if (abstime->tv_nsec >= 1000000000) {
838 abstime->tv_sec++;
839 abstime->tv_nsec -= 1000000000;
840 }
841}
842
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700843__LIBC_HIDDEN__
844int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700845{
846 clockid_t clock = CLOCK_MONOTONIC;
847 struct timespec abstime;
848 struct timespec ts;
Wink Savillea12c5442013-01-08 15:15:45 -0800849 int mvalue, mtype, tid, shared;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700850
851 /* compute absolute expiration time */
852 __timespec_to_relative_msec(&abstime, msecs, clock);
853
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700854 if (__unlikely(mutex == NULL))
855 return EINVAL;
856
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100857 mvalue = mutex->value;
858 mtype = (mvalue & MUTEX_TYPE_MASK);
859 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700860
861 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100862 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800863 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100864 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
865 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
866 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
867
868 /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */
869 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700870 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800871 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700872 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700873
874 /* loop while needed */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100875 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700876 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
877 return EBUSY;
878
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100879 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800880 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700881 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700882 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800883 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700884
885 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800886 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100887 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100888 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700889
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100890 /* the following implements the same loop than pthread_mutex_lock_impl
891 * but adds checks to ensure that the operation never exceeds the
892 * absolute expiration time.
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700893 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100894 mtype |= shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700895
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100896 /* first try a quick lock */
897 if (mvalue == mtype) {
898 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
899 if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
900 ANDROID_MEMBAR_FULL();
901 return 0;
902 }
903 mvalue = mutex->value;
904 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700905
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700906 for (;;) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100907 struct timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700908
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100909 /* if the value is 'unlocked', try to acquire it directly */
910 /* NOTE: put state to 2 since we know there is contention */
911 if (mvalue == mtype) /* unlocked */ {
912 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
913 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
914 ANDROID_MEMBAR_FULL();
915 return 0;
916 }
917 /* the value changed before we could lock it. We need to check
918 * the time to avoid livelocks, reload the value, then loop again. */
919 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
920 return EBUSY;
921
922 mvalue = mutex->value;
923 continue;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700924 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700925
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100926 /* The value is locked. If 'uncontended', try to switch its state
927 * to 'contented' to ensure we get woken up later. */
928 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
929 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
930 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
931 /* this failed because the value changed, reload it */
932 mvalue = mutex->value;
933 } else {
934 /* this succeeded, update mvalue */
935 mvalue = newval;
936 }
937 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700938
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100939 /* check time and update 'ts' */
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700940 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
941 return EBUSY;
942
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100943 /* Only wait to be woken up if the state is '2', otherwise we'll
944 * simply loop right now. This can happen when the second cmpxchg
945 * in our loop failed because the mutex was unlocked by another
946 * thread.
947 */
948 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
949 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) {
950 return EBUSY;
951 }
952 mvalue = mutex->value;
953 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700954 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100955 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700956}
957
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700958int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
959{
960 int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs);
961#ifdef PTHREAD_DEBUG
962 if (PTHREAD_DEBUG_ENABLED) {
963 if (!err) {
964 pthread_debug_mutex_lock_check(mutex);
965 }
966 }
967#endif
968 return err;
969}
970
971int pthread_mutex_destroy(pthread_mutex_t *mutex)
972{
973 int ret;
974
975 /* use trylock to ensure that the mutex value is
976 * valid and is not already locked. */
977 ret = pthread_mutex_trylock_impl(mutex);
978 if (ret != 0)
979 return ret;
980
981 mutex->value = 0xdead10cc;
982 return 0;
983}
984
985
986
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700987int pthread_condattr_init(pthread_condattr_t *attr)
988{
989 if (attr == NULL)
990 return EINVAL;
991
992 *attr = PTHREAD_PROCESS_PRIVATE;
993 return 0;
994}
995
996int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared)
997{
998 if (attr == NULL || pshared == NULL)
999 return EINVAL;
1000
1001 *pshared = *attr;
1002 return 0;
1003}
1004
1005int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1006{
1007 if (attr == NULL)
1008 return EINVAL;
1009
1010 if (pshared != PTHREAD_PROCESS_SHARED &&
1011 pshared != PTHREAD_PROCESS_PRIVATE)
1012 return EINVAL;
1013
1014 *attr = pshared;
1015 return 0;
1016}
1017
1018int pthread_condattr_destroy(pthread_condattr_t *attr)
1019{
1020 if (attr == NULL)
1021 return EINVAL;
1022
1023 *attr = 0xdeada11d;
1024 return 0;
1025}
1026
1027/* We use one bit in condition variable values as the 'shared' flag
1028 * The rest is a counter.
1029 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001030#define COND_SHARED_MASK 0x0001
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001031#define COND_COUNTER_INCREMENT 0x0002
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001032#define COND_COUNTER_MASK (~COND_SHARED_MASK)
1033
1034#define COND_IS_SHARED(c) (((c)->value & COND_SHARED_MASK) != 0)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001035
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001036/* XXX *technically* there is a race condition that could allow
1037 * XXX a signal to be missed. If thread A is preempted in _wait()
1038 * XXX after unlocking the mutex and before waiting, and if other
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001039 * XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001040 * XXX before thread A is scheduled again and calls futex_wait(),
1041 * XXX then the signal will be lost.
1042 */
1043
1044int pthread_cond_init(pthread_cond_t *cond,
1045 const pthread_condattr_t *attr)
1046{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001047 if (cond == NULL)
1048 return EINVAL;
1049
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001050 cond->value = 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001051
1052 if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED)
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001053 cond->value |= COND_SHARED_MASK;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001054
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001055 return 0;
1056}
1057
1058int pthread_cond_destroy(pthread_cond_t *cond)
1059{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001060 if (cond == NULL)
1061 return EINVAL;
1062
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001063 cond->value = 0xdeadc04d;
1064 return 0;
1065}
1066
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001067/* This function is used by pthread_cond_broadcast and
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001068 * pthread_cond_signal to atomically decrement the counter
1069 * then wake-up 'counter' threads.
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001070 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001071static int
1072__pthread_cond_pulse(pthread_cond_t *cond, int counter)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001073{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001074 long flags;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001075
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001076 if (__unlikely(cond == NULL))
1077 return EINVAL;
1078
1079 flags = (cond->value & ~COND_COUNTER_MASK);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001080 for (;;) {
1081 long oldval = cond->value;
1082 long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK)
1083 | flags;
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001084 if (__bionic_cmpxchg(oldval, newval, &cond->value) == 0)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001085 break;
1086 }
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001087
Andy McFaddene2ac8982010-09-02 13:34:53 -07001088 /*
1089 * Ensure that all memory accesses previously made by this thread are
1090 * visible to the woken thread(s). On the other side, the "wait"
1091 * code will issue any necessary barriers when locking the mutex.
1092 *
1093 * This may not strictly be necessary -- if the caller follows
1094 * recommended practice and holds the mutex before signaling the cond
1095 * var, the mutex ops will provide correct semantics. If they don't
1096 * hold the mutex, they're subject to race conditions anyway.
1097 */
1098 ANDROID_MEMBAR_FULL();
1099
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001100 __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter);
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001101 return 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001102}
1103
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001104int pthread_cond_broadcast(pthread_cond_t *cond)
1105{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001106 return __pthread_cond_pulse(cond, INT_MAX);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001107}
1108
1109int pthread_cond_signal(pthread_cond_t *cond)
1110{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001111 return __pthread_cond_pulse(cond, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112}
1113
1114int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1115{
1116 return pthread_cond_timedwait(cond, mutex, NULL);
1117}
1118
1119int __pthread_cond_timedwait_relative(pthread_cond_t *cond,
1120 pthread_mutex_t * mutex,
1121 const struct timespec *reltime)
1122{
1123 int status;
1124 int oldvalue = cond->value;
1125
1126 pthread_mutex_unlock(mutex);
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001127 status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond), oldvalue, reltime);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001128 pthread_mutex_lock(mutex);
1129
1130 if (status == (-ETIMEDOUT)) return ETIMEDOUT;
1131 return 0;
1132}
1133
1134int __pthread_cond_timedwait(pthread_cond_t *cond,
1135 pthread_mutex_t * mutex,
1136 const struct timespec *abstime,
1137 clockid_t clock)
1138{
1139 struct timespec ts;
1140 struct timespec * tsp;
1141
1142 if (abstime != NULL) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001143 if (__timespec_to_absolute(&ts, abstime, clock) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001144 return ETIMEDOUT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001145 tsp = &ts;
1146 } else {
1147 tsp = NULL;
1148 }
1149
1150 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
1151}
1152
1153int pthread_cond_timedwait(pthread_cond_t *cond,
1154 pthread_mutex_t * mutex,
1155 const struct timespec *abstime)
1156{
1157 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME);
1158}
1159
1160
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001161/* this one exists only for backward binary compatibility */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001162int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
1163 pthread_mutex_t * mutex,
1164 const struct timespec *abstime)
1165{
1166 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1167}
1168
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001169int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
1170 pthread_mutex_t * mutex,
1171 const struct timespec *abstime)
1172{
1173 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1174}
1175
1176int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
1177 pthread_mutex_t * mutex,
1178 const struct timespec *reltime)
1179{
1180 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
1181}
1182
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001183int pthread_cond_timeout_np(pthread_cond_t *cond,
1184 pthread_mutex_t * mutex,
1185 unsigned msecs)
1186{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001187 struct timespec ts;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001188
1189 ts.tv_sec = msecs / 1000;
1190 ts.tv_nsec = (msecs % 1000) * 1000000;
1191
Matthieu CASTETa4e67f42008-12-27 00:04:10 +01001192 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193}
1194
1195
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001196/* NOTE: this implementation doesn't support a init function that throws a C++ exception
1197 * or calls fork()
1198 */
Elliott Hughes9d23e042013-02-15 19:21:51 -08001199int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001200{
Andy McFaddenb1c9cc22010-09-23 12:30:12 -07001201 volatile pthread_once_t* ocptr = once_control;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001202
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001203 /* PTHREAD_ONCE_INIT is 0, we use the following bit flags
1204 *
1205 * bit 0 set -> initialization is under way
1206 * bit 1 set -> initialization is complete
1207 */
1208#define ONCE_INITIALIZING (1 << 0)
1209#define ONCE_COMPLETED (1 << 1)
1210
1211 /* First check if the once is already initialized. This will be the common
1212 * case and we want to make this as fast as possible. Note that this still
1213 * requires a load_acquire operation here to ensure that all the
1214 * stores performed by the initialization function are observable on
1215 * this CPU after we exit.
1216 */
1217 if (__likely((*ocptr & ONCE_COMPLETED) != 0)) {
1218 ANDROID_MEMBAR_FULL();
1219 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001220 }
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001221
1222 for (;;) {
1223 /* Try to atomically set the INITIALIZING flag.
1224 * This requires a cmpxchg loop, and we may need
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001225 * to exit prematurely if we detect that
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001226 * COMPLETED is now set.
1227 */
1228 int32_t oldval, newval;
1229
1230 do {
1231 oldval = *ocptr;
1232 if ((oldval & ONCE_COMPLETED) != 0)
1233 break;
1234
1235 newval = oldval | ONCE_INITIALIZING;
1236 } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0);
1237
1238 if ((oldval & ONCE_COMPLETED) != 0) {
1239 /* We detected that COMPLETED was set while in our loop */
1240 ANDROID_MEMBAR_FULL();
1241 return 0;
1242 }
1243
1244 if ((oldval & ONCE_INITIALIZING) == 0) {
1245 /* We got there first, we can jump out of the loop to
1246 * handle the initialization */
1247 break;
1248 }
1249
1250 /* Another thread is running the initialization and hasn't completed
1251 * yet, so wait for it, then try again. */
1252 __futex_wait_ex(ocptr, 0, oldval, NULL);
1253 }
1254
1255 /* call the initialization function. */
1256 (*init_routine)();
1257
1258 /* Do a store_release indicating that initialization is complete */
1259 ANDROID_MEMBAR_FULL();
1260 *ocptr = ONCE_COMPLETED;
1261
1262 /* Wake up any waiters, if any */
1263 __futex_wake_ex(ocptr, 0, INT_MAX);
1264
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001265 return 0;
1266}
André Goddard Rosa78c1c042010-05-19 23:17:16 -03001267
Elliott Hughes40eabe22013-02-14 18:59:37 -08001268pid_t __pthread_gettid(pthread_t thid) {
1269 pthread_internal_t* thread = (pthread_internal_t*) thid;
1270 return thread->tid;
Glenn Kastend53cae02011-07-11 15:41:28 -07001271}
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001272
Elliott Hughes40eabe22013-02-14 18:59:37 -08001273int __pthread_settid(pthread_t thid, pid_t tid) {
1274 if (thid == 0) {
1275 return EINVAL;
1276 }
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001277
Elliott Hughes40eabe22013-02-14 18:59:37 -08001278 pthread_internal_t* thread = (pthread_internal_t*) thid;
1279 thread->tid = tid;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001280
Elliott Hughes40eabe22013-02-14 18:59:37 -08001281 return 0;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001282}