Skip to content

Commit

Permalink
We can upgrade readers to writers when the current thread already has…
Browse files Browse the repository at this point in the history
… a writer lock

This is a missing condition in RWLock::canWriteLockFromRead() that is
possible in RWLock::upgradeToWrite() when the current thread already
has a writer lock. This case is handled as a re-entrant
lock (LockResult::Reentrant) and means that the thread can upgrade any
reader to writer anyway.

Completes: 157533d
Related to: aseprite/aseprite#4310
  • Loading branch information
dacap committed Feb 16, 2024
1 parent 7477e94 commit 763ce04
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions base/rw_lock.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// LAF Base Library
// Copyright (C) 2020-2023 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2001-2016 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -38,9 +38,17 @@ bool RWLock::canWriteLockFromRead() const
{
std::lock_guard lock(m_mutex);

// If this thread already have a writer lock, we can upgrade any
// reader lock to writer in the same thread (re-entrant locks).
if (m_write_lock &&
m_write_thread == std::this_thread::get_id()) {
return true;
}
// If only we are reading (one lock) and nobody is writing, we can
// lock for writing..
return (m_read_locks == 1 && !m_write_lock);
else {
return (m_read_locks == 1 && !m_write_lock);
}
}

RWLock::LockResult RWLock::lock(LockType lockType, int timeout)
Expand Down

0 comments on commit 763ce04

Please sign in to comment.