| 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_LINUX) | 6 #if defined(TARGET_OS_LINUX) |
| 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/resource.h> // NOLINT | 11 #include <sys/resource.h> // NOLINT |
| 12 #include <sys/time.h> // NOLINT | 12 #include <sys/time.h> // NOLINT |
| 13 | 13 |
| 14 #include "platform/assert.h" | 14 #include "platform/assert.h" |
| 15 #include "vm/isolate.h" | |
| 16 | 15 |
| 17 namespace dart { | 16 namespace dart { |
| 18 | 17 |
| 19 #define VALIDATE_PTHREAD_RESULT(result) \ | 18 #define VALIDATE_PTHREAD_RESULT(result) \ |
| 20 if (result != 0) { \ | 19 if (result != 0) { \ |
| 21 const int kBufferSize = 1024; \ | 20 const int kBufferSize = 1024; \ |
| 22 char error_buf[kBufferSize]; \ | 21 char error_buf[kBufferSize]; \ |
| 23 FATAL2("pthread error: %d (%s)", result, \ | 22 FATAL2("pthread error: %d (%s)", result, \ |
| 24 strerror_r(result, error_buf, kBufferSize)); \ | 23 strerror_r(result, error_buf, kBufferSize)); \ |
| 25 } | 24 } |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 | 190 |
| 192 result = pthread_mutex_init(data_.mutex(), &attr); | 191 result = pthread_mutex_init(data_.mutex(), &attr); |
| 193 // Verify that creating a pthread_mutex succeeded. | 192 // Verify that creating a pthread_mutex succeeded. |
| 194 VALIDATE_PTHREAD_RESULT(result); | 193 VALIDATE_PTHREAD_RESULT(result); |
| 195 | 194 |
| 196 result = pthread_mutexattr_destroy(&attr); | 195 result = pthread_mutexattr_destroy(&attr); |
| 197 VALIDATE_PTHREAD_RESULT(result); | 196 VALIDATE_PTHREAD_RESULT(result); |
| 198 | 197 |
| 199 // When running with assertions enabled we do track the owner. | 198 // When running with assertions enabled we do track the owner. |
| 200 #if defined(DEBUG) | 199 #if defined(DEBUG) |
| 201 owner_ = NULL; | 200 owner_ = OSThread::kInvalidThreadId; |
| 202 #endif // defined(DEBUG) | 201 #endif // defined(DEBUG) |
| 203 } | 202 } |
| 204 | 203 |
| 205 | 204 |
| 206 Mutex::~Mutex() { | 205 Mutex::~Mutex() { |
| 207 int result = pthread_mutex_destroy(data_.mutex()); | 206 int result = pthread_mutex_destroy(data_.mutex()); |
| 208 // Verify that the pthread_mutex was destroyed. | 207 // Verify that the pthread_mutex was destroyed. |
| 209 VALIDATE_PTHREAD_RESULT(result); | 208 VALIDATE_PTHREAD_RESULT(result); |
| 210 | 209 |
| 211 // When running with assertions enabled we do track the owner. | 210 // When running with assertions enabled we do track the owner. |
| 212 #if defined(DEBUG) | 211 #if defined(DEBUG) |
| 213 ASSERT(owner_ == NULL); | 212 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 214 #endif // defined(DEBUG) | 213 #endif // defined(DEBUG) |
| 215 } | 214 } |
| 216 | 215 |
| 217 | 216 |
| 218 void Mutex::Lock() { | 217 void Mutex::Lock() { |
| 219 int result = pthread_mutex_lock(data_.mutex()); | 218 int result = pthread_mutex_lock(data_.mutex()); |
| 220 // Specifically check for dead lock to help debugging. | 219 // Specifically check for dead lock to help debugging. |
| 221 ASSERT(result != EDEADLK); | 220 ASSERT(result != EDEADLK); |
| 222 ASSERT(result == 0); // Verify no other errors. | 221 ASSERT(result == 0); // Verify no other errors. |
| 223 // When running with assertions enabled we do track the owner. | 222 // When running with assertions enabled we do track the owner. |
| 224 #if defined(DEBUG) | 223 #if defined(DEBUG) |
| 225 owner_ = Isolate::Current(); | 224 owner_ = OSThread::GetCurrentThreadId(); |
| 226 #endif // defined(DEBUG) | 225 #endif // defined(DEBUG) |
| 227 } | 226 } |
| 228 | 227 |
| 229 | 228 |
| 230 bool Mutex::TryLock() { | 229 bool Mutex::TryLock() { |
| 231 int result = pthread_mutex_trylock(data_.mutex()); | 230 int result = pthread_mutex_trylock(data_.mutex()); |
| 232 // Return false if the lock is busy and locking failed. | 231 // Return false if the lock is busy and locking failed. |
| 233 if (result == EBUSY) { | 232 if (result == EBUSY) { |
| 234 return false; | 233 return false; |
| 235 } | 234 } |
| 236 ASSERT(result == 0); // Verify no other errors. | 235 ASSERT(result == 0); // Verify no other errors. |
| 237 // When running with assertions enabled we do track the owner. | 236 // When running with assertions enabled we do track the owner. |
| 238 #if defined(DEBUG) | 237 #if defined(DEBUG) |
| 239 owner_ = Isolate::Current(); | 238 owner_ = OSThread::GetCurrentThreadId(); |
| 240 #endif // defined(DEBUG) | 239 #endif // defined(DEBUG) |
| 241 return true; | 240 return true; |
| 242 } | 241 } |
| 243 | 242 |
| 244 | 243 |
| 245 void Mutex::Unlock() { | 244 void Mutex::Unlock() { |
| 246 // When running with assertions enabled we do track the owner. | 245 // When running with assertions enabled we do track the owner. |
| 247 #if defined(DEBUG) | 246 #if defined(DEBUG) |
| 248 ASSERT(owner_ == Isolate::Current()); | 247 ASSERT(IsOwnedByCurrentThread()); |
| 249 owner_ = NULL; | 248 owner_ = OSThread::kInvalidThreadId; |
| 250 #endif // defined(DEBUG) | 249 #endif // defined(DEBUG) |
| 251 int result = pthread_mutex_unlock(data_.mutex()); | 250 int result = pthread_mutex_unlock(data_.mutex()); |
| 252 // Specifically check for wrong thread unlocking to aid debugging. | 251 // Specifically check for wrong thread unlocking to aid debugging. |
| 253 ASSERT(result != EPERM); | 252 ASSERT(result != EPERM); |
| 254 ASSERT(result == 0); // Verify no other errors. | 253 ASSERT(result == 0); // Verify no other errors. |
| 255 } | 254 } |
| 256 | 255 |
| 257 | 256 |
| 258 Monitor::Monitor() { | 257 Monitor::Monitor() { |
| 259 pthread_mutexattr_t mutex_attr; | 258 pthread_mutexattr_t mutex_attr; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 | 342 |
| 344 void Monitor::NotifyAll() { | 343 void Monitor::NotifyAll() { |
| 345 // TODO(iposva): Do we need to track lock owners? | 344 // TODO(iposva): Do we need to track lock owners? |
| 346 int result = pthread_cond_broadcast(data_.cond()); | 345 int result = pthread_cond_broadcast(data_.cond()); |
| 347 VALIDATE_PTHREAD_RESULT(result); | 346 VALIDATE_PTHREAD_RESULT(result); |
| 348 } | 347 } |
| 349 | 348 |
| 350 } // namespace dart | 349 } // namespace dart |
| 351 | 350 |
| 352 #endif // defined(TARGET_OS_LINUX) | 351 #endif // defined(TARGET_OS_LINUX) |
| OLD | NEW |