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/Host/freebsd/Host.cpp b/lldb/source/Host/freebsd/Host.cpp
index dc092e8..5f065e2 100644
--- a/lldb/source/Host/freebsd/Host.cpp
+++ b/lldb/source/Host/freebsd/Host.cpp
@@ -50,80 +50,6 @@
 using namespace lldb;
 using namespace lldb_private;
 
-class FreeBSDThread
-{
-public:
-    FreeBSDThread(const char *thread_name)
-    {
-        Host::SetThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name);
-    }
-    static void PThreadDestructor (void *v)
-    {
-        delete (FreeBSDThread*)v;
-    }
-};
-
-static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_thread_create_key = 0;
-
-static void
-InitThreadCreated()
-{
-    ::pthread_key_create (&g_thread_create_key, FreeBSDThread::PThreadDestructor);
-}
-
-void
-Host::ThreadCreated (const char *thread_name)
-{
-    ::pthread_once (&g_thread_create_once, InitThreadCreated);
-    if (g_thread_create_key)
-    {
-        ::pthread_setspecific (g_thread_create_key, new FreeBSDThread(thread_name));
-    }
-
-    Host::SetShortThreadName (LLDB_INVALID_PROCESS_ID, LLDB_INVALID_THREAD_ID, thread_name, 16);
-}
-
-std::string
-Host::GetThreadName (lldb::pid_t pid, lldb::tid_t tid)
-{
-    struct kinfo_proc *kp = nullptr, *nkp;
-    size_t len = 0;
-    int error;
-    int name[4] = {
-        CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid
-    };
-
-    while (1) {
-        error = sysctl(name, 4, kp, &len, nullptr, 0);
-        if (kp == nullptr || (error != 0 && errno == ENOMEM)) {
-            // Add extra space in case threads are added before next call.
-            len += sizeof(*kp) + len / 10;
-            nkp = (struct kinfo_proc *)realloc(kp, len);
-            if (nkp == nullptr)
-            {
-                free(kp);
-                return std::string();
-            }
-            kp = nkp;
-            continue;
-        }
-        if (error != 0)
-            len = 0;
-        break;
-    }
-
-    std::string thread_name;
-    for (size_t i = 0; i < len / sizeof(*kp); i++) {
-        if (kp[i].ki_tid == (int)tid) {
-            thread_name = kp[i].ki_tdname;
-            break;
-        }
-    }
-    free(kp);
-    return thread_name;
-}
-
 void
 Host::Backtrace (Stream &strm, uint32_t max_frames)
 {
diff --git a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
index 7845825..d511093 100644
--- a/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
+++ b/lldb/source/Host/freebsd/HostInfoFreeBSD.cpp
@@ -17,6 +17,12 @@
 
 using namespace lldb_private;
 
+uint32_t
+HostInfoFreeBSD::GetMaxThreadNameLength()
+{
+    return 16;
+}
+
 bool
 HostInfoFreeBSD::GetOSVersion(uint32_t &major, uint32_t &minor, uint32_t &update)
 {
diff --git a/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp b/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp
new file mode 100644
index 0000000..ce2d44e
--- /dev/null
+++ b/lldb/source/Host/freebsd/HostThreadFreeBSD.cpp
@@ -0,0 +1,80 @@
+//===-- HostThreadFreeBSD.cpp -----------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// lldb Includes
+#include "lldb/Host/freebsd/HostThreadFreeBSD.h"
+
+// C includes
+#include <pthread.h>
+#include <pthread_np.h>
+#include <stdlib.h>
+#include <sys/sysctl.h>
+
+// C++ includes
+#include <string>
+
+using namespace lldb_private;
+
+HostThreadFreeBSD::HostThreadFreeBSD()
+{
+}
+
+HostThreadFreeBSD::HostThreadFreeBSD(lldb::thread_t thread)
+    : HostThreadPosix(thread)
+{
+}
+
+void
+HostThreadFreeBSD::SetName(lldb::thread_t thread, llvm::StringRef name)
+{
+    ::pthread_set_name_np(thread, name);
+}
+
+void
+HostThreadFreeBSD::GetName(lldb::thread_t thread, llvm::SmallVectorImpl<char> &name)
+{
+    name.clear();
+    int pid = Host::GetCurrentProcessID();
+
+    struct kinfo_proc *kp = nullptr, *nkp;
+    size_t len = 0;
+    int error;
+    int ctl[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID | KERN_PROC_INC_THREAD, (int)pid};
+
+    while (1)
+    {
+        error = sysctl(ctl, 4, kp, &len, nullptr, 0);
+        if (kp == nullptr || (error != 0 && errno == ENOMEM))
+        {
+            // Add extra space in case threads are added before next call.
+            len += sizeof(*kp) + len / 10;
+            nkp = (struct kinfo_proc *)realloc(kp, len);
+            if (nkp == nullptr)
+            {
+                free(kp);
+                return;
+            }
+            kp = nkp;
+            continue;
+        }
+        if (error != 0)
+            len = 0;
+        break;
+    }
+
+    for (size_t i = 0; i < len / sizeof(*kp); i++)
+    {
+        if (kp[i].ki_tid == (int)thread)
+        {
+            name.append(kp[i].ki_tdname, strlen(kp[i].ki_tdname));
+            break;
+        }
+    }
+    free(kp);
+}
diff --git a/lldb/source/Host/freebsd/ThisThread.cpp b/lldb/source/Host/freebsd/ThisThread.cpp
new file mode 100644
index 0000000..6a50618
--- /dev/null
+++ b/lldb/source/Host/freebsd/ThisThread.cpp
@@ -0,0 +1,29 @@
+//===-- ThisThread.cpp ------------------------------------------*- C++ -*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/Host/linux/HostThreadLinux.h"
+#include "lldb/Host/ThisThread.h"
+
+#include "llvm/ADT/SmallVector.h"
+
+#include <pthread.h>
+
+using namespace lldb_private;
+
+void
+ThisThread::SetName(llvm::StringRef name)
+{
+    HostThread::SetName(::pthread_self(), name);
+}
+
+void
+ThisThread::GetName(llvm::SmallVectorImpl<char> &name)
+{
+    HostThread::GetName(::pthread_self(), name);
+}