blob: c19a23361d815ec47aff4f16bcba9c37733ebc11 [file] [log] [blame]
Zachary Turner39de3112014-09-09 20:54:56 +00001//===-- ThreadLauncher.cpp ---------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// lldb Includes
11#include "lldb/Core/Log.h"
12#include "lldb/Host/HostNativeThread.h"
13#include "lldb/Host/HostThread.h"
14#include "lldb/Host/ThisThread.h"
15#include "lldb/Host/ThreadLauncher.h"
16
17#if defined(_WIN32)
18#include "lldb/Host/windows/windows.h"
19#endif
20
21using namespace lldb;
22using namespace lldb_private;
23
24HostThread
Greg Clayton807b6b32014-10-15 18:03:59 +000025ThreadLauncher::LaunchThread(llvm::StringRef name, lldb::thread_func_t thread_function, lldb::thread_arg_t thread_arg, Error *error_ptr, size_t min_stack_byte_size)
Zachary Turner39de3112014-09-09 20:54:56 +000026{
27 Error error;
28 if (error_ptr)
29 error_ptr->Clear();
30
31 // Host::ThreadCreateTrampoline will delete this pointer for us.
32 HostThreadCreateInfo *info_ptr = new HostThreadCreateInfo(name.data(), thread_function, thread_arg);
33 lldb::thread_t thread;
34#ifdef _WIN32
Zachary Turner7c2896a2014-10-24 22:06:29 +000035 thread =
36 (lldb::thread_t)::_beginthreadex(0, (unsigned)min_stack_byte_size, HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
Zachary Turner39de3112014-09-09 20:54:56 +000037 if (thread == (lldb::thread_t)(-1L))
38 error.SetError(::GetLastError(), eErrorTypeWin32);
39#else
Greg Clayton807b6b32014-10-15 18:03:59 +000040
Jason Molenda3db7ebc2015-01-29 06:28:36 +000041
42 // ASAN instrumentation adds a lot of bookkeeping overhead on stack frames.
43#if __has_feature(address_sanitizer)
44 const size_t eight_megabytes = 8 * 1024 * 1024;
45 if (min_stack_byte_size < eight_megabytes)
46 {
47 min_stack_byte_size += eight_megabytes;
48 }
49#endif
50
Greg Clayton807b6b32014-10-15 18:03:59 +000051 pthread_attr_t *thread_attr_ptr = NULL;
52 pthread_attr_t thread_attr;
53 bool destroy_attr = false;
54 if (min_stack_byte_size > 0)
55 {
56 if (::pthread_attr_init (&thread_attr) == 0)
57 {
58 destroy_attr = true;
59 size_t default_min_stack_byte_size = 0;
60 if (::pthread_attr_getstacksize(&thread_attr, &default_min_stack_byte_size) == 0)
61 {
62 if (default_min_stack_byte_size < min_stack_byte_size)
63 {
64 if (::pthread_attr_setstacksize (&thread_attr, min_stack_byte_size) == 0)
65 thread_attr_ptr = &thread_attr;
66 }
67 }
68
69 }
70 }
71 int err = ::pthread_create(&thread, thread_attr_ptr, HostNativeThread::ThreadCreateTrampoline, info_ptr);
72
73 if (destroy_attr)
74 ::pthread_attr_destroy(&thread_attr);
75
Zachary Turner39de3112014-09-09 20:54:56 +000076 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}