1 //===-- ThreadLauncher.cpp ---------------------------------------*- C++ 2 //-*-===// 3 // 4 // The LLVM Compiler Infrastructure 5 // 6 // This file is distributed under the University of Illinois Open Source 7 // License. See LICENSE.TXT for details. 8 // 9 //===----------------------------------------------------------------------===// 10 11 // lldb Includes 12 #include "lldb/Host/ThreadLauncher.h" 13 #include "lldb/Core/Log.h" 14 #include "lldb/Host/HostNativeThread.h" 15 #include "lldb/Host/HostThread.h" 16 #include "lldb/Host/ThisThread.h" 17 18 #if defined(_WIN32) 19 #include "lldb/Host/windows/windows.h" 20 #endif 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 HostThread ThreadLauncher::LaunchThread(llvm::StringRef name, 26 lldb::thread_func_t thread_function, 27 lldb::thread_arg_t thread_arg, 28 Error *error_ptr, 29 size_t min_stack_byte_size) { 30 Error error; 31 if (error_ptr) 32 error_ptr->Clear(); 33 34 // Host::ThreadCreateTrampoline will delete this pointer for us. 35 HostThreadCreateInfo *info_ptr = 36 new HostThreadCreateInfo(name.data(), thread_function, thread_arg); 37 lldb::thread_t thread; 38 #ifdef _WIN32 39 thread = (lldb::thread_t)::_beginthreadex( 40 0, (unsigned)min_stack_byte_size, 41 HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL); 42 if (thread == (lldb::thread_t)(-1L)) 43 error.SetError(::GetLastError(), eErrorTypeWin32); 44 #else 45 46 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames. 47 #if __has_feature(address_sanitizer) 48 const size_t eight_megabytes = 8 * 1024 * 1024; 49 if (min_stack_byte_size < eight_megabytes) { 50 min_stack_byte_size += eight_megabytes; 51 } 52 #endif 53 54 pthread_attr_t *thread_attr_ptr = NULL; 55 pthread_attr_t thread_attr; 56 bool destroy_attr = false; 57 if (min_stack_byte_size > 0) { 58 if (::pthread_attr_init(&thread_attr) == 0) { 59 destroy_attr = true; 60 size_t default_min_stack_byte_size = 0; 61 if (::pthread_attr_getstacksize(&thread_attr, 62 &default_min_stack_byte_size) == 0) { 63 if (default_min_stack_byte_size < min_stack_byte_size) { 64 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) == 65 0) 66 thread_attr_ptr = &thread_attr; 67 } 68 } 69 } 70 } 71 int err = 72 ::pthread_create(&thread, thread_attr_ptr, 73 HostNativeThread::ThreadCreateTrampoline, info_ptr); 74 75 if (destroy_attr) 76 ::pthread_attr_destroy(&thread_attr); 77 78 error.SetError(err, eErrorTypePOSIX); 79 #endif 80 if (error_ptr) 81 *error_ptr = error; 82 if (!error.Success()) 83 thread = LLDB_INVALID_HOST_THREAD; 84 85 return HostThread(thread); 86 } 87