2021-07-18 11:03:10 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-08-22 01:20:03 +03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2021-07-18 11:03:10 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-08-22 01:20:03 +03:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2021-07-18 11:03:10 +03:00
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
|
|
template<typename T>
|
2021-08-22 01:20:03 +03:00
|
|
|
class MutexProtected {
|
2021-08-22 00:31:15 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(MutexProtected);
|
|
|
|
AK_MAKE_NONMOVABLE(MutexProtected);
|
2021-07-18 11:03:10 +03:00
|
|
|
|
2021-08-22 01:20:03 +03:00
|
|
|
private:
|
|
|
|
template<typename U, LockMode lock_mode>
|
|
|
|
class Locked {
|
|
|
|
AK_MAKE_NONCOPYABLE(Locked);
|
|
|
|
AK_MAKE_NONMOVABLE(Locked);
|
2021-07-18 11:03:10 +03:00
|
|
|
|
2021-08-22 01:20:03 +03:00
|
|
|
public:
|
|
|
|
Locked(U& value, Mutex& mutex, LockLocation const& location)
|
|
|
|
: m_value(value)
|
|
|
|
, m_locker(mutex, lock_mode, location)
|
|
|
|
{
|
|
|
|
}
|
2021-07-18 11:03:10 +03:00
|
|
|
|
2021-08-22 01:20:03 +03:00
|
|
|
ALWAYS_INLINE U const* operator->() const { return &m_value; }
|
|
|
|
ALWAYS_INLINE U const& operator*() const { return m_value; }
|
|
|
|
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE U* operator->()
|
|
|
|
requires(!IsConst<U>)
|
|
|
|
{
|
|
|
|
return &m_value;
|
|
|
|
}
|
|
|
|
ALWAYS_INLINE U& operator*()
|
|
|
|
requires(!IsConst<U>)
|
|
|
|
{
|
|
|
|
return m_value;
|
|
|
|
}
|
2021-08-22 01:20:03 +03:00
|
|
|
|
|
|
|
ALWAYS_INLINE U const& get() const { return &m_value; }
|
2022-10-17 01:06:11 +03:00
|
|
|
ALWAYS_INLINE U& get()
|
|
|
|
requires(!IsConst<U>)
|
|
|
|
{
|
|
|
|
return &m_value;
|
|
|
|
}
|
2021-08-22 01:20:03 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
U& m_value;
|
|
|
|
MutexLocker m_locker;
|
|
|
|
};
|
2021-07-18 11:03:10 +03:00
|
|
|
|
2021-08-22 01:20:03 +03:00
|
|
|
auto lock_shared(LockLocation const& location) const { return Locked<T const, LockMode::Shared>(m_value, m_mutex, location); }
|
|
|
|
auto lock_exclusive(LockLocation const& location) { return Locked<T, LockMode::Exclusive>(m_value, m_mutex, location); }
|
|
|
|
|
|
|
|
public:
|
2021-08-22 00:31:15 +03:00
|
|
|
MutexProtected() = default;
|
2021-07-18 11:03:10 +03:00
|
|
|
|
|
|
|
template<typename Callback>
|
2021-08-07 14:28:59 +03:00
|
|
|
decltype(auto) with_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
|
2021-07-18 11:03:10 +03:00
|
|
|
{
|
2021-08-07 14:28:59 +03:00
|
|
|
auto lock = lock_shared(location);
|
2021-07-18 11:03:10 +03:00
|
|
|
return callback(*lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2021-08-07 14:28:59 +03:00
|
|
|
decltype(auto) with_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
|
2021-07-18 11:03:10 +03:00
|
|
|
{
|
2021-08-07 14:28:59 +03:00
|
|
|
auto lock = lock_exclusive(location);
|
2021-07-18 11:03:10 +03:00
|
|
|
return callback(*lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2021-08-07 14:28:59 +03:00
|
|
|
void for_each_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
|
2021-07-18 11:03:10 +03:00
|
|
|
{
|
2022-04-01 20:58:27 +03:00
|
|
|
with_shared([&](auto const& value) {
|
2021-07-18 11:03:10 +03:00
|
|
|
for (auto& item : value)
|
|
|
|
callback(item);
|
2021-08-07 14:28:59 +03:00
|
|
|
},
|
|
|
|
location);
|
2021-07-18 11:03:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callback>
|
2021-08-07 14:28:59 +03:00
|
|
|
void for_each_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
|
2021-07-18 11:03:10 +03:00
|
|
|
{
|
|
|
|
with_exclusive([&](auto& value) {
|
|
|
|
for (auto& item : value)
|
|
|
|
callback(item);
|
2021-08-07 14:28:59 +03:00
|
|
|
},
|
|
|
|
location);
|
2021-07-18 11:03:10 +03:00
|
|
|
}
|
2021-08-22 01:20:03 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
Mutex mutable m_mutex;
|
2021-07-18 11:03:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|