| 432 | // Multi-threading ("sprite wrappers" use this) |
| 433 | |
| 434 | bool Document::lock(LockType lockType, int timeout) |
| 435 | { |
| 436 | while (timeout >= 0) { |
| 437 | { |
| 438 | scoped_lock lock(m_mutex); |
| 439 | switch (lockType) { |
| 440 | |
| 441 | case ReadLock: |
| 442 | // If no body is writting the sprite... |
| 443 | if (!m_write_lock) { |
| 444 | // We can read it |
| 445 | ++m_read_locks; |
| 446 | return true; |
| 447 | } |
| 448 | break; |
| 449 | |
| 450 | case WriteLock: |
| 451 | // If no body is reading and writting... |
| 452 | if (m_read_locks == 0 && !m_write_lock) { |
| 453 | // We can start writting the sprite... |
| 454 | m_write_lock = true; |
| 455 | TRACE("Document::lock: Locked <%d> to write\n", id()); |
| 456 | return true; |
| 457 | } |
| 458 | break; |
| 459 | |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if (timeout > 0) { |
| 464 | int delay = MIN(100, timeout); |
| 465 | timeout -= delay; |
| 466 | |
| 467 | TRACE("Document::lock: wait 100 msecs for <%d>\n", id()); |
| 468 | base::this_thread::sleep_for(double(delay) / 1000.0); |
| 469 | } |
| 470 | else |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | TRACE("Document::lock: Cannot lock <%d> to %s (has %d read locks and %d write locks)\n", |
| 475 | id(), (lockType == ReadLock ? "read": "write"), m_read_locks, m_write_lock); |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | bool Document::lockToWrite(int timeout) |
| 480 | { |
no outgoing calls
no test coverage detected