2021-07-24 17:42:39 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-22 02:37:17 +03:00
|
|
|
#include <Kernel/Locking/Spinlock.h>
|
2021-07-24 17:42:39 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
2022-11-09 13:39:58 +03:00
|
|
|
template<typename T, LockRank Rank>
|
2021-08-22 02:37:17 +03:00
|
|
|
class SpinlockProtected {
|
|
|
|
AK_MAKE_NONCOPYABLE(SpinlockProtected);
|
|
|
|
AK_MAKE_NONMOVABLE(SpinlockProtected);
|
2021-07-24 17:42:39 +03:00
|
|
|
|
2021-08-22 01:58:42 +03:00
|
|
|
private:
|
|
|
|
template<typename U>
|
|
|
|
class Locked {
|
|
|
|
AK_MAKE_NONCOPYABLE(Locked);
|
|
|
|
AK_MAKE_NONMOVABLE(Locked);
|
2021-07-24 17:42:39 +03:00
|
|
|
|
2021-08-22 01:58:42 +03:00
|
|
|
public:
|
2022-11-09 13:39:58 +03:00
|
|
|
Locked(U& value, RecursiveSpinlock<Rank>& spinlock)
|
2021-08-22 01:58:42 +03:00
|
|
|
: m_value(value)
|
|
|
|
, m_locker(spinlock)
|
|
|
|
{
|
|
|
|
}
|
2021-07-24 17:42:39 +03:00
|
|
|
|
2021-08-22 01:58:42 +03:00
|
|
|
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;
|
2022-11-09 13:39:58 +03:00
|
|
|
SpinlockLocker<RecursiveSpinlock<Rank>> m_locker;
|
2021-08-22 01:58:42 +03:00
|
|
|
};
|
2021-07-24 17:42:39 +03:00
|
|
|
|
2021-08-22 01:58:42 +03:00
|
|
|
auto lock_const() const { return Locked<T const>(m_value, m_spinlock); }
|
|
|
|
auto lock_mutable() { return Locked<T>(m_value, m_spinlock); }
|
|
|
|
|
|
|
|
public:
|
2022-03-07 22:25:43 +03:00
|
|
|
template<typename... Args>
|
2022-11-09 13:39:58 +03:00
|
|
|
SpinlockProtected(Args&&... args)
|
2022-03-07 22:25:43 +03:00
|
|
|
: m_value(forward<Args>(args)...)
|
|
|
|
{
|
|
|
|
}
|
2021-07-24 17:42:39 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
with([&](auto const& value) {
|
2021-07-24 17:42:39 +03:00
|
|
|
for (auto& item : value)
|
|
|
|
callback(item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
|
|
|
void for_each(Callback callback)
|
|
|
|
{
|
|
|
|
with([&](auto& value) {
|
|
|
|
for (auto& item : value)
|
|
|
|
callback(item);
|
|
|
|
});
|
|
|
|
}
|
2021-08-22 01:58:42 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
T m_value;
|
2022-11-09 13:39:58 +03:00
|
|
|
RecursiveSpinlock<Rank> mutable m_spinlock;
|
|
|
|
static constexpr LockRank const m_rank { Rank };
|
2021-07-24 17:42:39 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|