mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40:19 +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:
parent
ed6f84c2c9
commit
7d5d26b048
Notes:
sideshowbarker
2024-07-18 05:24:57 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/7d5d26b0481
2 changed files with 34 additions and 64 deletions
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
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:
|
||||
using T::T;
|
||||
|
||||
SpinLockProtected() = default;
|
||||
|
||||
template<typename Callback>
|
||||
|
@ -60,6 +79,10 @@ public:
|
|||
callback(item);
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
T m_value;
|
||||
RecursiveSpinLock mutable m_spinlock;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in a new issue