1 //===-- HostThreadWindows.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 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/Status.h"
11 #include "lldb/Host/windows/HostThreadWindows.h"
12 #include "lldb/Host/windows/windows.h"
14 #include "llvm/ADT/STLExtras.h"
17 using namespace lldb_private
;
19 static void __stdcall
ExitThreadProxy(ULONG_PTR dwExitCode
) {
20 ::ExitThread(dwExitCode
);
23 HostThreadWindows::HostThreadWindows()
24 : HostNativeThreadBase(), m_owns_handle(true) {}
26 HostThreadWindows::HostThreadWindows(lldb::thread_t thread
)
27 : HostNativeThreadBase(thread
), m_owns_handle(true) {}
29 HostThreadWindows::~HostThreadWindows() { Reset(); }
31 void HostThreadWindows::SetOwnsHandle(bool owns
) { m_owns_handle
= owns
; }
33 Status
HostThreadWindows::Join(lldb::thread_result_t
*result
) {
36 DWORD wait_result
= ::WaitForSingleObject(m_thread
, INFINITE
);
37 if (WAIT_OBJECT_0
== wait_result
&& result
) {
39 if (!::GetExitCodeThread(m_thread
, &exit_code
))
42 } else if (WAIT_OBJECT_0
!= wait_result
)
43 error
.SetError(::GetLastError(), eErrorTypeWin32
);
45 error
.SetError(ERROR_INVALID_HANDLE
, eErrorTypeWin32
);
51 Status
HostThreadWindows::Cancel() {
54 DWORD result
= ::QueueUserAPC(::ExitThreadProxy
, m_thread
, 0);
55 error
.SetError(result
, eErrorTypeWin32
);
59 lldb::tid_t
HostThreadWindows::GetThreadId() const {
60 return ::GetThreadId(m_thread
);
63 void HostThreadWindows::Reset() {
64 if (m_owns_handle
&& m_thread
!= LLDB_INVALID_HOST_THREAD
)
65 ::CloseHandle(m_thread
);
67 HostNativeThreadBase::Reset();
70 bool HostThreadWindows::EqualsThread(lldb::thread_t thread
) const {
71 return GetThreadId() == ::GetThreadId(thread
);