diff --git a/Kernel/Locking/MutexContendedResource.h b/Kernel/Locking/MutexContendedResource.h deleted file mode 100644 index 6bb78eec7ba..00000000000 --- a/Kernel/Locking/MutexContendedResource.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2021, the SerenityOS developers. - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace Kernel { - -template -class LockedResource { - AK_MAKE_NONCOPYABLE(LockedResource); - -public: - LockedResource(T* value, Mutex& mutex, LockLocation const& location) - : m_value(value) - , m_mutex_locker(mutex, LockingMode, location) - { - } - - ALWAYS_INLINE T const* operator->() const { return m_value; } - ALWAYS_INLINE T const& operator*() const { return *m_value; } - - ALWAYS_INLINE T* operator->() requires(!IsConst) { return m_value; } - ALWAYS_INLINE T& operator*() requires(!IsConst) { return *m_value; } - - ALWAYS_INLINE T const* get() const { return m_value; } - ALWAYS_INLINE T* get() requires(!IsConst) { return m_value; } - -private: - T* m_value; - MutexLocker m_mutex_locker; -}; - -class MutexContendedResource { - template - friend class LockedResource; - - AK_MAKE_NONCOPYABLE(MutexContendedResource); - AK_MAKE_NONMOVABLE(MutexContendedResource); - -public: - MutexContendedResource() = default; - -protected: - mutable Mutex m_mutex; -}; - -} diff --git a/Kernel/Locking/MutexProtected.h b/Kernel/Locking/MutexProtected.h index 13761f9aa52..8fe7fa31d1f 100644 --- a/Kernel/Locking/MutexProtected.h +++ b/Kernel/Locking/MutexProtected.h @@ -1,33 +1,52 @@ /* * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include -#include +#include namespace Kernel { template -class MutexProtected - : private T - , public MutexContendedResource { +class MutexProtected { AK_MAKE_NONCOPYABLE(MutexProtected); AK_MAKE_NONMOVABLE(MutexProtected); -protected: - using LockedShared = LockedResource; - using LockedExclusive = LockedResource; +private: + template + class Locked { + AK_MAKE_NONCOPYABLE(Locked); + AK_MAKE_NONMOVABLE(Locked); - LockedShared lock_shared(LockLocation const& location) const { return LockedShared(this, this->MutexContendedResource::m_mutex, location); } - LockedExclusive lock_exclusive(LockLocation const& location) { return LockedExclusive(this, this->MutexContendedResource::m_mutex, location); } + public: + Locked(U& value, Mutex& mutex, LockLocation const& location) + : m_value(value) + , m_locker(mutex, lock_mode, location) + { + } + + ALWAYS_INLINE U const* operator->() const { return &m_value; } + ALWAYS_INLINE U const& operator*() const { return m_value; } + + ALWAYS_INLINE U* operator->() requires(!IsConst) { return &m_value; } + ALWAYS_INLINE U& operator*() requires(!IsConst) { return m_value; } + + ALWAYS_INLINE U const& get() const { return &m_value; } + ALWAYS_INLINE U& get() requires(!IsConst) { return &m_value; } + + private: + U& m_value; + MutexLocker m_locker; + }; + + auto lock_shared(LockLocation const& location) const { return Locked(m_value, m_mutex, location); } + auto lock_exclusive(LockLocation const& location) { return Locked(m_value, m_mutex, location); } public: - using T::T; - MutexProtected() = default; template @@ -63,6 +82,10 @@ public: }, location); } + +private: + T m_value; + Mutex mutable m_mutex; }; }