1 //===-- 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 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 HostThread
25 ThreadLauncher::LaunchThread(llvm::StringRef name, lldb::thread_func_t thread_function, lldb::thread_arg_t thread_arg, Error *error_ptr)
26 {
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
35     thread = (lldb::thread_t)::_beginthreadex(0, 0, HostNativeThread::ThreadCreateTrampoline, info_ptr, 0, NULL);
36     if (thread == (lldb::thread_t)(-1L))
37         error.SetError(::GetLastError(), eErrorTypeWin32);
38 #else
39     int err = ::pthread_create(&thread, NULL, HostNativeThread::ThreadCreateTrampoline, info_ptr);
40     error.SetError(err, eErrorTypePOSIX);
41 #endif
42     if (error_ptr)
43         *error_ptr = error;
44     if (!error.Success())
45         thread = LLDB_INVALID_HOST_THREAD;
46 
47     return HostThread(thread);
48 }
49