summaryrefslogtreecommitdiff
path: root/Kernel/Locking/Mutex.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2023-04-02 21:36:39 +0200
committerAndreas Kling <kling@serenityos.org>2023-04-04 10:33:42 +0200
commited1253ab900cf8aec5dc57fb4d5311f22cc5ce9f (patch)
tree9f20a202bbf95d4d9360b9a35130bf802e71f40d /Kernel/Locking/Mutex.h
parentc3915e4058341570e746979a69ce6696de232da5 (diff)
downloadserenity-ed1253ab900cf8aec5dc57fb4d5311f22cc5ce9f.zip
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<Thread> 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.
Diffstat (limited to 'Kernel/Locking/Mutex.h')
-rw-r--r--Kernel/Locking/Mutex.h13
1 files changed, 6 insertions, 7 deletions
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<uintptr_t>(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<Thread> 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<LockRank::None> m_lock {};
#if LOCK_SHARED_UPGRADE_DEBUG
- HashMap<Thread*, u32> m_shared_holders_map;
+ HashMap<uintptr_t, u32> m_shared_holders_map;
#endif
};