1 //===-- ThreadLauncher.cpp --------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
10 #include "lldb/Host/ThreadLauncher.h"
11 #include "lldb/Host/HostNativeThread.h"
12 #include "lldb/Host/HostThread.h"
13 #include "lldb/Utility/Log.h"
16 #include "lldb/Host/windows/windows.h"
19 #include "llvm/Support/WindowsError.h"
22 using namespace lldb_private;
24 llvm::Expected<HostThread> ThreadLauncher::LaunchThread(
25 llvm::StringRef name, lldb::thread_func_t thread_function,
26 lldb::thread_arg_t thread_arg, size_t min_stack_byte_size) {
27 // Host::ThreadCreateTrampoline will delete this pointer for us.
28 HostThreadCreateInfo *info_ptr =
29 new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
30 lldb::thread_t thread;
32 thread = (lldb::thread_t)::_beginthreadex(
33 0, (unsigned)min_stack_byte_size,
34 HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
35 if (thread == LLDB_INVALID_HOST_THREAD)
36 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
39 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
40 #if __has_feature(address_sanitizer)
41 const size_t eight_megabytes = 8 * 1024 * 1024;
42 if (min_stack_byte_size < eight_megabytes) {
43 min_stack_byte_size += eight_megabytes;
47 pthread_attr_t *thread_attr_ptr = nullptr;
48 pthread_attr_t thread_attr;
49 bool destroy_attr = false;
50 if (min_stack_byte_size > 0) {
51 if (::pthread_attr_init(&thread_attr) == 0) {
53 size_t default_min_stack_byte_size = 0;
54 if (::pthread_attr_getstacksize(&thread_attr,
55 &default_min_stack_byte_size) == 0) {
56 if (default_min_stack_byte_size < min_stack_byte_size) {
57 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
59 thread_attr_ptr = &thread_attr;
65 ::pthread_create(&thread, thread_attr_ptr,
66 HostNativeThread::ThreadCreateTrampoline, info_ptr);
69 ::pthread_attr_destroy(&thread_attr);
72 return llvm::errorCodeToError(
73 std::error_code(err, std::generic_category()));
76 return HostThread(thread);