diff options
author | Timon Kruiper <timonkruiper@gmail.com> | 2022-08-23 22:14:07 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-08-26 12:51:57 +0200 |
commit | 026f37b031abc4948505894b6d6a98402e334716 (patch) | |
tree | b6d972bd305bde52e16bafb412097b1380e6a694 /Kernel/Arch | |
parent | c9118de5a6360d69919ca157f55d10ee4eafe6d4 (diff) | |
download | serenity-026f37b031abc4948505894b6d6a98402e334716.zip |
Kernel: Move Spinlock functions back to arch independent Locking folder
Now that the Spinlock code is not dependent on architectural specific
code anymore, we can move it back to the Locking folder. This also means
that the Spinlock implemenation is now used for the aarch64 kernel.
Diffstat (limited to 'Kernel/Arch')
-rw-r--r-- | Kernel/Arch/Spinlock.h | 80 | ||||
-rw-r--r-- | Kernel/Arch/aarch64/Dummy.cpp | 8 | ||||
-rw-r--r-- | Kernel/Arch/aarch64/Spinlock.cpp | 32 | ||||
-rw-r--r-- | Kernel/Arch/x86/common/Spinlock.cpp | 66 |
4 files changed, 8 insertions, 178 deletions
diff --git a/Kernel/Arch/Spinlock.h b/Kernel/Arch/Spinlock.h deleted file mode 100644 index 05709d5f87..0000000000 --- a/Kernel/Arch/Spinlock.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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; -}; - -} diff --git a/Kernel/Arch/aarch64/Dummy.cpp b/Kernel/Arch/aarch64/Dummy.cpp index 4fc958f271..4bab613404 100644 --- a/Kernel/Arch/aarch64/Dummy.cpp +++ b/Kernel/Arch/aarch64/Dummy.cpp @@ -50,6 +50,14 @@ void Mutex::unlock() } +// LockRank +namespace Kernel { + +void track_lock_acquire(LockRank) { } +void track_lock_release(LockRank) { } + +} + // Inode namespace Kernel { diff --git a/Kernel/Arch/aarch64/Spinlock.cpp b/Kernel/Arch/aarch64/Spinlock.cpp deleted file mode 100644 index bcb81dd2c9..0000000000 --- a/Kernel/Arch/aarch64/Spinlock.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 2022, Timon Kruiper <timonkruiper@gmail.com> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include <Kernel/Arch/Spinlock.h> - -// FIXME: Actually implement the correct logic once the aarch64 build can -// do interrupts and/or has support for multiple processors. - -namespace Kernel { - -InterruptsState Spinlock::lock() -{ - return InterruptsState::Disabled; -} - -void Spinlock::unlock(InterruptsState) -{ -} - -InterruptsState RecursiveSpinlock::lock() -{ - return InterruptsState::Disabled; -} - -void RecursiveSpinlock::unlock(InterruptsState) -{ -} - -} diff --git a/Kernel/Arch/x86/common/Spinlock.cpp b/Kernel/Arch/x86/common/Spinlock.cpp deleted file mode 100644 index 6cf3cfbae5..0000000000 --- a/Kernel/Arch/x86/common/Spinlock.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include <Kernel/Arch/Spinlock.h> - -namespace Kernel { - -InterruptsState Spinlock::lock() -{ - InterruptsState previous_interrupts_state = processor_interrupts_state(); - Processor::enter_critical(); - Processor::disable_interrupts(); - while (m_lock.exchange(1, AK::memory_order_acquire) != 0) - Processor::wait_check(); - track_lock_acquire(m_rank); - return previous_interrupts_state; -} - -void Spinlock::unlock(InterruptsState previous_interrupts_state) -{ - VERIFY(is_locked()); - track_lock_release(m_rank); - m_lock.store(0, AK::memory_order_release); - - Processor::leave_critical(); - restore_processor_interrupts_state(previous_interrupts_state); -} - -InterruptsState RecursiveSpinlock::lock() -{ - InterruptsState previous_interrupts_state = processor_interrupts_state(); - Processor::disable_interrupts(); - Processor::enter_critical(); - auto& proc = Processor::current(); - FlatPtr cpu = FlatPtr(&proc); - FlatPtr expected = 0; - while (!m_lock.compare_exchange_strong(expected, cpu, AK::memory_order_acq_rel)) { - if (expected == cpu) - break; - Processor::wait_check(); - expected = 0; - } - if (m_recursions == 0) - track_lock_acquire(m_rank); - m_recursions++; - return previous_interrupts_state; -} - -void RecursiveSpinlock::unlock(InterruptsState previous_interrupts_state) -{ - VERIFY_INTERRUPTS_DISABLED(); - VERIFY(m_recursions > 0); - VERIFY(m_lock.load(AK::memory_order_relaxed) == FlatPtr(&Processor::current())); - if (--m_recursions == 0) { - track_lock_release(m_rank); - m_lock.store(0, AK::memory_order_release); - } - - Processor::leave_critical(); - restore_processor_interrupts_state(previous_interrupts_state); -} - -} |