From a51fbb13e8abcbf7682d5c6fac2a825afc1cc298 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 14 Jan 2021 19:14:10 -0700 Subject: [PATCH] Kernel: Make Locker remember whether the lock is held This allows temporarily unlocking a lock or re-locking it, and it will only unlock if it is still being held. Fixes #4352 --- Kernel/Lock.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Kernel/Lock.h b/Kernel/Lock.h index 4d5e8bd6f98..9756587a3ff 100644 --- a/Kernel/Lock.h +++ b/Kernel/Lock.h @@ -110,12 +110,27 @@ public: { 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