| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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" // NOLINT | 5 #include "platform/globals.h" // NOLINT |
| 6 | 6 #if defined(TARGET_OS_FUCHSIA) |
| 7 #if defined(TARGET_OS_LINUX) | |
| 8 | 7 |
| 9 #include "vm/os_thread.h" | 8 #include "vm/os_thread.h" |
| 9 #include "vm/os_thread_fuchsia.h" |
| 10 | 10 |
| 11 #include <errno.h> // NOLINT | 11 #include <errno.h> // NOLINT |
| 12 #include <sys/resource.h> // NOLINT | 12 #include <magenta/syscalls.h> |
| 13 #include <sys/syscall.h> // NOLINT | 13 #include <magenta/types.h> |
| 14 #include <sys/time.h> // NOLINT | |
| 15 | 14 |
| 16 #include "platform/assert.h" | 15 #include "platform/assert.h" |
| 17 #include "platform/signal_blocker.h" | |
| 18 #include "platform/utils.h" | |
| 19 | 16 |
| 20 namespace dart { | 17 namespace dart { |
| 21 | 18 |
| 22 #define VALIDATE_PTHREAD_RESULT(result) \ | 19 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 23 if (result != 0) { \ | 20 if (result != 0) { \ |
| 24 const int kBufferSize = 1024; \ | 21 FATAL1("pthread error: %d", result); \ |
| 25 char error_buf[kBufferSize]; \ | |
| 26 FATAL2("pthread error: %d (%s)", result, \ | |
| 27 Utils::StrError(result, error_buf, kBufferSize)); \ | |
| 28 } | 22 } |
| 29 | 23 |
| 30 | 24 |
| 31 #if defined(DEBUG) | 25 #if defined(DEBUG) |
| 32 #define ASSERT_PTHREAD_SUCCESS(result) VALIDATE_PTHREAD_RESULT(result) | 26 #define ASSERT_PTHREAD_SUCCESS(result) VALIDATE_PTHREAD_RESULT(result) |
| 33 #else | 27 #else |
| 34 // NOTE: This (currently) expands to a no-op. | 28 // NOTE: This (currently) expands to a no-op. |
| 35 #define ASSERT_PTHREAD_SUCCESS(result) ASSERT(result == 0) | 29 #define ASSERT_PTHREAD_SUCCESS(result) ASSERT(result == 0) |
| 36 #endif | 30 #endif |
| 37 | 31 |
| 38 | 32 |
| 39 #ifdef DEBUG | 33 #ifdef DEBUG |
| 40 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 34 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 41 if (result != 0) { \ | 35 if (result != 0) { \ |
| 42 const int kBufferSize = 1024; \ | 36 const int kBufferSize = 1024; \ |
| 43 char error_buf[kBufferSize]; \ | 37 char error_buf[kBufferSize]; \ |
| 44 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \ | 38 fprintf(stderr, "%s:%d: pthread error: %d\n", \ |
| 45 __FILE__, __LINE__, result, \ | 39 __FILE__, __LINE__, result); \ |
| 46 Utils::StrError(result, error_buf, kBufferSize)); \ | |
| 47 return result; \ | 40 return result; \ |
| 48 } | 41 } |
| 49 #else | 42 #else |
| 50 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 43 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 51 if (result != 0) return result; | 44 if (result != 0) return result; |
| 52 #endif | 45 #endif |
| 53 | 46 |
| 54 | 47 |
| 55 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { | 48 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { |
| 56 int64_t secs = micros / kMicrosecondsPerSecond; | 49 // time in nanoseconds. |
| 57 int64_t nanos = | 50 mx_time_t now = _magenta_current_time(); |
| 58 (micros - (secs * kMicrosecondsPerSecond)) * kNanosecondsPerMicrosecond; | 51 mx_time_t target = now + (micros * kNanosecondsPerMicrosecond); |
| 59 int result = clock_gettime(CLOCK_MONOTONIC, ts); | 52 int64_t secs = target / kNanosecondsPerSecond; |
| 60 ASSERT(result == 0); | 53 int64_t nanos = target - (secs * kNanosecondsPerSecond); |
| 54 |
| 61 ts->tv_sec += secs; | 55 ts->tv_sec += secs; |
| 62 ts->tv_nsec += nanos; | 56 ts->tv_nsec += nanos; |
| 63 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 57 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
| 64 ts->tv_sec += 1; | 58 ts->tv_sec += 1; |
| 65 ts->tv_nsec -= kNanosecondsPerSecond; | 59 ts->tv_nsec -= kNanosecondsPerSecond; |
| 66 } | 60 } |
| 67 } | 61 } |
| 68 | 62 |
| 69 | 63 |
| 70 class ThreadStartData { | 64 class ThreadStartData { |
| 71 public: | 65 public: |
| 72 ThreadStartData(const char* name, | 66 ThreadStartData(const char* name, |
| 73 OSThread::ThreadStartFunction function, | 67 OSThread::ThreadStartFunction function, |
| 74 uword parameter) | 68 uword parameter) |
| 75 : name_(name), function_(function), parameter_(parameter) {} | 69 : name_(name), function_(function), parameter_(parameter) {} |
| 76 | 70 |
| 77 const char* name() const { return name_; } | 71 const char* name() const { return name_; } |
| 78 OSThread::ThreadStartFunction function() const { return function_; } | 72 OSThread::ThreadStartFunction function() const { return function_; } |
| 79 uword parameter() const { return parameter_; } | 73 uword parameter() const { return parameter_; } |
| 80 | 74 |
| 81 private: | 75 private: |
| 82 const char* name_; | 76 const char* name_; |
| 83 OSThread::ThreadStartFunction function_; | 77 OSThread::ThreadStartFunction function_; |
| 84 uword parameter_; | 78 uword parameter_; |
| 85 | 79 |
| 86 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 80 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| 87 }; | 81 }; |
| 88 | 82 |
| 89 | 83 |
| 90 // Spawned threads inherit their spawner's signal mask. We sometimes spawn | |
| 91 // threads for running Dart code from a thread that is blocking SIGPROF. | |
| 92 // This function explicitly unblocks SIGPROF so the profiler continues to | |
| 93 // sample this thread. | |
| 94 static void UnblockSIGPROF() { | |
| 95 sigset_t set; | |
| 96 sigemptyset(&set); | |
| 97 sigaddset(&set, SIGPROF); | |
| 98 int r = pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
| 99 USE(r); | |
| 100 ASSERT(r == 0); | |
| 101 ASSERT(!CHECK_IS_BLOCKING(SIGPROF)); | |
| 102 } | |
| 103 | |
| 104 | |
| 105 // Dispatch to the thread start function provided by the caller. This trampoline | 84 // Dispatch to the thread start function provided by the caller. This trampoline |
| 106 // is used to ensure that the thread is properly destroyed if the thread just | 85 // is used to ensure that the thread is properly destroyed if the thread just |
| 107 // exits. | 86 // exits. |
| 108 static void* ThreadStart(void* data_ptr) { | 87 static void* ThreadStart(void* data_ptr) { |
| 109 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 88 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| 110 | 89 |
| 111 const char* name = data->name(); | 90 const char* name = data->name(); |
| 112 OSThread::ThreadStartFunction function = data->function(); | 91 OSThread::ThreadStartFunction function = data->function(); |
| 113 uword parameter = data->parameter(); | 92 uword parameter = data->parameter(); |
| 114 delete data; | 93 delete data; |
| 115 | 94 |
| 116 // Create new OSThread object and set as TLS for new thread. | 95 // Create new OSThread object and set as TLS for new thread. |
| 117 OSThread* thread = OSThread::CreateOSThread(); | 96 OSThread* thread = OSThread::CreateOSThread(); |
| 118 if (thread != NULL) { | 97 if (thread != NULL) { |
| 119 OSThread::SetCurrent(thread); | 98 OSThread::SetCurrent(thread); |
| 120 thread->set_name(name); | 99 thread->set_name(name); |
| 121 UnblockSIGPROF(); | |
| 122 // Call the supplied thread start function handing it its parameters. | 100 // Call the supplied thread start function handing it its parameters. |
| 123 function(parameter); | 101 function(parameter); |
| 124 } | 102 } |
| 125 | 103 |
| 126 return NULL; | 104 return NULL; |
| 127 } | 105 } |
| 128 | 106 |
| 129 | 107 |
| 130 int OSThread::Start(const char* name, | 108 int OSThread::Start(const char* name, |
| 131 ThreadStartFunction function, | 109 ThreadStartFunction function, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 const int kStackSize = (128 * kWordSize * KB); | 160 const int kStackSize = (128 * kWordSize * KB); |
| 183 return kStackSize; | 161 return kStackSize; |
| 184 } | 162 } |
| 185 | 163 |
| 186 | 164 |
| 187 ThreadId OSThread::GetCurrentThreadId() { | 165 ThreadId OSThread::GetCurrentThreadId() { |
| 188 return pthread_self(); | 166 return pthread_self(); |
| 189 } | 167 } |
| 190 | 168 |
| 191 | 169 |
| 170 #ifndef PRODUCT |
| 192 ThreadId OSThread::GetCurrentThreadTraceId() { | 171 ThreadId OSThread::GetCurrentThreadTraceId() { |
| 193 return syscall(__NR_gettid); | 172 UNIMPLEMENTED(); |
| 173 return 0; |
| 194 } | 174 } |
| 175 #endif // PRODUCT |
| 195 | 176 |
| 196 | 177 |
| 197 ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) { | 178 ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) { |
| 198 ASSERT(thread != NULL); | 179 ASSERT(thread != NULL); |
| 199 // Make sure we're filling in the join id for the current thread. | 180 // Make sure we're filling in the join id for the current thread. |
| 200 ASSERT(thread->id() == GetCurrentThreadId()); | 181 ASSERT(thread->id() == GetCurrentThreadId()); |
| 201 // Make sure the join_id_ hasn't been set, yet. | 182 // Make sure the join_id_ hasn't been set, yet. |
| 202 DEBUG_ASSERT(thread->join_id_ == kInvalidThreadJoinId); | 183 DEBUG_ASSERT(thread->join_id_ == kInvalidThreadJoinId); |
| 203 pthread_t id = pthread_self(); | 184 pthread_t id = pthread_self(); |
| 204 #if defined(DEBUG) | 185 #if defined(DEBUG) |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 426 |
| 446 void Monitor::NotifyAll() { | 427 void Monitor::NotifyAll() { |
| 447 // When running with assertions enabled we track the owner. | 428 // When running with assertions enabled we track the owner. |
| 448 ASSERT(IsOwnedByCurrentThread()); | 429 ASSERT(IsOwnedByCurrentThread()); |
| 449 int result = pthread_cond_broadcast(data_.cond()); | 430 int result = pthread_cond_broadcast(data_.cond()); |
| 450 VALIDATE_PTHREAD_RESULT(result); | 431 VALIDATE_PTHREAD_RESULT(result); |
| 451 } | 432 } |
| 452 | 433 |
| 453 } // namespace dart | 434 } // namespace dart |
| 454 | 435 |
| 455 #endif // defined(TARGET_OS_LINUX) | 436 #endif // defined(TARGET_OS_FUCHSIA) |
| OLD | NEW |