73 lines · cpp
1//===-- HostThreadWindows.cpp ---------------------------------------------===//2//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "lldb/Utility/Status.h"10 11#include "lldb/Host/windows/HostThreadWindows.h"12#include "lldb/Host/windows/windows.h"13 14#include "llvm/ADT/STLExtras.h"15 16using namespace lldb;17using namespace lldb_private;18 19static void __stdcall ExitThreadProxy(ULONG_PTR dwExitCode) {20 ::ExitThread(dwExitCode);21}22 23HostThreadWindows::HostThreadWindows()24 : HostNativeThreadBase(), m_owns_handle(true) {}25 26HostThreadWindows::HostThreadWindows(lldb::thread_t thread)27 : HostNativeThreadBase(thread), m_owns_handle(true) {}28 29HostThreadWindows::~HostThreadWindows() { Reset(); }30 31void HostThreadWindows::SetOwnsHandle(bool owns) { m_owns_handle = owns; }32 33Status HostThreadWindows::Join(lldb::thread_result_t *result) {34 Status error;35 if (IsJoinable()) {36 DWORD wait_result = ::WaitForSingleObject(m_thread, INFINITE);37 if (WAIT_OBJECT_0 == wait_result && result) {38 DWORD exit_code = 0;39 if (!::GetExitCodeThread(m_thread, &exit_code))40 *result = 0;41 *result = exit_code;42 } else if (WAIT_OBJECT_0 != wait_result)43 error = Status(::GetLastError(), eErrorTypeWin32);44 } else45 error = Status(ERROR_INVALID_HANDLE, eErrorTypeWin32);46 47 Reset();48 return error;49}50 51Status HostThreadWindows::Cancel() {52 Status error;53 54 DWORD result = ::QueueUserAPC(::ExitThreadProxy, m_thread, 0);55 error = Status(result, eErrorTypeWin32);56 return error;57}58 59lldb::tid_t HostThreadWindows::GetThreadId() const {60 return ::GetThreadId(m_thread);61}62 63void HostThreadWindows::Reset() {64 if (m_owns_handle && m_thread != LLDB_INVALID_HOST_THREAD)65 ::CloseHandle(m_thread);66 67 HostNativeThreadBase::Reset();68}69 70bool HostThreadWindows::EqualsThread(lldb::thread_t thread) const {71 return GetThreadId() == ::GetThreadId(thread);72}73