| 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_ANDROID) | 6 #if defined(TARGET_OS_ANDROID) |
| 7 | 7 |
| 8 #include "vm/thread.h" | 8 #include "vm/os_thread.h" |
| 9 | 9 |
| 10 #include <errno.h> // NOLINT | 10 #include <errno.h> // NOLINT |
| 11 #include <sys/time.h> // NOLINT | 11 #include <sys/time.h> // NOLINT |
| 12 | 12 |
| 13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
| 14 #include "vm/isolate.h" | 14 #include "vm/isolate.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 #define VALIDATE_PTHREAD_RESULT(result) \ | 18 #define VALIDATE_PTHREAD_RESULT(result) \ |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 ts->tv_nsec = (tv.tv_usec + remaining_micros) * kNanosecondsPerMicrosecond; | 50 ts->tv_nsec = (tv.tv_usec + remaining_micros) * kNanosecondsPerMicrosecond; |
| 51 if (ts->tv_nsec >= kNanosecondsPerSecond) { | 51 if (ts->tv_nsec >= kNanosecondsPerSecond) { |
| 52 ts->tv_sec += 1; | 52 ts->tv_sec += 1; |
| 53 ts->tv_nsec -= kNanosecondsPerSecond; | 53 ts->tv_nsec -= kNanosecondsPerSecond; |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 | 57 |
| 58 class ThreadStartData { | 58 class ThreadStartData { |
| 59 public: | 59 public: |
| 60 ThreadStartData(Thread::ThreadStartFunction function, | 60 ThreadStartData(OSThread::ThreadStartFunction function, |
| 61 uword parameter) | 61 uword parameter) |
| 62 : function_(function), parameter_(parameter) {} | 62 : function_(function), parameter_(parameter) {} |
| 63 | 63 |
| 64 Thread::ThreadStartFunction function() const { return function_; } | 64 OSThread::ThreadStartFunction function() const { return function_; } |
| 65 uword parameter() const { return parameter_; } | 65 uword parameter() const { return parameter_; } |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 Thread::ThreadStartFunction function_; | 68 OSThread::ThreadStartFunction function_; |
| 69 uword parameter_; | 69 uword parameter_; |
| 70 | 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 71 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 | 74 |
| 75 // Dispatch to the thread start function provided by the caller. This trampoline | 75 // Dispatch to the thread start function provided by the caller. This trampoline |
| 76 // is used to ensure that the thread is properly destroyed if the thread just | 76 // is used to ensure that the thread is properly destroyed if the thread just |
| 77 // exits. | 77 // exits. |
| 78 static void* ThreadStart(void* data_ptr) { | 78 static void* ThreadStart(void* data_ptr) { |
| 79 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 79 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| 80 | 80 |
| 81 Thread::ThreadStartFunction function = data->function(); | 81 OSThread::ThreadStartFunction function = data->function(); |
| 82 uword parameter = data->parameter(); | 82 uword parameter = data->parameter(); |
| 83 delete data; | 83 delete data; |
| 84 | 84 |
| 85 // Call the supplied thread start function handing it its parameters. | 85 // Call the supplied thread start function handing it its parameters. |
| 86 function(parameter); | 86 function(parameter); |
| 87 | 87 |
| 88 return NULL; | 88 return NULL; |
| 89 } | 89 } |
| 90 | 90 |
| 91 | 91 |
| 92 int Thread::Start(ThreadStartFunction function, uword parameter) { | 92 int OSThread::Start(ThreadStartFunction function, uword parameter) { |
| 93 pthread_attr_t attr; | 93 pthread_attr_t attr; |
| 94 int result = pthread_attr_init(&attr); | 94 int result = pthread_attr_init(&attr); |
| 95 RETURN_ON_PTHREAD_FAILURE(result); | 95 RETURN_ON_PTHREAD_FAILURE(result); |
| 96 | 96 |
| 97 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | 97 result = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
| 98 RETURN_ON_PTHREAD_FAILURE(result); | 98 RETURN_ON_PTHREAD_FAILURE(result); |
| 99 | 99 |
| 100 result = pthread_attr_setstacksize(&attr, Thread::GetMaxStackSize()); | 100 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); |
| 101 RETURN_ON_PTHREAD_FAILURE(result); | 101 RETURN_ON_PTHREAD_FAILURE(result); |
| 102 | 102 |
| 103 ThreadStartData* data = new ThreadStartData(function, parameter); | 103 ThreadStartData* data = new ThreadStartData(function, parameter); |
| 104 | 104 |
| 105 pthread_t tid; | 105 pthread_t tid; |
| 106 result = pthread_create(&tid, &attr, ThreadStart, data); | 106 result = pthread_create(&tid, &attr, ThreadStart, data); |
| 107 RETURN_ON_PTHREAD_FAILURE(result); | 107 RETURN_ON_PTHREAD_FAILURE(result); |
| 108 | 108 |
| 109 result = pthread_attr_destroy(&attr); | 109 result = pthread_attr_destroy(&attr); |
| 110 RETURN_ON_PTHREAD_FAILURE(result); | 110 RETURN_ON_PTHREAD_FAILURE(result); |
| 111 | 111 |
| 112 return 0; | 112 return 0; |
| 113 } | 113 } |
| 114 | 114 |
| 115 | 115 |
| 116 ThreadLocalKey Thread::kUnsetThreadLocalKey = static_cast<pthread_key_t>(-1); | 116 ThreadLocalKey OSThread::kUnsetThreadLocalKey = |
| 117 ThreadId Thread::kInvalidThreadId = static_cast<ThreadId>(0); | 117 static_cast<pthread_key_t>(-1); |
| 118 ThreadId OSThread::kInvalidThreadId = static_cast<ThreadId>(0); |
| 118 | 119 |
| 119 ThreadLocalKey Thread::CreateThreadLocal() { | 120 ThreadLocalKey OSThread::CreateThreadLocal() { |
| 120 pthread_key_t key = kUnsetThreadLocalKey; | 121 pthread_key_t key = kUnsetThreadLocalKey; |
| 121 int result = pthread_key_create(&key, NULL); | 122 int result = pthread_key_create(&key, NULL); |
| 122 VALIDATE_PTHREAD_RESULT(result); | 123 VALIDATE_PTHREAD_RESULT(result); |
| 123 ASSERT(key != kUnsetThreadLocalKey); | 124 ASSERT(key != kUnsetThreadLocalKey); |
| 124 return key; | 125 return key; |
| 125 } | 126 } |
| 126 | 127 |
| 127 | 128 |
| 128 void Thread::DeleteThreadLocal(ThreadLocalKey key) { | 129 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
| 129 ASSERT(key != kUnsetThreadLocalKey); | 130 ASSERT(key != kUnsetThreadLocalKey); |
| 130 int result = pthread_key_delete(key); | 131 int result = pthread_key_delete(key); |
| 131 VALIDATE_PTHREAD_RESULT(result); | 132 VALIDATE_PTHREAD_RESULT(result); |
| 132 } | 133 } |
| 133 | 134 |
| 134 | 135 |
| 135 void Thread::SetThreadLocal(ThreadLocalKey key, uword value) { | 136 void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) { |
| 136 ASSERT(key != kUnsetThreadLocalKey); | 137 ASSERT(key != kUnsetThreadLocalKey); |
| 137 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); | 138 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); |
| 138 VALIDATE_PTHREAD_RESULT(result); | 139 VALIDATE_PTHREAD_RESULT(result); |
| 139 } | 140 } |
| 140 | 141 |
| 141 | 142 |
| 142 intptr_t Thread::GetMaxStackSize() { | 143 intptr_t OSThread::GetMaxStackSize() { |
| 143 const int kStackSize = (128 * kWordSize * KB); | 144 const int kStackSize = (128 * kWordSize * KB); |
| 144 return kStackSize; | 145 return kStackSize; |
| 145 } | 146 } |
| 146 | 147 |
| 147 | 148 |
| 148 ThreadId Thread::GetCurrentThreadId() { | 149 ThreadId OSThread::GetCurrentThreadId() { |
| 149 return gettid(); | 150 return gettid(); |
| 150 } | 151 } |
| 151 | 152 |
| 152 | 153 |
| 153 bool Thread::Join(ThreadId id) { | 154 bool OSThread::Join(ThreadId id) { |
| 154 return false; | 155 return false; |
| 155 } | 156 } |
| 156 | 157 |
| 157 | 158 |
| 158 intptr_t Thread::ThreadIdToIntPtr(ThreadId id) { | 159 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
| 159 ASSERT(sizeof(id) == sizeof(intptr_t)); | 160 ASSERT(sizeof(id) == sizeof(intptr_t)); |
| 160 return static_cast<intptr_t>(id); | 161 return static_cast<intptr_t>(id); |
| 161 } | 162 } |
| 162 | 163 |
| 163 | 164 |
| 164 bool Thread::Compare(ThreadId a, ThreadId b) { | 165 bool OSThread::Compare(ThreadId a, ThreadId b) { |
| 165 return a == b; | 166 return a == b; |
| 166 } | 167 } |
| 167 | 168 |
| 168 | 169 |
| 169 void Thread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 170 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
| 170 ASSERT(thread_id == GetCurrentThreadId()); | 171 ASSERT(thread_id == GetCurrentThreadId()); |
| 171 ASSERT(cpu_usage != NULL); | 172 ASSERT(cpu_usage != NULL); |
| 172 struct timespec ts; | 173 struct timespec ts; |
| 173 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | 174 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 174 ASSERT(r == 0); | 175 ASSERT(r == 0); |
| 175 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / | 176 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / |
| 176 kNanosecondsPerMicrosecond; | 177 kNanosecondsPerMicrosecond; |
| 177 } | 178 } |
| 178 | 179 |
| 179 | 180 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 339 |
| 339 void Monitor::NotifyAll() { | 340 void Monitor::NotifyAll() { |
| 340 // TODO(iposva): Do we need to track lock owners? | 341 // TODO(iposva): Do we need to track lock owners? |
| 341 int result = pthread_cond_broadcast(data_.cond()); | 342 int result = pthread_cond_broadcast(data_.cond()); |
| 342 VALIDATE_PTHREAD_RESULT(result); | 343 VALIDATE_PTHREAD_RESULT(result); |
| 343 } | 344 } |
| 344 | 345 |
| 345 } // namespace dart | 346 } // namespace dart |
| 346 | 347 |
| 347 #endif // defined(TARGET_OS_ANDROID) | 348 #endif // defined(TARGET_OS_ANDROID) |
| OLD | NEW |