1 //===-- ThreadLauncher.cpp ------------------------------------------------===//
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
>
25 ThreadLauncher::LaunchThread(llvm::StringRef name
,
26 std::function
<thread_result_t()> impl
,
27 size_t min_stack_byte_size
) {
28 // Host::ThreadCreateTrampoline will take ownership if thread creation is
30 auto info_up
= std::make_unique
<HostThreadCreateInfo
>(name
.str(), impl
);
31 lldb::thread_t thread
;
33 thread
= (lldb::thread_t
)::_beginthreadex(
34 0, (unsigned)min_stack_byte_size
,
35 HostNativeThread::ThreadCreateTrampoline
, info_up
.get(), 0, NULL
);
36 if (thread
== LLDB_INVALID_HOST_THREAD
)
37 return llvm::errorCodeToError(llvm::mapWindowsError(GetLastError()));
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
;
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) {
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
) ==
60 thread_attr_ptr
= &thread_attr
;
66 ::pthread_create(&thread
, thread_attr_ptr
,
67 HostNativeThread::ThreadCreateTrampoline
, info_up
.get());
70 ::pthread_attr_destroy(&thread_attr
);
73 return llvm::errorCodeToError(
74 std::error_code(err
, std::generic_category()));
78 return HostThread(thread
);