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 return llvm::errorCodeToError(::GetLastError()); 36 #else 37 38 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames. 39 #if __has_feature(address_sanitizer) 40 const size_t eight_megabytes = 8 * 1024 * 1024; 41 if (min_stack_byte_size < eight_megabytes) { 42 min_stack_byte_size += eight_megabytes; 43 } 44 #endif 45 46 pthread_attr_t *thread_attr_ptr = nullptr; 47 pthread_attr_t thread_attr; 48 bool destroy_attr = false; 49 if (min_stack_byte_size > 0) { 50 if (::pthread_attr_init(&thread_attr) == 0) { 51 destroy_attr = true; 52 size_t default_min_stack_byte_size = 0; 53 if (::pthread_attr_getstacksize(&thread_attr, 54 &default_min_stack_byte_size) == 0) { 55 if (default_min_stack_byte_size < min_stack_byte_size) { 56 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) == 57 0) 58 thread_attr_ptr = &thread_attr; 59 } 60 } 61 } 62 } 63 int err = 64 ::pthread_create(&thread, thread_attr_ptr, 65 HostNativeThread::ThreadCreateTrampoline, info_ptr); 66 67 if (destroy_attr) 68 ::pthread_attr_destroy(&thread_attr); 69 70 if (err) 71 return llvm::errorCodeToError( 72 std::error_code(err, std::generic_category())); 73 #endif 74 75 return HostThread(thread); 76 } 77