1 //===-- HostThreadWindows.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 #include "lldb/Core/Error.h"
11 
12 #include "lldb/Host/windows/windows.h"
13 #include "lldb/Host/windows/HostThreadWindows.h"
14 
15 #include "llvm/ADT/STLExtras.h"
16 
17 using namespace lldb;
18 using namespace lldb_private;
19 
20 HostThreadWindows::HostThreadWindows()
21     : HostNativeThreadBase()
22 {
23 }
24 
25 HostThreadWindows::HostThreadWindows(lldb::thread_t thread)
26     : HostNativeThreadBase(thread)
27 {
28 }
29 
30 HostThreadWindows::~HostThreadWindows()
31 {
32     Reset();
33 }
34 
35 Error
36 HostThreadWindows::Join(lldb::thread_result_t *result)
37 {
38     Error error;
39     if (IsJoinable())
40     {
41         DWORD wait_result = ::WaitForSingleObject(m_thread, INFINITE);
42         if (WAIT_OBJECT_0 == wait_result && result)
43         {
44             DWORD exit_code = 0;
45             if (!::GetExitCodeThread(m_thread, &exit_code))
46                 *result = 0;
47             *result = exit_code;
48         }
49         else if (WAIT_OBJECT_0 != wait_result)
50             error.SetError(::GetLastError(), eErrorTypeWin32);
51     }
52     else
53         error.SetError(ERROR_INVALID_HANDLE, eErrorTypeWin32);
54     return error;
55 }
56 
57 Error
58 HostThreadWindows::Cancel()
59 {
60     Error error;
61 
62     DWORD result = ::QueueUserAPC(::ExitThread, m_thread, 0);
63     error.SetError(result, eErrorTypeWin32);
64     return error;
65 }
66 
67 lldb::tid_t
68 HostThreadWindows::GetThreadId() const
69 {
70     return ::GetThreadId(m_thread);
71 }
72 
73 void
74 HostThreadWindows::Reset()
75 {
76     if (m_thread != LLDB_INVALID_HOST_THREAD)
77         ::CloseHandle(m_thread);
78 
79     HostNativeThreadBase::Reset();
80 }
81