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 31 #include "bionic_tls.h" 32 #include "ScopedPthreadMutexLocker.h" 33 34 __LIBC_HIDDEN__ pthread_internal_t* gThreadList = NULL; 35 __LIBC_HIDDEN__ pthread_mutex_t gThreadListLock = PTHREAD_MUTEX_INITIALIZER; 36 37 void _pthread_internal_remove_locked(pthread_internal_t* thread) { 38 if (thread->next != NULL) { 39 thread->next->prev = thread->prev; 40 } 41 if (thread->prev != NULL) { 42 thread->prev->next = thread->next; 43 } else { 44 gThreadList = thread->next; 45 } 46 47 // The main thread is not heap-allocated. See __libc_init_tls for the declaration, 48 // and __libc_init_common for the point where it's added to the thread list. 49 if (thread->allocated_on_heap) { 50 free(thread); 51 } 52 } 53 54 __LIBC_ABI_PRIVATE__ void _pthread_internal_add(pthread_internal_t* thread) { 55 ScopedPthreadMutexLocker locker(&gThreadListLock); 56 57 // We insert at the head. 58 thread->next = gThreadList; 59 thread->prev = NULL; 60 if (thread->next != NULL) { 61 thread->next->prev = thread; 62 } 63 gThreadList = thread; 64 } 65 66 __LIBC_ABI_PRIVATE__ pthread_internal_t* __get_thread(void) { 67 void** tls = reinterpret_cast<void**>(const_cast<void*>(__get_tls())); 68 return reinterpret_cast<pthread_internal_t*>(tls[TLS_SLOT_THREAD_ID]); 69 } 70