| 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" // NOLINT | 5 #include "platform/globals.h" // NOLINT |
| 6 #if defined(TARGET_OS_ANDROID) | 6 #if defined(TARGET_OS_OPENBSD) |
| 7 | 7 |
| 8 #include "vm/os_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 "platform/utils.h" | 14 #include "platform/utils.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 RETURN_ON_PTHREAD_FAILURE(result); | 128 RETURN_ON_PTHREAD_FAILURE(result); |
| 129 | 129 |
| 130 return 0; | 130 return 0; |
| 131 } | 131 } |
| 132 | 132 |
| 133 | 133 |
| 134 const ThreadId OSThread::kInvalidThreadId = static_cast<ThreadId>(0); | 134 const ThreadId OSThread::kInvalidThreadId = static_cast<ThreadId>(0); |
| 135 const ThreadJoinId OSThread::kInvalidThreadJoinId = | 135 const ThreadJoinId OSThread::kInvalidThreadJoinId = |
| 136 static_cast<ThreadJoinId>(0); | 136 static_cast<ThreadJoinId>(0); |
| 137 | 137 |
| 138 | |
| 139 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { | 138 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { |
| 140 pthread_key_t key = kUnsetThreadLocalKey; | 139 pthread_key_t key = kUnsetThreadLocalKey; |
| 141 int result = pthread_key_create(&key, destructor); | 140 int result = pthread_key_create(&key, destructor); |
| 142 VALIDATE_PTHREAD_RESULT(result); | 141 VALIDATE_PTHREAD_RESULT(result); |
| 143 ASSERT(key != kUnsetThreadLocalKey); | 142 ASSERT(key != kUnsetThreadLocalKey); |
| 144 return key; | 143 return key; |
| 145 } | 144 } |
| 146 | 145 |
| 147 | 146 |
| 148 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { | 147 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 159 } | 158 } |
| 160 | 159 |
| 161 | 160 |
| 162 intptr_t OSThread::GetMaxStackSize() { | 161 intptr_t OSThread::GetMaxStackSize() { |
| 163 const int kStackSize = (128 * kWordSize * KB); | 162 const int kStackSize = (128 * kWordSize * KB); |
| 164 return kStackSize; | 163 return kStackSize; |
| 165 } | 164 } |
| 166 | 165 |
| 167 | 166 |
| 168 ThreadId OSThread::GetCurrentThreadId() { | 167 ThreadId OSThread::GetCurrentThreadId() { |
| 169 return gettid(); | 168 return pthread_self(); |
| 170 } | 169 } |
| 171 | 170 |
| 172 | 171 |
| 173 ThreadId OSThread::GetCurrentThreadTraceId() { | 172 ThreadId OSThread::GetCurrentThreadTraceId() { |
| 174 return GetCurrentThreadId(); | 173 return GetCurrentThreadId(); |
| 175 } | 174 } |
| 176 | 175 |
| 177 | 176 |
| 178 ThreadJoinId OSThread::GetCurrentThreadJoinId() { | 177 ThreadJoinId OSThread::GetCurrentThreadJoinId() { |
| 179 return pthread_self(); | 178 return pthread_self(); |
| 180 } | 179 } |
| 181 | 180 |
| 182 | 181 |
| 183 void OSThread::Join(ThreadJoinId id) { | 182 void OSThread::Join(ThreadJoinId id) { |
| 184 int result = pthread_join(id, NULL); | 183 int result = pthread_join(id, NULL); |
| 185 ASSERT(result == 0); | 184 ASSERT(result == 0); |
| 186 } | 185 } |
| 187 | 186 |
| 188 | 187 |
| 189 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { | 188 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
| 190 ASSERT(sizeof(id) == sizeof(intptr_t)); | 189 ASSERT(sizeof(id) == sizeof(intptr_t)); |
| 191 return static_cast<intptr_t>(id); | 190 return reinterpret_cast<intptr_t>(id); |
| 192 } | 191 } |
| 193 | 192 |
| 194 | 193 |
| 195 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { | 194 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { |
| 196 return static_cast<ThreadId>(id); | 195 return reinterpret_cast<ThreadId>(id); |
| 197 } | 196 } |
| 198 | 197 |
| 199 | 198 |
| 200 bool OSThread::Compare(ThreadId a, ThreadId b) { | 199 bool OSThread::Compare(ThreadId a, ThreadId b) { |
| 201 return a == b; | 200 return pthread_equal(a, b) != 0; |
| 202 } | 201 } |
| 203 | 202 |
| 204 | 203 |
| 205 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { | 204 void OSThread::GetThreadCpuUsage(ThreadId thread_id, int64_t* cpu_usage) { |
| 206 ASSERT(thread_id == GetCurrentThreadId()); | 205 ASSERT(thread_id == GetCurrentThreadId()); |
| 207 ASSERT(cpu_usage != NULL); | 206 ASSERT(cpu_usage != NULL); |
| 208 struct timespec ts; | 207 struct timespec ts; |
| 209 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); | 208 int r = clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); |
| 210 ASSERT(r == 0); | 209 ASSERT(r == 0); |
| 211 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / | 210 *cpu_usage = (ts.tv_sec * kNanosecondsPerSecond + ts.tv_nsec) / |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 VALIDATE_PTHREAD_RESULT(result); | 222 VALIDATE_PTHREAD_RESULT(result); |
| 224 #endif // defined(DEBUG) | 223 #endif // defined(DEBUG) |
| 225 | 224 |
| 226 result = pthread_mutex_init(data_.mutex(), &attr); | 225 result = pthread_mutex_init(data_.mutex(), &attr); |
| 227 // Verify that creating a pthread_mutex succeeded. | 226 // Verify that creating a pthread_mutex succeeded. |
| 228 VALIDATE_PTHREAD_RESULT(result); | 227 VALIDATE_PTHREAD_RESULT(result); |
| 229 | 228 |
| 230 result = pthread_mutexattr_destroy(&attr); | 229 result = pthread_mutexattr_destroy(&attr); |
| 231 VALIDATE_PTHREAD_RESULT(result); | 230 VALIDATE_PTHREAD_RESULT(result); |
| 232 | 231 |
| 232 // When running with assertions enabled we do track the owner. |
| 233 #if defined(DEBUG) | 233 #if defined(DEBUG) |
| 234 // When running with assertions enabled we do track the owner. | |
| 235 owner_ = OSThread::kInvalidThreadId; | 234 owner_ = OSThread::kInvalidThreadId; |
| 236 #endif // defined(DEBUG) | 235 #endif // defined(DEBUG) |
| 237 } | 236 } |
| 238 | 237 |
| 239 | 238 |
| 240 Mutex::~Mutex() { | 239 Mutex::~Mutex() { |
| 241 int result = pthread_mutex_destroy(data_.mutex()); | 240 int result = pthread_mutex_destroy(data_.mutex()); |
| 242 // Verify that the pthread_mutex was destroyed. | 241 // Verify that the pthread_mutex was destroyed. |
| 243 VALIDATE_PTHREAD_RESULT(result); | 242 VALIDATE_PTHREAD_RESULT(result); |
| 244 | 243 |
| 244 // When running with assertions enabled we do track the owner. |
| 245 #if defined(DEBUG) | 245 #if defined(DEBUG) |
| 246 // When running with assertions enabled we do track the owner. | |
| 247 ASSERT(owner_ == OSThread::kInvalidThreadId); | 246 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 248 #endif // defined(DEBUG) | 247 #endif // defined(DEBUG) |
| 249 } | 248 } |
| 250 | 249 |
| 251 | 250 |
| 252 void Mutex::Lock() { | 251 void Mutex::Lock() { |
| 253 int result = pthread_mutex_lock(data_.mutex()); | 252 int result = pthread_mutex_lock(data_.mutex()); |
| 254 // Specifically check for dead lock to help debugging. | 253 // Specifically check for dead lock to help debugging. |
| 255 ASSERT(result != EDEADLK); | 254 ASSERT(result != EDEADLK); |
| 256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 255 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 256 // When running with assertions enabled we do track the owner. |
| 257 #if defined(DEBUG) | 257 #if defined(DEBUG) |
| 258 // When running with assertions enabled we do track the owner. | |
| 259 owner_ = OSThread::GetCurrentThreadId(); | 258 owner_ = OSThread::GetCurrentThreadId(); |
| 260 #endif // defined(DEBUG) | 259 #endif // defined(DEBUG) |
| 261 } | 260 } |
| 262 | 261 |
| 263 | 262 |
| 264 bool Mutex::TryLock() { | 263 bool Mutex::TryLock() { |
| 265 int result = pthread_mutex_trylock(data_.mutex()); | 264 int result = pthread_mutex_trylock(data_.mutex()); |
| 266 // Return false if the lock is busy and locking failed. | 265 // Return false if the lock is busy and locking failed. |
| 267 if (result == EBUSY) { | 266 if (result == EBUSY) { |
| 268 return false; | 267 return false; |
| 269 } | 268 } |
| 270 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 269 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 270 // When running with assertions enabled we do track the owner. |
| 271 #if defined(DEBUG) | 271 #if defined(DEBUG) |
| 272 // When running with assertions enabled we do track the owner. | |
| 273 owner_ = OSThread::GetCurrentThreadId(); | 272 owner_ = OSThread::GetCurrentThreadId(); |
| 274 #endif // defined(DEBUG) | 273 #endif // defined(DEBUG) |
| 275 return true; | 274 return true; |
| 276 } | 275 } |
| 277 | 276 |
| 278 | 277 |
| 279 void Mutex::Unlock() { | 278 void Mutex::Unlock() { |
| 279 // When running with assertions enabled we do track the owner. |
| 280 #if defined(DEBUG) | 280 #if defined(DEBUG) |
| 281 // When running with assertions enabled we do track the owner. | |
| 282 ASSERT(IsOwnedByCurrentThread()); | 281 ASSERT(IsOwnedByCurrentThread()); |
| 283 owner_ = OSThread::kInvalidThreadId; | 282 owner_ = OSThread::kInvalidThreadId; |
| 284 #endif // defined(DEBUG) | 283 #endif // defined(DEBUG) |
| 285 int result = pthread_mutex_unlock(data_.mutex()); | 284 int result = pthread_mutex_unlock(data_.mutex()); |
| 286 // Specifically check for wrong thread unlocking to aid debugging. | 285 // Specifically check for wrong thread unlocking to aid debugging. |
| 287 ASSERT(result != EPERM); | 286 ASSERT(result != EPERM); |
| 288 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 287 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 289 } | 288 } |
| 290 | 289 |
| 291 | 290 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 | 407 |
| 409 void Monitor::NotifyAll() { | 408 void Monitor::NotifyAll() { |
| 410 // When running with assertions enabled we track the owner. | 409 // When running with assertions enabled we track the owner. |
| 411 ASSERT(IsOwnedByCurrentThread()); | 410 ASSERT(IsOwnedByCurrentThread()); |
| 412 int result = pthread_cond_broadcast(data_.cond()); | 411 int result = pthread_cond_broadcast(data_.cond()); |
| 413 VALIDATE_PTHREAD_RESULT(result); | 412 VALIDATE_PTHREAD_RESULT(result); |
| 414 } | 413 } |
| 415 | 414 |
| 416 } // namespace dart | 415 } // namespace dart |
| 417 | 416 |
| 418 #endif // defined(TARGET_OS_ANDROID) | 417 #endif // defined(TARGET_OS_OPENBSD) |
| OLD | NEW |