1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 11:47:45 +00:00

Kernel: Add lock debugging to ProtectedValue / RefCountedContended

Enable the LOCK_DEBUG functionality for these new APIs, as it looks
like we want to move the whole system to use this in the not so distant
future. :^)
This commit is contained in:
Brian Gianforcaro 2021-08-07 04:28:59 -07:00 committed by Andreas Kling
parent bea74f4b77
commit bffcb3e92a
3 changed files with 28 additions and 22 deletions

View file

@ -16,9 +16,9 @@ class LockedResource {
AK_MAKE_NONCOPYABLE(LockedResource); AK_MAKE_NONCOPYABLE(LockedResource);
public: public:
LockedResource(T* value, Mutex& mutex) LockedResource(T* value, Mutex& mutex, LockLocation const& location)
: m_value(value) : m_value(value)
, m_mutex_locker(mutex, LockingMode) , m_mutex_locker(mutex, LockingMode, location)
{ {
} }

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <Kernel/Locking/ContendedResource.h> #include <Kernel/Locking/ContendedResource.h>
#include <Kernel/Locking/LockLocation.h>
namespace Kernel { namespace Kernel {
@ -20,8 +21,8 @@ protected:
using LockedShared = LockedResource<T const, LockMode::Shared>; using LockedShared = LockedResource<T const, LockMode::Shared>;
using LockedExclusive = LockedResource<T, LockMode::Exclusive>; using LockedExclusive = LockedResource<T, LockMode::Exclusive>;
LockedShared lock_shared() const { return LockedShared(this, this->ContendedResource::m_mutex); } LockedShared lock_shared(LockLocation const& location) const { return LockedShared(this, this->ContendedResource::m_mutex, location); }
LockedExclusive lock_exclusive() { return LockedExclusive(this, this->ContendedResource::m_mutex); } LockedExclusive lock_exclusive(LockLocation const& location) { return LockedExclusive(this, this->ContendedResource::m_mutex, location); }
public: public:
using T::T; using T::T;
@ -29,35 +30,37 @@ public:
ProtectedValue() = default; ProtectedValue() = default;
template<typename Callback> template<typename Callback>
decltype(auto) with_shared(Callback callback) const decltype(auto) with_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{ {
auto lock = lock_shared(); auto lock = lock_shared(location);
return callback(*lock); return callback(*lock);
} }
template<typename Callback> template<typename Callback>
decltype(auto) with_exclusive(Callback callback) decltype(auto) with_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{ {
auto lock = lock_exclusive(); auto lock = lock_exclusive(location);
return callback(*lock); return callback(*lock);
} }
template<typename Callback> template<typename Callback>
void for_each_shared(Callback callback) const void for_each_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{ {
with_shared([&](const auto& value) { with_shared([&](const auto& value) {
for (auto& item : value) for (auto& item : value)
callback(item); callback(item);
}); },
location);
} }
template<typename Callback> template<typename Callback>
void for_each_exclusive(Callback callback) void for_each_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{ {
with_exclusive([&](auto& value) { with_exclusive([&](auto& value) {
for (auto& item : value) for (auto& item : value)
callback(item); callback(item);
}); },
location);
} }
}; };

View file

@ -8,6 +8,7 @@
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <Kernel/Locking/ContendedResource.h> #include <Kernel/Locking/ContendedResource.h>
#include <Kernel/Locking/LockLocation.h>
#include <Kernel/Locking/Mutex.h> #include <Kernel/Locking/Mutex.h>
namespace Kernel { namespace Kernel {
@ -22,8 +23,8 @@ protected:
using LockedShared = LockedResource<T const, LockMode::Shared>; using LockedShared = LockedResource<T const, LockMode::Shared>;
using LockedExclusive = LockedResource<T, LockMode::Exclusive>; using LockedExclusive = LockedResource<T, LockMode::Exclusive>;
LockedShared lock_shared() const { return LockedShared(static_cast<T const*>(this), this->ContendedResource::m_mutex); } LockedShared lock_shared(LockLocation const& location) const { return LockedShared(static_cast<T const*>(this), this->ContendedResource::m_mutex, location); }
LockedExclusive lock_exclusive() { return LockedExclusive(static_cast<T*>(this), this->ContendedResource::m_mutex); } LockedExclusive lock_exclusive(LockLocation const& location) { return LockedExclusive(static_cast<T*>(this), this->ContendedResource::m_mutex, location); }
public: public:
RefCountedContended() = default; RefCountedContended() = default;
@ -42,35 +43,37 @@ public:
} }
template<typename Callback> template<typename Callback>
decltype(auto) with_shared(Callback callback) const decltype(auto) with_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{ {
auto lock = lock_shared(); auto lock = lock_shared(location);
return callback(*lock); return callback(*lock);
} }
template<typename Callback> template<typename Callback>
decltype(auto) with_exclusive(Callback callback) decltype(auto) with_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{ {
auto lock = lock_exclusive(); auto lock = lock_exclusive(location);
return callback(*lock); return callback(*lock);
} }
template<typename Callback> template<typename Callback>
void for_each_shared(Callback callback) const void for_each_shared(Callback callback, LockLocation const& location = LockLocation::current()) const
{ {
with_shared([&](const auto& value) { with_shared([&](const auto& value) {
for (auto& item : value) for (auto& item : value)
callback(item); callback(item);
}); },
location);
} }
template<typename Callback> template<typename Callback>
void for_each_exclusive(Callback callback) void for_each_exclusive(Callback callback, LockLocation const& location = LockLocation::current())
{ {
with_exclusive([&](auto& value) { with_exclusive([&](auto& value) {
for (auto& item : value) for (auto& item : value)
callback(item); callback(item);
}); },
location);
} }
}; };