blob: 8589cd6a20bb2006eddb8932cb41626951190d43 [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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
60 * and thread cancelation
61 */
62
63void __pthread_cleanup_push( __pthread_cleanup_t* c,
64 __pthread_cleanup_func_t routine,
65 void* arg )
66{
67 pthread_internal_t* thread = __get_thread();
68
69 c->__cleanup_routine = routine;
70 c->__cleanup_arg = arg;
71 c->__cleanup_prev = thread->cleanup_stack;
72 thread->cleanup_stack = c;
73}
74
75void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
76{
77 pthread_internal_t* thread = __get_thread();
78
79 thread->cleanup_stack = c->__cleanup_prev;
80 if (execute)
81 c->__cleanup_routine(c->__cleanup_arg);
82}
83
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084void pthread_exit(void * retval)
85{
86 pthread_internal_t* thread = __get_thread();
87 void* stack_base = thread->attr.stack_base;
88 int stack_size = thread->attr.stack_size;
89 int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
Jack Rene480fc82011-09-21 12:44:11 +020090 sigset_t mask;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
92 // call the cleanup handlers first
93 while (thread->cleanup_stack) {
94 __pthread_cleanup_t* c = thread->cleanup_stack;
95 thread->cleanup_stack = c->__cleanup_prev;
96 c->__cleanup_routine(c->__cleanup_arg);
97 }
98
99 // call the TLS destructors, it is important to do that before removing this
100 // thread from the global list. this will ensure that if someone else deletes
101 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
102 // space (see pthread_key_delete)
103 pthread_key_clean_all();
104
105 // if the thread is detached, destroy the pthread_internal_t
Sergey Melnikov10ce9692012-10-26 14:06:43 +0400106 // otherwise, keep it in memory and signal any joiners.
Elliott Hughes9d23e042013-02-15 19:21:51 -0800107 pthread_mutex_lock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
Elliott Hughes9d23e042013-02-15 19:21:51 -0800109 _pthread_internal_remove_locked(thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110 } else {
Bjorn Andersson0753dc62012-05-03 17:12:39 -0700111 /* make sure that the thread struct doesn't have stale pointers to a stack that
112 * will be unmapped after the exit call below.
113 */
114 if (!user_stack) {
115 thread->attr.stack_base = NULL;
116 thread->attr.stack_size = 0;
117 thread->tls = NULL;
118 }
119
msg5550f020d12013-06-06 14:59:28 -0400120 /* Indicate that the thread has exited for joining threads. */
121 thread->attr.flags |= PTHREAD_ATTR_FLAG_ZOMBIE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122 thread->return_value = retval;
msg5550f020d12013-06-06 14:59:28 -0400123
124 /* Signal the joining thread if present. */
125 if (thread->attr.flags & PTHREAD_ATTR_FLAG_JOINED) {
126 pthread_cond_signal(&thread->join_cond);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800127 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128 }
Elliott Hughes9d23e042013-02-15 19:21:51 -0800129 pthread_mutex_unlock(&gThreadListLock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Jack Rene480fc82011-09-21 12:44:11 +0200131 sigfillset(&mask);
132 sigdelset(&mask, SIGSEGV);
133 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
134
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135 // destroy the thread stack
136 if (user_stack)
137 _exit_thread((int)retval);
138 else
139 _exit_with_stack_teardown(stack_base, stack_size, (int)retval);
140}
141
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142/* a mutex is implemented as a 32-bit integer holding the following fields
143 *
144 * bits: name description
Elliott Hughes40eabe22013-02-14 18:59:37 -0800145 * 31-16 tid owner thread's tid (recursive and errorcheck only)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700147 * 13 shared process-shared flag
148 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149 * 1-0 state lock state (0, 1 or 2)
150 */
151
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100152/* Convenience macro, creates a mask of 'bits' bits that starts from
153 * the 'shift'-th least significant bit in a 32-bit word.
154 *
155 * Examples: FIELD_MASK(0,4) -> 0xf
156 * FIELD_MASK(16,9) -> 0x1ff0000
157 */
158#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100160/* This one is used to create a bit pattern from a given field value */
161#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100162
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100163/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
164#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100166/* Mutex state:
167 *
168 * 0 for unlocked
169 * 1 for locked, no waiters
170 * 2 for locked, maybe waiters
171 */
172#define MUTEX_STATE_SHIFT 0
173#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100175#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
176#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
177#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
178
179#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
180#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
181#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
182
183#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
184#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
185
186#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
187#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
188#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
189
190/* return true iff the mutex if locked with no waiters */
191#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
192
193/* return true iff the mutex if locked with maybe waiters */
194#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
195
196/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
197#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
198
199/* Mutex counter:
200 *
201 * We need to check for overflow before incrementing, and we also need to
202 * detect when the counter is 0
203 */
204#define MUTEX_COUNTER_SHIFT 2
205#define MUTEX_COUNTER_LEN 11
206#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
207
208#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
209#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
210
211/* Used to increment the counter directly after overflow has been checked */
212#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
213
214/* Returns true iff the counter is 0 */
215#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
216
217/* Mutex shared bit flag
218 *
219 * This flag is set to indicate that the mutex is shared among processes.
220 * This changes the futex opcode we use for futex wait/wake operations
221 * (non-shared operations are much faster).
222 */
223#define MUTEX_SHARED_SHIFT 13
224#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
225
226/* Mutex type:
227 *
228 * We support normal, recursive and errorcheck mutexes.
229 *
230 * The constants defined here *cannot* be changed because they must match
231 * the C library ABI which defines the following initialization values in
232 * <pthread.h>:
233 *
234 * __PTHREAD_MUTEX_INIT_VALUE
235 * __PTHREAD_RECURSIVE_MUTEX_VALUE
236 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
237 */
238#define MUTEX_TYPE_SHIFT 14
239#define MUTEX_TYPE_LEN 2
240#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
241
242#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
243#define MUTEX_TYPE_RECURSIVE 1
244#define MUTEX_TYPE_ERRORCHECK 2
245
246#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
247
248#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
249#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
250#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
251
252/* Mutex owner field:
253 *
254 * This is only used for recursive and errorcheck mutexes. It holds the
Elliott Hughes40eabe22013-02-14 18:59:37 -0800255 * tid of the owning thread. Note that this works because the Linux
256 * kernel _only_ uses 16-bit values for tids.
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100257 *
258 * More specifically, it will wrap to 10000 when it reaches over 32768 for
259 * application processes. You can check this by running the following inside
260 * an adb shell session:
261 *
262 OLDPID=$$;
263 while true; do
264 NEWPID=$(sh -c 'echo $$')
265 if [ "$NEWPID" -gt 32768 ]; then
266 echo "AARGH: new PID $NEWPID is too high!"
267 exit 1
268 fi
269 if [ "$NEWPID" -lt "$OLDPID" ]; then
270 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
271 else
272 echo -n "$NEWPID!"
273 fi
274 OLDPID=$NEWPID
275 done
276
277 * Note that you can run the same example on a desktop Linux system,
278 * the wrapping will also happen at 32768, but will go back to 300 instead.
279 */
280#define MUTEX_OWNER_SHIFT 16
281#define MUTEX_OWNER_LEN 16
282
283#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
284#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
285
286/* Convenience macros.
287 *
288 * These are used to form or modify the bit pattern of a given mutex value
289 */
290
291
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700293/* a mutex attribute holds the following fields
294 *
295 * bits: name description
296 * 0-3 type type of mutex
297 * 4 shared process-shared flag
298 */
299#define MUTEXATTR_TYPE_MASK 0x000f
300#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800301
302
303int pthread_mutexattr_init(pthread_mutexattr_t *attr)
304{
305 if (attr) {
306 *attr = PTHREAD_MUTEX_DEFAULT;
307 return 0;
308 } else {
309 return EINVAL;
310 }
311}
312
313int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
314{
315 if (attr) {
316 *attr = -1;
317 return 0;
318 } else {
319 return EINVAL;
320 }
321}
322
323int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
324{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700325 if (attr) {
326 int atype = (*attr & MUTEXATTR_TYPE_MASK);
327
328 if (atype >= PTHREAD_MUTEX_NORMAL &&
329 atype <= PTHREAD_MUTEX_ERRORCHECK) {
330 *type = atype;
331 return 0;
332 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800333 }
334 return EINVAL;
335}
336
337int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
338{
339 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
340 type <= PTHREAD_MUTEX_ERRORCHECK ) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700341 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800342 return 0;
343 }
344 return EINVAL;
345}
346
347/* process-shared mutexes are not supported at the moment */
348
349int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
350{
351 if (!attr)
352 return EINVAL;
353
Mathias Agopianb7681162009-07-13 22:00:33 -0700354 switch (pshared) {
355 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700356 *attr &= ~MUTEXATTR_SHARED_MASK;
357 return 0;
358
Mathias Agopianb7681162009-07-13 22:00:33 -0700359 case PTHREAD_PROCESS_SHARED:
360 /* our current implementation of pthread actually supports shared
361 * mutexes but won't cleanup if a process dies with the mutex held.
362 * Nevertheless, it's better than nothing. Shared mutexes are used
363 * by surfaceflinger and audioflinger.
364 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700365 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700366 return 0;
367 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700368 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369}
370
371int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
372{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700373 if (!attr || !pshared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374 return EINVAL;
375
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700376 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
377 : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800378 return 0;
379}
380
381int pthread_mutex_init(pthread_mutex_t *mutex,
382 const pthread_mutexattr_t *attr)
383{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700384 int value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700386 if (mutex == NULL)
387 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800388
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700389 if (__predict_true(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100390 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700391 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700393
394 if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
395 value |= MUTEX_SHARED_MASK;
396
397 switch (*attr & MUTEXATTR_TYPE_MASK) {
398 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100399 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700400 break;
401 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100402 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700403 break;
404 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100405 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700406 break;
407 default:
408 return EINVAL;
409 }
410
411 mutex->value = value;
412 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413}
414
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800415
416/*
417 * Lock a non-recursive mutex.
418 *
419 * As noted above, there are three states:
420 * 0 (unlocked, no contention)
421 * 1 (locked, no contention)
422 * 2 (locked, contention)
423 *
424 * Non-recursive mutexes don't use the thread-id or counter fields, and the
425 * "type" value is zero, so the only bits that will be set are the ones in
426 * the lock state field.
427 */
428static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100429_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800430{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100431 /* convenience shortcuts */
432 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
433 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800434 /*
435 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100436 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
437 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
438 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800439 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100440 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
441 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800442 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800443 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100444 * requires promoting it to state 2 (CONTENDED). We need to
445 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800446 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100447 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800448 * see if we got zero back; if so, we have acquired the lock. If
449 * not, another thread still holds the lock and we wait again.
450 *
451 * The second argument to the __futex_wait() call is compared
452 * against the current value. If it doesn't match, __futex_wait()
453 * returns immediately (otherwise, it sleeps for a time specified
454 * by the third argument; 0 means sleep forever). This ensures
455 * that the mutex is in state 2 when we go to sleep on it, which
456 * guarantees a wake-up call.
457 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100458 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
459 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700461 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800462}
463
464/*
465 * Release a non-recursive mutex. The caller is responsible for determining
466 * that we are in fact the owner of this lock.
467 */
468static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100469_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800470{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700471 ANDROID_MEMBAR_FULL();
472
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800473 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700474 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100475 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800476 * if it wasn't 1 we have to do some additional work.
477 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100478 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800479 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800480 * Start by releasing the lock. The decrement changed it from
481 * "contended lock" to "uncontended lock", which means we still
482 * hold it, and anybody who tries to sneak in will push it back
483 * to state 2.
484 *
485 * Once we set it to zero the lock is up for grabs. We follow
486 * this with a __futex_wake() to ensure that one of the waiting
487 * threads has a chance to grab it.
488 *
489 * This doesn't cause a race with the swap/wait pair in
490 * _normal_lock(), because the __futex_wait() call there will
491 * return immediately if the mutex value isn't 2.
492 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700493 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800494
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800495 /*
496 * Wake up one waiting thread. We don't know which thread will be
497 * woken or when it'll start executing -- futexes make no guarantees
498 * here. There may not even be a thread waiting.
499 *
500 * The newly-woken thread will replace the 0 we just set above
501 * with 2, which means that when it eventually releases the mutex
502 * it will also call FUTEX_WAKE. This results in one extra wake
503 * call whenever a lock is contended, but lets us avoid forgetting
504 * anyone without requiring us to track the number of sleepers.
505 *
506 * It's possible for another thread to sneak in and grab the lock
507 * between the zero assignment above and the wake call below. If
508 * the new thread is "slow" and holds the lock for a while, we'll
509 * wake up a sleeper, which will swap in a 2 and then go back to
510 * sleep since the lock is still held. If the new thread is "fast",
511 * running to completion before we call wake, the thread we
512 * eventually wake will find an unlocked mutex and will execute.
513 * Either way we have correct behavior and nobody is orphaned on
514 * the wait queue.
515 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700516 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800517 }
518}
519
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100520/* This common inlined function is used to increment the counter of an
521 * errorcheck or recursive mutex.
522 *
523 * For errorcheck mutexes, it will return EDEADLK
524 * If the counter overflows, it will return EAGAIN
525 * Otherwise, it atomically increments the counter and returns 0
526 * after providing an acquire barrier.
527 *
528 * mtype is the current mutex type
529 * mvalue is the current mutex value (already loaded)
530 * mutex pointers to the mutex.
531 */
532static __inline__ __attribute__((always_inline)) int
533_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800534{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100535 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100536 /* trying to re-lock a mutex we already acquired */
537 return EDEADLK;
538 }
539
540 /* Detect recursive lock overflow and return EAGAIN.
541 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100542 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100543 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100544 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100545 return EAGAIN;
546 }
547
548 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +0100549 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100550 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100551 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100552 for (;;) {
553 /* increment counter, overflow was already checked */
554 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700555 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100556 /* mutex is still locked, not need for a memory barrier */
557 return 0;
558 }
559 /* the value was changed, this happens when another thread changes
560 * the lower state bits from 1 to 2 to indicate contention. This
561 * cannot change the counter, so simply reload and try again.
562 */
563 mvalue = mutex->value;
564 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800565}
566
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700567__LIBC_HIDDEN__
568int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800569{
Wink Savillea12c5442013-01-08 15:15:45 -0800570 int mvalue, mtype, tid, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800571
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700572 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700573 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800574
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100575 mvalue = mutex->value;
576 mtype = (mvalue & MUTEX_TYPE_MASK);
577 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800578
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700579 /* Handle normal case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700580 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100581 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800582 return 0;
583 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700584
585 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800586 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100587 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100588 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700589
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100590 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700591 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700592
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100593 /* First, if the mutex is unlocked, try to quickly acquire it.
594 * In the optimistic case where this works, set the state to 1 to
595 * indicate locked with no contention */
596 if (mvalue == mtype) {
597 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
598 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
599 ANDROID_MEMBAR_FULL();
600 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700601 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100602 /* argh, the value changed, reload before entering the loop */
603 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700604 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100605
606 for (;;) {
607 int newval;
608
609 /* if the mutex is unlocked, its value should be 'mtype' and
610 * we try to acquire it by setting its owner and state atomically.
611 * NOTE: We put the state to 2 since we _know_ there is contention
612 * when we are in this loop. This ensures all waiters will be
613 * unlocked.
614 */
615 if (mvalue == mtype) {
616 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
617 /* TODO: Change this to __bionic_cmpxchg_acquire when we
618 * implement it to get rid of the explicit memory
619 * barrier below.
620 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700621 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100622 mvalue = mutex->value;
623 continue;
624 }
625 ANDROID_MEMBAR_FULL();
626 return 0;
627 }
628
629 /* the mutex is already locked by another thread, if its state is 1
630 * we will change it to 2 to indicate contention. */
631 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
632 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700633 if (__predict_false(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100634 mvalue = mutex->value;
635 continue;
636 }
637 mvalue = newval;
638 }
639
640 /* wait until the mutex is unlocked */
641 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
642
643 mvalue = mutex->value;
644 }
645 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800646}
647
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700648int pthread_mutex_lock(pthread_mutex_t *mutex)
649{
650 int err = pthread_mutex_lock_impl(mutex);
651#ifdef PTHREAD_DEBUG
652 if (PTHREAD_DEBUG_ENABLED) {
653 if (!err) {
654 pthread_debug_mutex_lock_check(mutex);
655 }
656 }
657#endif
658 return err;
659}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800660
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700661__LIBC_HIDDEN__
662int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800663{
Wink Savillea12c5442013-01-08 15:15:45 -0800664 int mvalue, mtype, tid, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800665
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700666 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700667 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800668
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100669 mvalue = mutex->value;
670 mtype = (mvalue & MUTEX_TYPE_MASK);
671 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800672
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700673 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700674 if (__predict_true(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100675 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800676 return 0;
677 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700678
679 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800680 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100681 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700682 return EPERM;
683
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100684 /* If the counter is > 0, we can simply decrement it atomically.
685 * Since other threads can mutate the lower state bits (and only the
686 * lower state bits), use a cmpxchg to do it.
687 */
688 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
689 for (;;) {
690 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700691 if (__predict_true(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100692 /* success: we still own the mutex, so no memory barrier */
693 return 0;
694 }
695 /* the value changed, so reload and loop */
696 mvalue = mutex->value;
697 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700698 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100699
700 /* the counter is 0, so we're going to unlock the mutex by resetting
701 * its value to 'unlocked'. We need to perform a swap in order
702 * to read the current state, which will be 2 if there are waiters
703 * to awake.
704 *
705 * TODO: Change this to __bionic_swap_release when we implement it
706 * to get rid of the explicit memory barrier below.
707 */
708 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
709 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700710
711 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100712 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -0700713 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700714 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700715 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800716}
717
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700718int pthread_mutex_unlock(pthread_mutex_t *mutex)
719{
720#ifdef PTHREAD_DEBUG
721 if (PTHREAD_DEBUG_ENABLED) {
722 pthread_debug_mutex_unlock_check(mutex);
723 }
724#endif
725 return pthread_mutex_unlock_impl(mutex);
726}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700728__LIBC_HIDDEN__
729int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800730{
Wink Savillea12c5442013-01-08 15:15:45 -0800731 int mvalue, mtype, tid, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700732
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700733 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700734 return EINVAL;
735
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100736 mvalue = mutex->value;
737 mtype = (mvalue & MUTEX_TYPE_MASK);
738 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700739
740 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700741 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800742 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100743 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
744 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
745 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700746 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800747 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700748 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700749
750 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800751 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700752
753 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800754 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100755 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100756 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700757
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100758 /* Same as pthread_mutex_lock, except that we don't want to wait, and
759 * the only operation that can succeed is a single cmpxchg to acquire the
760 * lock if it is released / not owned by anyone. No need for a complex loop.
761 */
762 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
763 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700764
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700765 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100766 ANDROID_MEMBAR_FULL();
767 return 0;
768 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700769
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100770 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771}
772
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700773int pthread_mutex_trylock(pthread_mutex_t *mutex)
774{
775 int err = pthread_mutex_trylock_impl(mutex);
776#ifdef PTHREAD_DEBUG
777 if (PTHREAD_DEBUG_ENABLED) {
778 if (!err) {
779 pthread_debug_mutex_lock_check(mutex);
780 }
781 }
782#endif
783 return err;
784}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800785
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700786/* initialize 'ts' with the difference between 'abstime' and the current time
787 * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
788 */
789static int
790__timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock)
791{
792 clock_gettime(clock, ts);
793 ts->tv_sec = abstime->tv_sec - ts->tv_sec;
794 ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec;
795 if (ts->tv_nsec < 0) {
796 ts->tv_sec--;
797 ts->tv_nsec += 1000000000;
798 }
David 'Digit' Turnerbc10cd22009-09-23 15:56:50 -0700799 if ((ts->tv_nsec < 0) || (ts->tv_sec < 0))
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700800 return -1;
801
802 return 0;
803}
804
805/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
806 * milliseconds.
807 */
808static void
809__timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock)
810{
811 clock_gettime(clock, abstime);
812 abstime->tv_sec += msecs/1000;
813 abstime->tv_nsec += (msecs%1000)*1000000;
814 if (abstime->tv_nsec >= 1000000000) {
815 abstime->tv_sec++;
816 abstime->tv_nsec -= 1000000000;
817 }
818}
819
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700820__LIBC_HIDDEN__
821int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700822{
823 clockid_t clock = CLOCK_MONOTONIC;
824 struct timespec abstime;
825 struct timespec ts;
Wink Savillea12c5442013-01-08 15:15:45 -0800826 int mvalue, mtype, tid, shared;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700827
828 /* compute absolute expiration time */
829 __timespec_to_relative_msec(&abstime, msecs, clock);
830
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700831 if (__predict_false(mutex == NULL))
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700832 return EINVAL;
833
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100834 mvalue = mutex->value;
835 mtype = (mvalue & MUTEX_TYPE_MASK);
836 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700837
838 /* Handle common case first */
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700839 if ( __predict_true(mtype == MUTEX_TYPE_BITS_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800840 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100841 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
842 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
843 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
844
845 /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */
846 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700847 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800848 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700849 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700850
851 /* loop while needed */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100852 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700853 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
854 return EBUSY;
855
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100856 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800857 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -0700858 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700859 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800860 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700861
862 /* Do we already own this recursive or error-check mutex ? */
Elliott Hughes40eabe22013-02-14 18:59:37 -0800863 tid = __get_thread()->tid;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100864 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100865 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700866
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100867 /* the following implements the same loop than pthread_mutex_lock_impl
868 * but adds checks to ensure that the operation never exceeds the
869 * absolute expiration time.
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700870 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100871 mtype |= shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700872
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100873 /* first try a quick lock */
874 if (mvalue == mtype) {
875 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Elliott Hughesd4e753f2013-07-16 12:45:46 -0700876 if (__predict_true(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100877 ANDROID_MEMBAR_FULL();
878 return 0;
879 }
880 mvalue = mutex->value;
881 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700882
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700883 for (;;) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100884 struct timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700885
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100886 /* if the value is 'unlocked', try to acquire it directly */
887 /* NOTE: put state to 2 since we know there is contention */
888 if (mvalue == mtype) /* unlocked */ {
889 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
890 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
891 ANDROID_MEMBAR_FULL();
892 return 0;
893 }
894 /* the value changed before we could lock it. We need to check
895 * the time to avoid livelocks, reload the value, then loop again. */
896 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
897 return EBUSY;
898
899 mvalue = mutex->value;
900 continue;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700901 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700902
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100903 /* The value is locked. If 'uncontended', try to switch its state
904 * to 'contented' to ensure we get woken up later. */
905 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
906 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
907 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
908 /* this failed because the value changed, reload it */
909 mvalue = mutex->value;
910 } else {
911 /* this succeeded, update mvalue */
912 mvalue = newval;
913 }
914 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700915
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100916 /* check time and update 'ts' */
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700917 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
918 return EBUSY;
919
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100920 /* Only wait to be woken up if the state is '2', otherwise we'll
921 * simply loop right now. This can happen when the second cmpxchg
922 * in our loop failed because the mutex was unlocked by another
923 * thread.
924 */
925 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
926 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) {
927 return EBUSY;
928 }
929 mvalue = mutex->value;
930 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700931 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100932 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700933}
934
Mathias Agopian7c0c3792011-09-05 23:54:55 -0700935int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
936{
937 int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs);
938#ifdef PTHREAD_DEBUG
939 if (PTHREAD_DEBUG_ENABLED) {
940 if (!err) {
941 pthread_debug_mutex_lock_check(mutex);
942 }
943 }
944#endif
945 return err;
946}
947
948int pthread_mutex_destroy(pthread_mutex_t *mutex)
949{
950 int ret;
951
952 /* use trylock to ensure that the mutex value is
953 * valid and is not already locked. */
954 ret = pthread_mutex_trylock_impl(mutex);
955 if (ret != 0)
956 return ret;
957
958 mutex->value = 0xdead10cc;
959 return 0;
960}
961
962
963
David 'Digit' Turneree7b0772010-03-18 14:07:42 -0700964int pthread_condattr_init(pthread_condattr_t *attr)
965{
966 if (attr == NULL)
967 return EINVAL;
968
969 *attr = PTHREAD_PROCESS_PRIVATE;
970 return 0;
971}
972
973int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared)
974{
975 if (attr == NULL || pshared == NULL)
976 return EINVAL;
977
978 *pshared = *attr;
979 return 0;
980}
981
982int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
983{
984 if (attr == NULL)
985 return EINVAL;
986
987 if (pshared != PTHREAD_PROCESS_SHARED &&
988 pshared != PTHREAD_PROCESS_PRIVATE)
989 return EINVAL;
990
991 *attr = pshared;
992 return 0;
993}
994
995int pthread_condattr_destroy(pthread_condattr_t *attr)
996{
997 if (attr == NULL)
998 return EINVAL;
999
1000 *attr = 0xdeada11d;
1001 return 0;
1002}
1003
1004/* We use one bit in condition variable values as the 'shared' flag
1005 * The rest is a counter.
1006 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001007#define COND_SHARED_MASK 0x0001
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001008#define COND_COUNTER_INCREMENT 0x0002
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001009#define COND_COUNTER_MASK (~COND_SHARED_MASK)
1010
1011#define COND_IS_SHARED(c) (((c)->value & COND_SHARED_MASK) != 0)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001012
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001013/* XXX *technically* there is a race condition that could allow
1014 * XXX a signal to be missed. If thread A is preempted in _wait()
1015 * XXX after unlocking the mutex and before waiting, and if other
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001016 * XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001017 * XXX before thread A is scheduled again and calls futex_wait(),
1018 * XXX then the signal will be lost.
1019 */
1020
1021int pthread_cond_init(pthread_cond_t *cond,
1022 const pthread_condattr_t *attr)
1023{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001024 if (cond == NULL)
1025 return EINVAL;
1026
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001027 cond->value = 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001028
1029 if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED)
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001030 cond->value |= COND_SHARED_MASK;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001031
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001032 return 0;
1033}
1034
1035int pthread_cond_destroy(pthread_cond_t *cond)
1036{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001037 if (cond == NULL)
1038 return EINVAL;
1039
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001040 cond->value = 0xdeadc04d;
1041 return 0;
1042}
1043
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001044/* This function is used by pthread_cond_broadcast and
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001045 * pthread_cond_signal to atomically decrement the counter
1046 * then wake-up 'counter' threads.
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001047 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001048static int
1049__pthread_cond_pulse(pthread_cond_t *cond, int counter)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001050{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001051 long flags;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001052
Elliott Hughesd4e753f2013-07-16 12:45:46 -07001053 if (__predict_false(cond == NULL))
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001054 return EINVAL;
1055
1056 flags = (cond->value & ~COND_COUNTER_MASK);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001057 for (;;) {
1058 long oldval = cond->value;
1059 long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK)
1060 | flags;
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001061 if (__bionic_cmpxchg(oldval, newval, &cond->value) == 0)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001062 break;
1063 }
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001064
Andy McFaddene2ac8982010-09-02 13:34:53 -07001065 /*
1066 * Ensure that all memory accesses previously made by this thread are
1067 * visible to the woken thread(s). On the other side, the "wait"
1068 * code will issue any necessary barriers when locking the mutex.
1069 *
1070 * This may not strictly be necessary -- if the caller follows
1071 * recommended practice and holds the mutex before signaling the cond
1072 * var, the mutex ops will provide correct semantics. If they don't
1073 * hold the mutex, they're subject to race conditions anyway.
1074 */
1075 ANDROID_MEMBAR_FULL();
1076
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001077 __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter);
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001078 return 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001079}
1080
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001081int pthread_cond_broadcast(pthread_cond_t *cond)
1082{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001083 return __pthread_cond_pulse(cond, INT_MAX);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001084}
1085
1086int pthread_cond_signal(pthread_cond_t *cond)
1087{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001088 return __pthread_cond_pulse(cond, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001089}
1090
1091int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1092{
1093 return pthread_cond_timedwait(cond, mutex, NULL);
1094}
1095
1096int __pthread_cond_timedwait_relative(pthread_cond_t *cond,
1097 pthread_mutex_t * mutex,
1098 const struct timespec *reltime)
1099{
1100 int status;
1101 int oldvalue = cond->value;
1102
1103 pthread_mutex_unlock(mutex);
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001104 status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond), oldvalue, reltime);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001105 pthread_mutex_lock(mutex);
1106
1107 if (status == (-ETIMEDOUT)) return ETIMEDOUT;
1108 return 0;
1109}
1110
1111int __pthread_cond_timedwait(pthread_cond_t *cond,
1112 pthread_mutex_t * mutex,
1113 const struct timespec *abstime,
1114 clockid_t clock)
1115{
1116 struct timespec ts;
1117 struct timespec * tsp;
1118
1119 if (abstime != NULL) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001120 if (__timespec_to_absolute(&ts, abstime, clock) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001121 return ETIMEDOUT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001122 tsp = &ts;
1123 } else {
1124 tsp = NULL;
1125 }
1126
1127 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
1128}
1129
1130int pthread_cond_timedwait(pthread_cond_t *cond,
1131 pthread_mutex_t * mutex,
1132 const struct timespec *abstime)
1133{
1134 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME);
1135}
1136
1137
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001138/* this one exists only for backward binary compatibility */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001139int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
1140 pthread_mutex_t * mutex,
1141 const struct timespec *abstime)
1142{
1143 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1144}
1145
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001146int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
1147 pthread_mutex_t * mutex,
1148 const struct timespec *abstime)
1149{
1150 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1151}
1152
1153int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
1154 pthread_mutex_t * mutex,
1155 const struct timespec *reltime)
1156{
1157 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
1158}
1159
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001160int pthread_cond_timeout_np(pthread_cond_t *cond,
1161 pthread_mutex_t * mutex,
1162 unsigned msecs)
1163{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001164 struct timespec ts;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001165
1166 ts.tv_sec = msecs / 1000;
1167 ts.tv_nsec = (msecs % 1000) * 1000000;
1168
Matthieu CASTETa4e67f42008-12-27 00:04:10 +01001169 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001170}
1171
1172
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001173/* NOTE: this implementation doesn't support a init function that throws a C++ exception
1174 * or calls fork()
1175 */
Elliott Hughes9d23e042013-02-15 19:21:51 -08001176int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001177{
Andy McFaddenb1c9cc22010-09-23 12:30:12 -07001178 volatile pthread_once_t* ocptr = once_control;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001179
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001180 /* PTHREAD_ONCE_INIT is 0, we use the following bit flags
1181 *
1182 * bit 0 set -> initialization is under way
1183 * bit 1 set -> initialization is complete
1184 */
1185#define ONCE_INITIALIZING (1 << 0)
1186#define ONCE_COMPLETED (1 << 1)
1187
1188 /* First check if the once is already initialized. This will be the common
1189 * case and we want to make this as fast as possible. Note that this still
1190 * requires a load_acquire operation here to ensure that all the
1191 * stores performed by the initialization function are observable on
1192 * this CPU after we exit.
1193 */
Elliott Hughesd4e753f2013-07-16 12:45:46 -07001194 if (__predict_true((*ocptr & ONCE_COMPLETED) != 0)) {
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001195 ANDROID_MEMBAR_FULL();
1196 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001197 }
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001198
1199 for (;;) {
1200 /* Try to atomically set the INITIALIZING flag.
1201 * This requires a cmpxchg loop, and we may need
Elliott Hughesbfeab1b2012-09-05 17:47:37 -07001202 * to exit prematurely if we detect that
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01001203 * COMPLETED is now set.
1204 */
1205 int32_t oldval, newval;
1206
1207 do {
1208 oldval = *ocptr;
1209 if ((oldval & ONCE_COMPLETED) != 0)
1210 break;
1211
1212 newval = oldval | ONCE_INITIALIZING;
1213 } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0);
1214
1215 if ((oldval & ONCE_COMPLETED) != 0) {
1216 /* We detected that COMPLETED was set while in our loop */
1217 ANDROID_MEMBAR_FULL();
1218 return 0;
1219 }
1220
1221 if ((oldval & ONCE_INITIALIZING) == 0) {
1222 /* We got there first, we can jump out of the loop to
1223 * handle the initialization */
1224 break;
1225 }
1226
1227 /* Another thread is running the initialization and hasn't completed
1228 * yet, so wait for it, then try again. */
1229 __futex_wait_ex(ocptr, 0, oldval, NULL);
1230 }
1231
1232 /* call the initialization function. */
1233 (*init_routine)();
1234
1235 /* Do a store_release indicating that initialization is complete */
1236 ANDROID_MEMBAR_FULL();
1237 *ocptr = ONCE_COMPLETED;
1238
1239 /* Wake up any waiters, if any */
1240 __futex_wake_ex(ocptr, 0, INT_MAX);
1241
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001242 return 0;
1243}
André Goddard Rosa78c1c042010-05-19 23:17:16 -03001244
Elliott Hughes40eabe22013-02-14 18:59:37 -08001245pid_t __pthread_gettid(pthread_t thid) {
1246 pthread_internal_t* thread = (pthread_internal_t*) thid;
1247 return thread->tid;
Glenn Kastend53cae02011-07-11 15:41:28 -07001248}
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001249
Elliott Hughes40eabe22013-02-14 18:59:37 -08001250int __pthread_settid(pthread_t thid, pid_t tid) {
1251 if (thid == 0) {
1252 return EINVAL;
1253 }
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001254
Elliott Hughes40eabe22013-02-14 18:59:37 -08001255 pthread_internal_t* thread = (pthread_internal_t*) thid;
1256 thread->tid = tid;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001257
Elliott Hughes40eabe22013-02-14 18:59:37 -08001258 return 0;
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07001259}