blob: 4ffa9b8e3ff71a417377c5611da24ceacc2ad7a5 [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 */
28#include <sys/types.h>
29#include <unistd.h>
30#include <signal.h>
31#include <stdint.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <errno.h>
35#include <sys/atomics.h>
36#include <bionic_tls.h>
37#include <sys/mman.h>
38#include <pthread.h>
39#include <time.h>
40#include "pthread_internal.h"
41#include "thread_private.h"
42#include <limits.h>
43#include <memory.h>
44#include <assert.h>
45#include <malloc.h>
46
47extern int __pthread_clone(int (*fn)(void*), void *child_stack, int flags, void *arg);
48extern void _exit_with_stack_teardown(void * stackBase, int stackSize, int retCode);
49extern void _exit_thread(int retCode);
50extern int __set_errno(int);
51
52void _thread_created_hook(pid_t thread_id) __attribute__((noinline));
53
54#define PTHREAD_ATTR_FLAG_DETACHED 0x00000001
55#define PTHREAD_ATTR_FLAG_USER_STACK 0x00000002
56
57#define DEFAULT_STACKSIZE (1024 * 1024)
58#define STACKBASE 0x10000000
59
60static uint8_t * gStackBase = (uint8_t *)STACKBASE;
61
62static pthread_mutex_t mmap_lock = PTHREAD_MUTEX_INITIALIZER;
63
64
65static const pthread_attr_t gDefaultPthreadAttr = {
66 .flags = 0,
67 .stack_base = NULL,
68 .stack_size = DEFAULT_STACKSIZE,
69 .guard_size = PAGE_SIZE,
70 .sched_policy = SCHED_NORMAL,
71 .sched_priority = 0
72};
73
74#define INIT_THREADS 1
75
76static pthread_internal_t* gThreadList = NULL;
77static pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER;
78static pthread_mutex_t gDebuggerNotificationLock = PTHREAD_MUTEX_INITIALIZER;
79
80
81/* we simply malloc/free the internal pthread_internal_t structures. we may
82 * want to use a different allocation scheme in the future, but this one should
83 * be largely enough
84 */
85static pthread_internal_t*
86_pthread_internal_alloc(void)
87{
88 pthread_internal_t* thread;
89
90 thread = calloc( sizeof(*thread), 1 );
91 if (thread)
92 thread->intern = 1;
93
94 return thread;
95}
96
97static void
98_pthread_internal_free( pthread_internal_t* thread )
99{
100 if (thread && thread->intern) {
101 thread->intern = 0; /* just in case */
102 free (thread);
103 }
104}
105
106
107static void
108_pthread_internal_remove_locked( pthread_internal_t* thread )
109{
110 thread->next->pref = thread->pref;
111 thread->pref[0] = thread->next;
112}
113
114static void
115_pthread_internal_remove( pthread_internal_t* thread )
116{
117 pthread_mutex_lock(&gThreadListLock);
118 _pthread_internal_remove_locked(thread);
119 pthread_mutex_unlock(&gThreadListLock);
120}
121
122static void
123_pthread_internal_add( pthread_internal_t* thread )
124{
125 pthread_mutex_lock(&gThreadListLock);
126 thread->pref = &gThreadList;
127 thread->next = thread->pref[0];
128 if (thread->next)
129 thread->next->pref = &thread->next;
130 thread->pref[0] = thread;
131 pthread_mutex_unlock(&gThreadListLock);
132}
133
134pthread_internal_t*
135__get_thread(void)
136{
137 void** tls = (void**)__get_tls();
138
139 return (pthread_internal_t*) tls[TLS_SLOT_THREAD_ID];
140}
141
142
143void*
144__get_stack_base(int *p_stack_size)
145{
146 pthread_internal_t* thread = __get_thread();
147
148 *p_stack_size = thread->attr.stack_size;
149 return thread->attr.stack_base;
150}
151
152
153void __init_tls(void** tls, void* thread)
154{
155 int nn;
156
157 ((pthread_internal_t*)thread)->tls = tls;
158
159 // slot 0 must point to the tls area, this is required by the implementation
160 // of the x86 Linux kernel thread-local-storage
161 tls[TLS_SLOT_SELF] = (void*)tls;
162 tls[TLS_SLOT_THREAD_ID] = thread;
163 for (nn = TLS_SLOT_ERRNO; nn < BIONIC_TLS_SLOTS; nn++)
164 tls[nn] = 0;
165
166 __set_tls( (void*)tls );
167}
168
169
170/*
171 * This trampoline is called from the assembly clone() function
172 */
173void __thread_entry(int (*func)(void*), void *arg, void **tls)
174{
175 int retValue;
176 pthread_internal_t * thrInfo;
177
178 // Wait for our creating thread to release us. This lets it have time to
179 // notify gdb about this thread before it starts doing anything.
180 pthread_mutex_t * start_mutex = (pthread_mutex_t *)&tls[TLS_SLOT_SELF];
181 pthread_mutex_lock(start_mutex);
182 pthread_mutex_destroy(start_mutex);
183
184 thrInfo = (pthread_internal_t *) tls[TLS_SLOT_THREAD_ID];
185
186 __init_tls( tls, thrInfo );
187
188 pthread_exit( (void*)func(arg) );
189}
190
191void _init_thread(pthread_internal_t * thread, pid_t kernel_id, pthread_attr_t * attr, void * stack_base)
192{
193 if (attr == NULL) {
194 thread->attr = gDefaultPthreadAttr;
195 } else {
196 thread->attr = *attr;
197 }
198 thread->attr.stack_base = stack_base;
199 thread->kernel_id = kernel_id;
200
201 // set the scheduling policy/priority of the thread
202 if (thread->attr.sched_policy != SCHED_NORMAL) {
203 struct sched_param param;
204 param.sched_priority = thread->attr.sched_priority;
205 sched_setscheduler(kernel_id, thread->attr.sched_policy, &param);
206 }
207
208 pthread_cond_init(&thread->join_cond, NULL);
209 thread->join_count = 0;
210
211 thread->cleanup_stack = NULL;
212
213 _pthread_internal_add(thread);
214}
215
216
217/* XXX stacks not reclaimed if thread spawn fails */
218/* XXX stacks address spaces should be reused if available again */
219
220static void *mkstack(size_t size, size_t guard_size)
221{
222 void * stack;
223
224 pthread_mutex_lock(&mmap_lock);
225
226 stack = mmap((void *)gStackBase, size,
227 PROT_READ | PROT_WRITE,
228 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
229 -1, 0);
230
231 if(stack == MAP_FAILED) {
232 stack = NULL;
233 goto done;
234 }
235
236 if(mprotect(stack, guard_size, PROT_NONE)){
237 munmap(stack, size);
238 stack = NULL;
239 goto done;
240 }
241
242done:
243 pthread_mutex_unlock(&mmap_lock);
244 return stack;
245}
246
247/*
248 * Create a new thread. The thread's stack is layed out like so:
249 *
250 * +---------------------------+
251 * | pthread_internal_t |
252 * +---------------------------+
253 * | |
254 * | TLS area |
255 * | |
256 * +---------------------------+
257 * | |
258 * . .
259 * . stack area .
260 * . .
261 * | |
262 * +---------------------------+
263 * | guard page |
264 * +---------------------------+
265 *
266 * note that TLS[0] must be a pointer to itself, this is required
267 * by the thread-local storage implementation of the x86 Linux
268 * kernel, where the TLS pointer is read by reading fs:[0]
269 */
270int pthread_create(pthread_t *thread_out, pthread_attr_t const * attr,
271 void *(*start_routine)(void *), void * arg)
272{
273 char* stack;
274 void** tls;
275 int tid;
276 pthread_mutex_t * start_mutex;
277 pthread_internal_t * thread;
278 int madestack = 0;
279 int old_errno = errno;
280
281 /* this will inform the rest of the C library that at least one thread
282 * was created. this will enforce certain functions to acquire/release
283 * locks (e.g. atexit()) to protect shared global structures.
284 *
285 * this works because pthread_create() is not called by the C library
286 * initialization routine that sets up the main thread's data structures.
287 */
288 __isthreaded = 1;
289
290 thread = _pthread_internal_alloc();
291 if (thread == NULL)
292 return ENOMEM;
293
294 if (attr == NULL) {
295 attr = &gDefaultPthreadAttr;
296 }
297
298 // make sure the stack is PAGE_SIZE aligned
299 size_t stackSize = (attr->stack_size +
300 (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
301
302 if (!attr->stack_base) {
303 stack = mkstack(stackSize, attr->guard_size);
304 if(stack == NULL) {
305 _pthread_internal_free(thread);
306 return ENOMEM;
307 }
308 madestack = 1;
309 } else {
310 stack = attr->stack_base;
311 }
312
313 // Make room for TLS
314 tls = (void**)(stack + stackSize - BIONIC_TLS_SLOTS*sizeof(void*));
315
316 // Create a mutex for the thread in TLS_SLOT_SELF to wait on once it starts so we can keep
317 // it from doing anything until after we notify the debugger about it
318 start_mutex = (pthread_mutex_t *) &tls[TLS_SLOT_SELF];
319 pthread_mutex_init(start_mutex, NULL);
320 pthread_mutex_lock(start_mutex);
321
322 tls[TLS_SLOT_THREAD_ID] = thread;
323
324 tid = __pthread_clone((int(*)(void*))start_routine, tls,
325 CLONE_FILES | CLONE_FS | CLONE_VM | CLONE_SIGHAND
326 | CLONE_THREAD | CLONE_SYSVSEM | CLONE_DETACHED,
327 arg);
328
329 if(tid < 0) {
330 int result;
331 if (madestack)
332 munmap(stack, stackSize);
333 _pthread_internal_free(thread);
334 result = errno;
335 errno = old_errno;
336 return result;
337 }
338
339 _init_thread(thread, tid, (pthread_attr_t*)attr, stack);
340
341 if (!madestack)
342 thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_STACK;
343
344 // Notify any debuggers about the new thread
345 pthread_mutex_lock(&gDebuggerNotificationLock);
346 _thread_created_hook(tid);
347 pthread_mutex_unlock(&gDebuggerNotificationLock);
348
349 // Let the thread do it's thing
350 pthread_mutex_unlock(start_mutex);
351
352 *thread_out = (pthread_t)thread;
353 return 0;
354}
355
356
357int pthread_attr_init(pthread_attr_t * attr)
358{
359 *attr = gDefaultPthreadAttr;
360 return 0;
361}
362
363int pthread_attr_destroy(pthread_attr_t * attr)
364{
365 memset(attr, 0x42, sizeof(pthread_attr_t));
366 return 0;
367}
368
369int pthread_attr_setdetachstate(pthread_attr_t * attr, int state)
370{
371 if (state == PTHREAD_CREATE_DETACHED) {
372 attr->flags |= PTHREAD_ATTR_FLAG_DETACHED;
373 } else if (state == PTHREAD_CREATE_JOINABLE) {
374 attr->flags &= ~PTHREAD_ATTR_FLAG_DETACHED;
375 } else {
376 return EINVAL;
377 }
378 return 0;
379}
380
381int pthread_attr_getdetachstate(pthread_attr_t const * attr, int * state)
382{
383 *state = (attr->flags & PTHREAD_ATTR_FLAG_DETACHED)
384 ? PTHREAD_CREATE_DETACHED
385 : PTHREAD_CREATE_JOINABLE;
386 return 0;
387}
388
389int pthread_attr_setschedpolicy(pthread_attr_t * attr, int policy)
390{
391 attr->sched_policy = policy;
392 return 0;
393}
394
395int pthread_attr_getschedpolicy(pthread_attr_t const * attr, int * policy)
396{
397 *policy = attr->sched_policy;
398 return 0;
399}
400
401int pthread_attr_setschedparam(pthread_attr_t * attr, struct sched_param const * param)
402{
403 attr->sched_priority = param->sched_priority;
404 return 0;
405}
406
407int pthread_attr_getschedparam(pthread_attr_t const * attr, struct sched_param * param)
408{
409 param->sched_priority = attr->sched_priority;
410 return 0;
411}
412
413int pthread_attr_setstacksize(pthread_attr_t * attr, size_t stack_size)
414{
415 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
416 return EINVAL;
417 }
418 attr->stack_size = stack_size;
419 return 0;
420}
421
422int pthread_attr_getstacksize(pthread_attr_t const * attr, size_t * stack_size)
423{
424 *stack_size = attr->stack_size;
425 return 0;
426}
427
428int pthread_attr_setstackaddr(pthread_attr_t * attr, void * stack_addr)
429{
430#if 1
431 // It's not clear if this is setting the top or bottom of the stack, so don't handle it for now.
432 return ENOSYS;
433#else
434 if ((uint32_t)stack_addr & (PAGE_SIZE - 1)) {
435 return EINVAL;
436 }
437 attr->stack_base = stack_addr;
438 return 0;
439#endif
440}
441
442int pthread_attr_getstackaddr(pthread_attr_t const * attr, void ** stack_addr)
443{
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -0700444 *stack_addr = (char*)attr->stack_base + attr->stack_size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800445 return 0;
446}
447
448int pthread_attr_setstack(pthread_attr_t * attr, void * stack_base, size_t stack_size)
449{
450 if ((stack_size & (PAGE_SIZE - 1) || stack_size < PTHREAD_STACK_MIN)) {
451 return EINVAL;
452 }
453 if ((uint32_t)stack_base & (PAGE_SIZE - 1)) {
454 return EINVAL;
455 }
456 attr->stack_base = stack_base;
457 attr->stack_size = stack_size;
458 return 0;
459}
460
461int pthread_attr_getstack(pthread_attr_t const * attr, void ** stack_base, size_t * stack_size)
462{
463 *stack_base = attr->stack_base;
464 *stack_size = attr->stack_size;
465 return 0;
466}
467
468int pthread_attr_setguardsize(pthread_attr_t * attr, size_t guard_size)
469{
470 if (guard_size & (PAGE_SIZE - 1) || guard_size < PAGE_SIZE) {
471 return EINVAL;
472 }
473
474 attr->guard_size = guard_size;
475 return 0;
476}
477
478int pthread_attr_getguardsize(pthread_attr_t const * attr, size_t * guard_size)
479{
480 *guard_size = attr->guard_size;
481 return 0;
482}
483
484int pthread_getattr_np(pthread_t thid, pthread_attr_t * attr)
485{
486 pthread_internal_t * thread = (pthread_internal_t *)thid;
487 *attr = thread->attr;
488 return 0;
489}
490
491int pthread_attr_setscope(pthread_attr_t *attr, int scope)
492{
493 if (scope == PTHREAD_SCOPE_SYSTEM)
494 return 0;
495 if (scope == PTHREAD_SCOPE_PROCESS)
496 return ENOTSUP;
497
498 return EINVAL;
499}
500
501int pthread_attr_getscope(pthread_attr_t const *attr)
502{
503 return PTHREAD_SCOPE_SYSTEM;
504}
505
506
507/* CAVEAT: our implementation of pthread_cleanup_push/pop doesn't support C++ exceptions
508 * and thread cancelation
509 */
510
511void __pthread_cleanup_push( __pthread_cleanup_t* c,
512 __pthread_cleanup_func_t routine,
513 void* arg )
514{
515 pthread_internal_t* thread = __get_thread();
516
517 c->__cleanup_routine = routine;
518 c->__cleanup_arg = arg;
519 c->__cleanup_prev = thread->cleanup_stack;
520 thread->cleanup_stack = c;
521}
522
523void __pthread_cleanup_pop( __pthread_cleanup_t* c, int execute )
524{
525 pthread_internal_t* thread = __get_thread();
526
527 thread->cleanup_stack = c->__cleanup_prev;
528 if (execute)
529 c->__cleanup_routine(c->__cleanup_arg);
530}
531
532/* used by pthread_exit() to clean all TLS keys of the current thread */
533static void pthread_key_clean_all(void);
534
535void pthread_exit(void * retval)
536{
537 pthread_internal_t* thread = __get_thread();
538 void* stack_base = thread->attr.stack_base;
539 int stack_size = thread->attr.stack_size;
540 int user_stack = (thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) != 0;
541
542 // call the cleanup handlers first
543 while (thread->cleanup_stack) {
544 __pthread_cleanup_t* c = thread->cleanup_stack;
545 thread->cleanup_stack = c->__cleanup_prev;
546 c->__cleanup_routine(c->__cleanup_arg);
547 }
548
549 // call the TLS destructors, it is important to do that before removing this
550 // thread from the global list. this will ensure that if someone else deletes
551 // a TLS key, the corresponding value will be set to NULL in this thread's TLS
552 // space (see pthread_key_delete)
553 pthread_key_clean_all();
554
555 // if the thread is detached, destroy the pthread_internal_t
556 // otherwise, keep it in memory and signal any joiners
557 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
558 _pthread_internal_remove(thread);
559 _pthread_internal_free(thread);
560 } else {
561 /* the join_count field is used to store the number of threads waiting for
562 * the termination of this thread with pthread_join(),
563 *
564 * if it is positive we need to signal the waiters, and we do not touch
565 * the count (it will be decremented by the waiters, the last one will
566 * also remove/free the thread structure
567 *
568 * if it is zero, we set the count value to -1 to indicate that the
569 * thread is in 'zombie' state: it has stopped executing, and its stack
570 * is gone (as well as its TLS area). when another thread calls pthread_join()
571 * on it, it will immediately free the thread and return.
572 */
573 pthread_mutex_lock(&gThreadListLock);
574 thread->return_value = retval;
575 if (thread->join_count > 0) {
576 pthread_cond_broadcast(&thread->join_cond);
577 } else {
578 thread->join_count = -1; /* zombie thread */
579 }
580 pthread_mutex_unlock(&gThreadListLock);
581 }
582
583 // destroy the thread stack
584 if (user_stack)
585 _exit_thread((int)retval);
586 else
587 _exit_with_stack_teardown(stack_base, stack_size, (int)retval);
588}
589
590int pthread_join(pthread_t thid, void ** ret_val)
591{
592 pthread_internal_t* thread = (pthread_internal_t*)thid;
593 int count;
594
595 // check that the thread still exists and is not detached
596 pthread_mutex_lock(&gThreadListLock);
597
598 for (thread = gThreadList; thread != NULL; thread = thread->next)
599 if (thread == (pthread_internal_t*)thid)
600 break;
601
602 if (!thread) {
603 pthread_mutex_unlock(&gThreadListLock);
604 return ESRCH;
605 }
606
607 if (thread->attr.flags & PTHREAD_ATTR_FLAG_DETACHED) {
608 pthread_mutex_unlock(&gThreadListLock);
609 return EINVAL;
610 }
611
612 /* wait for thread death when needed
613 *
614 * if the 'join_count' is negative, this is a 'zombie' thread that
615 * is already dead and without stack/TLS
616 *
617 * otherwise, we need to increment 'join-count' and wait to be signaled
618 */
619 count = thread->join_count;
620 if (count >= 0) {
621 thread->join_count += 1;
622 pthread_cond_wait( &thread->join_cond, &gThreadListLock );
623 count = --thread->join_count;
624 }
625 if (ret_val)
626 *ret_val = thread->return_value;
627
628 /* remove thread descriptor when we're the last joiner or when the
629 * thread was already a zombie.
630 */
631 if (count <= 0) {
632 _pthread_internal_remove_locked(thread);
633 _pthread_internal_free(thread);
634 }
635 pthread_mutex_unlock(&gThreadListLock);
636 return 0;
637}
638
639int pthread_detach( pthread_t thid )
640{
641 pthread_internal_t* thread;
642 int result = 0;
643 int flags;
644
645 pthread_mutex_lock(&gThreadListLock);
646 for (thread = gThreadList; thread != NULL; thread = thread->next)
647 if (thread == (pthread_internal_t*)thid)
648 goto FoundIt;
649
650 result = ESRCH;
651 goto Exit;
652
653FoundIt:
654 do {
655 flags = thread->attr.flags;
656
657 if ( flags & PTHREAD_ATTR_FLAG_DETACHED ) {
658 /* thread is not joinable ! */
659 result = EINVAL;
660 goto Exit;
661 }
662 }
663 while ( __atomic_cmpxchg( flags, flags | PTHREAD_ATTR_FLAG_DETACHED,
664 (volatile int*)&thread->attr.flags ) != 0 );
665Exit:
666 pthread_mutex_unlock(&gThreadListLock);
667 return result;
668}
669
670pthread_t pthread_self(void)
671{
672 return (pthread_t)__get_thread();
673}
674
675int pthread_equal(pthread_t one, pthread_t two)
676{
677 return (one == two ? 1 : 0);
678}
679
680int pthread_getschedparam(pthread_t thid, int * policy,
681 struct sched_param * param)
682{
683 int old_errno = errno;
684
685 pthread_internal_t * thread = (pthread_internal_t *)thid;
686 int err = sched_getparam(thread->kernel_id, param);
687 if (!err) {
688 *policy = sched_getscheduler(thread->kernel_id);
689 } else {
690 err = errno;
691 errno = old_errno;
692 }
693 return err;
694}
695
696int pthread_setschedparam(pthread_t thid, int policy,
697 struct sched_param const * param)
698{
699 pthread_internal_t * thread = (pthread_internal_t *)thid;
700 int old_errno = errno;
701 int ret;
702
703 ret = sched_setscheduler(thread->kernel_id, policy, param);
704 if (ret < 0) {
705 ret = errno;
706 errno = old_errno;
707 }
708 return ret;
709}
710
711
712int __futex_wait(volatile void *ftx, int val, const struct timespec *timeout);
713int __futex_wake(volatile void *ftx, int count);
714
715// mutex lock states
716//
717// 0: unlocked
718// 1: locked, no waiters
719// 2: locked, maybe waiters
720
721/* a mutex is implemented as a 32-bit integer holding the following fields
722 *
723 * bits: name description
724 * 31-16 tid owner thread's kernel id (recursive and errorcheck only)
725 * 15-14 type mutex type
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800726 * 13-2 counter counter of recursive mutexes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800727 * 1-0 state lock state (0, 1 or 2)
728 */
729
730
731#define MUTEX_OWNER(m) (((m)->value >> 16) & 0xffff)
732#define MUTEX_COUNTER(m) (((m)->value >> 2) & 0xfff)
733
734#define MUTEX_TYPE_MASK 0xc000
735#define MUTEX_TYPE_NORMAL 0x0000
736#define MUTEX_TYPE_RECURSIVE 0x4000
737#define MUTEX_TYPE_ERRORCHECK 0x8000
738
739#define MUTEX_COUNTER_SHIFT 2
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800740#define MUTEX_COUNTER_MASK 0x3ffc
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800741
742
743
744
745int pthread_mutexattr_init(pthread_mutexattr_t *attr)
746{
747 if (attr) {
748 *attr = PTHREAD_MUTEX_DEFAULT;
749 return 0;
750 } else {
751 return EINVAL;
752 }
753}
754
755int pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
756{
757 if (attr) {
758 *attr = -1;
759 return 0;
760 } else {
761 return EINVAL;
762 }
763}
764
765int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type)
766{
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800767 if (attr && *attr >= PTHREAD_MUTEX_NORMAL &&
768 *attr <= PTHREAD_MUTEX_ERRORCHECK ) {
769 *type = *attr;
770 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800771 }
772 return EINVAL;
773}
774
775int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
776{
777 if (attr && type >= PTHREAD_MUTEX_NORMAL &&
778 type <= PTHREAD_MUTEX_ERRORCHECK ) {
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800779 *attr = type;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800780 return 0;
781 }
782 return EINVAL;
783}
784
785/* process-shared mutexes are not supported at the moment */
786
787int pthread_mutexattr_setpshared(pthread_mutexattr_t *attr, int pshared)
788{
789 if (!attr)
790 return EINVAL;
791
Mathias Agopianb7681162009-07-13 22:00:33 -0700792 switch (pshared) {
793 case PTHREAD_PROCESS_PRIVATE:
794 case PTHREAD_PROCESS_SHARED:
795 /* our current implementation of pthread actually supports shared
796 * mutexes but won't cleanup if a process dies with the mutex held.
797 * Nevertheless, it's better than nothing. Shared mutexes are used
798 * by surfaceflinger and audioflinger.
799 */
800 return 0;
801 }
802
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800803 return ENOTSUP;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800804}
805
806int pthread_mutexattr_getpshared(pthread_mutexattr_t *attr, int *pshared)
807{
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800808 if (!attr)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800809 return EINVAL;
810
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800811 *pshared = PTHREAD_PROCESS_PRIVATE;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800812 return 0;
813}
814
815int pthread_mutex_init(pthread_mutex_t *mutex,
816 const pthread_mutexattr_t *attr)
817{
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800818 if ( mutex ) {
819 if (attr == NULL) {
820 mutex->value = MUTEX_TYPE_NORMAL;
821 return 0;
822 }
823 switch ( *attr ) {
824 case PTHREAD_MUTEX_NORMAL:
825 mutex->value = MUTEX_TYPE_NORMAL;
826 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800827
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800828 case PTHREAD_MUTEX_RECURSIVE:
829 mutex->value = MUTEX_TYPE_RECURSIVE;
830 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800831
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800832 case PTHREAD_MUTEX_ERRORCHECK:
833 mutex->value = MUTEX_TYPE_ERRORCHECK;
834 return 0;
835 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836 }
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800837 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800838}
839
840int pthread_mutex_destroy(pthread_mutex_t *mutex)
841{
842 mutex->value = 0xdead10cc;
843 return 0;
844}
845
846
847/*
848 * Lock a non-recursive mutex.
849 *
850 * As noted above, there are three states:
851 * 0 (unlocked, no contention)
852 * 1 (locked, no contention)
853 * 2 (locked, contention)
854 *
855 * Non-recursive mutexes don't use the thread-id or counter fields, and the
856 * "type" value is zero, so the only bits that will be set are the ones in
857 * the lock state field.
858 */
859static __inline__ void
860_normal_lock(pthread_mutex_t* mutex)
861{
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800862 /*
863 * The common case is an unlocked mutex, so we begin by trying to
864 * change the lock's state from 0 to 1. __atomic_cmpxchg() returns 0
865 * if it made the swap successfully. If the result is nonzero, this
866 * lock is already held by another thread.
867 */
868 if (__atomic_cmpxchg(0, 1, &mutex->value ) != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800869 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800870 * We want to go to sleep until the mutex is available, which
871 * requires promoting it to state 2. We need to swap in the new
872 * state value and then wait until somebody wakes us up.
873 *
874 * __atomic_swap() returns the previous value. We swap 2 in and
875 * see if we got zero back; if so, we have acquired the lock. If
876 * not, another thread still holds the lock and we wait again.
877 *
878 * The second argument to the __futex_wait() call is compared
879 * against the current value. If it doesn't match, __futex_wait()
880 * returns immediately (otherwise, it sleeps for a time specified
881 * by the third argument; 0 means sleep forever). This ensures
882 * that the mutex is in state 2 when we go to sleep on it, which
883 * guarantees a wake-up call.
884 */
885 while (__atomic_swap(2, &mutex->value ) != 0)
886 __futex_wait(&mutex->value, 2, 0);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800887 }
888}
889
890/*
891 * Release a non-recursive mutex. The caller is responsible for determining
892 * that we are in fact the owner of this lock.
893 */
894static __inline__ void
895_normal_unlock(pthread_mutex_t* mutex)
896{
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800897 /*
898 * The mutex value will be 1 or (rarely) 2. We use an atomic decrement
899 * to release the lock. __atomic_dec() returns the previous value;
900 * if it wasn't 1 we have to do some additional work.
901 */
902 if (__atomic_dec(&mutex->value) != 1) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800903 /*
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800904 * Start by releasing the lock. The decrement changed it from
905 * "contended lock" to "uncontended lock", which means we still
906 * hold it, and anybody who tries to sneak in will push it back
907 * to state 2.
908 *
909 * Once we set it to zero the lock is up for grabs. We follow
910 * this with a __futex_wake() to ensure that one of the waiting
911 * threads has a chance to grab it.
912 *
913 * This doesn't cause a race with the swap/wait pair in
914 * _normal_lock(), because the __futex_wait() call there will
915 * return immediately if the mutex value isn't 2.
916 */
917 mutex->value = 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800918
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800919 /*
920 * Wake up one waiting thread. We don't know which thread will be
921 * woken or when it'll start executing -- futexes make no guarantees
922 * here. There may not even be a thread waiting.
923 *
924 * The newly-woken thread will replace the 0 we just set above
925 * with 2, which means that when it eventually releases the mutex
926 * it will also call FUTEX_WAKE. This results in one extra wake
927 * call whenever a lock is contended, but lets us avoid forgetting
928 * anyone without requiring us to track the number of sleepers.
929 *
930 * It's possible for another thread to sneak in and grab the lock
931 * between the zero assignment above and the wake call below. If
932 * the new thread is "slow" and holds the lock for a while, we'll
933 * wake up a sleeper, which will swap in a 2 and then go back to
934 * sleep since the lock is still held. If the new thread is "fast",
935 * running to completion before we call wake, the thread we
936 * eventually wake will find an unlocked mutex and will execute.
937 * Either way we have correct behavior and nobody is orphaned on
938 * the wait queue.
939 */
940 __futex_wake(&mutex->value, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800941 }
942}
943
944static pthread_mutex_t __recursive_lock = PTHREAD_MUTEX_INITIALIZER;
945
946static void
947_recursive_lock(void)
948{
949 _normal_lock( &__recursive_lock);
950}
951
952static void
953_recursive_unlock(void)
954{
955 _normal_unlock( &__recursive_lock );
956}
957
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800958#define __likely(cond) __builtin_expect(!!(cond), 1)
959#define __unlikely(cond) __builtin_expect(!!(cond), 0)
960
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800961int pthread_mutex_lock(pthread_mutex_t *mutex)
962{
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700963 int mtype, tid, new_lock_type;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800964
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700965 if (__unlikely(mutex == NULL))
966 return EINVAL;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800967
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700968 mtype = (mutex->value & MUTEX_TYPE_MASK);
Fabrice Di Meglio86418332010-03-11 14:47:47 -0800969
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700970 /* Handle normal case first */
971 if ( __likely(mtype == MUTEX_TYPE_NORMAL) ) {
972 _normal_lock(mutex);
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -0800973 return 0;
974 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -0700975
976 /* Do we already own this recursive or error-check mutex ? */
977 tid = __get_thread()->kernel_id;
978 if ( tid == MUTEX_OWNER(mutex) )
979 {
980 int oldv, counter;
981
982 if (mtype == MUTEX_TYPE_ERRORCHECK) {
983 /* trying to re-lock a mutex we already acquired */
984 return EDEADLK;
985 }
986 /*
987 * We own the mutex, but other threads are able to change
988 * the contents (e.g. promoting it to "contended"), so we
989 * need to hold the global lock.
990 */
991 _recursive_lock();
992 oldv = mutex->value;
993 counter = (oldv + (1 << MUTEX_COUNTER_SHIFT)) & MUTEX_COUNTER_MASK;
994 mutex->value = (oldv & ~MUTEX_COUNTER_MASK) | counter;
995 _recursive_unlock();
996 return 0;
997 }
998
999 /* We don't own the mutex, so try to get it.
1000 *
1001 * First, we try to change its state from 0 to 1, if this
1002 * doesn't work, try to change it to state 2.
1003 */
1004 new_lock_type = 1;
1005
1006 for (;;) {
1007 int oldv;
1008
1009 _recursive_lock();
1010 oldv = mutex->value;
1011 if (oldv == mtype) { /* uncontended released lock => 1 or 2 */
1012 mutex->value = ((tid << 16) | mtype | new_lock_type);
1013 } else if ((oldv & 3) == 1) { /* locked state 1 => state 2 */
1014 oldv ^= 3;
1015 mutex->value = oldv;
1016 }
1017 _recursive_unlock();
1018
1019 if (oldv == mtype)
1020 break;
1021
1022 /*
1023 * The lock was held, possibly contended by others. From
1024 * now on, if we manage to acquire the lock, we have to
1025 * assume that others are still contending for it so that
1026 * we'll wake them when we unlock it.
1027 */
1028 new_lock_type = 2;
1029
1030 __futex_wait( &mutex->value, oldv, 0 );
1031 }
1032 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001033}
1034
1035
1036int pthread_mutex_unlock(pthread_mutex_t *mutex)
1037{
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001038 int mtype, tid, oldv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001039
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001040 if (__unlikely(mutex == NULL))
1041 return EINVAL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001042
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001043 mtype = (mutex->value & MUTEX_TYPE_MASK);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001044
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001045 /* Handle common case first */
1046 if (__likely(mtype == MUTEX_TYPE_NORMAL)) {
1047 _normal_unlock(mutex);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001048 return 0;
1049 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001050
1051 /* Do we already own this recursive or error-check mutex ? */
1052 tid = __get_thread()->kernel_id;
1053 if ( tid != MUTEX_OWNER(mutex) )
1054 return EPERM;
1055
1056 /* We do, decrement counter or release the mutex if it is 0 */
1057 _recursive_lock();
1058 oldv = mutex->value;
1059 if (oldv & MUTEX_COUNTER_MASK) {
1060 mutex->value = oldv - (1 << MUTEX_COUNTER_SHIFT);
1061 oldv = 0;
1062 } else {
1063 mutex->value = mtype;
1064 }
1065 _recursive_unlock();
1066
1067 /* Wake one waiting thread, if any */
1068 if ((oldv & 3) == 2)
1069 __futex_wake( &mutex->value, 1 );
1070
1071 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001072}
1073
1074
1075int pthread_mutex_trylock(pthread_mutex_t *mutex)
1076{
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001077 int mtype, tid, oldv;
1078
1079 if (__unlikely(mutex == NULL))
1080 return EINVAL;
1081
1082 mtype = (mutex->value & MUTEX_TYPE_MASK);
1083
1084 /* Handle common case first */
1085 if ( __likely(mtype == MUTEX_TYPE_NORMAL) )
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001086 {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001087 if (__atomic_cmpxchg(0, 1, &mutex->value) == 0)
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001088 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001089
1090 return EBUSY;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001091 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001092
1093 /* Do we already own this recursive or error-check mutex ? */
1094 tid = __get_thread()->kernel_id;
1095 if ( tid == MUTEX_OWNER(mutex) )
1096 {
1097 int counter;
1098
1099 if (mtype == MUTEX_TYPE_ERRORCHECK) {
1100 /* already locked by ourselves */
1101 return EDEADLK;
1102 }
1103
1104 _recursive_lock();
1105 oldv = mutex->value;
1106 counter = (oldv + (1 << MUTEX_COUNTER_SHIFT)) & MUTEX_COUNTER_MASK;
1107 mutex->value = (oldv & ~MUTEX_COUNTER_MASK) | counter;
1108 _recursive_unlock();
1109 return 0;
1110 }
1111
1112 /* Try to lock it, just once. */
1113 _recursive_lock();
1114 oldv = mutex->value;
1115 if (oldv == mtype) /* uncontended released lock => state 1 */
1116 mutex->value = ((tid << 16) | mtype | 1);
1117 _recursive_unlock();
1118
1119 if (oldv != mtype)
1120 return EBUSY;
1121
1122 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001123}
1124
1125
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001126/* initialize 'ts' with the difference between 'abstime' and the current time
1127 * according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise.
1128 */
1129static int
1130__timespec_to_absolute(struct timespec* ts, const struct timespec* abstime, clockid_t clock)
1131{
1132 clock_gettime(clock, ts);
1133 ts->tv_sec = abstime->tv_sec - ts->tv_sec;
1134 ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec;
1135 if (ts->tv_nsec < 0) {
1136 ts->tv_sec--;
1137 ts->tv_nsec += 1000000000;
1138 }
David 'Digit' Turnerbc10cd22009-09-23 15:56:50 -07001139 if ((ts->tv_nsec < 0) || (ts->tv_sec < 0))
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001140 return -1;
1141
1142 return 0;
1143}
1144
1145/* initialize 'abstime' to the current time according to 'clock' plus 'msecs'
1146 * milliseconds.
1147 */
1148static void
1149__timespec_to_relative_msec(struct timespec* abstime, unsigned msecs, clockid_t clock)
1150{
1151 clock_gettime(clock, abstime);
1152 abstime->tv_sec += msecs/1000;
1153 abstime->tv_nsec += (msecs%1000)*1000000;
1154 if (abstime->tv_nsec >= 1000000000) {
1155 abstime->tv_sec++;
1156 abstime->tv_nsec -= 1000000000;
1157 }
1158}
1159
1160int pthread_mutex_lock_timeout_np(pthread_mutex_t *mutex, unsigned msecs)
1161{
1162 clockid_t clock = CLOCK_MONOTONIC;
1163 struct timespec abstime;
1164 struct timespec ts;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001165 int mtype, tid, oldv, new_lock_type;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001166
1167 /* compute absolute expiration time */
1168 __timespec_to_relative_msec(&abstime, msecs, clock);
1169
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001170 if (__unlikely(mutex == NULL))
1171 return EINVAL;
1172
1173 mtype = (mutex->value & MUTEX_TYPE_MASK);
1174
1175 /* Handle common case first */
1176 if ( __likely(mtype == MUTEX_TYPE_NORMAL) )
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001177 {
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001178 /* fast path for unconteded lock */
1179 if (__atomic_cmpxchg(0, 1, &mutex->value) == 0)
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001180 return 0;
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001181
1182 /* loop while needed */
1183 while (__atomic_swap(2, &mutex->value) != 0) {
1184 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
1185 return EBUSY;
1186
1187 __futex_wait(&mutex->value, 2, &ts);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001188 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001189 return 0;
David 'Digit' Turnerba9c6f02010-03-10 16:44:08 -08001190 }
David 'Digit' Turner40e6b822010-03-17 11:25:46 -07001191
1192 /* Do we already own this recursive or error-check mutex ? */
1193 tid = __get_thread()->kernel_id;
1194 if ( tid == MUTEX_OWNER(mutex) )
1195 {
1196 int oldv, counter;
1197
1198 if (mtype == MUTEX_TYPE_ERRORCHECK) {
1199 /* already locked by ourselves */
1200 return EDEADLK;
1201 }
1202
1203 _recursive_lock();
1204 oldv = mutex->value;
1205 counter = (oldv + (1 << MUTEX_COUNTER_SHIFT)) & MUTEX_COUNTER_MASK;
1206 mutex->value = (oldv & ~MUTEX_COUNTER_MASK) | counter;
1207 _recursive_unlock();
1208 return 0;
1209 }
1210
1211 /* We don't own the mutex, so try to get it.
1212 *
1213 * First, we try to change its state from 0 to 1, if this
1214 * doesn't work, try to change it to state 2.
1215 */
1216 new_lock_type = 1;
1217
1218 for (;;) {
1219 int oldv;
1220 struct timespec ts;
1221
1222 _recursive_lock();
1223 oldv = mutex->value;
1224 if (oldv == mtype) { /* uncontended released lock => 1 or 2 */
1225 mutex->value = ((tid << 16) | mtype | new_lock_type);
1226 } else if ((oldv & 3) == 1) { /* locked state 1 => state 2 */
1227 oldv ^= 3;
1228 mutex->value = oldv;
1229 }
1230 _recursive_unlock();
1231
1232 if (oldv == mtype)
1233 break;
1234
1235 /*
1236 * The lock was held, possibly contended by others. From
1237 * now on, if we manage to acquire the lock, we have to
1238 * assume that others are still contending for it so that
1239 * we'll wake them when we unlock it.
1240 */
1241 new_lock_type = 2;
1242
1243 if (__timespec_to_absolute(&ts, &abstime, clock) < 0)
1244 return EBUSY;
1245
1246 __futex_wait( &mutex->value, oldv, &ts );
1247 }
1248 return 0;
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001249}
1250
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001251int pthread_condattr_init(pthread_condattr_t *attr)
1252{
1253 if (attr == NULL)
1254 return EINVAL;
1255
1256 *attr = PTHREAD_PROCESS_PRIVATE;
1257 return 0;
1258}
1259
1260int pthread_condattr_getpshared(pthread_condattr_t *attr, int *pshared)
1261{
1262 if (attr == NULL || pshared == NULL)
1263 return EINVAL;
1264
1265 *pshared = *attr;
1266 return 0;
1267}
1268
1269int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
1270{
1271 if (attr == NULL)
1272 return EINVAL;
1273
1274 if (pshared != PTHREAD_PROCESS_SHARED &&
1275 pshared != PTHREAD_PROCESS_PRIVATE)
1276 return EINVAL;
1277
1278 *attr = pshared;
1279 return 0;
1280}
1281
1282int pthread_condattr_destroy(pthread_condattr_t *attr)
1283{
1284 if (attr == NULL)
1285 return EINVAL;
1286
1287 *attr = 0xdeada11d;
1288 return 0;
1289}
1290
1291/* We use one bit in condition variable values as the 'shared' flag
1292 * The rest is a counter.
1293 */
1294#define COND_SHARING_MASK 0x0001
1295#define COND_COUNTER_INCREMENT 0x0002
1296#define COND_COUNTER_MASK (~COND_SHARING_MASK)
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001297
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001298/* XXX *technically* there is a race condition that could allow
1299 * XXX a signal to be missed. If thread A is preempted in _wait()
1300 * XXX after unlocking the mutex and before waiting, and if other
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001301 * XXX threads call signal or broadcast UINT_MAX/2 times (exactly),
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001302 * XXX before thread A is scheduled again and calls futex_wait(),
1303 * XXX then the signal will be lost.
1304 */
1305
1306int pthread_cond_init(pthread_cond_t *cond,
1307 const pthread_condattr_t *attr)
1308{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001309 if (cond == NULL)
1310 return EINVAL;
1311
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001312 cond->value = 0;
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001313
1314 if (attr != NULL && *attr == PTHREAD_PROCESS_SHARED)
1315 cond->value |= COND_SHARING_MASK;
1316
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001317 return 0;
1318}
1319
1320int pthread_cond_destroy(pthread_cond_t *cond)
1321{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001322 if (cond == NULL)
1323 return EINVAL;
1324
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001325 cond->value = 0xdeadc04d;
1326 return 0;
1327}
1328
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001329/* This function is used by pthread_cond_broadcast and
1330 * pthread_cond_signal to atomically decrement the counter.
1331 */
1332static void
1333__pthread_cond_pulse(pthread_cond_t *cond)
1334{
1335 long flags = (cond->value & ~COND_COUNTER_MASK);
1336
1337 for (;;) {
1338 long oldval = cond->value;
1339 long newval = ((oldval - COND_COUNTER_INCREMENT) & COND_COUNTER_MASK)
1340 | flags;
1341 if (__atomic_cmpxchg(oldval, newval, &cond->value) == 0)
1342 break;
1343 }
1344}
1345
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001346int pthread_cond_broadcast(pthread_cond_t *cond)
1347{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001348 if (__unlikely(cond == NULL))
1349 return EINVAL;
1350
1351 __pthread_cond_pulse(cond);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001352 __futex_wake(&cond->value, INT_MAX);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001353 return 0;
1354}
1355
1356int pthread_cond_signal(pthread_cond_t *cond)
1357{
David 'Digit' Turneree7b0772010-03-18 14:07:42 -07001358 if (__unlikely(cond == NULL))
1359 return EINVAL;
1360
1361 __pthread_cond_pulse(cond);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001362 __futex_wake(&cond->value, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001363 return 0;
1364}
1365
1366int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
1367{
1368 return pthread_cond_timedwait(cond, mutex, NULL);
1369}
1370
1371int __pthread_cond_timedwait_relative(pthread_cond_t *cond,
1372 pthread_mutex_t * mutex,
1373 const struct timespec *reltime)
1374{
1375 int status;
1376 int oldvalue = cond->value;
1377
1378 pthread_mutex_unlock(mutex);
Fabrice Di Meglio86418332010-03-11 14:47:47 -08001379 status = __futex_wait(&cond->value, oldvalue, reltime);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001380 pthread_mutex_lock(mutex);
1381
1382 if (status == (-ETIMEDOUT)) return ETIMEDOUT;
1383 return 0;
1384}
1385
1386int __pthread_cond_timedwait(pthread_cond_t *cond,
1387 pthread_mutex_t * mutex,
1388 const struct timespec *abstime,
1389 clockid_t clock)
1390{
1391 struct timespec ts;
1392 struct timespec * tsp;
1393
1394 if (abstime != NULL) {
David 'Digit' Turner3f56b7f2009-09-22 12:40:22 -07001395 if (__timespec_to_absolute(&ts, abstime, clock) < 0)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001396 return ETIMEDOUT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001397 tsp = &ts;
1398 } else {
1399 tsp = NULL;
1400 }
1401
1402 return __pthread_cond_timedwait_relative(cond, mutex, tsp);
1403}
1404
1405int pthread_cond_timedwait(pthread_cond_t *cond,
1406 pthread_mutex_t * mutex,
1407 const struct timespec *abstime)
1408{
1409 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_REALTIME);
1410}
1411
1412
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001413/* this one exists only for backward binary compatibility */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001414int pthread_cond_timedwait_monotonic(pthread_cond_t *cond,
1415 pthread_mutex_t * mutex,
1416 const struct timespec *abstime)
1417{
1418 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1419}
1420
Mathias Agopiana2f5e212009-07-13 15:00:46 -07001421int pthread_cond_timedwait_monotonic_np(pthread_cond_t *cond,
1422 pthread_mutex_t * mutex,
1423 const struct timespec *abstime)
1424{
1425 return __pthread_cond_timedwait(cond, mutex, abstime, CLOCK_MONOTONIC);
1426}
1427
1428int pthread_cond_timedwait_relative_np(pthread_cond_t *cond,
1429 pthread_mutex_t * mutex,
1430 const struct timespec *reltime)
1431{
1432 return __pthread_cond_timedwait_relative(cond, mutex, reltime);
1433}
1434
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001435int pthread_cond_timeout_np(pthread_cond_t *cond,
1436 pthread_mutex_t * mutex,
1437 unsigned msecs)
1438{
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001439 struct timespec ts;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001440
1441 ts.tv_sec = msecs / 1000;
1442 ts.tv_nsec = (msecs % 1000) * 1000000;
1443
Matthieu CASTETa4e67f42008-12-27 00:04:10 +01001444 return __pthread_cond_timedwait_relative(cond, mutex, &ts);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001445}
1446
1447
1448
1449/* A technical note regarding our thread-local-storage (TLS) implementation:
1450 *
1451 * There can be up to TLSMAP_SIZE independent TLS keys in a given process,
1452 * though the first TLSMAP_START keys are reserved for Bionic to hold
1453 * special thread-specific variables like errno or a pointer to
1454 * the current thread's descriptor.
1455 *
1456 * while stored in the TLS area, these entries cannot be accessed through
1457 * pthread_getspecific() / pthread_setspecific() and pthread_key_delete()
1458 *
1459 * also, some entries in the key table are pre-allocated (see tlsmap_lock)
1460 * to greatly simplify and speedup some OpenGL-related operations. though the
1461 * initialy value will be NULL on all threads.
1462 *
1463 * you can use pthread_getspecific()/setspecific() on these, and in theory
1464 * you could also call pthread_key_delete() as well, though this would
1465 * probably break some apps.
1466 *
1467 * The 'tlsmap_t' type defined below implements a shared global map of
1468 * currently created/allocated TLS keys and the destructors associated
1469 * with them. You should use tlsmap_lock/unlock to access it to avoid
1470 * any race condition.
1471 *
1472 * the global TLS map simply contains a bitmap of allocated keys, and
1473 * an array of destructors.
1474 *
1475 * each thread has a TLS area that is a simple array of TLSMAP_SIZE void*
1476 * pointers. the TLS area of the main thread is stack-allocated in
1477 * __libc_init_common, while the TLS area of other threads is placed at
1478 * the top of their stack in pthread_create.
1479 *
1480 * when pthread_key_create() is called, it finds the first free key in the
1481 * bitmap, then set it to 1, saving the destructor altogether
1482 *
1483 * when pthread_key_delete() is called. it will erase the key's bitmap bit
1484 * and its destructor, and will also clear the key data in the TLS area of
1485 * all created threads. As mandated by Posix, it is the responsability of
1486 * the caller of pthread_key_delete() to properly reclaim the objects that
1487 * were pointed to by these data fields (either before or after the call).
1488 *
1489 */
1490
1491/* TLS Map implementation
1492 */
1493
1494#define TLSMAP_START (TLS_SLOT_MAX_WELL_KNOWN+1)
1495#define TLSMAP_SIZE BIONIC_TLS_SLOTS
1496#define TLSMAP_BITS 32
1497#define TLSMAP_WORDS ((TLSMAP_SIZE+TLSMAP_BITS-1)/TLSMAP_BITS)
1498#define TLSMAP_WORD(m,k) (m)->map[(k)/TLSMAP_BITS]
1499#define TLSMAP_MASK(k) (1U << ((k)&(TLSMAP_BITS-1)))
1500
1501/* this macro is used to quickly check that a key belongs to a reasonable range */
1502#define TLSMAP_VALIDATE_KEY(key) \
1503 ((key) >= TLSMAP_START && (key) < TLSMAP_SIZE)
1504
1505/* the type of tls key destructor functions */
1506typedef void (*tls_dtor_t)(void*);
1507
1508typedef struct {
1509 int init; /* see comment in tlsmap_lock() */
1510 uint32_t map[TLSMAP_WORDS]; /* bitmap of allocated keys */
1511 tls_dtor_t dtors[TLSMAP_SIZE]; /* key destructors */
1512} tlsmap_t;
1513
1514static pthread_mutex_t _tlsmap_lock = PTHREAD_MUTEX_INITIALIZER;
1515static tlsmap_t _tlsmap;
1516
1517/* lock the global TLS map lock and return a handle to it */
1518static __inline__ tlsmap_t* tlsmap_lock(void)
1519{
1520 tlsmap_t* m = &_tlsmap;
1521
1522 pthread_mutex_lock(&_tlsmap_lock);
1523 /* we need to initialize the first entry of the 'map' array
1524 * with the value TLS_DEFAULT_ALLOC_MAP. doing it statically
1525 * when declaring _tlsmap is a bit awkward and is going to
1526 * produce warnings, so do it the first time we use the map
1527 * instead
1528 */
1529 if (__unlikely(!m->init)) {
1530 TLSMAP_WORD(m,0) = TLS_DEFAULT_ALLOC_MAP;
1531 m->init = 1;
1532 }
1533 return m;
1534}
1535
1536/* unlock the global TLS map */
1537static __inline__ void tlsmap_unlock(tlsmap_t* m)
1538{
1539 pthread_mutex_unlock(&_tlsmap_lock);
1540 (void)m; /* a good compiler is a happy compiler */
1541}
1542
1543/* test to see wether a key is allocated */
1544static __inline__ int tlsmap_test(tlsmap_t* m, int key)
1545{
1546 return (TLSMAP_WORD(m,key) & TLSMAP_MASK(key)) != 0;
1547}
1548
1549/* set the destructor and bit flag on a newly allocated key */
1550static __inline__ void tlsmap_set(tlsmap_t* m, int key, tls_dtor_t dtor)
1551{
1552 TLSMAP_WORD(m,key) |= TLSMAP_MASK(key);
1553 m->dtors[key] = dtor;
1554}
1555
1556/* clear the destructor and bit flag on an existing key */
1557static __inline__ void tlsmap_clear(tlsmap_t* m, int key)
1558{
1559 TLSMAP_WORD(m,key) &= ~TLSMAP_MASK(key);
1560 m->dtors[key] = NULL;
1561}
1562
1563/* allocate a new TLS key, return -1 if no room left */
1564static int tlsmap_alloc(tlsmap_t* m, tls_dtor_t dtor)
1565{
1566 int key;
1567
1568 for ( key = TLSMAP_START; key < TLSMAP_SIZE; key++ ) {
1569 if ( !tlsmap_test(m, key) ) {
1570 tlsmap_set(m, key, dtor);
1571 return key;
1572 }
1573 }
1574 return -1;
1575}
1576
1577
1578int pthread_key_create(pthread_key_t *key, void (*destructor_function)(void *))
1579{
1580 uint32_t err = ENOMEM;
1581 tlsmap_t* map = tlsmap_lock();
1582 int k = tlsmap_alloc(map, destructor_function);
1583
1584 if (k >= 0) {
1585 *key = k;
1586 err = 0;
1587 }
1588 tlsmap_unlock(map);
1589 return err;
1590}
1591
1592
1593/* This deletes a pthread_key_t. note that the standard mandates that this does
1594 * not call the destructor of non-NULL key values. Instead, it is the
1595 * responsability of the caller to properly dispose of the corresponding data
1596 * and resources, using any mean it finds suitable.
1597 *
1598 * On the other hand, this function will clear the corresponding key data
1599 * values in all known threads. this prevents later (invalid) calls to
1600 * pthread_getspecific() to receive invalid/stale values.
1601 */
1602int pthread_key_delete(pthread_key_t key)
1603{
1604 uint32_t err;
1605 pthread_internal_t* thr;
1606 tlsmap_t* map;
1607
1608 if (!TLSMAP_VALIDATE_KEY(key)) {
1609 return EINVAL;
1610 }
1611
1612 map = tlsmap_lock();
1613
1614 if (!tlsmap_test(map, key)) {
1615 err = EINVAL;
1616 goto err1;
1617 }
1618
1619 /* clear value in all threads */
1620 pthread_mutex_lock(&gThreadListLock);
1621 for ( thr = gThreadList; thr != NULL; thr = thr->next ) {
1622 /* avoid zombie threads with a negative 'join_count'. these are really
1623 * already dead and don't have a TLS area anymore.
1624 *
1625 * similarly, it is possible to have thr->tls == NULL for threads that
1626 * were just recently created through pthread_create() but whose
1627 * startup trampoline (__thread_entry) hasn't been run yet by the
1628 * scheduler. so check for this too.
1629 */
1630 if (thr->join_count < 0 || !thr->tls)
1631 continue;
1632
1633 thr->tls[key] = NULL;
1634 }
1635 tlsmap_clear(map, key);
1636
1637 pthread_mutex_unlock(&gThreadListLock);
1638 err = 0;
1639
1640err1:
1641 tlsmap_unlock(map);
1642 return err;
1643}
1644
1645
1646int pthread_setspecific(pthread_key_t key, const void *ptr)
1647{
1648 int err = EINVAL;
1649 tlsmap_t* map;
1650
1651 if (TLSMAP_VALIDATE_KEY(key)) {
1652 /* check that we're trying to set data for an allocated key */
1653 map = tlsmap_lock();
1654 if (tlsmap_test(map, key)) {
1655 ((uint32_t *)__get_tls())[key] = (uint32_t)ptr;
1656 err = 0;
1657 }
1658 tlsmap_unlock(map);
1659 }
1660 return err;
1661}
1662
1663void * pthread_getspecific(pthread_key_t key)
1664{
1665 if (!TLSMAP_VALIDATE_KEY(key)) {
1666 return NULL;
1667 }
1668
1669 /* for performance reason, we do not lock/unlock the global TLS map
1670 * to check that the key is properly allocated. if the key was not
1671 * allocated, the value read from the TLS should always be NULL
1672 * due to pthread_key_delete() clearing the values for all threads.
1673 */
1674 return (void *)(((unsigned *)__get_tls())[key]);
1675}
1676
1677/* Posix mandates that this be defined in <limits.h> but we don't have
1678 * it just yet.
1679 */
1680#ifndef PTHREAD_DESTRUCTOR_ITERATIONS
1681# define PTHREAD_DESTRUCTOR_ITERATIONS 4
1682#endif
1683
1684/* this function is called from pthread_exit() to remove all TLS key data
1685 * from this thread's TLS area. this must call the destructor of all keys
1686 * that have a non-NULL data value (and a non-NULL destructor).
1687 *
1688 * because destructors can do funky things like deleting/creating other
1689 * keys, we need to implement this in a loop
1690 */
1691static void pthread_key_clean_all(void)
1692{
1693 tlsmap_t* map;
1694 void** tls = (void**)__get_tls();
1695 int rounds = PTHREAD_DESTRUCTOR_ITERATIONS;
1696
1697 map = tlsmap_lock();
1698
1699 for (rounds = PTHREAD_DESTRUCTOR_ITERATIONS; rounds > 0; rounds--)
1700 {
1701 int kk, count = 0;
1702
1703 for (kk = TLSMAP_START; kk < TLSMAP_SIZE; kk++) {
1704 if ( tlsmap_test(map, kk) )
1705 {
1706 void* data = tls[kk];
1707 tls_dtor_t dtor = map->dtors[kk];
1708
1709 if (data != NULL && dtor != NULL)
1710 {
1711 /* we need to clear the key data now, this will prevent the
1712 * destructor (or a later one) from seeing the old value if
1713 * it calls pthread_getspecific() for some odd reason
1714 *
1715 * we do not do this if 'dtor == NULL' just in case another
1716 * destructor function might be responsible for manually
1717 * releasing the corresponding data.
1718 */
1719 tls[kk] = NULL;
1720
1721 /* because the destructor is free to call pthread_key_create
1722 * and/or pthread_key_delete, we need to temporarily unlock
1723 * the TLS map
1724 */
1725 tlsmap_unlock(map);
1726 (*dtor)(data);
1727 map = tlsmap_lock();
1728
1729 count += 1;
1730 }
1731 }
1732 }
1733
1734 /* if we didn't call any destructor, there is no need to check the
1735 * TLS data again
1736 */
1737 if (count == 0)
1738 break;
1739 }
1740 tlsmap_unlock(map);
1741}
1742
1743// man says this should be in <linux/unistd.h>, but it isn't
1744extern int tkill(int tid, int sig);
1745
1746int pthread_kill(pthread_t tid, int sig)
1747{
1748 int ret;
1749 int old_errno = errno;
1750 pthread_internal_t * thread = (pthread_internal_t *)tid;
1751
1752 ret = tkill(thread->kernel_id, sig);
1753 if (ret < 0) {
1754 ret = errno;
1755 errno = old_errno;
1756 }
1757
1758 return ret;
1759}
1760
1761extern int __rt_sigprocmask(int, const sigset_t *, sigset_t *, size_t);
1762
1763int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
1764{
David 'Digit' Turner8f8b5312010-03-01 11:30:40 -08001765 /* pthread_sigmask must return the error code, but the syscall
1766 * will set errno instead and return 0/-1
1767 */
1768 int ret, old_errno = errno;
1769
1770 ret = __rt_sigprocmask(how, set, oset, _NSIG / 8);
1771 if (ret < 0)
1772 ret = errno;
1773
1774 errno = old_errno;
1775 return ret;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001776}
1777
1778
1779int pthread_getcpuclockid(pthread_t tid, clockid_t *clockid)
1780{
1781 const int CLOCK_IDTYPE_BITS = 3;
1782 pthread_internal_t* thread = (pthread_internal_t*)tid;
1783
1784 if (!thread)
1785 return ESRCH;
1786
1787 *clockid = CLOCK_THREAD_CPUTIME_ID | (thread->kernel_id << CLOCK_IDTYPE_BITS);
1788 return 0;
1789}
1790
1791
1792/* NOTE: this implementation doesn't support a init function that throws a C++ exception
1793 * or calls fork()
1794 */
1795int pthread_once( pthread_once_t* once_control, void (*init_routine)(void) )
1796{
1797 static pthread_mutex_t once_lock = PTHREAD_MUTEX_INITIALIZER;
1798
1799 if (*once_control == PTHREAD_ONCE_INIT) {
1800 _normal_lock( &once_lock );
1801 if (*once_control == PTHREAD_ONCE_INIT) {
1802 (*init_routine)();
1803 *once_control = ~PTHREAD_ONCE_INIT;
1804 }
1805 _normal_unlock( &once_lock );
1806 }
1807 return 0;
1808}