xref: /llvm-project/lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm (revision 2c77eefe85efa59081c48afa3edb652365f8d1ac)
1//===-- HostThreadMacOSX.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/Host/macosx/HostThreadMacOSX.h"
11#include "lldb/Host/Host.h"
12
13#include <CoreFoundation/CoreFoundation.h>
14#include <Foundation/Foundation.h>
15
16#include <pthread.h>
17
18using namespace lldb_private;
19
20namespace {
21
22pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
23pthread_key_t g_thread_create_key = 0;
24
25class MacOSXDarwinThread {
26public:
27  MacOSXDarwinThread() : m_pool(nil) {
28    m_pool = [[NSAutoreleasePool alloc] init];
29  }
30
31  ~MacOSXDarwinThread() {
32    if (m_pool) {
33      [m_pool drain];
34      m_pool = nil;
35    }
36  }
37
38  static void PThreadDestructor(void *v) {
39    if (v)
40      delete static_cast<MacOSXDarwinThread *>(v);
41    ::pthread_setspecific(g_thread_create_key, NULL);
42  }
43
44protected:
45  NSAutoreleasePool *m_pool;
46
47private:
48  DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread);
49};
50
51void InitThreadCreated() {
52  ::pthread_key_create(&g_thread_create_key,
53                       MacOSXDarwinThread::PThreadDestructor);
54}
55} // namespace
56
57HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {}
58
59HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
60    : HostThreadPosix(thread) {}
61
62lldb::thread_result_t
63HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) {
64  ::pthread_once(&g_thread_create_once, InitThreadCreated);
65  if (g_thread_create_key) {
66    ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread());
67  }
68
69  return HostThreadPosix::ThreadCreateTrampoline(arg);
70}
71