Create a HostThread abstraction.

This patch moves creates a thread abstraction that represents a
thread running inside the LLDB process.  This is a replacement for
otherwise using lldb::thread_t, and provides a platform agnostic
interface to managing these threads.

Differential Revision: http://reviews.llvm.org/D5198

Reviewed by: Jim Ingham

llvm-svn: 217460
diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index f35d954..28708be 100644
--- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -42,7 +42,9 @@
 #include "lldb/Core/StreamString.h"
 #include "lldb/Core/Timer.h"
 #include "lldb/Core/Value.h"
+#include "lldb/Host/HostThread.h"
 #include "lldb/Host/Symbols.h"
+#include "lldb/Host/ThreadLauncher.h"
 #include "lldb/Host/TimeValue.h"
 #include "lldb/Interpreter/CommandInterpreter.h"
 #include "lldb/Interpreter/CommandObject.h"
@@ -272,8 +274,6 @@
     m_last_stop_packet_mutex (Mutex::eMutexTypeNormal),
     m_register_info (),
     m_async_broadcaster (NULL, "lldb.process.gdb-remote.async-broadcaster"),
-    m_async_thread (LLDB_INVALID_HOST_THREAD),
-    m_async_thread_state(eAsyncThreadNotStarted),
     m_async_thread_state_mutex(Mutex::eMutexTypeRecursive),
     m_thread_ids (),
     m_continue_c_tids (),
@@ -1432,7 +1432,7 @@
             TimeValue timeout;
             timeout = TimeValue::Now();
             timeout.OffsetWithSeconds (5);
-            if (!IS_VALID_LLDB_HOST_THREAD(m_async_thread))
+            if (m_async_thread.GetState() != eThreadStateRunning)
             {
                 error.SetErrorString ("Trying to resume but the async thread is dead.");
                 if (log)
@@ -2891,30 +2891,22 @@
         log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
     
     Mutex::Locker start_locker(m_async_thread_state_mutex);
-    if (m_async_thread_state == eAsyncThreadNotStarted)
+    if (m_async_thread.GetState() != eThreadStateRunning)
     {
         // Create a thread that watches our internal state and controls which
         // events make it to clients (into the DCProcess event queue).
-        m_async_thread = Host::ThreadCreate ("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
-        if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
-        {
-            m_async_thread_state = eAsyncThreadRunning;
-            return true;
-        }
-        else
-            return false;
+
+        m_async_thread = ThreadLauncher::LaunchThread("<lldb.process.gdb-remote.async>", ProcessGDBRemote::AsyncThread, this, NULL);
     }
     else
     {
         // Somebody tried to start the async thread while it was either being started or stopped.  If the former, and
         // it started up successfully, then say all's well.  Otherwise it is an error, since we aren't going to restart it.
         if (log)
-            log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
-        if (m_async_thread_state == eAsyncThreadRunning)
-            return true;
-        else
-            return false;
+            log->Printf("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread.GetState());
     }
+
+    return (m_async_thread.GetState() == eThreadStateRunning);
 }
 
 void
@@ -2926,7 +2918,7 @@
         log->Printf ("ProcessGDBRemote::%s ()", __FUNCTION__);
 
     Mutex::Locker start_locker(m_async_thread_state_mutex);
-    if (m_async_thread_state == eAsyncThreadRunning)
+    if (m_async_thread.GetState() == eThreadStateRunning)
     {
         m_async_broadcaster.BroadcastEvent (eBroadcastBitAsyncThreadShouldExit);
         
@@ -2934,16 +2926,12 @@
         m_gdb_comm.Disconnect();    // Disconnect from the debug server.
 
         // Stop the stdio thread
-        if (IS_VALID_LLDB_HOST_THREAD(m_async_thread))
-        {
-            Host::ThreadJoin (m_async_thread, NULL, NULL);
-        }
-        m_async_thread_state = eAsyncThreadDone;
+        m_async_thread.Join(nullptr);
     }
     else
     {
         if (log)
-            log->Printf ("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread_state);
+            log->Printf("ProcessGDBRemote::%s () - Called when Async thread was in state: %d.", __FUNCTION__, m_async_thread.GetState());
     }
 }
 
@@ -3086,7 +3074,7 @@
     if (log)
         log->Printf ("ProcessGDBRemote::%s (arg = %p, pid = %" PRIu64 ") thread exiting...", __FUNCTION__, arg, process->GetID());
 
-    process->m_async_thread = LLDB_INVALID_HOST_THREAD;
+    process->m_async_thread.Reset();
     return NULL;
 }