| 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 |  | 
| Yabin Cui | 8574a06 | 2014-12-01 22:37:56 -0800 | [diff] [blame] | 31 | #include <errno.h> | 
| Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 32 | #include <stdlib.h> | 
| Yabin Cui | 8574a06 | 2014-12-01 22:37:56 -0800 | [diff] [blame] | 33 | #include <string.h> | 
|  | 34 | #include <sys/mman.h> | 
| Dan Albert | 75ef63d | 2014-11-21 00:18:07 -0800 | [diff] [blame] | 35 |  | 
| Elliott Hughes | c3f1140 | 2013-10-30 14:40:09 -0700 | [diff] [blame] | 36 | #include "private/bionic_futex.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 37 | #include "private/bionic_tls.h" | 
| Yabin Cui | 8574a06 | 2014-12-01 22:37:56 -0800 | [diff] [blame] | 38 | #include "private/libc_logging.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 39 | #include "private/ScopedPthreadMutexLocker.h" | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 40 |  | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 41 | static pthread_internal_t* g_thread_list = NULL; | 
|  | 42 | static pthread_mutex_t g_thread_list_lock = PTHREAD_MUTEX_INITIALIZER; | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 43 |  | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 44 | pthread_t __pthread_internal_add(pthread_internal_t* thread) { | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 45 | ScopedPthreadMutexLocker locker(&g_thread_list_lock); | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 46 |  | 
|  | 47 | // We insert at the head. | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 48 | thread->next = g_thread_list; | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 49 | thread->prev = NULL; | 
|  | 50 | if (thread->next != NULL) { | 
|  | 51 | thread->next->prev = thread; | 
|  | 52 | } | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 53 | g_thread_list = thread; | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 54 | return reinterpret_cast<pthread_t>(thread); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | void __pthread_internal_remove(pthread_internal_t* thread) { | 
|  | 58 | ScopedPthreadMutexLocker locker(&g_thread_list_lock); | 
|  | 59 |  | 
|  | 60 | if (thread->next != NULL) { | 
|  | 61 | thread->next->prev = thread->prev; | 
|  | 62 | } | 
|  | 63 | if (thread->prev != NULL) { | 
|  | 64 | thread->prev->next = thread->next; | 
|  | 65 | } else { | 
|  | 66 | g_thread_list = thread->next; | 
|  | 67 | } | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | static void __pthread_internal_free(pthread_internal_t* thread) { | 
|  | 71 | if (thread->mmap_size != 0) { | 
|  | 72 | // Free mapped space, including thread stack and pthread_internal_t. | 
|  | 73 | munmap(thread->attr.stack_base, thread->mmap_size); | 
|  | 74 | } | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | void __pthread_internal_remove_and_free(pthread_internal_t* thread) { | 
|  | 78 | __pthread_internal_remove(thread); | 
|  | 79 | __pthread_internal_free(thread); | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | pthread_internal_t* __pthread_internal_find(pthread_t thread_id) { | 
|  | 83 | pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(thread_id); | 
| Dimitry Ivanov | 4bc739a | 2016-02-04 16:21:01 -0800 | [diff] [blame] | 84 |  | 
|  | 85 | // check if thread is pthread_self() before acquiring the lock | 
|  | 86 | if (thread == __get_thread()) { | 
|  | 87 | return thread; | 
|  | 88 | } | 
|  | 89 |  | 
| Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 90 | ScopedPthreadMutexLocker locker(&g_thread_list_lock); | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 91 |  | 
| Yabin Cui | 220b99b | 2015-03-26 18:13:07 +0000 | [diff] [blame] | 92 | for (pthread_internal_t* t = g_thread_list; t != NULL; t = t->next) { | 
|  | 93 | if (t == thread) { | 
|  | 94 | return thread; | 
| Yabin Cui | 673b15e | 2015-03-19 14:19:19 -0700 | [diff] [blame] | 95 | } | 
|  | 96 | } | 
|  | 97 | return NULL; | 
| Elliott Hughes | 9d23e04 | 2013-02-15 19:21:51 -0800 | [diff] [blame] | 98 | } |