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 HostThread ThreadLauncher::LaunchThread(llvm::StringRef name,
24                                         lldb::thread_func_t thread_function,
25                                         lldb::thread_arg_t thread_arg,
26                                         Status *error_ptr,
27                                         size_t min_stack_byte_size) {
28   Status error;
29   if (error_ptr)
30     error_ptr->Clear();
31 
32   // Host::ThreadCreateTrampoline will delete this pointer for us.
33   HostThreadCreateInfo *info_ptr =
34       new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
35   lldb::thread_t thread;
36 #ifdef _WIN32
37   thread = (lldb::thread_t)::_beginthreadex(
38       0, (unsigned)min_stack_byte_size,
39       HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
40   if (thread == (lldb::thread_t)(-1L))
41     error.SetError(::GetLastError(), eErrorTypeWin32);
42 #else
43 
44 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
45 #if __has_feature(address_sanitizer)
46   const size_t eight_megabytes = 8 * 1024 * 1024;
47   if (min_stack_byte_size < eight_megabytes) {
48     min_stack_byte_size += eight_megabytes;
49   }
50 #endif
51 
52   pthread_attr_t *thread_attr_ptr = nullptr;
53   pthread_attr_t thread_attr;
54   bool destroy_attr = false;
55   if (min_stack_byte_size > 0) {
56     if (::pthread_attr_init(&thread_attr) == 0) {
57       destroy_attr = true;
58       size_t default_min_stack_byte_size = 0;
59       if (::pthread_attr_getstacksize(&thread_attr,
60                                       &default_min_stack_byte_size) == 0) {
61         if (default_min_stack_byte_size < min_stack_byte_size) {
62           if (::pthread_attr_setstacksize(&thread_attr, min_stack_byte_size) ==
63               0)
64             thread_attr_ptr = &thread_attr;
65         }
66       }
67     }
68   }
69   int err =
70       ::pthread_create(&thread, thread_attr_ptr,
71                        HostNativeThread::ThreadCreateTrampoline, info_ptr);
72 
73   if (destroy_attr)
74     ::pthread_attr_destroy(&thread_attr);
75 
76   error.SetError(err, eErrorTypePOSIX);
77 #endif
78   if (error_ptr)
79     *error_ptr = error;
80   if (!error.Success())
81     thread = LLDB_INVALID_HOST_THREAD;
82 
83   return HostThread(thread);
84 }
85