blob: c5051cdf30d77ee7f359573c891bbea9046a0110 [file] [log] [blame]
Zachary Turner39de3112014-09-09 20:54:56 +00001//===-- 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 Turner39de3112014-09-09 20:54:56 +000016#include <pthread.h>
17
18using namespace lldb_private;
19
Kate Stoneb9c1b512016-09-06 20:57:50 +000020namespace {
Zachary Turner39de3112014-09-09 20:54:56 +000021
22pthread_once_t g_thread_create_once = PTHREAD_ONCE_INIT;
23pthread_key_t g_thread_create_key = 0;
24
Kate Stoneb9c1b512016-09-06 20:57:50 +000025class MacOSXDarwinThread {
26public:
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 Turner39de3112014-09-09 20:54:56 +000035 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 }
Zachary Turner39de3112014-09-09 20:54:56 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 static void PThreadDestructor(void *v) {
39 if (v)
40 delete static_cast<MacOSXDarwinThread *>(v);
41 ::pthread_setspecific(g_thread_create_key, NULL);
42 }
Zachary Turner39de3112014-09-09 20:54:56 +000043
Kate Stoneb9c1b512016-09-06 20:57:50 +000044protected:
45 NSAutoreleasePool *m_pool;
Zachary Turner39de3112014-09-09 20:54:56 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047private:
48 DISALLOW_COPY_AND_ASSIGN(MacOSXDarwinThread);
Zachary Turner39de3112014-09-09 20:54:56 +000049};
50
Kate Stoneb9c1b512016-09-06 20:57:50 +000051void InitThreadCreated() {
52 ::pthread_key_create(&g_thread_create_key,
53 MacOSXDarwinThread::PThreadDestructor);
Zachary Turner39de3112014-09-09 20:54:56 +000054}
55} // namespace
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057HostThreadMacOSX::HostThreadMacOSX() : HostThreadPosix() {}
Zachary Turner39de3112014-09-09 20:54:56 +000058
59HostThreadMacOSX::HostThreadMacOSX(lldb::thread_t thread)
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 : HostThreadPosix(thread) {}
Zachary Turner39de3112014-09-09 20:54:56 +000061
62lldb::thread_result_t
Kate Stoneb9c1b512016-09-06 20:57:50 +000063HostThreadMacOSX::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 Turner39de3112014-09-09 20:54:56 +000068
Kate Stoneb9c1b512016-09-06 20:57:50 +000069 return HostThreadPosix::ThreadCreateTrampoline(arg);
Zachary Turner39de3112014-09-09 20:54:56 +000070}