mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-27 15:27:41 +03:00
e8aff0c1c8
This commit updates the lock function from Spinlock and RecursiveSpinlock to return the InterruptsState of the processor, instead of the processor flags. The unlock functions would only look at the interrupt flag of the processor flags, so we now use the InterruptsState enum to clarify the intent, and such that we can use the same Spinlock code for the aarch64 build. To not break the build, all the call sites are updated aswell.
81 lines
1.6 KiB
C++
81 lines
1.6 KiB
C++
/*
|
|
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <Kernel/Arch/Processor.h>
|
|
#include <Kernel/Locking/LockRank.h>
|
|
|
|
namespace Kernel {
|
|
|
|
class Spinlock {
|
|
AK_MAKE_NONCOPYABLE(Spinlock);
|
|
AK_MAKE_NONMOVABLE(Spinlock);
|
|
|
|
public:
|
|
Spinlock(LockRank rank)
|
|
: m_rank(rank)
|
|
{
|
|
}
|
|
|
|
InterruptsState lock();
|
|
void unlock(InterruptsState);
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_locked() const
|
|
{
|
|
// FIXME: Implement Spinlock on aarch64
|
|
#if ARCH(AARCH64)
|
|
return true;
|
|
#endif
|
|
return m_lock.load(AK::memory_order_relaxed) != 0;
|
|
}
|
|
|
|
ALWAYS_INLINE void initialize()
|
|
{
|
|
m_lock.store(0, AK::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
Atomic<u8> m_lock { 0 };
|
|
const LockRank m_rank;
|
|
};
|
|
|
|
class RecursiveSpinlock {
|
|
AK_MAKE_NONCOPYABLE(RecursiveSpinlock);
|
|
AK_MAKE_NONMOVABLE(RecursiveSpinlock);
|
|
|
|
public:
|
|
RecursiveSpinlock(LockRank rank)
|
|
: m_rank(rank)
|
|
{
|
|
}
|
|
|
|
InterruptsState lock();
|
|
void unlock(InterruptsState);
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_locked() const
|
|
{
|
|
return m_lock.load(AK::memory_order_relaxed) != 0;
|
|
}
|
|
|
|
[[nodiscard]] ALWAYS_INLINE bool is_locked_by_current_processor() const
|
|
{
|
|
return m_lock.load(AK::memory_order_relaxed) == FlatPtr(&Processor::current());
|
|
}
|
|
|
|
ALWAYS_INLINE void initialize()
|
|
{
|
|
m_lock.store(0, AK::memory_order_relaxed);
|
|
}
|
|
|
|
private:
|
|
Atomic<FlatPtr> m_lock { 0 };
|
|
u32 m_recursions { 0 };
|
|
const LockRank m_rank;
|
|
};
|
|
|
|
}
|