summaryrefslogtreecommitdiff
path: root/Kernel/Locking
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-08-22 00:20:03 +0200
committerAndreas Kling <kling@serenityos.org>2021-08-22 03:34:09 +0200
commit532ffa7ddba18fb3b04c787988599aca73a4d789 (patch)
tree5c8f7857da92b3cd7ae5c2409d5c7d68f1d3257f /Kernel/Locking
parentc2fc33becd8df6b84a7487565287d34e4607d588 (diff)
downloadserenity-532ffa7ddba18fb3b04c787988599aca73a4d789.zip
Kernel: Simplify MutexProtected<T>
This patch removes the MutexContendedResource<T> helper class, and MutexProtected<T> no longer inherits from T. Instead, MutexProtected<T> simply has a T and a Mutex. The LockedResource<T, LockMode> helper class is made a private nested class in MutexProtected.
Diffstat (limited to 'Kernel/Locking')
-rw-r--r--Kernel/Locking/MutexContendedResource.h53
-rw-r--r--Kernel/Locking/MutexProtected.h47
2 files changed, 35 insertions, 65 deletions
diff --git a/Kernel/Locking/MutexContendedResource.h b/Kernel/Locking/MutexContendedResource.h
deleted file mode 100644
index 6bb78eec7b..0000000000
--- 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 <AK/StdLibExtras.h>
-#include <Kernel/Locking/Mutex.h>
-
-namespace Kernel {
-
-template<typename T, LockMode LockingMode>
-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<T>) { return m_value; }
- ALWAYS_INLINE T& operator*() requires(!IsConst<T>) { return *m_value; }
-
- ALWAYS_INLINE T const* get() const { return m_value; }
- ALWAYS_INLINE T* get() requires(!IsConst<T>) { return m_value; }
-
-private:
- T* m_value;
- MutexLocker m_mutex_locker;
-};
-
-class MutexContendedResource {
- template<typename, LockMode>
- 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 13761f9aa5..8fe7fa31d1 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 <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
-#include <Kernel/Locking/LockLocation.h>
-#include <Kernel/Locking/MutexContendedResource.h>
+#include <Kernel/Locking/Mutex.h>
namespace Kernel {
template<typename T>
-class MutexProtected
- : private T
- , public MutexContendedResource {
+class MutexProtected {
AK_MAKE_NONCOPYABLE(MutexProtected);
AK_MAKE_NONMOVABLE(MutexProtected);
-protected:
- using LockedShared = LockedResource<T const, LockMode::Shared>;
- using LockedExclusive = LockedResource<T, LockMode::Exclusive>;
+private:
+ template<typename U, LockMode lock_mode>
+ 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)
+ {
+ }
-public:
- using T::T;
+ ALWAYS_INLINE U const* operator->() const { return &m_value; }
+ ALWAYS_INLINE U const& operator*() const { return m_value; }
+
+ ALWAYS_INLINE U* operator->() requires(!IsConst<U>) { return &m_value; }
+ ALWAYS_INLINE U& operator*() requires(!IsConst<U>) { return m_value; }
+
+ ALWAYS_INLINE U const& get() const { return &m_value; }
+ ALWAYS_INLINE U& get() requires(!IsConst<U>) { return &m_value; }
+
+ private:
+ U& m_value;
+ MutexLocker m_locker;
+ };
+ 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:
MutexProtected() = default;
template<typename Callback>
@@ -63,6 +82,10 @@ public:
},
location);
}
+
+private:
+ T m_value;
+ Mutex mutable m_mutex;
};
}