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 MacOSXDarwinThread(const MacOSXDarwinThread &) = delete; 48 const MacOSXDarwinThread &operator=(const MacOSXDarwinThread &) = delete; 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