| OLD | NEW |
| 1 // Copyright (c) 2016, 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 #if defined(HOST_OS_FUCHSIA) | 6 #if defined(HOST_OS_FUCHSIA) |
| 7 | 7 |
| 8 #include "vm/os.h" | 8 #include "vm/os.h" |
| 9 #include "vm/os_thread.h" | 9 #include "vm/os_thread.h" |
| 10 #include "vm/os_thread_fuchsia.h" | 10 #include "vm/os_thread_fuchsia.h" |
| 11 | 11 |
| 12 #include <errno.h> // NOLINT | 12 #include <errno.h> // NOLINT |
| 13 #include <magenta/status.h> | 13 #include <magenta/status.h> |
| 14 #include <magenta/syscalls.h> | 14 #include <magenta/syscalls.h> |
| 15 #include <magenta/syscalls/object.h> | 15 #include <magenta/syscalls/object.h> |
| 16 #include <magenta/threads.h> | 16 #include <magenta/threads.h> |
| 17 #include <magenta/types.h> | 17 #include <magenta/types.h> |
| 18 | 18 |
| 19 #include "platform/assert.h" | 19 #include "platform/assert.h" |
| 20 | 20 |
| 21 namespace dart { | 21 namespace dart { |
| 22 | 22 |
| 23 #define VALIDATE_PTHREAD_RESULT(result) \ | 23 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 24 if (result != 0) { \ | 24 if (result != 0) { \ |
| 25 FATAL1("pthread error: %d", result); \ | 25 FATAL1("pthread error: %d", result); \ |
| 26 } | 26 } |
| 27 | 27 |
| 28 | |
| 29 #if defined(DEBUG) | 28 #if defined(DEBUG) |
| 30 #define ASSERT_PTHREAD_SUCCESS(result) VALIDATE_PTHREAD_RESULT(result) | 29 #define ASSERT_PTHREAD_SUCCESS(result) VALIDATE_PTHREAD_RESULT(result) |
| 31 #else | 30 #else |
| 32 // NOTE: This (currently) expands to a no-op. | 31 // NOTE: This (currently) expands to a no-op. |
| 33 #define ASSERT_PTHREAD_SUCCESS(result) ASSERT(result == 0) | 32 #define ASSERT_PTHREAD_SUCCESS(result) ASSERT(result == 0) |
| 34 #endif | 33 #endif |
| 35 | 34 |
| 36 | |
| 37 #ifdef DEBUG | 35 #ifdef DEBUG |
| 38 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 36 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 39 if (result != 0) { \ | 37 if (result != 0) { \ |
| 40 fprintf(stderr, "%s:%d: pthread error: %d\n", __FILE__, __LINE__, result); \ | 38 fprintf(stderr, "%s:%d: pthread error: %d\n", __FILE__, __LINE__, result); \ |
| 41 return result; \ | 39 return result; \ |
| 42 } | 40 } |
| 43 #else | 41 #else |
| 44 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 42 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
| 45 if (result != 0) return result; | 43 if (result != 0) return result; |
| 46 #endif | 44 #endif |
| 47 | 45 |
| 48 | |
| 49 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { | 46 static void ComputeTimeSpecMicros(struct timespec* ts, int64_t micros) { |
| 50 // time in nanoseconds. | 47 // time in nanoseconds. |
| 51 mx_time_t now = mx_time_get(MX_CLOCK_MONOTONIC); | 48 mx_time_t now = mx_time_get(MX_CLOCK_MONOTONIC); |
| 52 mx_time_t target = now + (micros * kNanosecondsPerMicrosecond); | 49 mx_time_t target = now + (micros * kNanosecondsPerMicrosecond); |
| 53 int64_t secs = target / kNanosecondsPerSecond; | 50 int64_t secs = target / kNanosecondsPerSecond; |
| 54 int64_t nanos = target - (secs * kNanosecondsPerSecond); | 51 int64_t nanos = target - (secs * kNanosecondsPerSecond); |
| 55 | 52 |
| 56 ts->tv_sec = secs; | 53 ts->tv_sec = secs; |
| 57 ts->tv_nsec = nanos; | 54 ts->tv_nsec = nanos; |
| 58 } | 55 } |
| 59 | 56 |
| 60 | |
| 61 class ThreadStartData { | 57 class ThreadStartData { |
| 62 public: | 58 public: |
| 63 ThreadStartData(const char* name, | 59 ThreadStartData(const char* name, |
| 64 OSThread::ThreadStartFunction function, | 60 OSThread::ThreadStartFunction function, |
| 65 uword parameter) | 61 uword parameter) |
| 66 : name_(name), function_(function), parameter_(parameter) {} | 62 : name_(name), function_(function), parameter_(parameter) {} |
| 67 | 63 |
| 68 const char* name() const { return name_; } | 64 const char* name() const { return name_; } |
| 69 OSThread::ThreadStartFunction function() const { return function_; } | 65 OSThread::ThreadStartFunction function() const { return function_; } |
| 70 uword parameter() const { return parameter_; } | 66 uword parameter() const { return parameter_; } |
| 71 | 67 |
| 72 private: | 68 private: |
| 73 const char* name_; | 69 const char* name_; |
| 74 OSThread::ThreadStartFunction function_; | 70 OSThread::ThreadStartFunction function_; |
| 75 uword parameter_; | 71 uword parameter_; |
| 76 | 72 |
| 77 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); | 73 DISALLOW_COPY_AND_ASSIGN(ThreadStartData); |
| 78 }; | 74 }; |
| 79 | 75 |
| 80 | |
| 81 // Dispatch to the thread start function provided by the caller. This trampoline | 76 // Dispatch to the thread start function provided by the caller. This trampoline |
| 82 // is used to ensure that the thread is properly destroyed if the thread just | 77 // is used to ensure that the thread is properly destroyed if the thread just |
| 83 // exits. | 78 // exits. |
| 84 static void* ThreadStart(void* data_ptr) { | 79 static void* ThreadStart(void* data_ptr) { |
| 85 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); | 80 ThreadStartData* data = reinterpret_cast<ThreadStartData*>(data_ptr); |
| 86 | 81 |
| 87 const char* name = data->name(); | 82 const char* name = data->name(); |
| 88 OSThread::ThreadStartFunction function = data->function(); | 83 OSThread::ThreadStartFunction function = data->function(); |
| 89 uword parameter = data->parameter(); | 84 uword parameter = data->parameter(); |
| 90 delete data; | 85 delete data; |
| 91 | 86 |
| 92 // Create new OSThread object and set as TLS for new thread. | 87 // Create new OSThread object and set as TLS for new thread. |
| 93 OSThread* thread = OSThread::CreateOSThread(); | 88 OSThread* thread = OSThread::CreateOSThread(); |
| 94 if (thread != NULL) { | 89 if (thread != NULL) { |
| 95 OSThread::SetCurrent(thread); | 90 OSThread::SetCurrent(thread); |
| 96 thread->set_name(name); | 91 thread->set_name(name); |
| 97 // Call the supplied thread start function handing it its parameters. | 92 // Call the supplied thread start function handing it its parameters. |
| 98 function(parameter); | 93 function(parameter); |
| 99 } | 94 } |
| 100 | 95 |
| 101 return NULL; | 96 return NULL; |
| 102 } | 97 } |
| 103 | 98 |
| 104 | |
| 105 int OSThread::Start(const char* name, | 99 int OSThread::Start(const char* name, |
| 106 ThreadStartFunction function, | 100 ThreadStartFunction function, |
| 107 uword parameter) { | 101 uword parameter) { |
| 108 pthread_attr_t attr; | 102 pthread_attr_t attr; |
| 109 int result = pthread_attr_init(&attr); | 103 int result = pthread_attr_init(&attr); |
| 110 RETURN_ON_PTHREAD_FAILURE(result); | 104 RETURN_ON_PTHREAD_FAILURE(result); |
| 111 | 105 |
| 112 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); | 106 result = pthread_attr_setstacksize(&attr, OSThread::GetMaxStackSize()); |
| 113 RETURN_ON_PTHREAD_FAILURE(result); | 107 RETURN_ON_PTHREAD_FAILURE(result); |
| 114 | 108 |
| 115 ThreadStartData* data = new ThreadStartData(name, function, parameter); | 109 ThreadStartData* data = new ThreadStartData(name, function, parameter); |
| 116 | 110 |
| 117 pthread_t tid; | 111 pthread_t tid; |
| 118 result = pthread_create(&tid, &attr, ThreadStart, data); | 112 result = pthread_create(&tid, &attr, ThreadStart, data); |
| 119 RETURN_ON_PTHREAD_FAILURE(result); | 113 RETURN_ON_PTHREAD_FAILURE(result); |
| 120 | 114 |
| 121 result = pthread_attr_destroy(&attr); | 115 result = pthread_attr_destroy(&attr); |
| 122 RETURN_ON_PTHREAD_FAILURE(result); | 116 RETURN_ON_PTHREAD_FAILURE(result); |
| 123 | 117 |
| 124 return 0; | 118 return 0; |
| 125 } | 119 } |
| 126 | 120 |
| 127 | |
| 128 const ThreadId OSThread::kInvalidThreadId = MX_KOID_INVALID; | 121 const ThreadId OSThread::kInvalidThreadId = MX_KOID_INVALID; |
| 129 const ThreadJoinId OSThread::kInvalidThreadJoinId = | 122 const ThreadJoinId OSThread::kInvalidThreadJoinId = |
| 130 static_cast<ThreadJoinId>(0); | 123 static_cast<ThreadJoinId>(0); |
| 131 | 124 |
| 132 | |
| 133 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { | 125 ThreadLocalKey OSThread::CreateThreadLocal(ThreadDestructor destructor) { |
| 134 pthread_key_t key = kUnsetThreadLocalKey; | 126 pthread_key_t key = kUnsetThreadLocalKey; |
| 135 int result = pthread_key_create(&key, destructor); | 127 int result = pthread_key_create(&key, destructor); |
| 136 VALIDATE_PTHREAD_RESULT(result); | 128 VALIDATE_PTHREAD_RESULT(result); |
| 137 ASSERT(key != kUnsetThreadLocalKey); | 129 ASSERT(key != kUnsetThreadLocalKey); |
| 138 return key; | 130 return key; |
| 139 } | 131 } |
| 140 | 132 |
| 141 | |
| 142 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { | 133 void OSThread::DeleteThreadLocal(ThreadLocalKey key) { |
| 143 ASSERT(key != kUnsetThreadLocalKey); | 134 ASSERT(key != kUnsetThreadLocalKey); |
| 144 int result = pthread_key_delete(key); | 135 int result = pthread_key_delete(key); |
| 145 VALIDATE_PTHREAD_RESULT(result); | 136 VALIDATE_PTHREAD_RESULT(result); |
| 146 } | 137 } |
| 147 | 138 |
| 148 | |
| 149 void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) { | 139 void OSThread::SetThreadLocal(ThreadLocalKey key, uword value) { |
| 150 ASSERT(key != kUnsetThreadLocalKey); | 140 ASSERT(key != kUnsetThreadLocalKey); |
| 151 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); | 141 int result = pthread_setspecific(key, reinterpret_cast<void*>(value)); |
| 152 VALIDATE_PTHREAD_RESULT(result); | 142 VALIDATE_PTHREAD_RESULT(result); |
| 153 } | 143 } |
| 154 | 144 |
| 155 | |
| 156 intptr_t OSThread::GetMaxStackSize() { | 145 intptr_t OSThread::GetMaxStackSize() { |
| 157 const int kStackSize = (128 * kWordSize * KB); | 146 const int kStackSize = (128 * kWordSize * KB); |
| 158 return kStackSize; | 147 return kStackSize; |
| 159 } | 148 } |
| 160 | 149 |
| 161 | |
| 162 ThreadId OSThread::GetCurrentThreadId() { | 150 ThreadId OSThread::GetCurrentThreadId() { |
| 163 mx_info_handle_basic_t info; | 151 mx_info_handle_basic_t info; |
| 164 mx_handle_t thread_handle = thrd_get_mx_handle(thrd_current()); | 152 mx_handle_t thread_handle = thrd_get_mx_handle(thrd_current()); |
| 165 mx_status_t status = | 153 mx_status_t status = |
| 166 mx_object_get_info(thread_handle, MX_INFO_HANDLE_BASIC, &info, | 154 mx_object_get_info(thread_handle, MX_INFO_HANDLE_BASIC, &info, |
| 167 sizeof(info), nullptr, nullptr); | 155 sizeof(info), nullptr, nullptr); |
| 168 if (status != MX_OK) { | 156 if (status != MX_OK) { |
| 169 FATAL1("Failed to get thread koid: %s\n", mx_status_get_string(status)); | 157 FATAL1("Failed to get thread koid: %s\n", mx_status_get_string(status)); |
| 170 } | 158 } |
| 171 return info.koid; | 159 return info.koid; |
| 172 } | 160 } |
| 173 | 161 |
| 174 | |
| 175 #ifndef PRODUCT | 162 #ifndef PRODUCT |
| 176 ThreadId OSThread::GetCurrentThreadTraceId() { | 163 ThreadId OSThread::GetCurrentThreadTraceId() { |
| 177 return pthread_self(); | 164 return pthread_self(); |
| 178 } | 165 } |
| 179 #endif // PRODUCT | 166 #endif // PRODUCT |
| 180 | 167 |
| 181 | |
| 182 ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) { | 168 ThreadJoinId OSThread::GetCurrentThreadJoinId(OSThread* thread) { |
| 183 ASSERT(thread != NULL); | 169 ASSERT(thread != NULL); |
| 184 // Make sure we're filling in the join id for the current thread. | 170 // Make sure we're filling in the join id for the current thread. |
| 185 ASSERT(thread->id() == GetCurrentThreadId()); | 171 ASSERT(thread->id() == GetCurrentThreadId()); |
| 186 // Make sure the join_id_ hasn't been set, yet. | 172 // Make sure the join_id_ hasn't been set, yet. |
| 187 DEBUG_ASSERT(thread->join_id_ == kInvalidThreadJoinId); | 173 DEBUG_ASSERT(thread->join_id_ == kInvalidThreadJoinId); |
| 188 pthread_t id = pthread_self(); | 174 pthread_t id = pthread_self(); |
| 189 #if defined(DEBUG) | 175 #if defined(DEBUG) |
| 190 thread->join_id_ = id; | 176 thread->join_id_ = id; |
| 191 #endif | 177 #endif |
| 192 return id; | 178 return id; |
| 193 } | 179 } |
| 194 | 180 |
| 195 | |
| 196 void OSThread::Join(ThreadJoinId id) { | 181 void OSThread::Join(ThreadJoinId id) { |
| 197 int result = pthread_join(id, NULL); | 182 int result = pthread_join(id, NULL); |
| 198 ASSERT(result == 0); | 183 ASSERT(result == 0); |
| 199 } | 184 } |
| 200 | 185 |
| 201 | |
| 202 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { | 186 intptr_t OSThread::ThreadIdToIntPtr(ThreadId id) { |
| 203 ASSERT(sizeof(id) == sizeof(intptr_t)); | 187 ASSERT(sizeof(id) == sizeof(intptr_t)); |
| 204 return static_cast<intptr_t>(id); | 188 return static_cast<intptr_t>(id); |
| 205 } | 189 } |
| 206 | 190 |
| 207 | |
| 208 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { | 191 ThreadId OSThread::ThreadIdFromIntPtr(intptr_t id) { |
| 209 return static_cast<ThreadId>(id); | 192 return static_cast<ThreadId>(id); |
| 210 } | 193 } |
| 211 | 194 |
| 212 | |
| 213 bool OSThread::Compare(ThreadId a, ThreadId b) { | 195 bool OSThread::Compare(ThreadId a, ThreadId b) { |
| 214 return pthread_equal(a, b) != 0; | 196 return pthread_equal(a, b) != 0; |
| 215 } | 197 } |
| 216 | 198 |
| 217 | |
| 218 bool OSThread::GetCurrentStackBounds(uword* lower, uword* upper) { | 199 bool OSThread::GetCurrentStackBounds(uword* lower, uword* upper) { |
| 219 return false; | 200 return false; |
| 220 } | 201 } |
| 221 | 202 |
| 222 | |
| 223 Mutex::Mutex() { | 203 Mutex::Mutex() { |
| 224 pthread_mutexattr_t attr; | 204 pthread_mutexattr_t attr; |
| 225 int result = pthread_mutexattr_init(&attr); | 205 int result = pthread_mutexattr_init(&attr); |
| 226 VALIDATE_PTHREAD_RESULT(result); | 206 VALIDATE_PTHREAD_RESULT(result); |
| 227 | 207 |
| 228 #if defined(DEBUG) | 208 #if defined(DEBUG) |
| 229 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); | 209 result = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK); |
| 230 VALIDATE_PTHREAD_RESULT(result); | 210 VALIDATE_PTHREAD_RESULT(result); |
| 231 #endif // defined(DEBUG) | 211 #endif // defined(DEBUG) |
| 232 | 212 |
| 233 result = pthread_mutex_init(data_.mutex(), &attr); | 213 result = pthread_mutex_init(data_.mutex(), &attr); |
| 234 // Verify that creating a pthread_mutex succeeded. | 214 // Verify that creating a pthread_mutex succeeded. |
| 235 VALIDATE_PTHREAD_RESULT(result); | 215 VALIDATE_PTHREAD_RESULT(result); |
| 236 | 216 |
| 237 result = pthread_mutexattr_destroy(&attr); | 217 result = pthread_mutexattr_destroy(&attr); |
| 238 VALIDATE_PTHREAD_RESULT(result); | 218 VALIDATE_PTHREAD_RESULT(result); |
| 239 | 219 |
| 240 #if defined(DEBUG) | 220 #if defined(DEBUG) |
| 241 // When running with assertions enabled we track the owner. | 221 // When running with assertions enabled we track the owner. |
| 242 owner_ = OSThread::kInvalidThreadId; | 222 owner_ = OSThread::kInvalidThreadId; |
| 243 #endif // defined(DEBUG) | 223 #endif // defined(DEBUG) |
| 244 } | 224 } |
| 245 | 225 |
| 246 | |
| 247 Mutex::~Mutex() { | 226 Mutex::~Mutex() { |
| 248 int result = pthread_mutex_destroy(data_.mutex()); | 227 int result = pthread_mutex_destroy(data_.mutex()); |
| 249 // Verify that the pthread_mutex was destroyed. | 228 // Verify that the pthread_mutex was destroyed. |
| 250 VALIDATE_PTHREAD_RESULT(result); | 229 VALIDATE_PTHREAD_RESULT(result); |
| 251 | 230 |
| 252 #if defined(DEBUG) | 231 #if defined(DEBUG) |
| 253 // When running with assertions enabled we track the owner. | 232 // When running with assertions enabled we track the owner. |
| 254 ASSERT(owner_ == OSThread::kInvalidThreadId); | 233 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 255 #endif // defined(DEBUG) | 234 #endif // defined(DEBUG) |
| 256 } | 235 } |
| 257 | 236 |
| 258 | |
| 259 void Mutex::Lock() { | 237 void Mutex::Lock() { |
| 260 int result = pthread_mutex_lock(data_.mutex()); | 238 int result = pthread_mutex_lock(data_.mutex()); |
| 261 // Specifically check for dead lock to help debugging. | 239 // Specifically check for dead lock to help debugging. |
| 262 ASSERT(result != EDEADLK); | 240 ASSERT(result != EDEADLK); |
| 263 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 241 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 264 #if defined(DEBUG) | 242 #if defined(DEBUG) |
| 265 // When running with assertions enabled we track the owner. | 243 // When running with assertions enabled we track the owner. |
| 266 owner_ = OSThread::GetCurrentThreadId(); | 244 owner_ = OSThread::GetCurrentThreadId(); |
| 267 #endif // defined(DEBUG) | 245 #endif // defined(DEBUG) |
| 268 } | 246 } |
| 269 | 247 |
| 270 | |
| 271 bool Mutex::TryLock() { | 248 bool Mutex::TryLock() { |
| 272 int result = pthread_mutex_trylock(data_.mutex()); | 249 int result = pthread_mutex_trylock(data_.mutex()); |
| 273 // Return false if the lock is busy and locking failed. | 250 // Return false if the lock is busy and locking failed. |
| 274 if (result == EBUSY) { | 251 if (result == EBUSY) { |
| 275 return false; | 252 return false; |
| 276 } | 253 } |
| 277 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 254 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 278 #if defined(DEBUG) | 255 #if defined(DEBUG) |
| 279 // When running with assertions enabled we track the owner. | 256 // When running with assertions enabled we track the owner. |
| 280 owner_ = OSThread::GetCurrentThreadId(); | 257 owner_ = OSThread::GetCurrentThreadId(); |
| 281 #endif // defined(DEBUG) | 258 #endif // defined(DEBUG) |
| 282 return true; | 259 return true; |
| 283 } | 260 } |
| 284 | 261 |
| 285 | |
| 286 void Mutex::Unlock() { | 262 void Mutex::Unlock() { |
| 287 #if defined(DEBUG) | 263 #if defined(DEBUG) |
| 288 // When running with assertions enabled we track the owner. | 264 // When running with assertions enabled we track the owner. |
| 289 ASSERT(IsOwnedByCurrentThread()); | 265 ASSERT(IsOwnedByCurrentThread()); |
| 290 owner_ = OSThread::kInvalidThreadId; | 266 owner_ = OSThread::kInvalidThreadId; |
| 291 #endif // defined(DEBUG) | 267 #endif // defined(DEBUG) |
| 292 int result = pthread_mutex_unlock(data_.mutex()); | 268 int result = pthread_mutex_unlock(data_.mutex()); |
| 293 // Specifically check for wrong thread unlocking to aid debugging. | 269 // Specifically check for wrong thread unlocking to aid debugging. |
| 294 ASSERT(result != EPERM); | 270 ASSERT(result != EPERM); |
| 295 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 271 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 296 } | 272 } |
| 297 | 273 |
| 298 | |
| 299 Monitor::Monitor() { | 274 Monitor::Monitor() { |
| 300 pthread_mutexattr_t mutex_attr; | 275 pthread_mutexattr_t mutex_attr; |
| 301 int result = pthread_mutexattr_init(&mutex_attr); | 276 int result = pthread_mutexattr_init(&mutex_attr); |
| 302 VALIDATE_PTHREAD_RESULT(result); | 277 VALIDATE_PTHREAD_RESULT(result); |
| 303 | 278 |
| 304 #if defined(DEBUG) | 279 #if defined(DEBUG) |
| 305 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); | 280 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); |
| 306 VALIDATE_PTHREAD_RESULT(result); | 281 VALIDATE_PTHREAD_RESULT(result); |
| 307 #endif // defined(DEBUG) | 282 #endif // defined(DEBUG) |
| 308 | 283 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 324 | 299 |
| 325 result = pthread_condattr_destroy(&cond_attr); | 300 result = pthread_condattr_destroy(&cond_attr); |
| 326 VALIDATE_PTHREAD_RESULT(result); | 301 VALIDATE_PTHREAD_RESULT(result); |
| 327 | 302 |
| 328 #if defined(DEBUG) | 303 #if defined(DEBUG) |
| 329 // When running with assertions enabled we track the owner. | 304 // When running with assertions enabled we track the owner. |
| 330 owner_ = OSThread::kInvalidThreadId; | 305 owner_ = OSThread::kInvalidThreadId; |
| 331 #endif // defined(DEBUG) | 306 #endif // defined(DEBUG) |
| 332 } | 307 } |
| 333 | 308 |
| 334 | |
| 335 Monitor::~Monitor() { | 309 Monitor::~Monitor() { |
| 336 #if defined(DEBUG) | 310 #if defined(DEBUG) |
| 337 // When running with assertions enabled we track the owner. | 311 // When running with assertions enabled we track the owner. |
| 338 ASSERT(owner_ == OSThread::kInvalidThreadId); | 312 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 339 #endif // defined(DEBUG) | 313 #endif // defined(DEBUG) |
| 340 | 314 |
| 341 int result = pthread_mutex_destroy(data_.mutex()); | 315 int result = pthread_mutex_destroy(data_.mutex()); |
| 342 VALIDATE_PTHREAD_RESULT(result); | 316 VALIDATE_PTHREAD_RESULT(result); |
| 343 | 317 |
| 344 result = pthread_cond_destroy(data_.cond()); | 318 result = pthread_cond_destroy(data_.cond()); |
| 345 VALIDATE_PTHREAD_RESULT(result); | 319 VALIDATE_PTHREAD_RESULT(result); |
| 346 } | 320 } |
| 347 | 321 |
| 348 | |
| 349 bool Monitor::TryEnter() { | 322 bool Monitor::TryEnter() { |
| 350 int result = pthread_mutex_trylock(data_.mutex()); | 323 int result = pthread_mutex_trylock(data_.mutex()); |
| 351 // Return false if the lock is busy and locking failed. | 324 // Return false if the lock is busy and locking failed. |
| 352 if (result == EBUSY) { | 325 if (result == EBUSY) { |
| 353 return false; | 326 return false; |
| 354 } | 327 } |
| 355 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 328 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 356 #if defined(DEBUG) | 329 #if defined(DEBUG) |
| 357 // When running with assertions enabled we track the owner. | 330 // When running with assertions enabled we track the owner. |
| 358 ASSERT(owner_ == OSThread::kInvalidThreadId); | 331 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 359 owner_ = OSThread::GetCurrentThreadId(); | 332 owner_ = OSThread::GetCurrentThreadId(); |
| 360 #endif // defined(DEBUG) | 333 #endif // defined(DEBUG) |
| 361 return true; | 334 return true; |
| 362 } | 335 } |
| 363 | 336 |
| 364 | |
| 365 void Monitor::Enter() { | 337 void Monitor::Enter() { |
| 366 int result = pthread_mutex_lock(data_.mutex()); | 338 int result = pthread_mutex_lock(data_.mutex()); |
| 367 VALIDATE_PTHREAD_RESULT(result); | 339 VALIDATE_PTHREAD_RESULT(result); |
| 368 | 340 |
| 369 #if defined(DEBUG) | 341 #if defined(DEBUG) |
| 370 // When running with assertions enabled we track the owner. | 342 // When running with assertions enabled we track the owner. |
| 371 ASSERT(owner_ == OSThread::kInvalidThreadId); | 343 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 372 owner_ = OSThread::GetCurrentThreadId(); | 344 owner_ = OSThread::GetCurrentThreadId(); |
| 373 #endif // defined(DEBUG) | 345 #endif // defined(DEBUG) |
| 374 } | 346 } |
| 375 | 347 |
| 376 | |
| 377 void Monitor::Exit() { | 348 void Monitor::Exit() { |
| 378 #if defined(DEBUG) | 349 #if defined(DEBUG) |
| 379 // When running with assertions enabled we track the owner. | 350 // When running with assertions enabled we track the owner. |
| 380 ASSERT(IsOwnedByCurrentThread()); | 351 ASSERT(IsOwnedByCurrentThread()); |
| 381 owner_ = OSThread::kInvalidThreadId; | 352 owner_ = OSThread::kInvalidThreadId; |
| 382 #endif // defined(DEBUG) | 353 #endif // defined(DEBUG) |
| 383 | 354 |
| 384 int result = pthread_mutex_unlock(data_.mutex()); | 355 int result = pthread_mutex_unlock(data_.mutex()); |
| 385 VALIDATE_PTHREAD_RESULT(result); | 356 VALIDATE_PTHREAD_RESULT(result); |
| 386 } | 357 } |
| 387 | 358 |
| 388 | |
| 389 Monitor::WaitResult Monitor::Wait(int64_t millis) { | 359 Monitor::WaitResult Monitor::Wait(int64_t millis) { |
| 390 Monitor::WaitResult retval = WaitMicros(millis * kMicrosecondsPerMillisecond); | 360 Monitor::WaitResult retval = WaitMicros(millis * kMicrosecondsPerMillisecond); |
| 391 return retval; | 361 return retval; |
| 392 } | 362 } |
| 393 | 363 |
| 394 | |
| 395 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { | 364 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { |
| 396 #if defined(DEBUG) | 365 #if defined(DEBUG) |
| 397 // When running with assertions enabled we track the owner. | 366 // When running with assertions enabled we track the owner. |
| 398 ASSERT(IsOwnedByCurrentThread()); | 367 ASSERT(IsOwnedByCurrentThread()); |
| 399 ThreadId saved_owner = owner_; | 368 ThreadId saved_owner = owner_; |
| 400 owner_ = OSThread::kInvalidThreadId; | 369 owner_ = OSThread::kInvalidThreadId; |
| 401 #endif // defined(DEBUG) | 370 #endif // defined(DEBUG) |
| 402 | 371 |
| 403 Monitor::WaitResult retval = kNotified; | 372 Monitor::WaitResult retval = kNotified; |
| 404 if (micros == kNoTimeout) { | 373 if (micros == kNoTimeout) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 417 | 386 |
| 418 #if defined(DEBUG) | 387 #if defined(DEBUG) |
| 419 // When running with assertions enabled we track the owner. | 388 // When running with assertions enabled we track the owner. |
| 420 ASSERT(owner_ == OSThread::kInvalidThreadId); | 389 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 421 owner_ = OSThread::GetCurrentThreadId(); | 390 owner_ = OSThread::GetCurrentThreadId(); |
| 422 ASSERT(owner_ == saved_owner); | 391 ASSERT(owner_ == saved_owner); |
| 423 #endif // defined(DEBUG) | 392 #endif // defined(DEBUG) |
| 424 return retval; | 393 return retval; |
| 425 } | 394 } |
| 426 | 395 |
| 427 | |
| 428 void Monitor::Notify() { | 396 void Monitor::Notify() { |
| 429 // When running with assertions enabled we track the owner. | 397 // When running with assertions enabled we track the owner. |
| 430 ASSERT(IsOwnedByCurrentThread()); | 398 ASSERT(IsOwnedByCurrentThread()); |
| 431 int result = pthread_cond_signal(data_.cond()); | 399 int result = pthread_cond_signal(data_.cond()); |
| 432 VALIDATE_PTHREAD_RESULT(result); | 400 VALIDATE_PTHREAD_RESULT(result); |
| 433 } | 401 } |
| 434 | 402 |
| 435 | |
| 436 void Monitor::NotifyAll() { | 403 void Monitor::NotifyAll() { |
| 437 // When running with assertions enabled we track the owner. | 404 // When running with assertions enabled we track the owner. |
| 438 ASSERT(IsOwnedByCurrentThread()); | 405 ASSERT(IsOwnedByCurrentThread()); |
| 439 int result = pthread_cond_broadcast(data_.cond()); | 406 int result = pthread_cond_broadcast(data_.cond()); |
| 440 VALIDATE_PTHREAD_RESULT(result); | 407 VALIDATE_PTHREAD_RESULT(result); |
| 441 } | 408 } |
| 442 | 409 |
| 443 } // namespace dart | 410 } // namespace dart |
| 444 | 411 |
| 445 #endif // defined(HOST_OS_FUCHSIA) | 412 #endif // defined(HOST_OS_FUCHSIA) |
| OLD | NEW |