xref: /openbsd-src/gnu/llvm/lldb/source/Host/macosx/objcxx/HostThreadMacOSX.mm (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1//===-- HostThreadMacOSX.cpp ------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/Host/macosx/HostThreadMacOSX.h"
10#include "lldb/Host/Host.h"
11
12#include <CoreFoundation/CoreFoundation.h>
13#include <Foundation/Foundation.h>
14
15#include <pthread.h>
16
17using namespace lldb_private;
18
19namespace {
20
21pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
22pthread_key_t g_thread_create_key = 0;
23
24class MacOSXDarwinThread {
25public:
26  MacOSXDarwinThread() : m_pool(nil) {
27    m_pool = [[NSAutoreleasePool alloc] init];
28  }
29
30  ~MacOSXDarwinThread() {
31    if (m_pool) {
32      [m_pool drain];
33      m_pool = nil;
34    }
35  }
36
37  static void PThreadDestructor(void *v) {
38    if (v)
39      delete static_cast<MacOSXDarwinThread *>(v);
40    ::pthread_setspecific(g_thread_create_key, NULL);
41  }
42
43protected:
44  NSAutoreleasePool *m_pool;
45
46private:
47  DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread);
48};
49
50void InitThreadCreated() {
51  ::pthread_key_create(&g_thread_create_key,
52                       MacOSXDarwinThread::PThreadDestructor);
53}
54} // namespace
55
56HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {}
57
58HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
59    : HostThreadPosix(thread) {}
60
61lldb::thread_result_t
62HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) {
63  ::pthread_once(&g_thread_create_once, InitThreadCreated);
64  if (g_thread_create_key) {
65    ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread());
66  }
67
68  return HostThreadPosix::ThreadCreateTrampoline(arg);
69}
70