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 "bin/thread.h" | 8 #include "bin/thread.h" |
9 #include "bin/thread_macos.h" | 9 #include "bin/thread_macos.h" |
10 | 10 |
11 #include <mach/mach_host.h> // NOLINT | 11 #include <mach/mach_host.h> // NOLINT |
12 #include <mach/mach_init.h> // NOLINT | 12 #include <mach/mach_init.h> // NOLINT |
13 #include <mach/mach_port.h> // NOLINT | 13 #include <mach/mach_port.h> // NOLINT |
14 #include <mach/mach_traps.h> // NOLINT | 14 #include <mach/mach_traps.h> // NOLINT |
15 #include <mach/task_info.h> // NOLINT | 15 #include <mach/task_info.h> // NOLINT |
16 #include <mach/thread_act.h> // NOLINT | 16 #include <mach/thread_act.h> // NOLINT |
17 #include <mach/thread_info.h> // NOLINT | 17 #include <mach/thread_info.h> // NOLINT |
18 #include <sys/errno.h> // NOLINT | 18 #include <sys/errno.h> // NOLINT |
19 #include <sys/sysctl.h> // NOLINT | 19 #include <sys/sysctl.h> // NOLINT |
20 #include <sys/types.h> // NOLINT | 20 #include <sys/types.h> // NOLINT |
21 | 21 |
22 #include "platform/assert.h" | 22 #include "platform/assert.h" |
23 #include "platform/utils.h" | 23 #include "platform/utils.h" |
24 | 24 |
25 namespace dart { | 25 namespace dart { |
26 namespace bin { | 26 namespace bin { |
27 | 27 |
28 #define VALIDATE_PTHREAD_RESULT(result) \ | 28 #define VALIDATE_PTHREAD_RESULT(result) \ |
29 if (result != 0) { \ | 29 if (result != 0) { \ |
30 const int kBufferSize = 1024; \ | 30 const int kBufferSize = 1024; \ |
31 char error_message[kBufferSize]; \ | 31 char error_message[kBufferSize]; \ |
32 Utils::StrError(result, error_message, kBufferSize); \ | 32 Utils::StrError(result, error_message, kBufferSize); \ |
33 FATAL2("pthread error: %d (%s)", result, error_message); \ | 33 FATAL2("pthread error: %d (%s)", result, error_message); \ |
34 } | 34 } |
35 | 35 |
36 | 36 |
37 #ifdef DEBUG | 37 #ifdef DEBUG |
38 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 38 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
39 if (result != 0) { \ | 39 if (result != 0) { \ |
40 const int kBufferSize = 1024; \ | 40 const int kBufferSize = 1024; \ |
41 char error_message[kBufferSize]; \ | 41 char error_message[kBufferSize]; \ |
42 Utils::StrError(result, error_message, kBufferSize); \ | 42 Utils::StrError(result, error_message, kBufferSize); \ |
43 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \ | 43 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", __FILE__, __LINE__, \ |
44 __FILE__, __LINE__, result, error_message); \ | 44 result, error_message); \ |
45 return result; \ | 45 return result; \ |
46 } | 46 } |
47 #else | 47 #else |
48 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 48 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
49 if (result != 0) { \ | 49 if (result != 0) { \ |
50 return result; \ | 50 return result; \ |
51 } | 51 } |
52 #endif | 52 #endif |
53 | 53 |
54 | 54 |
55 class ThreadStartData { | 55 class ThreadStartData { |
56 public: | 56 public: |
57 ThreadStartData(Thread::ThreadStartFunction function, | 57 ThreadStartData(Thread::ThreadStartFunction function, uword parameter) |
58 uword parameter) | |
59 : function_(function), parameter_(parameter) {} | 58 : function_(function), parameter_(parameter) {} |
60 | 59 |
61 Thread::ThreadStartFunction function() const { return function_; } | 60 Thread::ThreadStartFunction function() const { return function_; } |
62 uword parameter() const { return parameter_; } | 61 uword parameter() const { return parameter_; } |
63 | 62 |
64 private: | 63 private: |
65 Thread::ThreadStartFunction function_; | 64 Thread::ThreadStartFunction function_; |
66 uword parameter_; | 65 uword parameter_; |
67 | 66 |
68 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 67 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 struct timespec ts; | 303 struct timespec ts; |
305 int64_t secs = micros / kMicrosecondsPerSecond; | 304 int64_t secs = micros / kMicrosecondsPerSecond; |
306 if (secs > kMaxInt32) { | 305 if (secs > kMaxInt32) { |
307 // Avoid truncation of overly large timeout values. | 306 // Avoid truncation of overly large timeout values. |
308 secs = kMaxInt32; | 307 secs = kMaxInt32; |
309 } | 308 } |
310 int64_t nanos = | 309 int64_t nanos = |
311 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; | 310 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; |
312 ts.tv_sec = static_cast<int32_t>(secs); | 311 ts.tv_sec = static_cast<int32_t>(secs); |
313 ts.tv_nsec = static_cast<long>(nanos); // NOLINT (long used in timespec). | 312 ts.tv_nsec = static_cast<long>(nanos); // NOLINT (long used in timespec). |
314 int result = pthread_cond_timedwait_relative_np(data_.cond(), | 313 int result = |
315 data_.mutex(), | 314 pthread_cond_timedwait_relative_np(data_.cond(), data_.mutex(), &ts); |
316 &ts); | |
317 ASSERT((result == 0) || (result == ETIMEDOUT)); | 315 ASSERT((result == 0) || (result == ETIMEDOUT)); |
318 if (result == ETIMEDOUT) { | 316 if (result == ETIMEDOUT) { |
319 retval = kTimedOut; | 317 retval = kTimedOut; |
320 } | 318 } |
321 } | 319 } |
322 return retval; | 320 return retval; |
323 } | 321 } |
324 | 322 |
325 | 323 |
326 void Monitor::Notify() { | 324 void Monitor::Notify() { |
327 // TODO(iposva): Do we need to track lock owners? | 325 // TODO(iposva): Do we need to track lock owners? |
328 int result = pthread_cond_signal(data_.cond()); | 326 int result = pthread_cond_signal(data_.cond()); |
329 VALIDATE_PTHREAD_RESULT(result); | 327 VALIDATE_PTHREAD_RESULT(result); |
330 } | 328 } |
331 | 329 |
332 | 330 |
333 void Monitor::NotifyAll() { | 331 void Monitor::NotifyAll() { |
334 // TODO(iposva): Do we need to track lock owners? | 332 // TODO(iposva): Do we need to track lock owners? |
335 int result = pthread_cond_broadcast(data_.cond()); | 333 int result = pthread_cond_broadcast(data_.cond()); |
336 VALIDATE_PTHREAD_RESULT(result); | 334 VALIDATE_PTHREAD_RESULT(result); |
337 } | 335 } |
338 | 336 |
339 } // namespace bin | 337 } // namespace bin |
340 } // namespace dart | 338 } // namespace dart |
341 | 339 |
342 #endif // defined(TARGET_OS_MACOS) | 340 #endif // defined(TARGET_OS_MACOS) |
OLD | NEW |