pthread_create.cpp revision 4b4a8824289c48c823cd38bc63289d121aae3d67
14b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes/* 24b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Copyright (C) 2008 The Android Open Source Project 34b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * All rights reserved. 44b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * 54b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * Redistribution and use in source and binary forms, with or without 64b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * modification, are permitted provided that the following conditions 74b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * are met: 84b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * * Redistributions of source code must retain the above copyright 94b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * notice, this list of conditions and the following disclaimer. 104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * * Redistributions in binary form must reproduce the above copyright 114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * notice, this list of conditions and the following disclaimer in 124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * the documentation and/or other materials provided with the 134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * distribution. 144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * 154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes * SUCH DAMAGE. 274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes */ 284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <pthread.h> 304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <errno.h> 324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include <sys/mman.h> 334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "pthread_internal.h" 354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_ssp.h" 374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/bionic_tls.h" 384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/debug_format.h" 394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/logd.h" 404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/thread_private.h" 414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ErrnoRestorer.h" 424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#include "private/ScopedPthreadMutexLocker.h" 434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesextern "C" int __pthread_clone(int (*fn)(void*), void* child_stack, int flags, void* arg); 454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#ifdef __i386__ 474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#define ATTRIBUTES __attribute__((noinline)) __attribute__((fastcall)) 484b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#else 494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#define ATTRIBUTES __attribute__((noinline)) 504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes#endif 514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesextern "C" void ATTRIBUTES _thread_created_hook(pid_t thread_id); 534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic const int kPthreadInitFailed = 1; 554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic pthread_mutex_t gPthreadStackCreationLock = PTHREAD_MUTEX_INITIALIZER; 574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic pthread_mutex_t gDebuggerNotificationLock = PTHREAD_MUTEX_INITIALIZER; 594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesvoid __init_tls(void** tls, void* thread) { 614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes ((pthread_internal_t*) thread)->tls = tls; 624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Zero-initialize all the slots. 644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes for (size_t i = 0; i < BIONIC_TLS_SLOTS; ++i) { 654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes tls[i] = NULL; 664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 684b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Slot 0 must point to itself. The x86 Linux kernel reads the TLS from %fs:0. 694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes tls[TLS_SLOT_SELF] = tls; 704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes tls[TLS_SLOT_THREAD_ID] = thread; 714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // GCC looks in the TLS for the stack guard on x86, so copy it there from our global. 724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes tls[TLS_SLOT_STACK_GUARD] = (void*) __stack_chk_guard; 734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 744b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes __set_tls((void*) tls); 754b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes} 764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes// This trampoline is called from the assembly _pthread_clone() function. 784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesextern "C" void __thread_entry(int (*func)(void*), void *arg, void **tls) { 794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Wait for our creating thread to release us. This lets it have time to 804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // notify gdb about this thread before we start doing anything. 814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // This also provides the memory barrier needed to ensure that all memory 824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // accesses previously made by the creating thread are visible to us. 834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_mutex_t* start_mutex = (pthread_mutex_t*) &tls[TLS_SLOT_SELF]; 844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_mutex_lock(start_mutex); 854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_mutex_destroy(start_mutex); 864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_internal_t* thread = (pthread_internal_t*) tls[TLS_SLOT_THREAD_ID]; 884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes __init_tls(tls, thread); 894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 904b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if ((thread->internal_flags & kPthreadInitFailed) != 0) { 914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_exit(NULL); 924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int result = func(arg); 954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_exit((void*) result); 964b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes} 974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes__LIBC_ABI_PRIVATE__ 994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint _init_thread(pthread_internal_t* thread, pid_t kernel_id, bool add_to_thread_list) { 1004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int error = 0; 1014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->kernel_id = kernel_id; 1034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Set the scheduling policy/priority of the thread. 1054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (thread->attr.sched_policy != SCHED_NORMAL) { 1064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes struct sched_param param; 1074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes param.sched_priority = thread->attr.sched_priority; 1084b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (sched_setscheduler(kernel_id, thread->attr.sched_policy, ¶m) == -1) { 1094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // For backwards compatibility reasons, we just warn about failures here. 1104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // error = errno; 1114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes const char* msg = "pthread_create sched_setscheduler call failed: %s\n"; 1124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes __libc_format_log(ANDROID_LOG_WARN, "libc", msg, strerror(errno)); 1134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_cond_init(&thread->join_cond, NULL); 1174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->join_count = 0; 1184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->cleanup_stack = NULL; 1194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (add_to_thread_list) { 1214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes _pthread_internal_add(thread); 1224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return error; 1254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes} 1264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesstatic void* __create_thread_stack(size_t stack_size, size_t guard_size) { 1284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes ScopedPthreadMutexLocker lock(&gPthreadStackCreationLock); 1294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Create a new private anonymous map. 1314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int prot = PROT_READ | PROT_WRITE; 1324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 1334b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes void* stack = mmap(NULL, stack_size, prot, flags, -1, 0); 1344b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (stack == MAP_FAILED) { 1354b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return NULL; 1364b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1374b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1384b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Set the guard region at the end of the stack to PROT_NONE. 1394b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (mprotect(stack, guard_size, PROT_NONE) == -1) { 1404b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes munmap(stack, stack_size); 1414b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return NULL; 1424b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1434b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1444b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return stack; 1454b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes} 1464b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1474b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughesint pthread_create(pthread_t* thread_out, pthread_attr_t const* attr, 1484b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes void* (*start_routine)(void*), void* arg) { 1494b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes ErrnoRestorer errno_restorer; 1504b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1514b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Inform the rest of the C library that at least one thread 1524b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // was created. This will enforce certain functions to acquire/release 1534b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // locks (e.g. atexit()) to protect shared global structures. 1544b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // This works because pthread_create() is not called by the C library 1554b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // initialization routine that sets up the main thread's data structures. 1564b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes __isthreaded = 1; 1574b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1584b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_internal_t* thread = reinterpret_cast<pthread_internal_t*>(calloc(sizeof(*thread), 1)); 1594b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (thread == NULL) { 1604b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return EAGAIN; 1614b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1624b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->allocated_on_heap = true; 1634b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1644b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (attr == NULL) { 1654b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_attr_init(&thread->attr); 1664b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } else { 1674b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->attr = *attr; 1684b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes attr = NULL; // Prevent misuse below. 1694b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1704b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1714b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Make sure the stack size is PAGE_SIZE aligned. 1724b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes size_t stack_size = (thread->attr.stack_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); 1734b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1744b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (thread->attr.stack_base == NULL) { 1754b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // The caller didn't provide a stack, so allocate one. 1764b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->attr.stack_base = __create_thread_stack(stack_size, thread->attr.guard_size); 1774b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (thread->attr.stack_base == NULL) { 1784b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes free(thread); 1794b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return EAGAIN; 1804b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1814b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } else { 1824b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // The caller did provide a stack, so remember we're not supposed to free it. 1834b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->attr.flags |= PTHREAD_ATTR_FLAG_USER_STACK; 1844b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 1854b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1864b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Make room for TLS. 1874b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes void** tls = (void**)((uint8_t*)(thread->attr.stack_base) + stack_size - BIONIC_TLS_SLOTS * sizeof(void*)); 1884b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1894b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Create a mutex for the thread in TLS_SLOT_SELF to wait on once it starts so we can keep 1904b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // it from doing anything until after we notify the debugger about it 1914b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // 1924b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // This also provides the memory barrier we need to ensure that all 1934b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // memory accesses previously performed by this thread are visible to 1944b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // the new thread. 1954b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_mutex_t* start_mutex = (pthread_mutex_t*) &tls[TLS_SLOT_SELF]; 1964b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes pthread_mutex_init(start_mutex, NULL); 1974b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes ScopedPthreadMutexLocker start_locker(start_mutex); 1984b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 1994b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes tls[TLS_SLOT_THREAD_ID] = thread; 2004b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2014b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int flags = CLONE_FILES | CLONE_FS | CLONE_VM | CLONE_SIGHAND | CLONE_THREAD | CLONE_SYSVSEM | CLONE_DETACHED; 2024b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int tid = __pthread_clone((int(*)(void*))start_routine, tls, flags, arg); 2034b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2044b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (tid < 0) { 2054b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int clone_errno = errno; 2064b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if ((thread->attr.flags & PTHREAD_ATTR_FLAG_USER_STACK) == 0) { 2074b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes munmap(thread->attr.stack_base, stack_size); 2084b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 2094b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes free(thread); 2104b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return clone_errno; 2114b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 2124b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2134b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes int init_errno = _init_thread(thread, tid, true); 2144b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes if (init_errno != 0) { 2154b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Mark the thread detached and let its __thread_entry run to 2164b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // completion. (It'll just exit immediately, cleaning up its resources.) 2174b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->internal_flags |= kPthreadInitFailed; 2184b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes thread->attr.flags |= PTHREAD_ATTR_FLAG_DETACHED; 2194b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return init_errno; 2204b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 2214b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2224b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Notify any debuggers about the new thread. 2234b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes { 2244b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes ScopedPthreadMutexLocker debugger_locker(&gDebuggerNotificationLock); 2254b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes _thread_created_hook(tid); 2264b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes } 2274b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2284b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes // Publish the pthread_t and let the thread run. 2294b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes *thread_out = (pthread_t) thread; 2304b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes 2314b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes return 0; 2324b4a8824289c48c823cd38bc63289d121aae3d67Elliott Hughes} 233