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" |
6 #if defined(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
7 | 7 |
8 #include "platform/thread.h" | 8 #include "platform/thread.h" |
9 | 9 |
10 #include <sys/errno.h> // NOLINT | 10 #include <sys/errno.h> // NOLINT |
| 11 #include <sys/types.h> // NOLINT |
| 12 #include <sys/sysctl.h> // NOLINT |
| 13 #include <mach/mach_init.h> // NOLINT |
| 14 #include <mach/mach_host.h> // NOLINT |
| 15 #include <mach/mach_port.h> // NOLINT |
| 16 #include <mach/mach_traps.h> // NOLINT |
| 17 #include <mach/task_info.h> // NOLINT |
| 18 #include <mach/thread_info.h> // NOLINT |
| 19 #include <mach/thread_act.h> // NOLINT |
11 | 20 |
12 #include "platform/assert.h" | 21 #include "platform/assert.h" |
13 | 22 |
14 namespace dart { | 23 namespace dart { |
15 | 24 |
16 #define VALIDATE_PTHREAD_RESULT(result) \ | 25 #define VALIDATE_PTHREAD_RESULT(result) \ |
17 if (result != 0) { \ | 26 if (result != 0) { \ |
18 const int kBufferSize = 1024; \ | 27 const int kBufferSize = 1024; \ |
19 char error_message[kBufferSize]; \ | 28 char error_message[kBufferSize]; \ |
20 strerror_r(result, error_message, kBufferSize); \ | 29 strerror_r(result, error_message, kBufferSize); \ |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 VALIDATE_PTHREAD_RESULT(result); | 130 VALIDATE_PTHREAD_RESULT(result); |
122 } | 131 } |
123 | 132 |
124 | 133 |
125 intptr_t Thread::GetMaxStackSize() { | 134 intptr_t Thread::GetMaxStackSize() { |
126 const int kStackSize = (128 * kWordSize * KB); | 135 const int kStackSize = (128 * kWordSize * KB); |
127 return kStackSize; | 136 return kStackSize; |
128 } | 137 } |
129 | 138 |
130 | 139 |
| 140 ThreadId Thread::GetCurrentThreadId() { |
| 141 return pthread_self(); |
| 142 } |
| 143 |
| 144 |
| 145 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
| 146 ASSERT(thread_id == GetCurrentThreadId()); |
| 147 ASSERT(cpu_usage != NULL); |
| 148 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; |
| 149 thread_basic_info_data_t info_data; |
| 150 thread_basic_info_t info = &info_data; |
| 151 mach_port_t thread_port = mach_thread_self(); |
| 152 kern_return_t r = thread_info(thread_port, THREAD_BASIC_INFO, |
| 153 (thread_info_t)info, &count); |
| 154 mach_port_deallocate(mach_task_self(), thread_port); |
| 155 if (r == KERN_SUCCESS) { |
| 156 *cpu_usage = (info->user_time.seconds * kMicrosecondsPerSecond) + |
| 157 info->user_time.microseconds; |
| 158 return; |
| 159 } |
| 160 *cpu_usage = 0; |
| 161 } |
| 162 |
| 163 |
131 Mutex::Mutex() { | 164 Mutex::Mutex() { |
132 pthread_mutexattr_t attr; | 165 pthread_mutexattr_t attr; |
133 int result = pthread_mutexattr_init(&attr); | 166 int result = pthread_mutexattr_init(&attr); |
134 VALIDATE_PTHREAD_RESULT(result); | 167 VALIDATE_PTHREAD_RESULT(result); |
135 | 168 |
136 #if defined(DEBUG) | 169 #if defined(DEBUG) |
137 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 170 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
138 VALIDATE_PTHREAD_RESULT(result); | 171 VALIDATE_PTHREAD_RESULT(result); |
139 #endif // defined(DEBUG) | 172 #endif // defined(DEBUG) |
140 | 173 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 255 |
223 | 256 |
224 void Monitor::Exit() { | 257 void Monitor::Exit() { |
225 // TODO(iposva): Do we need to track lock owners? | 258 // TODO(iposva): Do we need to track lock owners? |
226 int result = pthread_mutex_unlock(data_.mutex()); | 259 int result = pthread_mutex_unlock(data_.mutex()); |
227 VALIDATE_PTHREAD_RESULT(result); | 260 VALIDATE_PTHREAD_RESULT(result); |
228 } | 261 } |
229 | 262 |
230 | 263 |
231 Monitor::WaitResult Monitor::Wait(int64_t millis) { | 264 Monitor::WaitResult Monitor::Wait(int64_t millis) { |
| 265 return WaitMicros(millis * kMicrosecondsPerMillisecond); |
| 266 } |
| 267 |
| 268 |
| 269 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { |
232 // TODO(iposva): Do we need to track lock owners? | 270 // TODO(iposva): Do we need to track lock owners? |
233 Monitor::WaitResult retval = kNotified; | 271 Monitor::WaitResult retval = kNotified; |
234 if (millis == 0) { | 272 if (micros == kNoTimeout) { |
235 // Wait forever. | 273 // Wait forever. |
236 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | 274 int result = pthread_cond_wait(data_.cond(), data_.mutex()); |
237 VALIDATE_PTHREAD_RESULT(result); | 275 VALIDATE_PTHREAD_RESULT(result); |
238 } else { | 276 } else { |
239 struct timespec ts; | 277 struct timespec ts; |
240 int64_t secs = millis / kMillisecondsPerSecond; | 278 int64_t secs = micros / kMicrosecondsPerSecond; |
241 int64_t nanos = (millis - (secs * kMillisecondsPerSecond)) * | 279 int64_t nanos = |
242 kNanosecondsPerMillisecond; | 280 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; |
243 ts.tv_sec = secs; | 281 ts.tv_sec = secs; |
244 ts.tv_nsec = nanos; | 282 ts.tv_nsec = nanos; |
245 int result = pthread_cond_timedwait_relative_np(data_.cond(), | 283 int result = pthread_cond_timedwait_relative_np(data_.cond(), |
246 data_.mutex(), | 284 data_.mutex(), |
247 &ts); | 285 &ts); |
248 ASSERT((result == 0) || (result == ETIMEDOUT)); | 286 ASSERT((result == 0) || (result == ETIMEDOUT)); |
249 if (result == ETIMEDOUT) { | 287 if (result == ETIMEDOUT) { |
250 retval = kTimedOut; | 288 retval = kTimedOut; |
251 } | 289 } |
252 } | 290 } |
(...skipping 10 matching lines...) Expand all Loading... |
263 | 301 |
264 void Monitor::NotifyAll() { | 302 void Monitor::NotifyAll() { |
265 // TODO(iposva): Do we need to track lock owners? | 303 // TODO(iposva): Do we need to track lock owners? |
266 int result = pthread_cond_broadcast(data_.cond()); | 304 int result = pthread_cond_broadcast(data_.cond()); |
267 VALIDATE_PTHREAD_RESULT(result); | 305 VALIDATE_PTHREAD_RESULT(result); |
268 } | 306 } |
269 | 307 |
270 } // namespace dart | 308 } // namespace dart |
271 | 309 |
272 #endif // defined(TARGET_OS_MACOS) | 310 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |