blob: 73e4a6403fd809f23e3bc00072168a0ebcfc0809 [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
29#include <assert.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <limits.h>
33#include <malloc.h>
34#include <memory.h>
35#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036#include <signal.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <stdlib.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <sys/atomics.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#include <sys/mman.h>
André Goddard Rosa78c1c042010-05-19 23:17:16 -030042#include <sys/prctl.h>
43#include <sys/stat.h>
Pierre Peifferd0c884d2012-02-22 16:40:15 +010044#include <sys/types.h>
45#include <time.h>
46#include <unistd.h>
47
48#include "bionic_atomic_inline.h"
49#include "bionic_futex.h"
50#include "bionic_pthread.h"
51#include "bionic_tls.h"
52#include "pthread_internal.h"
53#include "thread_private.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054
Mathias Agopian7c0c3792011-09-05 23:54:55 -070055extern void pthread_debug_mutex_lock_check(pthread_mutex_t *mutex);
56extern void pthread_debug_mutex_unlock_check(pthread_mutex_t *mutex);
57
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058extern int __pthread_clone(int (*fn)(void*), void *child_stack, int flags, void *arg);
59extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
60extern void _exit_thread(int retCode);
61extern int __set_errno(int);
62
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -070063int __futex_wake_ex(volatile void *ftx, int pshared, int val)
64{
65 return __futex_syscall3(ftx, pshared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, val);
66}
67
68int __futex_wait_ex(volatile void *ftx, int pshared, int val, const struct timespec *timeout)
69{
70 return __futex_syscall4(ftx, pshared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, val, timeout);
71}
72
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -070073#define __likely(cond) __builtin_expect(!!(cond), 1)
74#define __unlikely(cond) __builtin_expect(!!(cond), 0)
75
Bruce Beare8e551a62011-03-28 09:47:35 -070076#ifdef __i386__
77#define ATTRIBUTES __attribute__((noinline)) __attribute__((fastcall))
78#else
79#define ATTRIBUTES __attribute__((noinline))
80#endif
81
82void ATTRIBUTES _thread_created_hook(pid_t thread_id);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Pierre Peifferd0c884d2012-02-22 16:40:15 +010084static const int kPthreadInitFailed = 1;
85
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086#define PTHREAD_ATTR_FLAG_DETACHED 0x00000001
87#define PTHREAD_ATTR_FLAG_USER_STACK 0x00000002
88
89#define DEFAULT_STACKSIZE (1024 * 1024)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
91static pthread_mutex_t mmap_lock = PTHREAD_MUTEX_INITIALIZER;
92
93
94static const pthread_attr_t gDefaultPthreadAttr = {
95 .flags = 0,
96 .stack_base = NULL,
97 .stack_size = DEFAULT_STACKSIZE,
98 .guard_size = PAGE_SIZE,
99 .sched_policy = SCHED_NORMAL,
100 .sched_priority = 0
101};
102
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100103static pthread_internal_t* gThreadList = NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104static pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER;
105static pthread_mutex_t gDebuggerNotificationLock = PTHREAD_MUTEX_INITIALIZER;
106
107
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108static void
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100109_pthread_internal_free(pthread_internal_t* thread)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110{
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100111 if (thread != NULL) {
112 free(thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800113 }
114}
115
116
117static void
118_pthread_internal_remove_locked( pthread_internal_t* thread )
119{
120 thread->next->pref = thread->pref;
121 thread->pref[0] = thread->next;
122}
123
124static void
125_pthread_internal_remove( pthread_internal_t* thread )
126{
127 pthread_mutex_lock(&gThreadListLock);
128 _pthread_internal_remove_locked(thread);
129 pthread_mutex_unlock(&gThreadListLock);
130}
131
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400132__LIBC_ABI_PRIVATE__ void
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133_pthread_internal_add( pthread_internal_t* thread )
134{
135 pthread_mutex_lock(&gThreadListLock);
136 thread->pref = &gThreadList;
137 thread->next = thread->pref[0];
138 if (thread->next)
139 thread->next->pref = &thread->next;
140 thread->pref[0] = thread;
141 pthread_mutex_unlock(&gThreadListLock);
142}
143
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400144__LIBC_ABI_PRIVATE__ pthread_internal_t*
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800145__get_thread(void)
146{
147 void** tls = (void**)__get_tls();
148
149 return (pthread_internal_t*) tls[TLS_SLOT_THREAD_ID];
150}
151
152
153void*
154__get_stack_base(int *p_stack_size)
155{
156 pthread_internal_t* thread = __get_thread();
157
158 *p_stack_size = thread->attr.stack_size;
159 return thread->attr.stack_base;
160}
161
162
163void __init_tls(void** tls, void* thread)
164{
165 int nn;
166
167 ((pthread_internal_t*)thread)->tls = tls;
168
169 // slot 0 must point to the tls area, this is required by the implementation
170 // of the x86 Linux kernel thread-local-storage
171 tls[TLS_SLOT_SELF] = (void*)tls;
172 tls[TLS_SLOT_THREAD_ID] = thread;
173 for (nn = TLS_SLOT_ERRNO; nn < BIONIC_TLS_SLOTS; nn++)
174 tls[nn] = 0;
175
176 __set_tls( (void*)tls );
177}
178
179
180/*
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100181 * This trampoline is called from the assembly _pthread_clone() function.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182 */
183void __thread_entry(int (*func)(void*), void *arg, void **tls)
184{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 // Wait for our creating thread to release us. This lets it have time to
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100186 // notify gdb about this thread before we start doing anything.
Andy McFaddene2ac8982010-09-02 13:34:53 -0700187 //
188 // This also provides the memory barrier needed to ensure that all memory
189 // accesses previously made by the creating thread are visible to us.
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100190 pthread_mutex_t* start_mutex = (pthread_mutex_t*) &tls[TLS_SLOT_SELF];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191 pthread_mutex_lock(start_mutex);
192 pthread_mutex_destroy(start_mutex);
193
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100194 pthread_internal_t* thread = (pthread_internal_t*) tls[TLS_SLOT_THREAD_ID];
195 __init_tls(tls, thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100197 if ((thread->internal_flags & kPthreadInitFailed) != 0) {
198 pthread_exit(NULL);
199 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100201 int result = func(arg);
202 pthread_exit((void*) result);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203}
204
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +0400205__LIBC_ABI_PRIVATE__
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100206int _init_thread(pthread_internal_t * thread, pid_t kernel_id, pthread_attr_t * attr, void * stack_base)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207{
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100208 int error = 0;
209
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210 if (attr == NULL) {
211 thread->attr = gDefaultPthreadAttr;
212 } else {
213 thread->attr = *attr;
214 }
215 thread->attr.stack_base = stack_base;
216 thread->kernel_id = kernel_id;
217
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100218 // Make a note of whether the user supplied this stack (so we know whether or not to free it).
219 if (attr->stack_base == stack_base) {
220 thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_STACK;
221 }
222
223 // Set the scheduling policy/priority of the thread.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224 if (thread->attr.sched_policy != SCHED_NORMAL) {
225 struct sched_param param;
226 param.sched_priority = thread->attr.sched_priority;
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100227 if (sched_setscheduler(kernel_id, thread->attr.sched_policy, &param) == -1) {
228 error = errno;
229 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230 }
231
232 pthread_cond_init(&thread->join_cond, NULL);
233 thread->join_count = 0;
234
235 thread->cleanup_stack = NULL;
236
237 _pthread_internal_add(thread);
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100238 return error;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239}
240
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241static void *mkstack(size_t size, size_t guard_size)
242{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243 pthread_mutex_lock(&mmap_lock);
244
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100245 int prot = PROT_READ | PROT_WRITE;
246 int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE;
Elliott Hughes9c3eca72012-05-08 13:26:28 -0700247 void* stack = mmap(NULL, size, prot, flags, -1, 0);
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100248 if (stack == MAP_FAILED) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800249 stack = NULL;
250 goto done;
251 }
252
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100253 if (mprotect(stack, guard_size, PROT_NONE) == -1) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254 munmap(stack, size);
255 stack = NULL;
256 goto done;
257 }
258
259done:
260 pthread_mutex_unlock(&mmap_lock);
261 return stack;
262}
263
264/*
Andy McFaddene2ac8982010-09-02 13:34:53 -0700265 * Create a new thread. The thread's stack is laid out like so:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266 *
267 * +---------------------------+
268 * | pthread_internal_t |
269 * +---------------------------+
270 * | |
271 * | TLS area |
272 * | |
273 * +---------------------------+
274 * | |
275 * . .
276 * . stack area .
277 * . .
278 * | |
279 * +---------------------------+
280 * | guard page |
281 * +---------------------------+
282 *
283 * note that TLS[0] must be a pointer to itself, this is required
284 * by the thread-local storage implementation of the x86 Linux
285 * kernel, where the TLS pointer is read by reading fs:[0]
286 */
287int pthread_create(pthread_t *thread_out, pthread_attr_t const * attr,
288 void *(*start_routine)(void *), void * arg)
289{
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100290 int old_errno = errno;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291
292 /* this will inform the rest of the C library that at least one thread
293 * was created. this will enforce certain functions to acquire/release
294 * locks (e.g. atexit()) to protect shared global structures.
295 *
296 * this works because pthread_create() is not called by the C library
297 * initialization routine that sets up the main thread's data structures.
298 */
299 __isthreaded = 1;
300
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100301 pthread_internal_t* thread = calloc(sizeof(*thread), 1);
302 if (thread == NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 return ENOMEM;
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100304 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305
306 if (attr == NULL) {
307 attr = &gDefaultPthreadAttr;
308 }
309
310 // make sure the stack is PAGE_SIZE aligned
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100311 size_t stack_size = (attr->stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
312 uint8_t* stack = attr->stack_base;
313 if (stack == NULL) {
314 stack = mkstack(stack_size, attr->guard_size);
315 if (stack == NULL) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316 _pthread_internal_free(thread);
317 return ENOMEM;
318 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800319 }
320
321 // Make room for TLS
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100322 void** tls = (void**)(stack + stack_size - BIONIC_TLS_SLOTS*sizeof(void*));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323
324 // Create a mutex for the thread in TLS_SLOT_SELF to wait on once it starts so we can keep
325 // it from doing anything until after we notify the debugger about it
Andy McFaddene2ac8982010-09-02 13:34:53 -0700326 //
327 // This also provides the memory barrier we need to ensure that all
328 // memory accesses previously performed by this thread are visible to
329 // the new thread.
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100330 pthread_mutex_t* start_mutex = (pthread_mutex_t*) &tls[TLS_SLOT_SELF];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800331 pthread_mutex_init(start_mutex, NULL);
332 pthread_mutex_lock(start_mutex);
333
334 tls[TLS_SLOT_THREAD_ID] = thread;
335
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100336 int flags = CLONE_FILES | CLONE_FS | CLONE_VM | CLONE_SIGHAND |
337 CLONE_THREAD | CLONE_SYSVSEM | CLONE_DETACHED;
338 int tid = __pthread_clone((int(*)(void*))start_routine, tls, flags, arg);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100340 if (tid < 0) {
341 int clone_errno = errno;
342 pthread_mutex_unlock(start_mutex);
343 if (stack != attr->stack_base) {
344 munmap(stack, stack_size);
345 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800346 _pthread_internal_free(thread);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800347 errno = old_errno;
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100348 return clone_errno;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 }
350
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100351 int init_errno = _init_thread(thread, tid, (pthread_attr_t*) attr, stack);
352 if (init_errno != 0) {
353 // Mark the thread detached and let its __thread_entry run to
354 // completion. (It'll just exit immediately, cleaning up its resources.)
355 thread->internal_flags |= kPthreadInitFailed;
356 thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED;
357 pthread_mutex_unlock(start_mutex);
358 errno = old_errno;
359 return init_errno;
360 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100362 // Notify any debuggers about the new thread.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363 pthread_mutex_lock(&gDebuggerNotificationLock);
364 _thread_created_hook(tid);
365 pthread_mutex_unlock(&gDebuggerNotificationLock);
366
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100367 // Let the thread run.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368 pthread_mutex_unlock(start_mutex);
369
Pierre Peifferd0c884d2012-02-22 16:40:15 +0100370 *thread_out = (pthread_t) thread;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800371 return 0;
372}
373
374
375int pthread_attr_init(pthread_attr_t * attr)
376{
377 *attr = gDefaultPthreadAttr;
378 return 0;
379}
380
381int pthread_attr_destroy(pthread_attr_t * attr)
382{
383 memset(attr, 0x42, sizeof(pthread_attr_t));
384 return 0;
385}
386
387int pthread_attr_setdetachstate(pthread_attr_t * attr, int state)
388{
389 if (state == PTHREAD_CREATE_DETACHED) {
390 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
391 } else if (state == PTHREAD_CREATE_JOINABLE) {
392 attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED;
393 } else {
394 return EINVAL;
395 }
396 return 0;
397}
398
399int pthread_attr_getdetachstate(pthread_attr_t const * attr, int * state)
400{
401 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED)
402 ? PTHREAD_CREATE_DETACHED
403 : PTHREAD_CREATE_JOINABLE;
404 return 0;
405}
406
407int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy)
408{
409 attr->sched_policy = policy;
410 return 0;
411}
412
413int pthread_attr_getschedpolicy(pthread_attr_t const * attr, int * policy)
414{
415 *policy = attr->sched_policy;
416 return 0;
417}
418
419int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const * param)
420{
421 attr->sched_priority = param->sched_priority;
422 return 0;
423}
424
425int pthread_attr_getschedparam(pthread_attr_t const * attr, struct sched_param * param)
426{
427 param->sched_priority = attr->sched_priority;
428 return 0;
429}
430
431int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size)
432{
433 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
434 return EINVAL;
435 }
436 attr->stack_size = stack_size;
437 return 0;
438}
439
440int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t * stack_size)
441{
442 *stack_size = attr->stack_size;
443 return 0;
444}
445
446int pthread_attr_setstackaddr(pthread_attr_t * attr, void * stack_addr)
447{
448#if 1
449 // It's not clear if this is setting the top or bottom of the stack, so don't handle it for now.
450 return ENOSYS;
451#else
452 if ((uint32_t)stack_addr & (PAGE_SIZE - 1)) {
453 return EINVAL;
454 }
455 attr->stack_base = stack_addr;
456 return 0;
457#endif
458}
459
460int pthread_attr_getstackaddr(pthread_attr_t const * attr, void ** stack_addr)
461{
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700462 *stack_addr = (char*)attr->stack_base + attr->stack_size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800463 return 0;
464}
465
466int pthread_attr_setstack(pthread_attr_t * attr, void * stack_base, size_t stack_size)
467{
468 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
469 return EINVAL;
470 }
471 if ((uint32_t)stack_base & (PAGE_SIZE - 1)) {
472 return EINVAL;
473 }
474 attr->stack_base = stack_base;
475 attr->stack_size = stack_size;
476 return 0;
477}
478
479int pthread_attr_getstack(pthread_attr_t const * attr, void ** stack_base, size_t * stack_size)
480{
481 *stack_base = attr->stack_base;
482 *stack_size = attr->stack_size;
483 return 0;
484}
485
486int pthread_attr_setguardsize(pthread_attr_t * attr, size_t guard_size)
487{
488 if (guard_size & (PAGE_SIZE - 1) || guard_size < PAGE_SIZE) {
489 return EINVAL;
490 }
491
492 attr->guard_size = guard_size;
493 return 0;
494}
495
496int pthread_attr_getguardsize(pthread_attr_t const * attr, size_t * guard_size)
497{
498 *guard_size = attr->guard_size;
499 return 0;
500}
501
502int pthread_getattr_np(pthread_t thid, pthread_attr_t * attr)
503{
504 pthread_internal_t * thread = (pthread_internal_t *)thid;
505 *attr = thread->attr;
506 return 0;
507}
508
509int pthread_attr_setscope(pthread_attr_t *attr, int scope)
510{
511 if (scope == PTHREAD_SCOPE_SYSTEM)
512 return 0;
513 if (scope == PTHREAD_SCOPE_PROCESS)
514 return ENOTSUP;
515
516 return EINVAL;
517}
518
519int pthread_attr_getscope(pthread_attr_t const *attr)
520{
521 return PTHREAD_SCOPE_SYSTEM;
522}
523
524
525/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
526 * and thread cancelation
527 */
528
529void __pthread_cleanup_push( __pthread_cleanup_t* c,
530 __pthread_cleanup_func_t routine,
531 void* arg )
532{
533 pthread_internal_t* thread = __get_thread();
534
535 c->__cleanup_routine = routine;
536 c->__cleanup_arg = arg;
537 c->__cleanup_prev = thread->cleanup_stack;
538 thread->cleanup_stack = c;
539}
540
541void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
542{
543 pthread_internal_t* thread = __get_thread();
544
545 thread->cleanup_stack = c->__cleanup_prev;
546 if (execute)
547 c->__cleanup_routine(c->__cleanup_arg);
548}
549
550/* used by pthread_exit() to clean all TLS keys of the current thread */
551static void pthread_key_clean_all(void);
552
553void pthread_exit(void * retval)
554{
555 pthread_internal_t* thread = __get_thread();
556 void* stack_base = thread->attr.stack_base;
557 int stack_size = thread->attr.stack_size;
558 int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
Jack Rene480fc82011-09-21 12:44:11 +0200559 sigset_t mask;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800560
561 // call the cleanup handlers first
562 while (thread->cleanup_stack) {
563 __pthread_cleanup_t* c = thread->cleanup_stack;
564 thread->cleanup_stack = c->__cleanup_prev;
565 c->__cleanup_routine(c->__cleanup_arg);
566 }
567
568 // call the TLS destructors, it is important to do that before removing this
569 // thread from the global list. this will ensure that if someone else deletes
570 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
571 // space (see pthread_key_delete)
572 pthread_key_clean_all();
573
574 // if the thread is detached, destroy the pthread_internal_t
575 // otherwise, keep it in memory and signal any joiners
576 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
577 _pthread_internal_remove(thread);
578 _pthread_internal_free(thread);
579 } else {
580 /* the join_count field is used to store the number of threads waiting for
581 * the termination of this thread with pthread_join(),
582 *
583 * if it is positive we need to signal the waiters, and we do not touch
584 * the count (it will be decremented by the waiters, the last one will
585 * also remove/free the thread structure
586 *
587 * if it is zero, we set the count value to -1 to indicate that the
588 * thread is in 'zombie' state: it has stopped executing, and its stack
589 * is gone (as well as its TLS area). when another thread calls pthread_join()
590 * on it, it will immediately free the thread and return.
591 */
592 pthread_mutex_lock(&gThreadListLock);
593 thread->return_value = retval;
594 if (thread->join_count > 0) {
595 pthread_cond_broadcast(&thread->join_cond);
596 } else {
597 thread->join_count = -1; /* zombie thread */
598 }
599 pthread_mutex_unlock(&gThreadListLock);
600 }
601
Jack Rene480fc82011-09-21 12:44:11 +0200602 sigfillset(&mask);
603 sigdelset(&mask, SIGSEGV);
604 (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL);
605
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800606 // destroy the thread stack
607 if (user_stack)
608 _exit_thread((int)retval);
609 else
610 _exit_with_stack_teardown(stack_base, stack_size, (int)retval);
611}
612
613int pthread_join(pthread_t thid, void ** ret_val)
614{
615 pthread_internal_t* thread = (pthread_internal_t*)thid;
616 int count;
617
618 // check that the thread still exists and is not detached
619 pthread_mutex_lock(&gThreadListLock);
620
621 for (thread = gThreadList; thread != NULL; thread = thread->next)
622 if (thread == (pthread_internal_t*)thid)
André Goddard Rosaa28336c2010-02-05 16:21:07 -0200623 goto FoundIt;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800624
André Goddard Rosaa28336c2010-02-05 16:21:07 -0200625 pthread_mutex_unlock(&gThreadListLock);
626 return ESRCH;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800627
André Goddard Rosaa28336c2010-02-05 16:21:07 -0200628FoundIt:
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800629 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
630 pthread_mutex_unlock(&gThreadListLock);
631 return EINVAL;
632 }
633
634 /* wait for thread death when needed
635 *
636 * if the 'join_count' is negative, this is a 'zombie' thread that
637 * is already dead and without stack/TLS
638 *
639 * otherwise, we need to increment 'join-count' and wait to be signaled
640 */
641 count = thread->join_count;
642 if (count >= 0) {
643 thread->join_count += 1;
644 pthread_cond_wait( &thread->join_cond, &gThreadListLock );
645 count = --thread->join_count;
646 }
647 if (ret_val)
648 *ret_val = thread->return_value;
649
650 /* remove thread descriptor when we're the last joiner or when the
651 * thread was already a zombie.
652 */
653 if (count <= 0) {
654 _pthread_internal_remove_locked(thread);
655 _pthread_internal_free(thread);
656 }
657 pthread_mutex_unlock(&gThreadListLock);
658 return 0;
659}
660
661int pthread_detach( pthread_t thid )
662{
663 pthread_internal_t* thread;
664 int result = 0;
665 int flags;
666
667 pthread_mutex_lock(&gThreadListLock);
668 for (thread = gThreadList; thread != NULL; thread = thread->next)
669 if (thread == (pthread_internal_t*)thid)
670 goto FoundIt;
671
672 result = ESRCH;
673 goto Exit;
674
675FoundIt:
676 do {
677 flags = thread->attr.flags;
678
679 if ( flags & PTHREAD_ATTR_FLAG_DETACHED ) {
680 /* thread is not joinable ! */
681 result = EINVAL;
682 goto Exit;
683 }
684 }
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +0100685 while ( __bionic_cmpxchg( flags, flags | PTHREAD_ATTR_FLAG_DETACHED,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800686 (volatile int*)&thread->attr.flags ) != 0 );
687Exit:
688 pthread_mutex_unlock(&gThreadListLock);
689 return result;
690}
691
692pthread_t pthread_self(void)
693{
694 return (pthread_t)__get_thread();
695}
696
697int pthread_equal(pthread_t one, pthread_t two)
698{
699 return (one == two ? 1 : 0);
700}
701
702int pthread_getschedparam(pthread_t thid, int * policy,
703 struct sched_param * param)
704{
705 int old_errno = errno;
706
707 pthread_internal_t * thread = (pthread_internal_t *)thid;
708 int err = sched_getparam(thread->kernel_id, param);
709 if (!err) {
710 *policy = sched_getscheduler(thread->kernel_id);
711 } else {
712 err = errno;
713 errno = old_errno;
714 }
715 return err;
716}
717
718int pthread_setschedparam(pthread_t thid, int policy,
719 struct sched_param const * param)
720{
721 pthread_internal_t * thread = (pthread_internal_t *)thid;
722 int old_errno = errno;
723 int ret;
724
725 ret = sched_setscheduler(thread->kernel_id, policy, param);
726 if (ret < 0) {
727 ret = errno;
728 errno = old_errno;
729 }
730 return ret;
731}
732
733
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800734/* a mutex is implemented as a 32-bit integer holding the following fields
735 *
736 * bits: name description
737 * 31-16 tid owner thread's kernel id (recursive and errorcheck only)
738 * 15-14 type mutex type
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700739 * 13 shared process-shared flag
740 * 12-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741 * 1-0 state lock state (0, 1 or 2)
742 */
743
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100744/* Convenience macro, creates a mask of 'bits' bits that starts from
745 * the 'shift'-th least significant bit in a 32-bit word.
746 *
747 * Examples: FIELD_MASK(0,4) -> 0xf
748 * FIELD_MASK(16,9) -> 0x1ff0000
749 */
750#define FIELD_MASK(shift,bits) (((1 << (bits))-1) << (shift))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800751
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100752/* This one is used to create a bit pattern from a given field value */
753#define FIELD_TO_BITS(val,shift,bits) (((val) & ((1 << (bits))-1)) << (shift))
David 'Digit' Turner022d3032011-12-07 14:02:17 +0100754
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100755/* And this one does the opposite, i.e. extract a field's value from a bit pattern */
756#define FIELD_FROM_BITS(val,shift,bits) (((val) >> (shift)) & ((1 << (bits))-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800757
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100758/* Mutex state:
759 *
760 * 0 for unlocked
761 * 1 for locked, no waiters
762 * 2 for locked, maybe waiters
763 */
764#define MUTEX_STATE_SHIFT 0
765#define MUTEX_STATE_LEN 2
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800766
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100767#define MUTEX_STATE_MASK FIELD_MASK(MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
768#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
769#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
770
771#define MUTEX_STATE_UNLOCKED 0 /* must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
772#define MUTEX_STATE_LOCKED_UNCONTENDED 1 /* must be 1 due to atomic dec in unlock operation */
773#define MUTEX_STATE_LOCKED_CONTENDED 2 /* must be 1 + LOCKED_UNCONTENDED due to atomic dec */
774
775#define MUTEX_STATE_FROM_BITS(v) FIELD_FROM_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
776#define MUTEX_STATE_TO_BITS(v) FIELD_TO_BITS(v, MUTEX_STATE_SHIFT, MUTEX_STATE_LEN)
777
778#define MUTEX_STATE_BITS_UNLOCKED MUTEX_STATE_TO_BITS(MUTEX_STATE_UNLOCKED)
779#define MUTEX_STATE_BITS_LOCKED_UNCONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_UNCONTENDED)
780#define MUTEX_STATE_BITS_LOCKED_CONTENDED MUTEX_STATE_TO_BITS(MUTEX_STATE_LOCKED_CONTENDED)
781
782/* return true iff the mutex if locked with no waiters */
783#define MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_UNCONTENDED)
784
785/* return true iff the mutex if locked with maybe waiters */
786#define MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(v) (((v) & MUTEX_STATE_MASK) == MUTEX_STATE_BITS_LOCKED_CONTENDED)
787
788/* used to flip from LOCKED_UNCONTENDED to LOCKED_CONTENDED */
789#define MUTEX_STATE_BITS_FLIP_CONTENTION(v) ((v) ^ (MUTEX_STATE_BITS_LOCKED_CONTENDED ^ MUTEX_STATE_BITS_LOCKED_UNCONTENDED))
790
791/* Mutex counter:
792 *
793 * We need to check for overflow before incrementing, and we also need to
794 * detect when the counter is 0
795 */
796#define MUTEX_COUNTER_SHIFT 2
797#define MUTEX_COUNTER_LEN 11
798#define MUTEX_COUNTER_MASK FIELD_MASK(MUTEX_COUNTER_SHIFT, MUTEX_COUNTER_LEN)
799
800#define MUTEX_COUNTER_BITS_WILL_OVERFLOW(v) (((v) & MUTEX_COUNTER_MASK) == MUTEX_COUNTER_MASK)
801#define MUTEX_COUNTER_BITS_IS_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
802
803/* Used to increment the counter directly after overflow has been checked */
804#define MUTEX_COUNTER_BITS_ONE FIELD_TO_BITS(1,MUTEX_COUNTER_SHIFT,MUTEX_COUNTER_LEN)
805
806/* Returns true iff the counter is 0 */
807#define MUTEX_COUNTER_BITS_ARE_ZERO(v) (((v) & MUTEX_COUNTER_MASK) == 0)
808
809/* Mutex shared bit flag
810 *
811 * This flag is set to indicate that the mutex is shared among processes.
812 * This changes the futex opcode we use for futex wait/wake operations
813 * (non-shared operations are much faster).
814 */
815#define MUTEX_SHARED_SHIFT 13
816#define MUTEX_SHARED_MASK FIELD_MASK(MUTEX_SHARED_SHIFT,1)
817
818/* Mutex type:
819 *
820 * We support normal, recursive and errorcheck mutexes.
821 *
822 * The constants defined here *cannot* be changed because they must match
823 * the C library ABI which defines the following initialization values in
824 * <pthread.h>:
825 *
826 * __PTHREAD_MUTEX_INIT_VALUE
827 * __PTHREAD_RECURSIVE_MUTEX_VALUE
828 * __PTHREAD_ERRORCHECK_MUTEX_INIT_VALUE
829 */
830#define MUTEX_TYPE_SHIFT 14
831#define MUTEX_TYPE_LEN 2
832#define MUTEX_TYPE_MASK FIELD_MASK(MUTEX_TYPE_SHIFT,MUTEX_TYPE_LEN)
833
834#define MUTEX_TYPE_NORMAL 0 /* Must be 0 to match __PTHREAD_MUTEX_INIT_VALUE */
835#define MUTEX_TYPE_RECURSIVE 1
836#define MUTEX_TYPE_ERRORCHECK 2
837
838#define MUTEX_TYPE_TO_BITS(t) FIELD_TO_BITS(t, MUTEX_TYPE_SHIFT, MUTEX_TYPE_LEN)
839
840#define MUTEX_TYPE_BITS_NORMAL MUTEX_TYPE_TO_BITS(MUTEX_TYPE_NORMAL)
841#define MUTEX_TYPE_BITS_RECURSIVE MUTEX_TYPE_TO_BITS(MUTEX_TYPE_RECURSIVE)
842#define MUTEX_TYPE_BITS_ERRORCHECK MUTEX_TYPE_TO_BITS(MUTEX_TYPE_ERRORCHECK)
843
844/* Mutex owner field:
845 *
846 * This is only used for recursive and errorcheck mutexes. It holds the
847 * kernel TID of the owning thread. Note that this works because the Linux
848 * kernel _only_ uses 16-bit values for thread ids.
849 *
850 * More specifically, it will wrap to 10000 when it reaches over 32768 for
851 * application processes. You can check this by running the following inside
852 * an adb shell session:
853 *
854 OLDPID=$$;
855 while true; do
856 NEWPID=$(sh -c 'echo $$')
857 if [ "$NEWPID" -gt 32768 ]; then
858 echo "AARGH: new PID $NEWPID is too high!"
859 exit 1
860 fi
861 if [ "$NEWPID" -lt "$OLDPID" ]; then
862 echo "****** Wrapping from PID $OLDPID to $NEWPID. *******"
863 else
864 echo -n "$NEWPID!"
865 fi
866 OLDPID=$NEWPID
867 done
868
869 * Note that you can run the same example on a desktop Linux system,
870 * the wrapping will also happen at 32768, but will go back to 300 instead.
871 */
872#define MUTEX_OWNER_SHIFT 16
873#define MUTEX_OWNER_LEN 16
874
875#define MUTEX_OWNER_FROM_BITS(v) FIELD_FROM_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
876#define MUTEX_OWNER_TO_BITS(v) FIELD_TO_BITS(v,MUTEX_OWNER_SHIFT,MUTEX_OWNER_LEN)
877
878/* Convenience macros.
879 *
880 * These are used to form or modify the bit pattern of a given mutex value
881 */
882
883
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800884
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700885/* a mutex attribute holds the following fields
886 *
887 * bits: name description
888 * 0-3 type type of mutex
889 * 4 shared process-shared flag
890 */
891#define MUTEXATTR_TYPE_MASK 0x000f
892#define MUTEXATTR_SHARED_MASK 0x0010
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800893
894
895int pthread_mutexattr_init(pthread_mutexattr_t *attr)
896{
897 if (attr) {
898 *attr = PTHREAD_MUTEX_DEFAULT;
899 return 0;
900 } else {
901 return EINVAL;
902 }
903}
904
905int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
906{
907 if (attr) {
908 *attr = -1;
909 return 0;
910 } else {
911 return EINVAL;
912 }
913}
914
915int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
916{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700917 if (attr) {
918 int atype = (*attr & MUTEXATTR_TYPE_MASK);
919
920 if (atype >= PTHREAD_MUTEX_NORMAL &&
921 atype <= PTHREAD_MUTEX_ERRORCHECK) {
922 *type = atype;
923 return 0;
924 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800925 }
926 return EINVAL;
927}
928
929int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
930{
931 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
932 type <= PTHREAD_MUTEX_ERRORCHECK ) {
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700933 *attr = (*attr & ~MUTEXATTR_TYPE_MASK) | type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800934 return 0;
935 }
936 return EINVAL;
937}
938
939/* process-shared mutexes are not supported at the moment */
940
941int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
942{
943 if (!attr)
944 return EINVAL;
945
Mathias Agopianb7681162009-07-13 22:00:33 -0700946 switch (pshared) {
947 case PTHREAD_PROCESS_PRIVATE:
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700948 *attr &= ~MUTEXATTR_SHARED_MASK;
949 return 0;
950
Mathias Agopianb7681162009-07-13 22:00:33 -0700951 case PTHREAD_PROCESS_SHARED:
952 /* our current implementation of pthread actually supports shared
953 * mutexes but won't cleanup if a process dies with the mutex held.
954 * Nevertheless, it's better than nothing. Shared mutexes are used
955 * by surfaceflinger and audioflinger.
956 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700957 *attr |= MUTEXATTR_SHARED_MASK;
Mathias Agopianb7681162009-07-13 22:00:33 -0700958 return 0;
959 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700960 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961}
962
963int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
964{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700965 if (!attr || !pshared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800966 return EINVAL;
967
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700968 *pshared = (*attr & MUTEXATTR_SHARED_MASK) ? PTHREAD_PROCESS_SHARED
969 : PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800970 return 0;
971}
972
973int pthread_mutex_init(pthread_mutex_t *mutex,
974 const pthread_mutexattr_t *attr)
975{
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700976 int value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800977
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700978 if (mutex == NULL)
979 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800980
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700981 if (__likely(attr == NULL)) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100982 mutex->value = MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700983 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800984 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700985
986 if ((*attr & MUTEXATTR_SHARED_MASK) != 0)
987 value |= MUTEX_SHARED_MASK;
988
989 switch (*attr & MUTEXATTR_TYPE_MASK) {
990 case PTHREAD_MUTEX_NORMAL:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100991 value |= MUTEX_TYPE_BITS_NORMAL;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700992 break;
993 case PTHREAD_MUTEX_RECURSIVE:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100994 value |= MUTEX_TYPE_BITS_RECURSIVE;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700995 break;
996 case PTHREAD_MUTEX_ERRORCHECK:
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +0100997 value |= MUTEX_TYPE_BITS_ERRORCHECK;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -0700998 break;
999 default:
1000 return EINVAL;
1001 }
1002
1003 mutex->value = value;
1004 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001005}
1006
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001007
1008/*
1009 * Lock a non-recursive mutex.
1010 *
1011 * As noted above, there are three states:
1012 * 0 (unlocked, no contention)
1013 * 1 (locked, no contention)
1014 * 2 (locked, contention)
1015 *
1016 * Non-recursive mutexes don't use the thread-id or counter fields, and the
1017 * "type" value is zero, so the only bits that will be set are the ones in
1018 * the lock state field.
1019 */
1020static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001021_normal_lock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001022{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001023 /* convenience shortcuts */
1024 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
1025 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001026 /*
1027 * The common case is an unlocked mutex, so we begin by trying to
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001028 * change the lock's state from 0 (UNLOCKED) to 1 (LOCKED).
1029 * __bionic_cmpxchg() returns 0 if it made the swap successfully.
1030 * If the result is nonzero, this lock is already held by another thread.
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001031 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001032 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) != 0) {
1033 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001034 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001035 * We want to go to sleep until the mutex is available, which
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001036 * requires promoting it to state 2 (CONTENDED). We need to
1037 * swap in the new state value and then wait until somebody wakes us up.
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001038 *
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001039 * __bionic_swap() returns the previous value. We swap 2 in and
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001040 * see if we got zero back; if so, we have acquired the lock. If
1041 * not, another thread still holds the lock and we wait again.
1042 *
1043 * The second argument to the __futex_wait() call is compared
1044 * against the current value. If it doesn't match, __futex_wait()
1045 * returns immediately (otherwise, it sleeps for a time specified
1046 * by the third argument; 0 means sleep forever). This ensures
1047 * that the mutex is in state 2 when we go to sleep on it, which
1048 * guarantees a wake-up call.
1049 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001050 while (__bionic_swap(locked_contended, &mutex->value) != unlocked)
1051 __futex_wait_ex(&mutex->value, shared, locked_contended, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001052 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001053 ANDROID_MEMBAR_FULL();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001054}
1055
1056/*
1057 * Release a non-recursive mutex. The caller is responsible for determining
1058 * that we are in fact the owner of this lock.
1059 */
1060static __inline__ void
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001061_normal_unlock(pthread_mutex_t* mutex, int shared)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001062{
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001063 ANDROID_MEMBAR_FULL();
1064
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001065 /*
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001066 * The mutex state will be 1 or (rarely) 2. We use an atomic decrement
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001067 * to release the lock. __bionic_atomic_dec() returns the previous value;
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001068 * if it wasn't 1 we have to do some additional work.
1069 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001070 if (__bionic_atomic_dec(&mutex->value) != (shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001071 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001072 * Start by releasing the lock. The decrement changed it from
1073 * "contended lock" to "uncontended lock", which means we still
1074 * hold it, and anybody who tries to sneak in will push it back
1075 * to state 2.
1076 *
1077 * Once we set it to zero the lock is up for grabs. We follow
1078 * this with a __futex_wake() to ensure that one of the waiting
1079 * threads has a chance to grab it.
1080 *
1081 * This doesn't cause a race with the swap/wait pair in
1082 * _normal_lock(), because the __futex_wait() call there will
1083 * return immediately if the mutex value isn't 2.
1084 */
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001085 mutex->value = shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001086
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001087 /*
1088 * Wake up one waiting thread. We don't know which thread will be
1089 * woken or when it'll start executing -- futexes make no guarantees
1090 * here. There may not even be a thread waiting.
1091 *
1092 * The newly-woken thread will replace the 0 we just set above
1093 * with 2, which means that when it eventually releases the mutex
1094 * it will also call FUTEX_WAKE. This results in one extra wake
1095 * call whenever a lock is contended, but lets us avoid forgetting
1096 * anyone without requiring us to track the number of sleepers.
1097 *
1098 * It's possible for another thread to sneak in and grab the lock
1099 * between the zero assignment above and the wake call below. If
1100 * the new thread is "slow" and holds the lock for a while, we'll
1101 * wake up a sleeper, which will swap in a 2 and then go back to
1102 * sleep since the lock is still held. If the new thread is "fast",
1103 * running to completion before we call wake, the thread we
1104 * eventually wake will find an unlocked mutex and will execute.
1105 * Either way we have correct behavior and nobody is orphaned on
1106 * the wait queue.
1107 */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001108 __futex_wake_ex(&mutex->value, shared, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001109 }
1110}
1111
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001112/* This common inlined function is used to increment the counter of an
1113 * errorcheck or recursive mutex.
1114 *
1115 * For errorcheck mutexes, it will return EDEADLK
1116 * If the counter overflows, it will return EAGAIN
1117 * Otherwise, it atomically increments the counter and returns 0
1118 * after providing an acquire barrier.
1119 *
1120 * mtype is the current mutex type
1121 * mvalue is the current mutex value (already loaded)
1122 * mutex pointers to the mutex.
1123 */
1124static __inline__ __attribute__((always_inline)) int
1125_recursive_increment(pthread_mutex_t* mutex, int mvalue, int mtype)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001126{
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001127 if (mtype == MUTEX_TYPE_BITS_ERRORCHECK) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001128 /* trying to re-lock a mutex we already acquired */
1129 return EDEADLK;
1130 }
1131
1132 /* Detect recursive lock overflow and return EAGAIN.
1133 * This is safe because only the owner thread can modify the
David 'Digit' Turnerb57db752012-01-24 13:20:38 +01001134 * counter bits in the mutex value.
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001135 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001136 if (MUTEX_COUNTER_BITS_WILL_OVERFLOW(mvalue)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001137 return EAGAIN;
1138 }
1139
1140 /* We own the mutex, but other threads are able to change
David 'Digit' Turnerb57db752012-01-24 13:20:38 +01001141 * the lower bits (e.g. promoting it to "contended"), so we
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001142 * need to use an atomic cmpxchg loop to update the counter.
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001143 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001144 for (;;) {
1145 /* increment counter, overflow was already checked */
1146 int newval = mvalue + MUTEX_COUNTER_BITS_ONE;
1147 if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
1148 /* mutex is still locked, not need for a memory barrier */
1149 return 0;
1150 }
1151 /* the value was changed, this happens when another thread changes
1152 * the lower state bits from 1 to 2 to indicate contention. This
1153 * cannot change the counter, so simply reload and try again.
1154 */
1155 mvalue = mutex->value;
1156 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001157}
1158
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001159__LIBC_HIDDEN__
1160int pthread_mutex_lock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001161{
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001162 int mvalue, mtype, tid, new_lock_type, shared;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001163
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001164 if (__unlikely(mutex == NULL))
1165 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001166
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001167 mvalue = mutex->value;
1168 mtype = (mvalue & MUTEX_TYPE_MASK);
1169 shared = (mvalue & MUTEX_SHARED_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001170
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001171 /* Handle normal case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001172 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) ) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001173 _normal_lock(mutex, shared);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001174 return 0;
1175 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001176
1177 /* Do we already own this recursive or error-check mutex ? */
1178 tid = __get_thread()->kernel_id;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001179 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001180 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001181
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001182 /* Add in shared state to avoid extra 'or' operations below */
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001183 mtype |= shared;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001184
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001185 /* First, if the mutex is unlocked, try to quickly acquire it.
1186 * In the optimistic case where this works, set the state to 1 to
1187 * indicate locked with no contention */
1188 if (mvalue == mtype) {
1189 int newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
1190 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0) {
1191 ANDROID_MEMBAR_FULL();
1192 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001193 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001194 /* argh, the value changed, reload before entering the loop */
1195 mvalue = mutex->value;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001196 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001197
1198 for (;;) {
1199 int newval;
1200
1201 /* if the mutex is unlocked, its value should be 'mtype' and
1202 * we try to acquire it by setting its owner and state atomically.
1203 * NOTE: We put the state to 2 since we _know_ there is contention
1204 * when we are in this loop. This ensures all waiters will be
1205 * unlocked.
1206 */
1207 if (mvalue == mtype) {
1208 newval = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
1209 /* TODO: Change this to __bionic_cmpxchg_acquire when we
1210 * implement it to get rid of the explicit memory
1211 * barrier below.
1212 */
1213 if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
1214 mvalue = mutex->value;
1215 continue;
1216 }
1217 ANDROID_MEMBAR_FULL();
1218 return 0;
1219 }
1220
1221 /* the mutex is already locked by another thread, if its state is 1
1222 * we will change it to 2 to indicate contention. */
1223 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
1224 newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue); /* locked state 1 => state 2 */
1225 if (__unlikely(__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0)) {
1226 mvalue = mutex->value;
1227 continue;
1228 }
1229 mvalue = newval;
1230 }
1231
1232 /* wait until the mutex is unlocked */
1233 __futex_wait_ex(&mutex->value, shared, mvalue, NULL);
1234
1235 mvalue = mutex->value;
1236 }
1237 /* NOTREACHED */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001238}
1239
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001240int pthread_mutex_lock(pthread_mutex_t *mutex)
1241{
1242 int err = pthread_mutex_lock_impl(mutex);
1243#ifdef PTHREAD_DEBUG
1244 if (PTHREAD_DEBUG_ENABLED) {
1245 if (!err) {
1246 pthread_debug_mutex_lock_check(mutex);
1247 }
1248 }
1249#endif
1250 return err;
1251}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001252
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001253__LIBC_HIDDEN__
1254int pthread_mutex_unlock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001255{
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001256 int mvalue, mtype, tid, oldv, shared;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001257
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001258 if (__unlikely(mutex == NULL))
1259 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001260
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001261 mvalue = mutex->value;
1262 mtype = (mvalue & MUTEX_TYPE_MASK);
1263 shared = (mvalue & MUTEX_SHARED_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001264
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001265 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001266 if (__likely(mtype == MUTEX_TYPE_BITS_NORMAL)) {
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001267 _normal_unlock(mutex, shared);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001268 return 0;
1269 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001270
1271 /* Do we already own this recursive or error-check mutex ? */
1272 tid = __get_thread()->kernel_id;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001273 if ( tid != MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001274 return EPERM;
1275
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001276 /* If the counter is > 0, we can simply decrement it atomically.
1277 * Since other threads can mutate the lower state bits (and only the
1278 * lower state bits), use a cmpxchg to do it.
1279 */
1280 if (!MUTEX_COUNTER_BITS_IS_ZERO(mvalue)) {
1281 for (;;) {
1282 int newval = mvalue - MUTEX_COUNTER_BITS_ONE;
1283 if (__likely(__bionic_cmpxchg(mvalue, newval, &mutex->value) == 0)) {
1284 /* success: we still own the mutex, so no memory barrier */
1285 return 0;
1286 }
1287 /* the value changed, so reload and loop */
1288 mvalue = mutex->value;
1289 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001290 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001291
1292 /* the counter is 0, so we're going to unlock the mutex by resetting
1293 * its value to 'unlocked'. We need to perform a swap in order
1294 * to read the current state, which will be 2 if there are waiters
1295 * to awake.
1296 *
1297 * TODO: Change this to __bionic_swap_release when we implement it
1298 * to get rid of the explicit memory barrier below.
1299 */
1300 ANDROID_MEMBAR_FULL(); /* RELEASE BARRIER */
1301 mvalue = __bionic_swap(mtype | shared | MUTEX_STATE_BITS_UNLOCKED, &mutex->value);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001302
1303 /* Wake one waiting thread, if any */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001304 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001305 __futex_wake_ex(&mutex->value, shared, 1);
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001306 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001307 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001308}
1309
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001310int pthread_mutex_unlock(pthread_mutex_t *mutex)
1311{
1312#ifdef PTHREAD_DEBUG
1313 if (PTHREAD_DEBUG_ENABLED) {
1314 pthread_debug_mutex_unlock_check(mutex);
1315 }
1316#endif
1317 return pthread_mutex_unlock_impl(mutex);
1318}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001319
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001320__LIBC_HIDDEN__
1321int pthread_mutex_trylock_impl(pthread_mutex_t *mutex)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001322{
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001323 int mvalue, mtype, tid, oldv, shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001324
1325 if (__unlikely(mutex == NULL))
1326 return EINVAL;
1327
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001328 mvalue = mutex->value;
1329 mtype = (mvalue & MUTEX_TYPE_MASK);
1330 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001331
1332 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001333 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001334 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001335 if (__bionic_cmpxchg(shared|MUTEX_STATE_BITS_UNLOCKED,
1336 shared|MUTEX_STATE_BITS_LOCKED_UNCONTENDED,
1337 &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001338 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001339 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001340 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001341
1342 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001343 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001344
1345 /* Do we already own this recursive or error-check mutex ? */
1346 tid = __get_thread()->kernel_id;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001347 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001348 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001349
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001350 /* Same as pthread_mutex_lock, except that we don't want to wait, and
1351 * the only operation that can succeed is a single cmpxchg to acquire the
1352 * lock if it is released / not owned by anyone. No need for a complex loop.
1353 */
1354 mtype |= shared | MUTEX_STATE_BITS_UNLOCKED;
1355 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001356
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001357 if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
1358 ANDROID_MEMBAR_FULL();
1359 return 0;
1360 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001361
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001362 return EBUSY;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001363}
1364
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001365int pthread_mutex_trylock(pthread_mutex_t *mutex)
1366{
1367 int err = pthread_mutex_trylock_impl(mutex);
1368#ifdef PTHREAD_DEBUG
1369 if (PTHREAD_DEBUG_ENABLED) {
1370 if (!err) {
1371 pthread_debug_mutex_lock_check(mutex);
1372 }
1373 }
1374#endif
1375 return err;
1376}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001377
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001378/* initialize 'ts' with the difference between 'abstime' and the current time
1379 * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
1380 */
1381static int
1382__timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock)
1383{
1384 clock_gettime(clock, ts);
1385 ts->tv_sec = abstime->tv_sec - ts->tv_sec;
1386 ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec;
1387 if (ts->tv_nsec < 0) {
1388 ts->tv_sec--;
1389 ts->tv_nsec += 1000000000;
1390 }
David 'Digit' Turnerbc10cd22009-09-23 15:56:50 -07001391 if ((ts->tv_nsec < 0) || (ts->tv_sec < 0))
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001392 return -1;
1393
1394 return 0;
1395}
1396
1397/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
1398 * milliseconds.
1399 */
1400static void
1401__timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock)
1402{
1403 clock_gettime(clock, abstime);
1404 abstime->tv_sec += msecs/1000;
1405 abstime->tv_nsec += (msecs%1000)*1000000;
1406 if (abstime->tv_nsec >= 1000000000) {
1407 abstime->tv_sec++;
1408 abstime->tv_nsec -= 1000000000;
1409 }
1410}
1411
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001412__LIBC_HIDDEN__
1413int pthread_mutex_lock_timeout_np_impl(pthread_mutex_t *mutex, unsigned msecs)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001414{
1415 clockid_t clock = CLOCK_MONOTONIC;
1416 struct timespec abstime;
1417 struct timespec ts;
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001418 int mvalue, mtype, tid, oldv, new_lock_type, shared;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001419
1420 /* compute absolute expiration time */
1421 __timespec_to_relative_msec(&abstime, msecs, clock);
1422
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001423 if (__unlikely(mutex == NULL))
1424 return EINVAL;
1425
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001426 mvalue = mutex->value;
1427 mtype = (mvalue & MUTEX_TYPE_MASK);
1428 shared = (mvalue & MUTEX_SHARED_MASK);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001429
1430 /* Handle common case first */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001431 if ( __likely(mtype == MUTEX_TYPE_BITS_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001432 {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001433 const int unlocked = shared | MUTEX_STATE_BITS_UNLOCKED;
1434 const int locked_uncontended = shared | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
1435 const int locked_contended = shared | MUTEX_STATE_BITS_LOCKED_CONTENDED;
1436
1437 /* fast path for uncontended lock. Note: MUTEX_TYPE_BITS_NORMAL is 0 */
1438 if (__bionic_cmpxchg(unlocked, locked_uncontended, &mutex->value) == 0) {
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001439 ANDROID_MEMBAR_FULL();
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001440 return 0;
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001441 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001442
1443 /* loop while needed */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001444 while (__bionic_swap(locked_contended, &mutex->value) != unlocked) {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001445 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
1446 return EBUSY;
1447
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001448 __futex_wait_ex(&mutex->value, shared, locked_contended, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001449 }
Andy McFaddenfcd00eb2010-05-28 13:31:45 -07001450 ANDROID_MEMBAR_FULL();
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001451 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001452 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001453
1454 /* Do we already own this recursive or error-check mutex ? */
1455 tid = __get_thread()->kernel_id;
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001456 if ( tid == MUTEX_OWNER_FROM_BITS(mvalue) )
David 'Digit' Turner022d3032011-12-07 14:02:17 +01001457 return _recursive_increment(mutex, mvalue, mtype);
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001458
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001459 /* the following implements the same loop than pthread_mutex_lock_impl
1460 * but adds checks to ensure that the operation never exceeds the
1461 * absolute expiration time.
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001462 */
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001463 mtype |= shared;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001464
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001465 /* first try a quick lock */
1466 if (mvalue == mtype) {
1467 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_UNCONTENDED;
1468 if (__likely(__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0)) {
1469 ANDROID_MEMBAR_FULL();
1470 return 0;
1471 }
1472 mvalue = mutex->value;
1473 }
David 'Digit' Turner88f06cd2010-03-18 17:13:41 -07001474
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001475 for (;;) {
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001476 struct timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001477
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001478 /* if the value is 'unlocked', try to acquire it directly */
1479 /* NOTE: put state to 2 since we know there is contention */
1480 if (mvalue == mtype) /* unlocked */ {
1481 mvalue = MUTEX_OWNER_TO_BITS(tid) | mtype | MUTEX_STATE_BITS_LOCKED_CONTENDED;
1482 if (__bionic_cmpxchg(mtype, mvalue, &mutex->value) == 0) {
1483 ANDROID_MEMBAR_FULL();
1484 return 0;
1485 }
1486 /* the value changed before we could lock it. We need to check
1487 * the time to avoid livelocks, reload the value, then loop again. */
1488 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
1489 return EBUSY;
1490
1491 mvalue = mutex->value;
1492 continue;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001493 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001494
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001495 /* The value is locked. If 'uncontended', try to switch its state
1496 * to 'contented' to ensure we get woken up later. */
1497 if (MUTEX_STATE_BITS_IS_LOCKED_UNCONTENDED(mvalue)) {
1498 int newval = MUTEX_STATE_BITS_FLIP_CONTENTION(mvalue);
1499 if (__bionic_cmpxchg(mvalue, newval, &mutex->value) != 0) {
1500 /* this failed because the value changed, reload it */
1501 mvalue = mutex->value;
1502 } else {
1503 /* this succeeded, update mvalue */
1504 mvalue = newval;
1505 }
1506 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001507
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001508 /* check time and update 'ts' */
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001509 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
1510 return EBUSY;
1511
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001512 /* Only wait to be woken up if the state is '2', otherwise we'll
1513 * simply loop right now. This can happen when the second cmpxchg
1514 * in our loop failed because the mutex was unlocked by another
1515 * thread.
1516 */
1517 if (MUTEX_STATE_BITS_IS_LOCKED_CONTENDED(mvalue)) {
1518 if (__futex_wait_ex(&mutex->value, shared, mvalue, &ts) == ETIMEDOUT) {
1519 return EBUSY;
1520 }
1521 mvalue = mutex->value;
1522 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001523 }
David 'Digit' Turnere1414aa2012-01-24 15:26:54 +01001524 /* NOTREACHED */
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001525}
1526
Mathias Agopian7c0c3792011-09-05 23:54:55 -07001527int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
1528{
1529 int err = pthread_mutex_lock_timeout_np_impl(mutex, msecs);
1530#ifdef PTHREAD_DEBUG
1531 if (PTHREAD_DEBUG_ENABLED) {
1532 if (!err) {
1533 pthread_debug_mutex_lock_check(mutex);
1534 }
1535 }
1536#endif
1537 return err;
1538}
1539
1540int pthread_mutex_destroy(pthread_mutex_t *mutex)
1541{
1542 int ret;
1543
1544 /* use trylock to ensure that the mutex value is
1545 * valid and is not already locked. */
1546 ret = pthread_mutex_trylock_impl(mutex);
1547 if (ret != 0)
1548 return ret;
1549
1550 mutex->value = 0xdead10cc;
1551 return 0;
1552}
1553
1554
1555
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001556int pthread_condattr_init(pthread_condattr_t *attr)
1557{
1558 if (attr == NULL)
1559 return EINVAL;
1560
1561 *attr = PTHREAD_PROCESS_PRIVATE;
1562 return 0;
1563}
1564
1565int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared)
1566{
1567 if (attr == NULL || pshared == NULL)
1568 return EINVAL;
1569
1570 *pshared = *attr;
1571 return 0;
1572}
1573
1574int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1575{
1576 if (attr == NULL)
1577 return EINVAL;
1578
1579 if (pshared != PTHREAD_PROCESS_SHARED &&
1580 pshared != PTHREAD_PROCESS_PRIVATE)
1581 return EINVAL;
1582
1583 *attr = pshared;
1584 return 0;
1585}
1586
1587int pthread_condattr_destroy(pthread_condattr_t *attr)
1588{
1589 if (attr == NULL)
1590 return EINVAL;
1591
1592 *attr = 0xdeada11d;
1593 return 0;
1594}
1595
1596/* We use one bit in condition variable values as the 'shared' flag
1597 * The rest is a counter.
1598 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001599#define COND_SHARED_MASK 0x0001
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001600#define COND_COUNTER_INCREMENT 0x0002
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001601#define COND_COUNTER_MASK (~COND_SHARED_MASK)
1602
1603#define COND_IS_SHARED(c) (((c)->value & COND_SHARED_MASK) != 0)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001604
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001605/* XXX *technically* there is a race condition that could allow
1606 * XXX a signal to be missed. If thread A is preempted in _wait()
1607 * XXX after unlocking the mutex and before waiting, and if other
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001608 * XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001609 * XXX before thread A is scheduled again and calls futex_wait(),
1610 * XXX then the signal will be lost.
1611 */
1612
1613int pthread_cond_init(pthread_cond_t *cond,
1614 const pthread_condattr_t *attr)
1615{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001616 if (cond == NULL)
1617 return EINVAL;
1618
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001619 cond->value = 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001620
1621 if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED)
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001622 cond->value |= COND_SHARED_MASK;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001623
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001624 return 0;
1625}
1626
1627int pthread_cond_destroy(pthread_cond_t *cond)
1628{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001629 if (cond == NULL)
1630 return EINVAL;
1631
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001632 cond->value = 0xdeadc04d;
1633 return 0;
1634}
1635
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001636/* This function is used by pthread_cond_broadcast and
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001637 * pthread_cond_signal to atomically decrement the counter
1638 * then wake-up 'counter' threads.
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001639 */
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001640static int
1641__pthread_cond_pulse(pthread_cond_t *cond, int counter)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001642{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001643 long flags;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001644
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001645 if (__unlikely(cond == NULL))
1646 return EINVAL;
1647
1648 flags = (cond->value & ~COND_COUNTER_MASK);
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001649 for (;;) {
1650 long oldval = cond->value;
1651 long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK)
1652 | flags;
David 'Digit' Turnere31bfae2011-11-15 15:47:02 +01001653 if (__bionic_cmpxchg(oldval, newval, &cond->value) == 0)
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001654 break;
1655 }
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001656
Andy McFaddene2ac8982010-09-02 13:34:53 -07001657 /*
1658 * Ensure that all memory accesses previously made by this thread are
1659 * visible to the woken thread(s). On the other side, the "wait"
1660 * code will issue any necessary barriers when locking the mutex.
1661 *
1662 * This may not strictly be necessary -- if the caller follows
1663 * recommended practice and holds the mutex before signaling the cond
1664 * var, the mutex ops will provide correct semantics. If they don't
1665 * hold the mutex, they're subject to race conditions anyway.
1666 */
1667 ANDROID_MEMBAR_FULL();
1668
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001669 __futex_wake_ex(&cond->value, COND_IS_SHARED(cond), counter);
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001670 return 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001671}
1672
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001673int pthread_cond_broadcast(pthread_cond_t *cond)
1674{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001675 return __pthread_cond_pulse(cond, INT_MAX);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001676}
1677
1678int pthread_cond_signal(pthread_cond_t *cond)
1679{
David 'Digit' Turnerb5e4a412010-03-19 17:59:23 -07001680 return __pthread_cond_pulse(cond, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001681}
1682
1683int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1684{
1685 return pthread_cond_timedwait(cond, mutex, NULL);
1686}
1687
1688int __pthread_cond_timedwait_relative(pthread_cond_t *cond,
1689 pthread_mutex_t * mutex,
1690 const struct timespec *reltime)
1691{
1692 int status;
1693 int oldvalue = cond->value;
1694
1695 pthread_mutex_unlock(mutex);
David 'Digit' Turner6304d8b2010-06-02 18:12:12 -07001696 status = __futex_wait_ex(&cond->value, COND_IS_SHARED(cond), oldvalue, reltime);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697 pthread_mutex_lock(mutex);
1698
1699 if (status == (-ETIMEDOUT)) return ETIMEDOUT;
1700 return 0;
1701}
1702
1703int __pthread_cond_timedwait(pthread_cond_t *cond,
1704 pthread_mutex_t * mutex,
1705 const struct timespec *abstime,
1706 clockid_t clock)
1707{
1708 struct timespec ts;
1709 struct timespec * tsp;
1710
1711 if (abstime != NULL) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001712 if (__timespec_to_absolute(&ts, abstime, clock) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001713 return ETIMEDOUT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001714 tsp = &ts;
1715 } else {
1716 tsp = NULL;
1717 }
1718
1719 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
1720}
1721
1722int pthread_cond_timedwait(pthread_cond_t *cond,
1723 pthread_mutex_t * mutex,
1724 const struct timespec *abstime)
1725{
1726 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME);
1727}
1728
1729
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001730/* this one exists only for backward binary compatibility */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001731int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
1732 pthread_mutex_t * mutex,
1733 const struct timespec *abstime)
1734{
1735 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1736}
1737
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001738int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
1739 pthread_mutex_t * mutex,
1740 const struct timespec *abstime)
1741{
1742 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1743}
1744
1745int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
1746 pthread_mutex_t * mutex,
1747 const struct timespec *reltime)
1748{
1749 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
1750}
1751
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001752int pthread_cond_timeout_np(pthread_cond_t *cond,
1753 pthread_mutex_t * mutex,
1754 unsigned msecs)
1755{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001756 struct timespec ts;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001757
1758 ts.tv_sec = msecs / 1000;
1759 ts.tv_nsec = (msecs % 1000) * 1000000;
1760
Matthieu CASTETa4e67f42008-12-27 00:04:10 +01001761 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001762}
1763
1764
1765
1766/* A technical note regarding our thread-local-storage (TLS) implementation:
1767 *
1768 * There can be up to TLSMAP_SIZE independent TLS keys in a given process,
1769 * though the first TLSMAP_START keys are reserved for Bionic to hold
1770 * special thread-specific variables like errno or a pointer to
1771 * the current thread's descriptor.
1772 *
1773 * while stored in the TLS area, these entries cannot be accessed through
1774 * pthread_getspecific() / pthread_setspecific() and pthread_key_delete()
1775 *
1776 * also, some entries in the key table are pre-allocated (see tlsmap_lock)
1777 * to greatly simplify and speedup some OpenGL-related operations. though the
1778 * initialy value will be NULL on all threads.
1779 *
1780 * you can use pthread_getspecific()/setspecific() on these, and in theory
1781 * you could also call pthread_key_delete() as well, though this would
1782 * probably break some apps.
1783 *
1784 * The 'tlsmap_t' type defined below implements a shared global map of
1785 * currently created/allocated TLS keys and the destructors associated
1786 * with them. You should use tlsmap_lock/unlock to access it to avoid
1787 * any race condition.
1788 *
1789 * the global TLS map simply contains a bitmap of allocated keys, and
1790 * an array of destructors.
1791 *
1792 * each thread has a TLS area that is a simple array of TLSMAP_SIZE void*
1793 * pointers. the TLS area of the main thread is stack-allocated in
1794 * __libc_init_common, while the TLS area of other threads is placed at
1795 * the top of their stack in pthread_create.
1796 *
1797 * when pthread_key_create() is called, it finds the first free key in the
1798 * bitmap, then set it to 1, saving the destructor altogether
1799 *
1800 * when pthread_key_delete() is called. it will erase the key's bitmap bit
1801 * and its destructor, and will also clear the key data in the TLS area of
1802 * all created threads. As mandated by Posix, it is the responsability of
1803 * the caller of pthread_key_delete() to properly reclaim the objects that
1804 * were pointed to by these data fields (either before or after the call).
1805 *
1806 */
1807
1808/* TLS Map implementation
1809 */
1810
1811#define TLSMAP_START (TLS_SLOT_MAX_WELL_KNOWN+1)
1812#define TLSMAP_SIZE BIONIC_TLS_SLOTS
1813#define TLSMAP_BITS 32
1814#define TLSMAP_WORDS ((TLSMAP_SIZE+TLSMAP_BITS-1)/TLSMAP_BITS)
1815#define TLSMAP_WORD(m,k) (m)->map[(k)/TLSMAP_BITS]
1816#define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
1817
1818/* this macro is used to quickly check that a key belongs to a reasonable range */
1819#define TLSMAP_VALIDATE_KEY(key) \
1820 ((key) >= TLSMAP_START && (key) < TLSMAP_SIZE)
1821
1822/* the type of tls key destructor functions */
1823typedef void (*tls_dtor_t)(void*);
1824
1825typedef struct {
1826 int init; /* see comment in tlsmap_lock() */
1827 uint32_t map[TLSMAP_WORDS]; /* bitmap of allocated keys */
1828 tls_dtor_t dtors[TLSMAP_SIZE]; /* key destructors */
1829} tlsmap_t;
1830
1831static pthread_mutex_t _tlsmap_lock = PTHREAD_MUTEX_INITIALIZER;
1832static tlsmap_t _tlsmap;
1833
1834/* lock the global TLS map lock and return a handle to it */
1835static __inline__ tlsmap_t* tlsmap_lock(void)
1836{
1837 tlsmap_t* m = &_tlsmap;
1838
1839 pthread_mutex_lock(&_tlsmap_lock);
1840 /* we need to initialize the first entry of the 'map' array
1841 * with the value TLS_DEFAULT_ALLOC_MAP. doing it statically
1842 * when declaring _tlsmap is a bit awkward and is going to
1843 * produce warnings, so do it the first time we use the map
1844 * instead
1845 */
1846 if (__unlikely(!m->init)) {
1847 TLSMAP_WORD(m,0) = TLS_DEFAULT_ALLOC_MAP;
1848 m->init = 1;
1849 }
1850 return m;
1851}
1852
1853/* unlock the global TLS map */
1854static __inline__ void tlsmap_unlock(tlsmap_t* m)
1855{
1856 pthread_mutex_unlock(&_tlsmap_lock);
1857 (void)m; /* a good compiler is a happy compiler */
1858}
1859
1860/* test to see wether a key is allocated */
1861static __inline__ int tlsmap_test(tlsmap_t* m, int key)
1862{
1863 return (TLSMAP_WORD(m,key) & TLSMAP_MASK(key)) != 0;
1864}
1865
1866/* set the destructor and bit flag on a newly allocated key */
1867static __inline__ void tlsmap_set(tlsmap_t* m, int key, tls_dtor_t dtor)
1868{
1869 TLSMAP_WORD(m,key) |= TLSMAP_MASK(key);
1870 m->dtors[key] = dtor;
1871}
1872
1873/* clear the destructor and bit flag on an existing key */
1874static __inline__ void tlsmap_clear(tlsmap_t* m, int key)
1875{
1876 TLSMAP_WORD(m,key) &= ~TLSMAP_MASK(key);
1877 m->dtors[key] = NULL;
1878}
1879
1880/* allocate a new TLS key, return -1 if no room left */
1881static int tlsmap_alloc(tlsmap_t* m, tls_dtor_t dtor)
1882{
1883 int key;
1884
1885 for ( key = TLSMAP_START; key < TLSMAP_SIZE; key++ ) {
1886 if ( !tlsmap_test(m, key) ) {
1887 tlsmap_set(m, key, dtor);
1888 return key;
1889 }
1890 }
1891 return -1;
1892}
1893
1894
1895int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *))
1896{
1897 uint32_t err = ENOMEM;
1898 tlsmap_t* map = tlsmap_lock();
1899 int k = tlsmap_alloc(map, destructor_function);
1900
1901 if (k >= 0) {
1902 *key = k;
1903 err = 0;
1904 }
1905 tlsmap_unlock(map);
1906 return err;
1907}
1908
1909
1910/* This deletes a pthread_key_t. note that the standard mandates that this does
1911 * not call the destructor of non-NULL key values. Instead, it is the
1912 * responsability of the caller to properly dispose of the corresponding data
1913 * and resources, using any mean it finds suitable.
1914 *
1915 * On the other hand, this function will clear the corresponding key data
1916 * values in all known threads. this prevents later (invalid) calls to
1917 * pthread_getspecific() to receive invalid/stale values.
1918 */
1919int pthread_key_delete(pthread_key_t key)
1920{
1921 uint32_t err;
1922 pthread_internal_t* thr;
1923 tlsmap_t* map;
1924
1925 if (!TLSMAP_VALIDATE_KEY(key)) {
1926 return EINVAL;
1927 }
1928
1929 map = tlsmap_lock();
1930
1931 if (!tlsmap_test(map, key)) {
1932 err = EINVAL;
1933 goto err1;
1934 }
1935
1936 /* clear value in all threads */
1937 pthread_mutex_lock(&gThreadListLock);
1938 for ( thr = gThreadList; thr != NULL; thr = thr->next ) {
1939 /* avoid zombie threads with a negative 'join_count'. these are really
1940 * already dead and don't have a TLS area anymore.
1941 *
1942 * similarly, it is possible to have thr->tls == NULL for threads that
1943 * were just recently created through pthread_create() but whose
1944 * startup trampoline (__thread_entry) hasn't been run yet by the
1945 * scheduler. so check for this too.
1946 */
1947 if (thr->join_count < 0 || !thr->tls)
1948 continue;
1949
1950 thr->tls[key] = NULL;
1951 }
1952 tlsmap_clear(map, key);
1953
1954 pthread_mutex_unlock(&gThreadListLock);
1955 err = 0;
1956
1957err1:
1958 tlsmap_unlock(map);
1959 return err;
1960}
1961
1962
1963int pthread_setspecific(pthread_key_t key, const void *ptr)
1964{
1965 int err = EINVAL;
1966 tlsmap_t* map;
1967
1968 if (TLSMAP_VALIDATE_KEY(key)) {
1969 /* check that we're trying to set data for an allocated key */
1970 map = tlsmap_lock();
1971 if (tlsmap_test(map, key)) {
1972 ((uint32_t *)__get_tls())[key] = (uint32_t)ptr;
1973 err = 0;
1974 }
1975 tlsmap_unlock(map);
1976 }
1977 return err;
1978}
1979
1980void * pthread_getspecific(pthread_key_t key)
1981{
1982 if (!TLSMAP_VALIDATE_KEY(key)) {
1983 return NULL;
1984 }
1985
1986 /* for performance reason, we do not lock/unlock the global TLS map
1987 * to check that the key is properly allocated. if the key was not
1988 * allocated, the value read from the TLS should always be NULL
1989 * due to pthread_key_delete() clearing the values for all threads.
1990 */
1991 return (void *)(((unsigned *)__get_tls())[key]);
1992}
1993
1994/* Posix mandates that this be defined in <limits.h> but we don't have
1995 * it just yet.
1996 */
1997#ifndef PTHREAD_DESTRUCTOR_ITERATIONS
1998# define PTHREAD_DESTRUCTOR_ITERATIONS 4
1999#endif
2000
2001/* this function is called from pthread_exit() to remove all TLS key data
2002 * from this thread's TLS area. this must call the destructor of all keys
2003 * that have a non-NULL data value (and a non-NULL destructor).
2004 *
2005 * because destructors can do funky things like deleting/creating other
2006 * keys, we need to implement this in a loop
2007 */
2008static void pthread_key_clean_all(void)
2009{
2010 tlsmap_t* map;
2011 void** tls = (void**)__get_tls();
2012 int rounds = PTHREAD_DESTRUCTOR_ITERATIONS;
2013
2014 map = tlsmap_lock();
2015
2016 for (rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; rounds--)
2017 {
2018 int kk, count = 0;
2019
2020 for (kk = TLSMAP_START; kk < TLSMAP_SIZE; kk++) {
2021 if ( tlsmap_test(map, kk) )
2022 {
2023 void* data = tls[kk];
2024 tls_dtor_t dtor = map->dtors[kk];
2025
2026 if (data != NULL && dtor != NULL)
2027 {
2028 /* we need to clear the key data now, this will prevent the
2029 * destructor (or a later one) from seeing the old value if
2030 * it calls pthread_getspecific() for some odd reason
2031 *
2032 * we do not do this if 'dtor == NULL' just in case another
2033 * destructor function might be responsible for manually
2034 * releasing the corresponding data.
2035 */
2036 tls[kk] = NULL;
2037
2038 /* because the destructor is free to call pthread_key_create
2039 * and/or pthread_key_delete, we need to temporarily unlock
2040 * the TLS map
2041 */
2042 tlsmap_unlock(map);
2043 (*dtor)(data);
2044 map = tlsmap_lock();
2045
2046 count += 1;
2047 }
2048 }
2049 }
2050
2051 /* if we didn't call any destructor, there is no need to check the
2052 * TLS data again
2053 */
2054 if (count == 0)
2055 break;
2056 }
2057 tlsmap_unlock(map);
2058}
2059
2060// man says this should be in <linux/unistd.h>, but it isn't
Jeff Brown10c8ce52011-11-18 15:17:07 -08002061extern int tgkill(int tgid, int tid, int sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002062
2063int pthread_kill(pthread_t tid, int sig)
2064{
2065 int ret;
2066 int old_errno = errno;
2067 pthread_internal_t * thread = (pthread_internal_t *)tid;
2068
Jeff Brown10c8ce52011-11-18 15:17:07 -08002069 ret = tgkill(getpid(), thread->kernel_id, sig);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002070 if (ret < 0) {
2071 ret = errno;
2072 errno = old_errno;
2073 }
2074
2075 return ret;
2076}
2077
David 'Digit' Turner9bf330b2011-11-14 12:57:47 +01002078/* Despite the fact that our kernel headers define sigset_t explicitly
2079 * as a 32-bit integer, the kernel system call really expects a 64-bit
2080 * bitmap for the signal set, or more exactly an array of two-32-bit
2081 * values (see $KERNEL/arch/$ARCH/include/asm/signal.h for details).
2082 *
2083 * Unfortunately, we cannot fix the sigset_t definition without breaking
2084 * the C library ABI, so perform a little runtime translation here.
2085 */
2086typedef union {
2087 sigset_t bionic;
2088 uint32_t kernel[2];
2089} kernel_sigset_t;
2090
2091/* this is a private syscall stub */
2092extern int __rt_sigprocmask(int, const kernel_sigset_t *, kernel_sigset_t *, size_t);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002093
2094int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
2095{
David 'Digit' Turner8f8b5312010-03-01 11:30:40 -08002096 /* pthread_sigmask must return the error code, but the syscall
2097 * will set errno instead and return 0/-1
2098 */
2099 int ret, old_errno = errno;
2100
David 'Digit' Turner9bf330b2011-11-14 12:57:47 +01002101 /* We must convert *set into a kernel_sigset_t */
2102 kernel_sigset_t in_set, *in_set_ptr;
2103 kernel_sigset_t out_set;
2104
Bruce Bearee4a21c82011-12-05 11:25:37 -08002105 in_set.kernel[0] = in_set.kernel[1] = 0;
David 'Digit' Turner9bf330b2011-11-14 12:57:47 +01002106 out_set.kernel[0] = out_set.kernel[1] = 0;
2107
2108 /* 'in_set_ptr' is the second parameter to __rt_sigprocmask. It must be NULL
2109 * if 'set' is NULL to ensure correct semantics (which in this case would
2110 * be to ignore 'how' and return the current signal set into 'oset'.
David 'Digit' Turner5c8c00a2010-12-20 15:58:06 +01002111 */
David 'Digit' Turner9bf330b2011-11-14 12:57:47 +01002112 if (set == NULL) {
2113 in_set_ptr = NULL;
2114 } else {
2115 in_set.bionic = *set;
2116 in_set_ptr = &in_set;
2117 }
2118
2119 ret = __rt_sigprocmask(how, in_set_ptr, &out_set, sizeof(kernel_sigset_t));
David 'Digit' Turner8f8b5312010-03-01 11:30:40 -08002120 if (ret < 0)
2121 ret = errno;
2122
David 'Digit' Turner9bf330b2011-11-14 12:57:47 +01002123 if (oset)
2124 *oset = out_set.bionic;
2125
David 'Digit' Turner8f8b5312010-03-01 11:30:40 -08002126 errno = old_errno;
2127 return ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002128}
2129
2130
2131int pthread_getcpuclockid(pthread_t tid, clockid_t *clockid)
2132{
2133 const int CLOCK_IDTYPE_BITS = 3;
2134 pthread_internal_t* thread = (pthread_internal_t*)tid;
2135
2136 if (!thread)
2137 return ESRCH;
2138
2139 *clockid = CLOCK_THREAD_CPUTIME_ID | (thread->kernel_id << CLOCK_IDTYPE_BITS);
2140 return 0;
2141}
2142
2143
2144/* NOTE: this implementation doesn't support a init function that throws a C++ exception
2145 * or calls fork()
2146 */
2147int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) )
2148{
Wink Savilleccc3d1e2010-06-28 11:41:16 -07002149 static pthread_mutex_t once_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER;
Andy McFaddenb1c9cc22010-09-23 12:30:12 -07002150 volatile pthread_once_t* ocptr = once_control;
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01002151 pthread_once_t value;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002152
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01002153 /* PTHREAD_ONCE_INIT is 0, we use the following bit flags
2154 *
2155 * bit 0 set -> initialization is under way
2156 * bit 1 set -> initialization is complete
2157 */
2158#define ONCE_INITIALIZING (1 << 0)
2159#define ONCE_COMPLETED (1 << 1)
2160
2161 /* First check if the once is already initialized. This will be the common
2162 * case and we want to make this as fast as possible. Note that this still
2163 * requires a load_acquire operation here to ensure that all the
2164 * stores performed by the initialization function are observable on
2165 * this CPU after we exit.
2166 */
2167 if (__likely((*ocptr & ONCE_COMPLETED) != 0)) {
2168 ANDROID_MEMBAR_FULL();
2169 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002170 }
David 'Digit' Turner6c6de442011-12-07 12:20:44 +01002171
2172 for (;;) {
2173 /* Try to atomically set the INITIALIZING flag.
2174 * This requires a cmpxchg loop, and we may need
2175 * to exit prematurely if we detect that
2176 * COMPLETED is now set.
2177 */
2178 int32_t oldval, newval;
2179
2180 do {
2181 oldval = *ocptr;
2182 if ((oldval & ONCE_COMPLETED) != 0)
2183 break;
2184
2185 newval = oldval | ONCE_INITIALIZING;
2186 } while (__bionic_cmpxchg(oldval, newval, ocptr) != 0);
2187
2188 if ((oldval & ONCE_COMPLETED) != 0) {
2189 /* We detected that COMPLETED was set while in our loop */
2190 ANDROID_MEMBAR_FULL();
2191 return 0;
2192 }
2193
2194 if ((oldval & ONCE_INITIALIZING) == 0) {
2195 /* We got there first, we can jump out of the loop to
2196 * handle the initialization */
2197 break;
2198 }
2199
2200 /* Another thread is running the initialization and hasn't completed
2201 * yet, so wait for it, then try again. */
2202 __futex_wait_ex(ocptr, 0, oldval, NULL);
2203 }
2204
2205 /* call the initialization function. */
2206 (*init_routine)();
2207
2208 /* Do a store_release indicating that initialization is complete */
2209 ANDROID_MEMBAR_FULL();
2210 *ocptr = ONCE_COMPLETED;
2211
2212 /* Wake up any waiters, if any */
2213 __futex_wake_ex(ocptr, 0, INT_MAX);
2214
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002215 return 0;
2216}
André Goddard Rosa78c1c042010-05-19 23:17:16 -03002217
2218/* This value is not exported by kernel headers, so hardcode it here */
2219#define MAX_TASK_COMM_LEN 16
2220#define TASK_COMM_FMT "/proc/self/task/%u/comm"
2221
2222int pthread_setname_np(pthread_t thid, const char *thname)
2223{
2224 size_t thname_len;
2225 int saved_errno, ret;
2226
2227 if (thid == 0 || thname == NULL)
2228 return EINVAL;
2229
2230 thname_len = strlen(thname);
2231 if (thname_len >= MAX_TASK_COMM_LEN)
2232 return ERANGE;
2233
2234 saved_errno = errno;
2235 if (thid == pthread_self())
2236 {
2237 ret = prctl(PR_SET_NAME, (unsigned long)thname, 0, 0, 0) ? errno : 0;
2238 }
2239 else
2240 {
2241 /* Have to change another thread's name */
2242 pthread_internal_t *thread = (pthread_internal_t *)thid;
2243 char comm_name[sizeof(TASK_COMM_FMT) + 8];
2244 ssize_t n;
2245 int fd;
2246
2247 snprintf(comm_name, sizeof(comm_name), TASK_COMM_FMT, (unsigned int)thread->kernel_id);
2248 fd = open(comm_name, O_RDWR);
2249 if (fd == -1)
2250 {
2251 ret = errno;
2252 goto exit;
2253 }
2254 n = TEMP_FAILURE_RETRY(write(fd, thname, thname_len));
2255 close(fd);
2256
2257 if (n < 0)
2258 ret = errno;
2259 else if ((size_t)n != thname_len)
2260 ret = EIO;
2261 else
2262 ret = 0;
2263 }
2264exit:
2265 errno = saved_errno;
2266 return ret;
2267}
Glenn Kastend53cae02011-07-11 15:41:28 -07002268
2269/* Return the kernel thread ID for a pthread.
2270 * This is only defined for implementations where pthread <-> kernel is 1:1, which this is.
2271 * Not the same as pthread_getthreadid_np, which is commonly defined to be opaque.
2272 * Internal, not an NDK API.
2273 */
2274
2275pid_t __pthread_gettid(pthread_t thid)
2276{
2277 pthread_internal_t* thread = (pthread_internal_t*)thid;
2278 return thread->kernel_id;
2279}
Jean-Baptiste Querufaca92f2012-03-26 15:25:19 -07002280
2281int __pthread_settid(pthread_t thid, pid_t tid)
2282{
2283 if (thid == 0)
2284 return EINVAL;
2285
2286 pthread_internal_t* thread = (pthread_internal_t*)thid;
2287 thread->kernel_id = tid;
2288
2289 return 0;
2290}