1 //===-- ThreadLauncher.cpp ---------------------------------------*- C++
4 // The LLVM Compiler Infrastructure
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
12 #include "lldb/Host/ThreadLauncher.h"
13 #include "lldb/Core/Log.h"
14 #include "lldb/Host/HostNativeThread.h"
15 #include "lldb/Host/HostThread.h"
16 #include "lldb/Host/ThisThread.h"
19 #include "lldb/Host/windows/windows.h"
23 using namespace lldb_private;
25 HostThread ThreadLauncher::LaunchThread(llvm::StringRef name,
26 lldb::thread_func_t thread_function,
27 lldb::thread_arg_t thread_arg,
29 size_t min_stack_byte_size) {
34 // Host::ThreadCreateTrampoline will delete this pointer for us.
35 HostThreadCreateInfo *info_ptr =
36 new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
37 lldb::thread_t thread;
39 thread = (lldb::thread_t)::_beginthreadex(
40 0, (unsigned)min_stack_byte_size,
41 HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
42 if (thread == (lldb::thread_t)(-1L))
43 error.SetError(::GetLastError(), eErrorTypeWin32);
46 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
47 #if __has_feature(address_sanitizer)
48 const size_t eight_megabytes = 8 * 1024 * 1024;
49 if (min_stack_byte_size < eight_megabytes) {
50 min_stack_byte_size += eight_megabytes;
54 pthread_attr_t *thread_attr_ptr = NULL;
55 pthread_attr_t thread_attr;
56 bool destroy_attr = false;
57 if (min_stack_byte_size > 0) {
58 if (::pthread_attr_init(&thread_attr) == 0) {
60 size_t default_min_stack_byte_size = 0;
61 if (::pthread_attr_getstacksize(&thread_attr,
62 &default_min_stack_byte_size) == 0) {
63 if (default_min_stack_byte_size < min_stack_byte_size) {
64 if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
66 thread_attr_ptr = &thread_attr;
72 ::pthread_create(&thread, thread_attr_ptr,
73 HostNativeThread::ThreadCreateTrampoline, info_ptr);
76 ::pthread_attr_destroy(&thread_attr);
78 error.SetError(err, eErrorTypePOSIX);
83 thread = LLDB_INVALID_HOST_THREAD;
85 return HostThread(thread);