Skip to content

Commit

Permalink
Kernel: Make Locker remember whether the lock is held
Browse files Browse the repository at this point in the history
This allows temporarily unlocking a lock or re-locking it, and it will
only unlock if it is still being held.

Fixes SerenityOS#4352
  • Loading branch information
tomuta authored and awesomekling committed Jan 15, 2021
1 parent 2910205 commit a51fbb1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Kernel/Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,27 @@ class Locker {
{
m_lock.lock(mode);
}
ALWAYS_INLINE ~Locker() { unlock(); }
ALWAYS_INLINE void unlock() { m_lock.unlock(); }
ALWAYS_INLINE void lock(Lock::Mode mode = Lock::Mode::Exclusive) { m_lock.lock(mode); }
ALWAYS_INLINE ~Locker()
{
if (m_locked)
unlock();
}
ALWAYS_INLINE void unlock()
{
ASSERT(m_locked);
m_locked = false;
m_lock.unlock();
}
ALWAYS_INLINE void lock(Lock::Mode mode = Lock::Mode::Exclusive)
{
ASSERT(!m_locked);
m_locked = true;
m_lock.lock(mode);
}

private:
Lock& m_lock;
bool m_locked { true };
};

#ifdef LOCK_DEBUG
Expand Down

0 comments on commit a51fbb1

Please sign in to comment.