ladybird/Kernel/Locking/SpinlockProtected.h
kleines Filmröllchen a6a439243f Kernel: Turn lock ranks into template parameters
This step would ideally not have been necessary (increases amount of
refactoring and templates necessary, which in turn increases build
times), but it gives us a couple of nice properties:
- SpinlockProtected inside Singleton (a very common combination) can now
  obtain any lock rank just via the template parameter. It was not
  previously possible to do this with SingletonInstanceCreator magic.
- SpinlockProtected's lock rank is now mandatory; this is the majority
  of cases and allows us to see where we're still missing proper ranks.
- The type already informs us what lock rank a lock has, which aids code
  readability and (possibly, if gdb cooperates) lock mismatch debugging.
- The rank of a lock can no longer be dynamic, which is not something we
  wanted in the first place (or made use of). Locks randomly changing
  their rank sounds like a disaster waiting to happen.
- In some places, we might be able to statically check that locks are
  taken in the right order (with the right lock rank checking
  implementation) as rank information is fully statically known.

This refactoring even more exposes the fact that Mutex has no lock rank
capabilites, which is not fixed here.
2023-01-02 18:15:27 -05:00

94 lines
2.2 KiB
C++

/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <Kernel/Locking/Spinlock.h>
namespace Kernel {
template<typename T, LockRank Rank>
class SpinlockProtected {
AK_MAKE_NONCOPYABLE(SpinlockProtected);
AK_MAKE_NONMOVABLE(SpinlockProtected);
private:
template<typename U>
class Locked {
AK_MAKE_NONCOPYABLE(Locked);
AK_MAKE_NONMOVABLE(Locked);
public:
Locked(U& value, RecursiveSpinlock<Rank>& 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;
SpinlockLocker<RecursiveSpinlock<Rank>> 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:
template<typename... Args>
SpinlockProtected(Args&&... args)
: m_value(forward<Args>(args)...)
{
}
template<typename Callback>
decltype(auto) with(Callback callback) const
{
auto lock = lock_const();
return callback(*lock);
}
template<typename Callback>
decltype(auto) with(Callback callback)
{
auto lock = lock_mutable();
return callback(*lock);
}
template<typename Callback>
void for_each_const(Callback callback) const
{
with([&](auto const& value) {
for (auto& item : value)
callback(item);
});
}
template<typename Callback>
void for_each(Callback callback)
{
with([&](auto& value) {
for (auto& item : value)
callback(item);
});
}
private:
T m_value;
RecursiveSpinlock<Rank> mutable m_spinlock;
static constexpr LockRank const m_rank { Rank };
};
}