| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/globals.h" | 5 #include "platform/globals.h" // NOLINT |
| 6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 7 | 7 |
| 8 #include "vm/thread.h" | 8 #include "vm/os_thread.h" |
| 9 | 9 |
| 10 #include <sys/errno.h> // NOLINT | 10 #include <sys/errno.h> // NOLINT |
| 11 #include <sys/types.h> // NOLINT | 11 #include <sys/types.h> // NOLINT |
| 12 #include <sys/sysctl.h> // NOLINT | 12 #include <sys/sysctl.h> // NOLINT |
| 13 #include <mach/mach_init.h> // NOLINT | 13 #include <mach/mach_init.h> // NOLINT |
| 14 #include <mach/mach_host.h> // NOLINT | 14 #include <mach/mach_host.h> // NOLINT |
| 15 #include <mach/mach_port.h> // NOLINT | 15 #include <mach/mach_port.h> // NOLINT |
| 16 #include <mach/mach_traps.h> // NOLINT | 16 #include <mach/mach_traps.h> // NOLINT |
| 17 #include <mach/task_info.h> // NOLINT | 17 #include <mach/task_info.h> // NOLINT |
| 18 #include <mach/thread_info.h> // NOLINT | 18 #include <mach/thread_info.h> // NOLINT |
| (...skipping 24 matching lines...) Expand all Loading... |
| 43 return result; \ | 43 return result; \ |
| 44 } | 44 } |
| 45 #else | 45 #else |
| 46 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 46 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 47 if (result != 0) return result; | 47 if (result != 0) return result; |
| 48 #endif | 48 #endif |
| 49 | 49 |
| 50 | 50 |
| 51 class ThreadStartData { | 51 class ThreadStartData { |
| 52 public: | 52 public: |
| 53 ThreadStartData(Thread::ThreadStartFunction function, | 53 ThreadStartData(OSThread::ThreadStartFunction function, |
| 54 uword parameter) | 54 uword parameter) |
| 55 : function_(function), parameter_(parameter) {} | 55 : function_(function), parameter_(parameter) {} |
| 56 | 56 |
| 57 Thread::ThreadStartFunction function() const { return function_; } | 57 OSThread::ThreadStartFunction function() const { return function_; } |
| 58 uword parameter() const { return parameter_; } | 58 uword parameter() const { return parameter_; } |
| 59 | 59 |
| 60 private: | 60 private: |
| 61 Thread::ThreadStartFunction function_; | 61 OSThread::ThreadStartFunction function_; |
| 62 uword parameter_; | 62 uword parameter_; |
| 63 | 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 64 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 | 67 |
| 68 // Dispatch to the thread start function provided by the caller. This trampoline | 68 // Dispatch to the thread start function provided by the caller. This trampoline |
| 69 // is used to ensure that the thread is properly destroyed if the thread just | 69 // is used to ensure that the thread is properly destroyed if the thread just |
| 70 // exits. | 70 // exits. |
| 71 static void* ThreadStart(void* data_ptr) { | 71 static void* ThreadStart(void* data_ptr) { |
| 72 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 72 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| 73 | 73 |
| 74 Thread::ThreadStartFunction function = data->function(); | 74 OSThread::ThreadStartFunction function = data->function(); |
| 75 uword parameter = data->parameter(); | 75 uword parameter = data->parameter(); |
| 76 delete data; | 76 delete data; |
| 77 | 77 |
| 78 // Call the supplied thread start function handing it its parameters. | 78 // Call the supplied thread start function handing it its parameters. |
| 79 function(parameter); | 79 function(parameter); |
| 80 | 80 |
| 81 return NULL; | 81 return NULL; |
| 82 } | 82 } |
| 83 | 83 |
| 84 | 84 |
| 85 int Thread::Start(ThreadStartFunction function, uword parameter) { | 85 int OSThread::Start(ThreadStartFunction function, uword parameter) { |
| 86 pthread_attr_t attr; | 86 pthread_attr_t attr; |
| 87 int result = pthread_attr_init(&attr); | 87 int result = pthread_attr_init(&attr); |
| 88 RETURN_ON_PTHREAD_FAILURE(result); | 88 RETURN_ON_PTHREAD_FAILURE(result); |
| 89 | 89 |
| 90 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 90 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 91 RETURN_ON_PTHREAD_FAILURE(result); | 91 RETURN_ON_PTHREAD_FAILURE(result); |
| 92 | 92 |
| 93 result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize()); | 93 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); |
| 94 RETURN_ON_PTHREAD_FAILURE(result); | 94 RETURN_ON_PTHREAD_FAILURE(result); |
| 95 | 95 |
| 96 ThreadStartData* data = new ThreadStartData(function, parameter); | 96 ThreadStartData* data = new ThreadStartData(function, parameter); |
| 97 | 97 |
| 98 pthread_t tid; | 98 pthread_t tid; |
| 99 result = pthread_create(&tid, &attr, ThreadStart, data); | 99 result = pthread_create(&tid, &attr, ThreadStart, data); |
| 100 RETURN_ON_PTHREAD_FAILURE(result); | 100 RETURN_ON_PTHREAD_FAILURE(result); |
| 101 | 101 |
| 102 result = pthread_attr_destroy(&attr); | 102 result = pthread_attr_destroy(&attr); |
| 103 RETURN_ON_PTHREAD_FAILURE(result); | 103 RETURN_ON_PTHREAD_FAILURE(result); |
| 104 | 104 |
| 105 return 0; | 105 return 0; |
| 106 } | 106 } |
| 107 | 107 |
| 108 | 108 |
| 109 ThreadLocalKey Thread::kUnsetThreadLocalKey = static_cast<pthread_key_t>(-1); | 109 ThreadLocalKey OSThread::kUnsetThreadLocalKey = |
| 110 ThreadId Thread::kInvalidThreadId = reinterpret_cast<ThreadId>(NULL); | 110 static_cast<pthread_key_t>(-1); |
| 111 ThreadId OSThread::kInvalidThreadId = reinterpret_cast<ThreadId>(NULL); |
| 111 | 112 |
| 112 ThreadLocalKey Thread::CreateThreadLocal() { | 113 ThreadLocalKey OSThread::CreateThreadLocal() { |
| 113 pthread_key_t key = kUnsetThreadLocalKey; | 114 pthread_key_t key = kUnsetThreadLocalKey; |
| 114 int result = pthread_key_create(&key, NULL); | 115 int result = pthread_key_create(&key, NULL); |
| 115 VALIDATE_PTHREAD_RESULT(result); | 116 VALIDATE_PTHREAD_RESULT(result); |
| 116 ASSERT(key != kUnsetThreadLocalKey); | 117 ASSERT(key != kUnsetThreadLocalKey); |
| 117 return key; | 118 return key; |
| 118 } | 119 } |
| 119 | 120 |
| 120 | 121 |
| 121 void Thread::DeleteThreadLocal(ThreadLocalKey key) { | 122 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
| 122 ASSERT(key != kUnsetThreadLocalKey); | 123 ASSERT(key != kUnsetThreadLocalKey); |
| 123 int result = pthread_key_delete(key); | 124 int result = pthread_key_delete(key); |
| 124 VALIDATE_PTHREAD_RESULT(result); | 125 VALIDATE_PTHREAD_RESULT(result); |
| 125 } | 126 } |
| 126 | 127 |
| 127 | 128 |
| 128 void Thread::SetThreadLocal(ThreadLocalKey key, uword value) { | 129 void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) { |
| 129 ASSERT(key != kUnsetThreadLocalKey); | 130 ASSERT(key != kUnsetThreadLocalKey); |
| 130 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); | 131 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); |
| 131 VALIDATE_PTHREAD_RESULT(result); | 132 VALIDATE_PTHREAD_RESULT(result); |
| 132 } | 133 } |
| 133 | 134 |
| 134 | 135 |
| 135 intptr_t Thread::GetMaxStackSize() { | 136 intptr_t OSThread::GetMaxStackSize() { |
| 136 const int kStackSize = (128 * kWordSize * KB); | 137 const int kStackSize = (128 * kWordSize * KB); |
| 137 return kStackSize; | 138 return kStackSize; |
| 138 } | 139 } |
| 139 | 140 |
| 140 | 141 |
| 141 ThreadId Thread::GetCurrentThreadId() { | 142 ThreadId OSThread::GetCurrentThreadId() { |
| 142 return pthread_self(); | 143 return pthread_self(); |
| 143 } | 144 } |
| 144 | 145 |
| 145 | 146 |
| 146 bool Thread::Join(ThreadId id) { | 147 bool OSThread::Join(ThreadId id) { |
| 147 return false; | 148 return false; |
| 148 } | 149 } |
| 149 | 150 |
| 150 | 151 |
| 151 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { | 152 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
| 152 ASSERT(sizeof(id) == sizeof(intptr_t)); | 153 ASSERT(sizeof(id) == sizeof(intptr_t)); |
| 153 return reinterpret_cast<intptr_t>(id); | 154 return reinterpret_cast<intptr_t>(id); |
| 154 } | 155 } |
| 155 | 156 |
| 156 | 157 |
| 157 bool Thread::Compare(ThreadId a, ThreadId b) { | 158 bool OSThread::Compare(ThreadId a, ThreadId b) { |
| 158 return pthread_equal(a, b) != 0; | 159 return pthread_equal(a, b) != 0; |
| 159 } | 160 } |
| 160 | 161 |
| 161 | 162 |
| 162 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 163 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
| 163 ASSERT(thread_id == GetCurrentThreadId()); | 164 ASSERT(thread_id == GetCurrentThreadId()); |
| 164 ASSERT(cpu_usage != NULL); | 165 ASSERT(cpu_usage != NULL); |
| 165 // TODO(johnmccutchan): Enable this after fixing issue with macos directory | 166 // TODO(johnmccutchan): Enable this after fixing issue with macos directory |
| 166 // watcher. | 167 // watcher. |
| 167 const bool get_cpu_usage = false; | 168 const bool get_cpu_usage = false; |
| 168 if (get_cpu_usage) { | 169 if (get_cpu_usage) { |
| 169 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | 170 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
| 170 thread_basic_info_data_t info_data; | 171 thread_basic_info_data_t info_data; |
| 171 thread_basic_info_t info = &info_data; | 172 thread_basic_info_t info = &info_data; |
| 172 mach_port_t thread_port = mach_thread_self(); | 173 mach_port_t thread_port = mach_thread_self(); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 | 348 |
| 348 void Monitor::NotifyAll() { | 349 void Monitor::NotifyAll() { |
| 349 // TODO(iposva): Do we need to track lock owners? | 350 // TODO(iposva): Do we need to track lock owners? |
| 350 int result = pthread_cond_broadcast(data_.cond()); | 351 int result = pthread_cond_broadcast(data_.cond()); |
| 351 VALIDATE_PTHREAD_RESULT(result); | 352 VALIDATE_PTHREAD_RESULT(result); |
| 352 } | 353 } |
| 353 | 354 |
| 354 } // namespace dart | 355 } // namespace dart |
| 355 | 356 |
| 356 #endif // defined(TARGET_OS_MACOS) | 357 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |