1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:27:45 +00:00

Kernel: Simplify SpinLockProtected<T>

Same treatment as MutexProtected<T>: inheritance and helper class is
removed, SpinLockProtected now holds a T and a SpinLock.
This commit is contained in:
Andreas Kling 2021-08-22 00:58:42 +02:00
parent ed6f84c2c9
commit 7d5d26b048
2 changed files with 34 additions and 64 deletions

View file

@ -6,27 +6,46 @@
#pragma once #pragma once
#include <Kernel/Locking/SpinLockResource.h> #include <Kernel/Locking/SpinLock.h>
namespace Kernel { namespace Kernel {
template<typename T> template<typename T>
class SpinLockProtected class SpinLockProtected {
: private T
, public SpinLockContendedResource {
AK_MAKE_NONCOPYABLE(SpinLockProtected); AK_MAKE_NONCOPYABLE(SpinLockProtected);
AK_MAKE_NONMOVABLE(SpinLockProtected); AK_MAKE_NONMOVABLE(SpinLockProtected);
protected: private:
using LockedConst = SpinLockLockedResource<T const>; template<typename U>
using LockedMutable = SpinLockLockedResource<T>; 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); } public:
LockedMutable lock_mutable() { return LockedMutable(static_cast<T*>(this), this->SpinLockContendedResource::m_spinlock); } Locked(U& value, RecursiveSpinLock& spinlock)
: m_value(value)
, m_locker(spinlock)
{
}
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: public:
using T::T;
SpinLockProtected() = default; SpinLockProtected() = default;
template<typename Callback> template<typename Callback>
@ -60,6 +79,10 @@ public:
callback(item); callback(item);
}); });
} }
private:
T m_value;
RecursiveSpinLock mutable m_spinlock;
}; };
} }

View file

@ -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;
};
}