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 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 VALIDATE_PTHREAD_RESULT(result); | 209 VALIDATE_PTHREAD_RESULT(result); |
210 #endif // defined(DEBUG) | 210 #endif // defined(DEBUG) |
211 | 211 |
212 result = pthread_mutex_init(data_.mutex(), &attr); | 212 result = pthread_mutex_init(data_.mutex(), &attr); |
213 // Verify that creating a pthread_mutex succeeded. | 213 // Verify that creating a pthread_mutex succeeded. |
214 VALIDATE_PTHREAD_RESULT(result); | 214 VALIDATE_PTHREAD_RESULT(result); |
215 | 215 |
216 result = pthread_mutexattr_destroy(&attr); | 216 result = pthread_mutexattr_destroy(&attr); |
217 VALIDATE_PTHREAD_RESULT(result); | 217 VALIDATE_PTHREAD_RESULT(result); |
218 | 218 |
| 219 #if defined(DEBUG) |
219 // When running with assertions enabled we do track the owner. | 220 // When running with assertions enabled we do track the owner. |
220 #if defined(DEBUG) | |
221 owner_ = OSThread::kInvalidThreadId; | 221 owner_ = OSThread::kInvalidThreadId; |
222 #endif // defined(DEBUG) | 222 #endif // defined(DEBUG) |
223 } | 223 } |
224 | 224 |
225 | 225 |
226 Mutex::~Mutex() { | 226 Mutex::~Mutex() { |
227 int result = pthread_mutex_destroy(data_.mutex()); | 227 int result = pthread_mutex_destroy(data_.mutex()); |
228 // Verify that the pthread_mutex was destroyed. | 228 // Verify that the pthread_mutex was destroyed. |
229 VALIDATE_PTHREAD_RESULT(result); | 229 VALIDATE_PTHREAD_RESULT(result); |
230 | 230 |
| 231 #if defined(DEBUG) |
231 // When running with assertions enabled we do track the owner. | 232 // When running with assertions enabled we do track the owner. |
232 #if defined(DEBUG) | |
233 ASSERT(owner_ == OSThread::kInvalidThreadId); | 233 ASSERT(owner_ == OSThread::kInvalidThreadId); |
234 #endif // defined(DEBUG) | 234 #endif // defined(DEBUG) |
235 } | 235 } |
236 | 236 |
237 | 237 |
238 void Mutex::Lock() { | 238 void Mutex::Lock() { |
239 int result = pthread_mutex_lock(data_.mutex()); | 239 int result = pthread_mutex_lock(data_.mutex()); |
240 // Specifically check for dead lock to help debugging. | 240 // Specifically check for dead lock to help debugging. |
241 ASSERT(result != EDEADLK); | 241 ASSERT(result != EDEADLK); |
242 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 242 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 243 #if defined(DEBUG) |
243 // When running with assertions enabled we do track the owner. | 244 // When running with assertions enabled we do track the owner. |
244 #if defined(DEBUG) | |
245 owner_ = OSThread::GetCurrentThreadId(); | 245 owner_ = OSThread::GetCurrentThreadId(); |
246 #endif // defined(DEBUG) | 246 #endif // defined(DEBUG) |
247 } | 247 } |
248 | 248 |
249 | 249 |
250 bool Mutex::TryLock() { | 250 bool Mutex::TryLock() { |
251 int result = pthread_mutex_trylock(data_.mutex()); | 251 int result = pthread_mutex_trylock(data_.mutex()); |
252 // Return false if the lock is busy and locking failed. | 252 // Return false if the lock is busy and locking failed. |
253 if (result == EBUSY) { | 253 if (result == EBUSY) { |
254 return false; | 254 return false; |
255 } | 255 } |
256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 256 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
| 257 #if defined(DEBUG) |
257 // When running with assertions enabled we do track the owner. | 258 // When running with assertions enabled we do track the owner. |
258 #if defined(DEBUG) | |
259 owner_ = OSThread::GetCurrentThreadId(); | 259 owner_ = OSThread::GetCurrentThreadId(); |
260 #endif // defined(DEBUG) | 260 #endif // defined(DEBUG) |
261 return true; | 261 return true; |
262 } | 262 } |
263 | 263 |
264 | 264 |
265 void Mutex::Unlock() { | 265 void Mutex::Unlock() { |
| 266 #if defined(DEBUG) |
266 // When running with assertions enabled we do track the owner. | 267 // When running with assertions enabled we do track the owner. |
267 #if defined(DEBUG) | |
268 ASSERT(IsOwnedByCurrentThread()); | 268 ASSERT(IsOwnedByCurrentThread()); |
269 owner_ = OSThread::kInvalidThreadId; | 269 owner_ = OSThread::kInvalidThreadId; |
270 #endif // defined(DEBUG) | 270 #endif // defined(DEBUG) |
271 int result = pthread_mutex_unlock(data_.mutex()); | 271 int result = pthread_mutex_unlock(data_.mutex()); |
272 // Specifically check for wrong thread unlocking to aid debugging. | 272 // Specifically check for wrong thread unlocking to aid debugging. |
273 ASSERT(result != EPERM); | 273 ASSERT(result != EPERM); |
274 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. | 274 ASSERT_PTHREAD_SUCCESS(result); // Verify no other errors. |
275 } | 275 } |
276 | 276 |
277 | 277 |
(...skipping 15 matching lines...) Expand all Loading... |
293 | 293 |
294 pthread_condattr_t cond_attr; | 294 pthread_condattr_t cond_attr; |
295 result = pthread_condattr_init(&cond_attr); | 295 result = pthread_condattr_init(&cond_attr); |
296 VALIDATE_PTHREAD_RESULT(result); | 296 VALIDATE_PTHREAD_RESULT(result); |
297 | 297 |
298 result = pthread_cond_init(data_.cond(), &cond_attr); | 298 result = pthread_cond_init(data_.cond(), &cond_attr); |
299 VALIDATE_PTHREAD_RESULT(result); | 299 VALIDATE_PTHREAD_RESULT(result); |
300 | 300 |
301 result = pthread_condattr_destroy(&cond_attr); | 301 result = pthread_condattr_destroy(&cond_attr); |
302 VALIDATE_PTHREAD_RESULT(result); | 302 VALIDATE_PTHREAD_RESULT(result); |
| 303 |
| 304 #if defined(DEBUG) |
| 305 // When running with assertions enabled we track the owner. |
| 306 owner_ = OSThread::kInvalidThreadId; |
| 307 #endif // defined(DEBUG) |
303 } | 308 } |
304 | 309 |
305 | 310 |
306 Monitor::~Monitor() { | 311 Monitor::~Monitor() { |
| 312 #if defined(DEBUG) |
| 313 // When running with assertions enabled we track the owner. |
| 314 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 315 #endif // defined(DEBUG) |
| 316 |
307 int result = pthread_mutex_destroy(data_.mutex()); | 317 int result = pthread_mutex_destroy(data_.mutex()); |
308 VALIDATE_PTHREAD_RESULT(result); | 318 VALIDATE_PTHREAD_RESULT(result); |
309 | 319 |
310 result = pthread_cond_destroy(data_.cond()); | 320 result = pthread_cond_destroy(data_.cond()); |
311 VALIDATE_PTHREAD_RESULT(result); | 321 VALIDATE_PTHREAD_RESULT(result); |
312 } | 322 } |
313 | 323 |
314 | 324 |
315 void Monitor::Enter() { | 325 void Monitor::Enter() { |
316 int result = pthread_mutex_lock(data_.mutex()); | 326 int result = pthread_mutex_lock(data_.mutex()); |
317 VALIDATE_PTHREAD_RESULT(result); | 327 VALIDATE_PTHREAD_RESULT(result); |
318 // TODO(iposva): Do we need to track lock owners? | 328 |
| 329 #if defined(DEBUG) |
| 330 // When running with assertions enabled we track the owner. |
| 331 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 332 owner_ = OSThread::GetCurrentThreadId(); |
| 333 #endif // defined(DEBUG) |
319 } | 334 } |
320 | 335 |
321 | 336 |
322 void Monitor::Exit() { | 337 void Monitor::Exit() { |
323 // TODO(iposva): Do we need to track lock owners? | 338 #if defined(DEBUG) |
| 339 // When running with assertions enabled we track the owner. |
| 340 ASSERT(IsOwnedByCurrentThread()); |
| 341 owner_ = OSThread::kInvalidThreadId; |
| 342 #endif // defined(DEBUG) |
| 343 |
324 int result = pthread_mutex_unlock(data_.mutex()); | 344 int result = pthread_mutex_unlock(data_.mutex()); |
325 VALIDATE_PTHREAD_RESULT(result); | 345 VALIDATE_PTHREAD_RESULT(result); |
326 } | 346 } |
327 | 347 |
328 | 348 |
329 Monitor::WaitResult Monitor::Wait(int64_t millis) { | 349 Monitor::WaitResult Monitor::Wait(int64_t millis) { |
330 return WaitMicros(millis * kMicrosecondsPerMillisecond); | 350 return WaitMicros(millis * kMicrosecondsPerMillisecond); |
331 } | 351 } |
332 | 352 |
333 | 353 |
334 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { | 354 Monitor::WaitResult Monitor::WaitMicros(int64_t micros) { |
335 // TODO(iposva): Do we need to track lock owners? | 355 #if defined(DEBUG) |
| 356 // When running with assertions enabled we track the owner. |
| 357 ASSERT(IsOwnedByCurrentThread()); |
| 358 ThreadId saved_owner = owner_; |
| 359 owner_ = OSThread::kInvalidThreadId; |
| 360 #endif // defined(DEBUG) |
| 361 |
336 Monitor::WaitResult retval = kNotified; | 362 Monitor::WaitResult retval = kNotified; |
337 if (micros == kNoTimeout) { | 363 if (micros == kNoTimeout) { |
338 // Wait forever. | 364 // Wait forever. |
339 int result = pthread_cond_wait(data_.cond(), data_.mutex()); | 365 int result = pthread_cond_wait(data_.cond(), data_.mutex()); |
340 VALIDATE_PTHREAD_RESULT(result); | 366 VALIDATE_PTHREAD_RESULT(result); |
341 } else { | 367 } else { |
342 struct timespec ts; | 368 struct timespec ts; |
343 ComputeTimeSpecMicros(&ts, micros); | 369 ComputeTimeSpecMicros(&ts, micros); |
344 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); | 370 int result = pthread_cond_timedwait(data_.cond(), data_.mutex(), &ts); |
345 ASSERT((result == 0) || (result == ETIMEDOUT)); | 371 ASSERT((result == 0) || (result == ETIMEDOUT)); |
346 if (result == ETIMEDOUT) { | 372 if (result == ETIMEDOUT) { |
347 retval = kTimedOut; | 373 retval = kTimedOut; |
348 } | 374 } |
349 } | 375 } |
| 376 |
| 377 #if defined(DEBUG) |
| 378 // When running with assertions enabled we track the owner. |
| 379 ASSERT(owner_ == OSThread::kInvalidThreadId); |
| 380 owner_ = OSThread::GetCurrentThreadId(); |
| 381 ASSERT(owner_ == saved_owner); |
| 382 #endif // defined(DEBUG) |
350 return retval; | 383 return retval; |
351 } | 384 } |
352 | 385 |
353 | 386 |
354 void Monitor::Notify() { | 387 void Monitor::Notify() { |
355 // TODO(iposva): Do we need to track lock owners? | 388 // When running with assertions enabled we track the owner. |
| 389 ASSERT(IsOwnedByCurrentThread()); |
356 int result = pthread_cond_signal(data_.cond()); | 390 int result = pthread_cond_signal(data_.cond()); |
357 VALIDATE_PTHREAD_RESULT(result); | 391 VALIDATE_PTHREAD_RESULT(result); |
358 } | 392 } |
359 | 393 |
360 | 394 |
361 void Monitor::NotifyAll() { | 395 void Monitor::NotifyAll() { |
362 // TODO(iposva): Do we need to track lock owners? | 396 // When running with assertions enabled we track the owner. |
| 397 ASSERT(IsOwnedByCurrentThread()); |
363 int result = pthread_cond_broadcast(data_.cond()); | 398 int result = pthread_cond_broadcast(data_.cond()); |
364 VALIDATE_PTHREAD_RESULT(result); | 399 VALIDATE_PTHREAD_RESULT(result); |
365 } | 400 } |
366 | 401 |
367 } // namespace dart | 402 } // namespace dart |
368 | 403 |
369 #endif // defined(TARGET_OS_ANDROID) | 404 #endif // defined(TARGET_OS_ANDROID) |
OLD | NEW |