blob: 33cddd74e771683f587a9398275b234e60ee0449 [file] [log] [blame]
Elliott Hughes9d23e042013-02-15 19:21:51 -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
29#include "pthread_internal.h"
30
Yabin Cui8574a062014-12-01 22:37:56 -080031#include <errno.h>
Dan Albert75ef63d2014-11-21 00:18:07 -080032#include <stdlib.h>
Yabin Cui8574a062014-12-01 22:37:56 -080033#include <string.h>
34#include <sys/mman.h>
Dan Albert75ef63d2014-11-21 00:18:07 -080035
Elliott Hughesc3f11402013-10-30 14:40:09 -070036#include "private/bionic_futex.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070037#include "private/bionic_tls.h"
Yabin Cui8574a062014-12-01 22:37:56 -080038#include "private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070039#include "private/ScopedPthreadMutexLocker.h"
Elliott Hughes9d23e042013-02-15 19:21:51 -080040
Elliott Hughes1728b232014-05-14 10:02:03 -070041pthread_internal_t* g_thread_list = NULL;
42pthread_mutex_t g_thread_list_lock = PTHREAD_MUTEX_INITIALIZER;
Elliott Hughes9d23e042013-02-15 19:21:51 -080043
Yabin Cui8574a062014-12-01 22:37:56 -080044pthread_internal_t* __create_thread_struct() {
45 void* result = mmap(NULL, sizeof(pthread_internal_t), PROT_READ | PROT_WRITE,
46 MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
47 if (result == MAP_FAILED) {
48 __libc_format_log(ANDROID_LOG_WARN, "libc",
49 "__create_thread_struct() failed: %s", strerror(errno));
50 return NULL;
51 }
52 return reinterpret_cast<pthread_internal_t*>(result);
53}
54
55void __free_thread_struct(pthread_internal_t* thread) {
56 int result = munmap(thread, sizeof(pthread_internal_t));
57 if (result != 0) {
58 __libc_format_log(ANDROID_LOG_WARN, "libc",
59 "__free_thread_struct() failed: %s", strerror(errno));
60 }
61}
62
Elliott Hughes9d23e042013-02-15 19:21:51 -080063void _pthread_internal_remove_locked(pthread_internal_t* thread) {
64 if (thread->next != NULL) {
65 thread->next->prev = thread->prev;
66 }
67 if (thread->prev != NULL) {
68 thread->prev->next = thread->next;
69 } else {
Elliott Hughes1728b232014-05-14 10:02:03 -070070 g_thread_list = thread->next;
Elliott Hughes9d23e042013-02-15 19:21:51 -080071 }
72
73 // The main thread is not heap-allocated. See __libc_init_tls for the declaration,
74 // and __libc_init_common for the point where it's added to the thread list.
Elliott Hughescef3fae2013-11-19 16:52:24 -080075 if ((thread->attr.flags & PTHREAD_ATTR_FLAG_MAIN_THREAD) == 0) {
Yabin Cui8574a062014-12-01 22:37:56 -080076 __free_thread_struct(thread);
Elliott Hughes9d23e042013-02-15 19:21:51 -080077 }
78}
79
Elliott Hughesc3f11402013-10-30 14:40:09 -070080void _pthread_internal_add(pthread_internal_t* thread) {
Elliott Hughes1728b232014-05-14 10:02:03 -070081 ScopedPthreadMutexLocker locker(&g_thread_list_lock);
Elliott Hughes9d23e042013-02-15 19:21:51 -080082
83 // We insert at the head.
Elliott Hughes1728b232014-05-14 10:02:03 -070084 thread->next = g_thread_list;
Elliott Hughes9d23e042013-02-15 19:21:51 -080085 thread->prev = NULL;
86 if (thread->next != NULL) {
87 thread->next->prev = thread;
88 }
Elliott Hughes1728b232014-05-14 10:02:03 -070089 g_thread_list = thread;
Elliott Hughes9d23e042013-02-15 19:21:51 -080090}
91
Elliott Hughesc3f11402013-10-30 14:40:09 -070092pthread_internal_t* __get_thread(void) {
Elliott Hughes2a0b8732013-10-08 18:50:24 -070093 return reinterpret_cast<pthread_internal_t*>(__get_tls()[TLS_SLOT_THREAD_ID]);
Elliott Hughes9d23e042013-02-15 19:21:51 -080094}