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 | 15 |
16 namespace dart { | 16 namespace dart { |
17 | 17 |
18 #define VALIDATE_PTHREAD_RESULT(result) \ | 18 #define VALIDATE_PTHREAD_RESULT(result) \ |
19 if (result != 0) { \ | 19 if (result != 0) { \ |
20 const int kBufferSize = 1024; \ | 20 const int kBufferSize = 1024; \ |
21 char error_buf[kBufferSize]; \ | 21 char error_buf[kBufferSize]; \ |
22 FATAL2("pthread error: %d (%s)", result, \ | 22 FATAL2("pthread error: %d (%s)", result, \ |
23 strerror_r(result, error_buf, kBufferSize)); \ | 23 strerror_r(result, error_buf, kBufferSize)); \ |
24 } | 24 } |
25 | 25 |
26 | 26 |
| 27 #if defined(DEBUG) |
| 28 #define ASSERT_PTHREAD_SUCCESS(result) VALIDATE_PTHREAD_RESULT(result) |
| 29 #else |
| 30 // NOTE: This (currently) expands to a no-op. |
| 31 #define ASSERT_PTHREAD_SUCCESS(result) ASSERT(result == 0) |
| 32 #endif |
| 33 |
| 34 |
27 #ifdef DEBUG | 35 #ifdef DEBUG |
28 #define RETURN_ON_PTHREAD_FAILURE(result) \ | 36 #define RETURN_ON_PTHREAD_FAILURE(result) \ |
29 if (result != 0) { \ | 37 if (result != 0) { \ |
30 const int kBufferSize = 1024; \ | 38 const int kBufferSize = 1024; \ |
31 char error_buf[kBufferSize]; \ | 39 char error_buf[kBufferSize]; \ |
32 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \ | 40 fprintf(stderr, "%s:%d: pthread error: %d (%s)\n", \ |
33 __FILE__, __LINE__, result, \ | 41 __FILE__, __LINE__, result, \ |
34 strerror_r(result, error_buf, kBufferSize)); \ | 42 strerror_r(result, error_buf, kBufferSize)); \ |
35 return result; \ | 43 return result; \ |
36 } | 44 } |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 #if defined(DEBUG) | 219 #if defined(DEBUG) |
212 ASSERT(owner_ == OSThread::kInvalidThreadId); | 220 ASSERT(owner_ == OSThread::kInvalidThreadId); |
213 #endif // defined(DEBUG) | 221 #endif // defined(DEBUG) |
214 } | 222 } |
215 | 223 |
216 | 224 |
217 void Mutex::Lock() { | 225 void Mutex::Lock() { |
218 int result = pthread_mutex_lock(data_.mutex()); | 226 int result = pthread_mutex_lock(data_.mutex()); |
219 // Specifically check for dead lock to help debugging. | 227 // Specifically check for dead lock to help debugging. |
220 ASSERT(result != EDEADLK); | 228 ASSERT(result != EDEADLK); |
221 ASSERT(result == 0); // Verify no other errors. | 229 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
222 // When running with assertions enabled we do track the owner. | 230 // When running with assertions enabled we do track the owner. |
223 #if defined(DEBUG) | 231 #if defined(DEBUG) |
224 owner_ = OSThread::GetCurrentThreadId(); | 232 owner_ = OSThread::GetCurrentThreadId(); |
225 #endif // defined(DEBUG) | 233 #endif // defined(DEBUG) |
226 } | 234 } |
227 | 235 |
228 | 236 |
229 bool Mutex::TryLock() { | 237 bool Mutex::TryLock() { |
230 int result = pthread_mutex_trylock(data_.mutex()); | 238 int result = pthread_mutex_trylock(data_.mutex()); |
231 // Return false if the lock is busy and locking failed. | 239 // Return false if the lock is busy and locking failed. |
232 if (result == EBUSY) { | 240 if (result == EBUSY) { |
233 return false; | 241 return false; |
234 } | 242 } |
235 ASSERT(result == 0); // Verify no other errors. | 243 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
236 // When running with assertions enabled we do track the owner. | 244 // When running with assertions enabled we do track the owner. |
237 #if defined(DEBUG) | 245 #if defined(DEBUG) |
238 owner_ = OSThread::GetCurrentThreadId(); | 246 owner_ = OSThread::GetCurrentThreadId(); |
239 #endif // defined(DEBUG) | 247 #endif // defined(DEBUG) |
240 return true; | 248 return true; |
241 } | 249 } |
242 | 250 |
243 | 251 |
244 void Mutex::Unlock() { | 252 void Mutex::Unlock() { |
245 // When running with assertions enabled we do track the owner. | 253 // When running with assertions enabled we do track the owner. |
246 #if defined(DEBUG) | 254 #if defined(DEBUG) |
247 ASSERT(IsOwnedByCurrentThread()); | 255 ASSERT(IsOwnedByCurrentThread()); |
248 owner_ = OSThread::kInvalidThreadId; | 256 owner_ = OSThread::kInvalidThreadId; |
249 #endif // defined(DEBUG) | 257 #endif // defined(DEBUG) |
250 int result = pthread_mutex_unlock(data_.mutex()); | 258 int result = pthread_mutex_unlock(data_.mutex()); |
251 // Specifically check for wrong thread unlocking to aid debugging. | 259 // Specifically check for wrong thread unlocking to aid debugging. |
252 ASSERT(result != EPERM); | 260 ASSERT(result != EPERM); |
253 ASSERT(result == 0); // Verify no other errors. | 261 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
254 } | 262 } |
255 | 263 |
256 | 264 |
257 Monitor::Monitor() { | 265 Monitor::Monitor() { |
258 pthread_mutexattr_t mutex_attr; | 266 pthread_mutexattr_t mutex_attr; |
259 int result = pthread_mutexattr_init(&mutex_attr); | 267 int result = pthread_mutexattr_init(&mutex_attr); |
260 VALIDATE_PTHREAD_RESULT(result); | 268 VALIDATE_PTHREAD_RESULT(result); |
261 | 269 |
262 #if defined(DEBUG) | 270 #if defined(DEBUG) |
263 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); | 271 result = pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 | 350 |
343 void Monitor::NotifyAll() { | 351 void Monitor::NotifyAll() { |
344 // TODO(iposva): Do we need to track lock owners? | 352 // TODO(iposva): Do we need to track lock owners? |
345 int result = pthread_cond_broadcast(data_.cond()); | 353 int result = pthread_cond_broadcast(data_.cond()); |
346 VALIDATE_PTHREAD_RESULT(result); | 354 VALIDATE_PTHREAD_RESULT(result); |
347 } | 355 } |
348 | 356 |
349 } // namespace dart | 357 } // namespace dart |
350 | 358 |
351 #endif // defined(TARGET_OS_LINUX) | 359 #endif // defined(TARGET_OS_LINUX) |
OLD | NEW |