Kernel: Use spinlocks instead of mutexes for RWLock

All the operations are super fast, just checking or changing few
integers, there is no need for a yielding mutex :^)
This commit is contained in:
2026-05-21 02:16:41 +03:00
parent 81d8ab3d79
commit 5c94c30e1b
2 changed files with 16 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
#include <kernel/Lock/Mutex.h> #include <kernel/Lock/SpinLock.h>
#include <kernel/Lock/LockGuard.h> #include <kernel/Lock/SpinLockAsMutex.h>
namespace Kernel namespace Kernel
{ {
@@ -15,15 +15,18 @@ namespace Kernel
void rd_lock() void rd_lock()
{ {
LockGuard _(m_mutex); SpinLockGuard _(m_lock);
while (m_writers_waiting > 0 || m_writer != -1) while (m_writers_waiting > 0 || m_writer != -1)
m_thread_blocker.block_indefinite(&m_mutex); {
SpinLockGuardAsMutex smutex(_);
m_thread_blocker.block_indefinite(&smutex);
}
m_readers_active++; m_readers_active++;
} }
void rd_unlock() void rd_unlock()
{ {
LockGuard _(m_mutex); SpinLockGuard _(m_lock);
if (--m_readers_active == 0) if (--m_readers_active == 0)
m_thread_blocker.unblock(); m_thread_blocker.unblock();
} }
@@ -36,11 +39,14 @@ namespace Kernel
return; return;
} }
LockGuard _(m_mutex); SpinLockGuard _(m_lock);
m_writers_waiting++; m_writers_waiting++;
while (m_readers_active > 0 || m_writer != -1) while (m_readers_active > 0 || m_writer != -1)
m_thread_blocker.block_indefinite(&m_mutex); {
SpinLockGuardAsMutex smutex(_);
m_thread_blocker.block_indefinite(&smutex);
}
m_writers_waiting--; m_writers_waiting--;
m_writer = Thread::current_tid(); m_writer = Thread::current_tid();
@@ -51,13 +57,13 @@ namespace Kernel
{ {
if (--m_writer_depth != 0) if (--m_writer_depth != 0)
return; return;
LockGuard _(m_mutex); SpinLockGuard _(m_lock);
m_writer = -1; m_writer = -1;
m_thread_blocker.unblock(); m_thread_blocker.unblock();
} }
private: private:
Mutex m_mutex; SpinLock m_lock;
ThreadBlocker m_thread_blocker; ThreadBlocker m_thread_blocker;
uint32_t m_readers_active { 0 }; uint32_t m_readers_active { 0 };
uint32_t m_writers_waiting { 0 }; uint32_t m_writers_waiting { 0 };

View File

@@ -1,5 +1,6 @@
#include <BAN/ScopeGuard.h> #include <BAN/ScopeGuard.h>
#include <kernel/BootInfo.h> #include <kernel/BootInfo.h>
#include <kernel/Lock/LockGuard.h>
#include <kernel/Memory/Heap.h> #include <kernel/Memory/Heap.h>
#include <kernel/Memory/PageTable.h> #include <kernel/Memory/PageTable.h>
#include <kernel/Storage/DiskCache.h> #include <kernel/Storage/DiskCache.h>