| 477 | } |
| 478 | |
| 479 | bool Document::lockToWrite(int timeout) |
| 480 | { |
| 481 | while (timeout >= 0) { |
| 482 | { |
| 483 | scoped_lock lock(m_mutex); |
| 484 | // this only is possible if there are just one reader |
| 485 | if (m_read_locks == 1) { |
| 486 | ASSERT(!m_write_lock); |
| 487 | m_read_locks = 0; |
| 488 | m_write_lock = true; |
| 489 | TRACE("Document::lockToWrite: Locked <%d> to write\n", id()); |
| 490 | return true; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (timeout > 0) { |
| 495 | int delay = MIN(100, timeout); |
| 496 | timeout -= delay; |
| 497 | |
| 498 | TRACE("Document::lockToWrite: wait 100 msecs for <%d>\n", id()); |
| 499 | base::this_thread::sleep_for(double(delay) / 1000.0); |
| 500 | } |
| 501 | else |
| 502 | break; |
| 503 | } |
| 504 | |
| 505 | TRACE("Document::lockToWrite: Cannot lock <%d> to write (has %d read locks and %d write locks)\n", |
| 506 | id(), m_read_locks, m_write_lock); |
| 507 | return false; |
| 508 | } |
| 509 | |
| 510 | void Document::unlockToRead() |
| 511 | { |
no outgoing calls
no test coverage detected