pthread_internal.h revision 7086ad6919feb2415c6027163f5c63323bcca27c
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved. 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met: 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions of source code must retain the above copyright 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer. 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions in binary form must reproduce the above copyright 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer in 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the documentation and/or other materials provided with the 131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * distribution. 141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE. 271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef _PTHREAD_INTERNAL_H_ 291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _PTHREAD_INTERNAL_H_ 301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <pthread.h> 321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 33c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughesstruct pthread_internal_t { 34877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes struct pthread_internal_t* next; 35877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes struct pthread_internal_t* prev; 36877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 37877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pid_t tid; 38877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 397086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes private: 407086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes pid_t cached_pid_; 417086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes 427086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes public: 437086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes pid_t invalidate_cached_pid() { 447086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes pid_t old_value; 457086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes get_cached_pid(&old_value); 467086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes set_cached_pid(0); 477086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes return old_value; 487086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes } 497086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes 507086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes void set_cached_pid(pid_t value) { 517086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes cached_pid_ = value; 527086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes } 537086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes 547086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes bool get_cached_pid(pid_t* cached_pid) { 557086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes *cached_pid = cached_pid_; 567086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes return (*cached_pid != 0); 577086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes } 587086ad6919feb2415c6027163f5c63323bcca27cElliott Hughes 59877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes void** tls; 60877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 61877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes pthread_attr_t attr; 62877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 63877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes __pthread_cleanup_t* cleanup_stack; 64877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 65877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes void* (*start_routine)(void*); 66877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes void* start_routine_arg; 67877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes void* return_value; 68877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 69877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes void* alternate_signal_stack; 70877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes 71b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes pthread_mutex_t startup_handshake_mutex; 72b30aff405a220495941f1673b0a5e66c4fa8b84cElliott Hughes 73877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes /* 74877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes * The dynamic linker implements dlerror(3), which makes it hard for us to implement this 75877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes * per-thread buffer by simply using malloc(3) and free(3). 76877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes */ 775419b9474753d25dff947c7740532f86d130c0beElliott Hughes#define __BIONIC_DLERROR_BUFFER_SIZE 512 78877ec6d90418ff1d6597147d355a2229fdffae7eElliott Hughes char dlerror_buffer[__BIONIC_DLERROR_BUFFER_SIZE]; 79c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes}; 801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 81cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes__LIBC_HIDDEN__ int __init_thread(pthread_internal_t* thread, bool add_to_thread_list); 82c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ void __init_tls(pthread_internal_t* thread); 8370b24b1cc2a1a4436b1fea3f8b76616fdcb27224Elliott Hughes__LIBC_HIDDEN__ void __init_alternate_signal_stack(pthread_internal_t*); 84c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ void _pthread_internal_add(pthread_internal_t* thread); 85c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ pthread_internal_t* __get_thread(void); 861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8744b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes__LIBC_HIDDEN__ void pthread_key_clean_all(void); 889d23e04c43dbb8480bea8be28b8a2f37423bec49Elliott Hughes__LIBC_HIDDEN__ void _pthread_internal_remove_locked(pthread_internal_t* thread); 8944b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes 900f020d18b138e24b1fe34074808e07ac412f35a4msg/* Has the thread been detached by a pthread_join or pthread_detach call? */ 912b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes#define PTHREAD_ATTR_FLAG_DETACHED 0x00000001 920f020d18b138e24b1fe34074808e07ac412f35a4msg 930f020d18b138e24b1fe34074808e07ac412f35a4msg/* Was the thread's stack allocated by the user rather than by us? */ 942b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes#define PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK 0x00000002 953e898476c7230b60a0f76968e64ff25f475b48c0Elliott Hughes 960f020d18b138e24b1fe34074808e07ac412f35a4msg/* Has the thread been joined by another thread? */ 972b6e43e00ece68b3aba26d8f95f07cd9d9294ab4Elliott Hughes#define PTHREAD_ATTR_FLAG_JOINED 0x00000004 980f020d18b138e24b1fe34074808e07ac412f35a4msg 99cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes/* Is this the main thread? */ 100cef3faec0ea40fdfe58e425fd0be64f00de6a26dElliott Hughes#define PTHREAD_ATTR_FLAG_MAIN_THREAD 0x80000000 101eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes 10250af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom/* 10350af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom * Traditionally we give threads a 1MiB stack. When we started 10450af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom * allocating per-thread alternate signal stacks to ease debugging of 10550af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom * stack overflows, we subtracted the same amount we were using there 10650af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom * from the default thread stack size. This should keep memory usage 10750af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom * roughly constant. 10850af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom */ 10950af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom#define PTHREAD_STACK_SIZE_DEFAULT ((1 * 1024 * 1024) - SIGSTKSZ) 11050af69e8f326b2762a44d5fea2b118e7616e5d20Brian Carlstrom 1111728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ extern pthread_internal_t* g_thread_list; 1121728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ extern pthread_mutex_t g_thread_list_lock; 11344b53ad6818de344e0b499ad8fdbb21fcb0ff2b6Elliott Hughes 1140e714a5b41451e84c5ded93a42c9a4b0a9440691Elliott Hughes__LIBC_HIDDEN__ int __timespec_from_absolute(timespec*, const timespec*, clockid_t); 1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1164b558f50a42c97d461f1dede5aaaae490ea99e2eElliott Hughes/* Needed by fork. */ 117c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ extern void __bionic_atfork_run_prepare(); 118c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ extern void __bionic_atfork_run_child(); 119c3f114037dbf028896310609fd28cf2b3da99c4dElliott Hughes__LIBC_HIDDEN__ extern void __bionic_atfork_run_parent(); 1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* _PTHREAD_INTERNAL_H_ */ 122