Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | #include "pthread_internal.h" |
| 30 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 31 | #include "private/bionic_futex.h" |
| 32 | #include "private/bionic_pthread.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 33 | #include "private/bionic_tls.h" |
| 34 | #include "private/ScopedPthreadMutexLocker.h" |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 36 | pthread_internal_t* gThreadList = NULL; |
| 37 | pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER; |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 38 | |
| 39 | void _pthread_internal_remove_locked(pthread_internal_t* thread) { |
| 40 | if (thread->next != NULL) { |
| 41 | thread->next->prev = thread->prev; |
| 42 | } |
| 43 | if (thread->prev != NULL) { |
| 44 | thread->prev->next = thread->next; |
| 45 | } else { |
| 46 | gThreadList = thread->next; |
| 47 | } |
| 48 | |
| 49 | // The main thread is not heap-allocated. See __libc_init_tls for the declaration, |
| 50 | // and __libc_init_common for the point where it's added to the thread list. |
Elliott Hughes | cef3fae | 2013-11-19 16:52:24 -0800 | [diff] [blame] | 51 | if ((thread->attr.flags & PTHREAD_ATTR_FLAG_MAIN_THREAD) == 0) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 52 | free(thread); |
| 53 | } |
| 54 | } |
| 55 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 56 | void _pthread_internal_add(pthread_internal_t* thread) { |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 57 | ScopedPthreadMutexLocker locker(&gThreadListLock); |
| 58 | |
| 59 | // We insert at the head. |
| 60 | thread->next = gThreadList; |
| 61 | thread->prev = NULL; |
| 62 | if (thread->next != NULL) { |
| 63 | thread->next->prev = thread; |
| 64 | } |
| 65 | gThreadList = thread; |
| 66 | } |
| 67 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 68 | pthread_internal_t* __get_thread(void) { |
Elliott Hughes | 2a0b873 | 2013-10-08 18:50:24 -0700 | [diff] [blame] | 69 | return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]); |
Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 70 | } |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 71 | |
| 72 | pid_t __pthread_gettid(pthread_t t) { |
| 73 | return reinterpret_cast<pthread_internal_t*>(t)->tid; |
| 74 | } |
| 75 | |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 76 | // Initialize 'ts' with the difference between 'abstime' and the current time |
| 77 | // according to 'clock'. Returns -1 if abstime already expired, or 0 otherwise. |
Elliott Hughes | 0e714a5 | 2014-03-03 16:42:47 -0800 | [diff] [blame^] | 78 | int __timespec_from_absolute(timespec* ts, const timespec* abstime, clockid_t clock) { |
Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 79 | clock_gettime(clock, ts); |
| 80 | ts->tv_sec = abstime->tv_sec - ts->tv_sec; |
| 81 | ts->tv_nsec = abstime->tv_nsec - ts->tv_nsec; |
| 82 | if (ts->tv_nsec < 0) { |
| 83 | ts->tv_sec--; |
| 84 | ts->tv_nsec += 1000000000; |
| 85 | } |
| 86 | if ((ts->tv_nsec < 0) || (ts->tv_sec < 0)) { |
| 87 | return -1; |
| 88 | } |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int __futex_wake_ex(volatile void* ftx, int pshared, int val) { |
| 93 | return __futex_syscall3(ftx, pshared ? FUTEX_WAKE : FUTEX_WAKE_PRIVATE, val); |
| 94 | } |
| 95 | |
| 96 | int __futex_wait_ex(volatile void* ftx, int pshared, int val, const timespec* timeout) { |
| 97 | return __futex_syscall4(ftx, pshared ? FUTEX_WAIT : FUTEX_WAIT_PRIVATE, val, timeout); |
| 98 | } |