From ed1253ab900cf8aec5dc57fb4d5311f22cc5ce9f Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 2 Apr 2023 21:36:39 +0200 Subject: Kernel: Don't ref/unref the holder thread in Mutex There was a whole bunch of ref counting churn coming from Mutex, which had a RefPtr m_holder to (mostly) point at the thread holding the mutex. Since we never actually dereference the m_holder value, but only use it for identity checks against thread pointers, we can store it as an uintptr_t and skip the ref counting entirely. Threads can't die while holding a mutex anyway, so there's no risk of them going missing on us. --- Kernel/Locking/Mutex.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'Kernel/Locking/Mutex.h') diff --git a/Kernel/Locking/Mutex.h b/Kernel/Locking/Mutex.h index 25a2eb3572..d083fb3baa 100644 --- a/Kernel/Locking/Mutex.h +++ b/Kernel/Locking/Mutex.h @@ -57,7 +57,7 @@ public: VERIFY(m_mode != Mode::Shared); // This method should only be used on exclusively-held locks if (m_mode == Mode::Unlocked) return false; - return m_holder == Thread::current(); + return m_holder == bit_cast(Thread::current()); } [[nodiscard]] StringView name() const { return m_name; } @@ -96,12 +96,11 @@ private: // lock it again. When locked in shared mode, any thread can do that. u32 m_times_locked { 0 }; - // One of the threads that hold this lock, or nullptr. When locked in shared - // mode, this is stored on best effort basis: nullptr value does *not* mean + // The address of one of the threads that hold this lock, or 0. + // When locked in shared mode, this is stored on best effort basis: 0 does *not* mean // the lock is unlocked, it just means we don't know which threads hold it. - // When locked exclusively, this is always the one thread that holds the - // lock. - RefPtr m_holder; + // When locked exclusively, this is always the one thread that holds the lock. + uintptr_t m_holder { 0 }; size_t m_shared_holders { 0 }; struct BlockedThreadLists { @@ -124,7 +123,7 @@ private: mutable Spinlock m_lock {}; #if LOCK_SHARED_UPGRADE_DEBUG - HashMap m_shared_holders_map; + HashMap m_shared_holders_map; #endif }; -- cgit v1.2.3