Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 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 <objc/objc-auto.h> |
| 17 | #include <pthread.h> |
| 18 | |
| 19 | using namespace lldb_private; |
| 20 | |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT; |
| 25 | pthread_key_t g_thread_create_key = 0; |
| 26 | |
| 27 | class MacOSXDarwinThread |
| 28 | { |
| 29 | public: |
| 30 | MacOSXDarwinThread() |
| 31 | : m_pool(nil) |
| 32 | { |
| 33 | // Register our thread with the collector if garbage collection is enabled. |
| 34 | if (objc_collectingEnabled()) |
| 35 | { |
| 36 | #if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_5 |
| 37 | // On Leopard and earlier there is no way objc_registerThreadWithCollector |
| 38 | // function, so we do it manually. |
| 39 | auto_zone_register_thread(auto_zone()); |
| 40 | #else |
| 41 | // On SnowLeopard and later we just call the thread registration function. |
| 42 | objc_registerThreadWithCollector(); |
| 43 | #endif |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | m_pool = [[NSAutoreleasePool alloc] init]; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | ~MacOSXDarwinThread() |
| 52 | { |
| 53 | if (m_pool) |
| 54 | { |
| 55 | [m_pool drain]; |
| 56 | m_pool = nil; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void |
| 61 | PThreadDestructor(void *v) |
| 62 | { |
| 63 | if (v) |
| 64 | delete static_cast<MacOSXDarwinThread *>(v); |
| 65 | ::pthread_setspecific(g_thread_create_key, NULL); |
| 66 | } |
| 67 | |
| 68 | protected: |
| 69 | NSAutoreleasePool *m_pool; |
| 70 | |
| 71 | private: |
| 72 | DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread); |
| 73 | }; |
| 74 | |
| 75 | void |
| 76 | InitThreadCreated() |
| 77 | { |
| 78 | ::pthread_key_create(&g_thread_create_key, MacOSXDarwinThread::PThreadDestructor); |
| 79 | } |
| 80 | } // namespace |
| 81 | |
| 82 | HostThreadMacOSX::HostThreadMacOSX() |
| 83 | : HostThreadPosix() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread) |
| 88 | : HostThreadPosix(thread) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | lldb::thread_result_t |
| 93 | HostThreadMacOSX::ThreadCreateTrampoline(lldb::thread_arg_t arg) |
| 94 | { |
| 95 | ::pthread_once(&g_thread_create_once, InitThreadCreated); |
| 96 | if (g_thread_create_key) |
| 97 | { |
| 98 | ::pthread_setspecific(g_thread_create_key, new MacOSXDarwinThread()); |
| 99 | } |
| 100 | |
| 101 | return HostThreadPosix::ThreadCreateTrampoline(arg); |
| 102 | } |