Skip to content

Commit

Permalink
Kernel/Locking: Add lock rank tracking to Spinlock/RecursiveSpinlock
Browse files Browse the repository at this point in the history
  • Loading branch information
bgianfo authored and awesomekling committed Sep 7, 2021
1 parent 066b059 commit f6b1517
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions Kernel/Locking/Spinlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <AK/Atomic.h>
#include <AK/Types.h>
#include <Kernel/Arch/x86/Processor.h>
#include <Kernel/Forward.h>
#include <Kernel/Locking/LockRank.h>

namespace Kernel {

Expand All @@ -18,7 +18,10 @@ class Spinlock {
AK_MAKE_NONMOVABLE(Spinlock);

public:
Spinlock() = default;
Spinlock(LockRank rank = LockRank::None)
: m_rank(rank)
{
}

ALWAYS_INLINE u32 lock()
{
Expand All @@ -28,17 +31,20 @@ class Spinlock {
while (m_lock.exchange(1, AK::memory_order_acquire) != 0) {
Processor::wait_check();
}
track_lock_acquire(m_rank);
return prev_flags;
}

ALWAYS_INLINE void unlock(u32 prev_flags)
{
VERIFY(is_locked());
track_lock_release(m_rank);
m_lock.store(0, AK::memory_order_release);
if (prev_flags & 0x200)
sti();
else
cli();

Processor::leave_critical();
}

Expand All @@ -54,14 +60,18 @@ class Spinlock {

private:
Atomic<u8> m_lock { 0 };
const LockRank m_rank;
};

class RecursiveSpinlock {
AK_MAKE_NONCOPYABLE(RecursiveSpinlock);
AK_MAKE_NONMOVABLE(RecursiveSpinlock);

public:
RecursiveSpinlock() = default;
RecursiveSpinlock(LockRank rank = LockRank::None)
: m_rank(rank)
{
}

ALWAYS_INLINE u32 lock()
{
Expand All @@ -77,6 +87,8 @@ class RecursiveSpinlock {
Processor::wait_check();
expected = 0;
}
if (m_recursions == 0)
track_lock_acquire(m_rank);
m_recursions++;
return prev_flags;
}
Expand All @@ -85,12 +97,15 @@ class RecursiveSpinlock {
{
VERIFY(m_recursions > 0);
VERIFY(m_lock.load(AK::memory_order_relaxed) == FlatPtr(&Processor::current()));
if (--m_recursions == 0)
if (--m_recursions == 0) {
track_lock_release(m_rank);
m_lock.store(0, AK::memory_order_release);
}
if (prev_flags & 0x200)
sti();
else
cli();

Processor::leave_critical();
}

Expand All @@ -112,6 +127,7 @@ class RecursiveSpinlock {
private:
Atomic<FlatPtr> m_lock { 0 };
u32 m_recursions { 0 };
const LockRank m_rank;
};

template<typename LockType>
Expand Down

0 comments on commit f6b1517

Please sign in to comment.