Path: blob/main/contrib/llvm-project/lldb/source/Host/common/ThreadLauncher.cpp
39606 views
//===-- ThreadLauncher.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78// lldb Includes9#include "lldb/Host/ThreadLauncher.h"10#include "lldb/Host/HostNativeThread.h"11#include "lldb/Host/HostThread.h"12#include "lldb/Utility/Log.h"1314#if defined(_WIN32)15#include "lldb/Host/windows/windows.h"16#endif1718#include "llvm/Support/WindowsError.h"1920using namespace lldb;21using namespace lldb_private;2223llvm::Expected<HostThread>24ThreadLauncher::LaunchThread(llvm::StringRef name,25std::function<thread_result_t()> impl,26size_t min_stack_byte_size) {27// Host::ThreadCreateTrampoline will take ownership if thread creation is28// successful.29auto info_up = std::make_unique<HostThreadCreateInfo>(name.str(), impl);30lldb::thread_t thread;31#ifdef _WIN3232thread = (lldb::thread_t)::_beginthreadex(330, (unsigned)min_stack_byte_size,34HostNativeThread::ThreadCreateTrampoline, info_up.get(), 0, NULL);35if (thread == LLDB_INVALID_HOST_THREAD)36return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));37#else3839// ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.40#if __has_feature(address_sanitizer)41const size_t eight_megabytes = 8 * 1024 * 1024;42if (min_stack_byte_size < eight_megabytes) {43min_stack_byte_size += eight_megabytes;44}45#endif4647pthread_attr_t *thread_attr_ptr = nullptr;48pthread_attr_t thread_attr;49bool destroy_attr = false;50if (min_stack_byte_size > 0) {51if (::pthread_attr_init(&thread_attr) == 0) {52destroy_attr = true;53size_t default_min_stack_byte_size = 0;54if (::pthread_attr_getstacksize(&thread_attr,55&default_min_stack_byte_size) == 0) {56if (default_min_stack_byte_size < min_stack_byte_size) {57if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==580)59thread_attr_ptr = &thread_attr;60}61}62}63}64int err =65::pthread_create(&thread, thread_attr_ptr,66HostNativeThread::ThreadCreateTrampoline, info_up.get());6768if (destroy_attr)69::pthread_attr_destroy(&thread_attr);7071if (err)72return llvm::errorCodeToError(73std::error_code(err, std::generic_category()));74#endif7576info_up.release();77return HostThread(thread);78}798081