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 | |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 16 | #include <pthread.h> |
| 17 | |
| 18 | using namespace lldb_private; |
| 19 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 20 | namespace { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 21 | |
| 22 | pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT; |
| 23 | pthread_key_t g_thread_create_key = 0; |
| 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | class MacOSXDarwinThread { |
| 26 | public: |
| 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; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 35 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | } |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | static void PThreadDestructor(void *v) { |
| 39 | if (v) |
| 40 | delete static_cast<MacOSXDarwinThread *>(v); |
| 41 | ::pthread_setspecific(g_thread_create_key, NULL); |
| 42 | } |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | protected: |
| 45 | NSAutoreleasePool *m_pool; |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | private: |
| 48 | DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | void InitThreadCreated() { |
| 52 | ::pthread_key_create(&g_thread_create_key, |
| 53 | MacOSXDarwinThread::PThreadDestructor); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 54 | } |
| 55 | } // namespace |
| 56 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 57 | HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {} |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 58 | |
| 59 | HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | : HostThreadPosix(thread) {} |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 61 | |
| 62 | lldb::thread_result_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | HostThreadMacOSX::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 | } |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | return HostThreadPosix::ThreadCreateTrampoline(arg); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 70 | } |