1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:17:34 +00:00

Kernel: Reduce LOCK_DEBUG ifdefs by utilizing Kernel::LockLocation

The LOCK_DEBUG conditional code is pretty ugly for a feature that we
only use rarely. We can remove a significant amount of this code by
utilizing a zero sized fake type when not building in LOCK_DEBUG mode.

This lets us keep the same API, but just let the compiler optimize it
away when don't actually care about the location the caller came from.
This commit is contained in:
Brian Gianforcaro 2021-08-07 04:19:39 -07:00 committed by Andreas Kling
parent 6c18b4e558
commit bea74f4b77
4 changed files with 13 additions and 53 deletions

View file

@ -11,6 +11,7 @@
#include <AK/HashMap.h>
#include <AK/Types.h>
#include <Kernel/Forward.h>
#include <Kernel/Locking/LockLocation.h>
#include <Kernel/Locking/LockMode.h>
#include <Kernel/WaitQueue.h>
@ -31,13 +32,8 @@ public:
}
~Mutex() = default;
#if LOCK_DEBUG
void lock(Mode mode = Mode::Exclusive, const SourceLocation& location = SourceLocation::current());
void restore_lock(Mode, u32, const SourceLocation& location = SourceLocation::current());
#else
void lock(Mode = Mode::Exclusive);
void restore_lock(Mode, u32);
#endif
void lock(Mode mode = Mode::Exclusive, const LockLocation& location = LockLocation::current());
void restore_lock(Mode, u32, const LockLocation& location = LockLocation::current());
void unlock();
[[nodiscard]] Mode force_unlock_if_locked(u32&);
@ -115,18 +111,10 @@ public:
{
}
#if LOCK_DEBUG
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
#else
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive)
#endif
ALWAYS_INLINE explicit MutexLocker(Mutex& l, Mutex::Mode mode = Mutex::Mode::Exclusive, const LockLocation& location = LockLocation::current())
: m_lock(&l)
{
#if LOCK_DEBUG
m_lock->lock(mode, location);
#else
m_lock->lock(mode);
#endif
}
ALWAYS_INLINE ~MutexLocker()
@ -143,38 +131,22 @@ public:
m_lock->unlock();
}
#if LOCK_DEBUG
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
#else
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive)
#endif
ALWAYS_INLINE void attach_and_lock(Mutex& lock, Mutex::Mode mode = Mutex::Mode::Exclusive, const LockLocation& location = LockLocation::current())
{
VERIFY(!m_locked);
m_lock = &lock;
m_locked = true;
#if LOCK_DEBUG
m_lock->lock(mode, location);
#else
m_lock->lock(mode);
#endif
}
#if LOCK_DEBUG
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, const SourceLocation& location = SourceLocation::current())
#else
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive)
#endif
ALWAYS_INLINE void lock(Mutex::Mode mode = Mutex::Mode::Exclusive, const LockLocation& location = LockLocation::current())
{
VERIFY(m_lock);
VERIFY(!m_locked);
m_locked = true;
#if LOCK_DEBUG
m_lock->lock(mode, location);
#else
m_lock->lock(mode);
#endif
}
private: