MCPcopy Create free account
hub / github.com/LibreSprite/LibreSprite / lock

Method lock

src/app/document.cpp:434–477  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

432// Multi-threading ("sprite wrappers" use this)
433
434bool 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
479bool Document::lockToWrite(int timeout)
480{

Callers 6

abortMethod · 0.45
doneMethod · 0.45
DocumentReaderMethod · 0.45
DocumentWriterMethod · 0.45
updateMethod · 0.45
putImageDataMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected