1 //===-- ThreadLauncher.cpp ---------------------------------------*- C++ 2 //-*-===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 // lldb Includes 11 #include "lldb/Host/ThreadLauncher.h" 12 #include "lldb/Host/HostNativeThread.h" 13 #include "lldb/Host/HostThread.h" 14 #include "lldb/Utility/Log.h" 15 16 #if defined(_WIN32) 17 #include "lldb/Host/windows/windows.h" 18 #endif 19 20 using namespace lldb; 21 using namespace lldb_private; 22 23 llvm::Expected<HostThread> ThreadLauncher::LaunchThread( 24 llvm::StringRef name, lldb::thread_func_t thread_function, 25 lldb::thread_arg_t thread_arg, size_t min_stack_byte_size) { 26 // Host::ThreadCreateTrampoline will delete this pointer for us. 27 HostThreadCreateInfo *info_ptr = 28 new HostThreadCreateInfo(name.data(), thread_function, thread_arg); 29 lldb::thread_t thread; 30 #ifdef _WIN32 31 thread = (lldb::thread_t)::_beginthreadex( 32 0, (unsigned)min_stack_byte_size, 33 HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL); 34 if (thread == (lldb::thread_t)(-1L)) { 35 DWORD err = GetLastError(); 36 return llvm::errorCodeToError(std::error_code(err, std::system_category())); 37 } 38 #else 39 40 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames. 41 #if __has_feature(address_sanitizer) 42 const size_t eight_megabytes = 8 * 1024 * 1024; 43 if (min_stack_byte_size < eight_megabytes) { 44 min_stack_byte_size += eight_megabytes; 45 } 46 #endif 47 48 pthread_attr_t *thread_attr_ptr = nullptr; 49 pthread_attr_t thread_attr; 50 bool destroy_attr = false; 51 if (min_stack_byte_size > 0) { 52 if (::pthread_attr_init(&thread_attr) == 0) { 53 destroy_attr = true; 54 size_t default_min_stack_byte_size = 0; 55 if (::pthread_attr_getstacksize(&thread_attr, 56 &default_min_stack_byte_size) == 0) { 57 if (default_min_stack_byte_size < min_stack_byte_size) { 58 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) == 59 0) 60 thread_attr_ptr = &thread_attr; 61 } 62 } 63 } 64 } 65 int err = 66 ::pthread_create(&thread, thread_attr_ptr, 67 HostNativeThread::ThreadCreateTrampoline, info_ptr); 68 69 if (destroy_attr) 70 ::pthread_attr_destroy(&thread_attr); 71 72 if (err) 73 return llvm::errorCodeToError( 74 std::error_code(err, std::generic_category())); 75 #endif 76 77 return HostThread(thread); 78 } 79