labath created this revision.
labath added reviewers: JDevlieghere, jingham.
labath requested review of this revision.
Herald added a project: LLDB.

The class is using an incredibly elaborate setup to create and destroy
an NSAutoreleasePool object. We can do it in a much simpler way by
making those calls inside our thread startup function.

The only effect of this patch is that the pool gets released at the end
of the ThreadCreateTrampoline function, instead of slightly later, when
pthreads begin thread-specific cleanup. However, the key destruction
order is unspecified, so nothing should be relying on that.

I didn't find a specific reason for why this would have to be done that
way in git history. It seems that before D5198 
<https://reviews.llvm.org/D5198>, this was thread-specific
keys were the only way an os implementation (in Host::ThreadCreated)
could attach some value to a thread.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120322

Files:
  lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
  lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm


Index: lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
+++ lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
@@ -7,62 +7,15 @@
 
//===----------------------------------------------------------------------===//
 
 #include "lldb/Host/macosx/HostThreadMacOSX.h"
-#include "lldb/Host/Host.h"
-
 #include <CoreFoundation/CoreFoundation.h>
 #include <Foundation/Foundation.h>
 
-#include <pthread.h>
-
 using namespace lldb_private;
 
-
-static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_thread_create_key = 0;
-
-namespace {
-class MacOSXDarwinThread {
-public:
-  MacOSXDarwinThread() { m_pool = [[NSAutoreleasePool alloc] init]; }
-
-  ~MacOSXDarwinThread() {
-    if (m_pool) {
-      [m_pool drain];
-      m_pool = nil;
-    }
-  }
-
-  static void PThreadDestructor(void *v) {
-    if (v)
-      delete static_cast<MacOSXDarwinThread *>(v);
-    ::pthread_setspecific(g_thread_create_key, NULL);
-  }
-
-protected:
-  NSAutoreleasePool *m_pool = nil;
-
-private:
-  MacOSXDarwinThread(const MacOSXDarwinThread &) = delete;
-  const MacOSXDarwinThread &operator=(const MacOSXDarwinThread &) = delete;
-};
-} // namespace
-
-static void InitThreadCreated() {
-  ::pthread_key_create(&g_thread_create_key,
-                       MacOSXDarwinThread::PThreadDestructor);
-}
-
-HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {}
-
-HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
-    : HostThreadPosix(thread) {}
-
 lldb::thread_result_t
 HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) {
-  ::pthread_once(&g_thread_create_once, InitThreadCreated);
-  if (g_thread_create_key) {
-    ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread());
-  }
-
-  return HostThreadPosix::ThreadCreateTrampoline(arg);
+  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+  lldb::thread_result_t result = HostThreadPosix::ThreadCreateTrampoline(arg);
+  [pool drain];
+  return result;
 }
Index: lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
===================================================================
--- lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
+++ lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
@@ -17,8 +17,7 @@
   friend class ThreadLauncher;
 
 public:
-  HostThreadMacOSX();
-  HostThreadMacOSX(lldb::thread_t thread);
+  using HostThreadPosix::HostThreadPosix;
 
 protected:
   static lldb::thread_result_t ThreadCreateTrampoline(lldb::thread_arg_t arg);


Index: lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
===================================================================
--- lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
+++ lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm
@@ -7,62 +7,15 @@
 //===----------------------------------------------------------------------===//
 
 #include "lldb/Host/macosx/HostThreadMacOSX.h"
-#include "lldb/Host/Host.h"
-
 #include <CoreFoundation/CoreFoundation.h>
 #include <Foundation/Foundation.h>
 
-#include <pthread.h>
-
 using namespace lldb_private;
 
-
-static pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
-static pthread_key_t g_thread_create_key = 0;
-
-namespace {
-class MacOSXDarwinThread {
-public:
-  MacOSXDarwinThread() { m_pool = [[NSAutoreleasePool alloc] init]; }
-
-  ~MacOSXDarwinThread() {
-    if (m_pool) {
-      [m_pool drain];
-      m_pool = nil;
-    }
-  }
-
-  static void PThreadDestructor(void *v) {
-    if (v)
-      delete static_cast<MacOSXDarwinThread *>(v);
-    ::pthread_setspecific(g_thread_create_key, NULL);
-  }
-
-protected:
-  NSAutoreleasePool *m_pool = nil;
-
-private:
-  MacOSXDarwinThread(const MacOSXDarwinThread &) = delete;
-  const MacOSXDarwinThread &operator=(const MacOSXDarwinThread &) = delete;
-};
-} // namespace
-
-static void InitThreadCreated() {
-  ::pthread_key_create(&g_thread_create_key,
-                       MacOSXDarwinThread::PThreadDestructor);
-}
-
-HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {}
-
-HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
-    : HostThreadPosix(thread) {}
-
 lldb::thread_result_t
 HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) {
-  ::pthread_once(&g_thread_create_once, InitThreadCreated);
-  if (g_thread_create_key) {
-    ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread());
-  }
-
-  return HostThreadPosix::ThreadCreateTrampoline(arg);
+  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+  lldb::thread_result_t result = HostThreadPosix::ThreadCreateTrampoline(arg);
+  [pool drain];
+  return result;
 }
Index: lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
===================================================================
--- lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
+++ lldb/include/lldb/Host/macosx/HostThreadMacOSX.h
@@ -17,8 +17,7 @@
   friend class ThreadLauncher;
 
 public:
-  HostThreadMacOSX();
-  HostThreadMacOSX(lldb::thread_t thread);
+  using HostThreadPosix::HostThreadPosix;
 
 protected:
   static lldb::thread_result_t ThreadCreateTrampoline(lldb::thread_arg_t arg);
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
  • [Lldb-commits] [PATCH] ... Pavel Labath via Phabricator via lldb-commits

Reply via email to