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(int64_t* cpu_usage) { | |
146 ASSERT(cpu_usage != NULL); | |
147 mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT; | |
148 thread_basic_info_data_t info_data; | |
149 thread_basic_info_t info = &info_data; | |
150 mach_port_t thread_port = mach_thread_self(); | |
151 kern_return_t r = thread_info(thread_port, THREAD_BASIC_INFO, | |
152 (thread_info_t)info, &count); | |
153 mach_port_deallocate(mach_task_self(), thread_port); | |
154 if (r == KERN_SUCCESS) { | |
155 *cpu_usage = (info->user_time.seconds * kMicrosecondsPerSecond) + | |
156 info->user_time.microseconds; | |
157 return; | |
158 } | |
159 *cpu_usage = 0; | |
160 } | |
161 | |
162 | |
131 Mutex::Mutex() { | 163 Mutex::Mutex() { |
132 pthread_mutexattr_t attr; | 164 pthread_mutexattr_t attr; |
133 int result = pthread_mutexattr_init(&attr); | 165 int result = pthread_mutexattr_init(&attr); |
134 VALIDATE_PTHREAD_RESULT(result); | 166 VALIDATE_PTHREAD_RESULT(result); |
135 | 167 |
136 #if defined(DEBUG) | 168 #if defined(DEBUG) |
137 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 169 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
138 VALIDATE_PTHREAD_RESULT(result); | 170 VALIDATE_PTHREAD_RESULT(result); |
139 #endif // defined(DEBUG) | 171 #endif // defined(DEBUG) |
140 | 172 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
247 &ts); | 279 &ts); |
248 ASSERT((result == 0) || (result == ETIMEDOUT)); | 280 ASSERT((result == 0) || (result == ETIMEDOUT)); |
249 if (result == ETIMEDOUT) { | 281 if (result == ETIMEDOUT) { |
250 retval = kTimedOut; | 282 retval = kTimedOut; |
251 } | 283 } |
252 } | 284 } |
253 return retval; | 285 return retval; |
254 } | 286 } |
255 | 287 |
256 | 288 |
289 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { | |
290 // TODO(iposva): Do we need to track lock owners? | |
291 Monitor::WaitResult retval = kNotified; | |
292 if (micros == 0) { | |
siva
2013/10/28 05:19:21
ditto comment about using the named constant.
Cutch
2013/11/04 20:36:05
Done.
| |
293 // Wait forever. | |
294 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | |
295 VALIDATE_PTHREAD_RESULT(result); | |
296 } else { | |
297 struct timespec ts; | |
298 int64_t secs = micros / kMicrosecondsPerSecond; | |
299 int64_t nanos = | |
300 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; | |
301 ts.tv_sec = secs; | |
302 ts.tv_nsec = nanos; | |
303 int result = pthread_cond_timedwait_relative_np(data_.cond(), | |
304 data_.mutex(), | |
305 &ts); | |
306 ASSERT((result == 0) || (result == ETIMEDOUT)); | |
307 if (result == ETIMEDOUT) { | |
308 retval = kTimedOut; | |
309 } | |
siva
2013/10/28 05:19:21
else {
VALIDATE_PTHREAD_RESULT(result);
}
Cutch
2013/11/04 20:36:05
Above assert handles this.
| |
310 } | |
311 return retval; | |
312 } | |
313 | |
314 | |
257 void Monitor::Notify() { | 315 void Monitor::Notify() { |
258 // TODO(iposva): Do we need to track lock owners? | 316 // TODO(iposva): Do we need to track lock owners? |
259 int result = pthread_cond_signal(data_.cond()); | 317 int result = pthread_cond_signal(data_.cond()); |
260 VALIDATE_PTHREAD_RESULT(result); | 318 VALIDATE_PTHREAD_RESULT(result); |
261 } | 319 } |
262 | 320 |
263 | 321 |
264 void Monitor::NotifyAll() { | 322 void Monitor::NotifyAll() { |
265 // TODO(iposva): Do we need to track lock owners? | 323 // TODO(iposva): Do we need to track lock owners? |
266 int result = pthread_cond_broadcast(data_.cond()); | 324 int result = pthread_cond_broadcast(data_.cond()); |
267 VALIDATE_PTHREAD_RESULT(result); | 325 VALIDATE_PTHREAD_RESULT(result); |
268 } | 326 } |
269 | 327 |
270 } // namespace dart | 328 } // namespace dart |
271 | 329 |
272 #endif // defined(TARGET_OS_MACOS) | 330 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |