diff options
author | Andreas Kling <kling@serenityos.org> | 2021-08-22 00:58:42 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-08-22 03:34:09 +0200 |
commit | 7d5d26b0481221d3ebf420de346cc33b3e003147 (patch) | |
tree | 1ecae82a92376d142f3b4ac1319869a59d0ecf26 | |
parent | ed6f84c2c96dc5f850a51077e5c5a02fbe29d96b (diff) | |
download | serenity-7d5d26b0481221d3ebf420de346cc33b3e003147.zip |
Kernel: Simplify SpinLockProtected<T>
Same treatment as MutexProtected<T>: inheritance and helper class is
removed, SpinLockProtected now holds a T and a SpinLock.
-rw-r--r-- | Kernel/Locking/SpinLockProtected.h | 45 | ||||
-rw-r--r-- | Kernel/Locking/SpinLockResource.h | 53 |
2 files changed, 34 insertions, 64 deletions
diff --git a/Kernel/Locking/SpinLockProtected.h b/Kernel/Locking/SpinLockProtected.h index 143eb98133..34a09029a2 100644 --- a/Kernel/Locking/SpinLockProtected.h +++ b/Kernel/Locking/SpinLockProtected.h @@ -6,27 +6,46 @@ #pragma once -#include <Kernel/Locking/SpinLockResource.h> +#include <Kernel/Locking/SpinLock.h> namespace Kernel { template<typename T> -class SpinLockProtected - : private T - , public SpinLockContendedResource { +class SpinLockProtected { AK_MAKE_NONCOPYABLE(SpinLockProtected); AK_MAKE_NONMOVABLE(SpinLockProtected); -protected: - using LockedConst = SpinLockLockedResource<T const>; - using LockedMutable = SpinLockLockedResource<T>; +private: + template<typename U> + class Locked { + AK_MAKE_NONCOPYABLE(Locked); + AK_MAKE_NONMOVABLE(Locked); - LockedConst lock_const() const { return LockedConst(static_cast<T const*>(this), this->SpinLockContendedResource::m_spinlock); } - LockedMutable lock_mutable() { return LockedMutable(static_cast<T*>(this), this->SpinLockContendedResource::m_spinlock); } + public: + Locked(U& value, RecursiveSpinLock& spinlock) + : m_value(value) + , m_locker(spinlock) + { + } -public: - using T::T; + ALWAYS_INLINE U const* operator->() const { return &m_value; } + ALWAYS_INLINE U const& operator*() const { return m_value; } + + ALWAYS_INLINE U* operator->() { return &m_value; } + ALWAYS_INLINE U& operator*() { return m_value; } + + ALWAYS_INLINE U const& get() const { return m_value; } + ALWAYS_INLINE U& get() { return m_value; } + + private: + U& m_value; + ScopedSpinLock<RecursiveSpinLock> m_locker; + }; + auto lock_const() const { return Locked<T const>(m_value, m_spinlock); } + auto lock_mutable() { return Locked<T>(m_value, m_spinlock); } + +public: SpinLockProtected() = default; template<typename Callback> @@ -60,6 +79,10 @@ public: callback(item); }); } + +private: + T m_value; + RecursiveSpinLock mutable m_spinlock; }; } diff --git a/Kernel/Locking/SpinLockResource.h b/Kernel/Locking/SpinLockResource.h deleted file mode 100644 index f8d23b5da0..0000000000 --- a/Kernel/Locking/SpinLockResource.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2021, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include <AK/StdLibExtras.h> -#include <Kernel/Locking/SpinLock.h> - -namespace Kernel { - -template<typename T> -class SpinLockLockedResource { - AK_MAKE_NONCOPYABLE(SpinLockLockedResource); - -public: - SpinLockLockedResource(T* value, RecursiveSpinLock& spinlock) - : m_value(value) - , m_scoped_spinlock(spinlock) - { - } - - ALWAYS_INLINE T const* operator->() const { return m_value; } - ALWAYS_INLINE T const& operator*() const { return *m_value; } - - ALWAYS_INLINE T* operator->() { return m_value; } - ALWAYS_INLINE T& operator*() { return *m_value; } - - ALWAYS_INLINE T const* get() const { return m_value; } - ALWAYS_INLINE T* get() { return m_value; } - -private: - T* m_value; - ScopedSpinLock<RecursiveSpinLock> m_scoped_spinlock; -}; - -class SpinLockContendedResource { - template<typename> - friend class SpinLockLockedResource; - - AK_MAKE_NONCOPYABLE(SpinLockContendedResource); - AK_MAKE_NONMOVABLE(SpinLockContendedResource); - -public: - SpinLockContendedResource() = default; - -protected: - mutable RecursiveSpinLock m_spinlock; -}; - -} |