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