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 = [[NSAutoreleasePool alloc] init]; } 27 28 ~MacOSXDarwinThread() { 29 if (m_pool) { 30 [m_pool drain]; 31 m_pool = nil; 32 } 33 } 34 35 static void PThreadDestructor(void *v) { 36 if (v) 37 delete static_cast<MacOSXDarwinThread *>(v); 38 ::pthread_setspecific(g_thread_create_key, NULL); 39 } 40 41protected: 42 NSAutoreleasePool *m_pool = nil; 43 44private: 45 MacOSXDarwinThread(const MacOSXDarwinThread &) = delete; 46 const MacOSXDarwinThread &operator=(const MacOSXDarwinThread &) = delete; 47}; 48 49void InitThreadCreated() { 50 ::pthread_key_create(&g_thread_create_key, 51 MacOSXDarwinThread::PThreadDestructor); 52} 53} // namespace 54 55HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {} 56 57HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread) 58 : HostThreadPosix(thread) {} 59 60lldb::thread_result_t 61HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) { 62 ::pthread_once(&g_thread_create_once, InitThreadCreated); 63 if (g_thread_create_key) { 64 ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread()); 65 } 66 67 return HostThreadPosix::ThreadCreateTrampoline(arg); 68} 69