Kernel: Implement contended, ref-counted resource framework

This is some syntaxic sugar to use a ContendedResource object with
reference counting. This effectively dissociates merely holding a
reference to an object and actually using the object in a way that
requires locking it against concurrent use.
This commit is contained in:
Jean-Baptiste Boric 2021-07-19 00:17:17 +02:00 committed by Andreas Kling
parent 019ad8a507
commit 39ceefa5dd
Notes: sideshowbarker 2024-07-18 07:20:40 +09:00
4 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullRefPtr.h>
#include <Kernel/Locking/RefCountedContended.h>
namespace Kernel {
template<typename T>
using NonnullRefContendedPtr = NonnullRefPtr<RefCountedContended<T>>;
}

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullPtrVector.h>
#include <Kernel/Locking/NonnullRefContendedPtr.h>
namespace Kernel {
template<typename T, size_t inline_capacity = 0>
using NonnullRefContendedPtrVector = AK::NonnullPtrVector<NonnullRefContendedPtr<T>, inline_capacity>;
}

View File

@ -0,0 +1,17 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefPtr.h>
#include <Kernel/Locking/RefCountedContended.h>
namespace Kernel {
template<typename T>
using RefContendedPtr = RefPtr<RefCountedContended<T>>;
}

View File

@ -0,0 +1,77 @@
/*
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <Kernel/Locking/ContendedResource.h>
#include <Kernel/Locking/Mutex.h>
namespace Kernel {
template<typename T>
class RefCountedContended : public ContendedResource
, public AK::RefCountedBase {
AK_MAKE_NONCOPYABLE(RefCountedContended);
AK_MAKE_NONMOVABLE(RefCountedContended);
protected:
using LockedShared = LockedResource<T const, LockMode::Shared>;
using LockedExclusive = LockedResource<T, LockMode::Exclusive>;
LockedShared lock_shared() const { return LockedShared(static_cast<T const*>(this), this->ContendedResource::m_mutex); }
LockedExclusive lock_exclusive() { return LockedExclusive(static_cast<T*>(this), this->ContendedResource::m_mutex); }
public:
RefCountedContended() = default;
bool unref() const
{
auto new_ref_count = deref_base();
if (new_ref_count == 0) {
AK::call_will_be_destroyed_if_present(static_cast<T*>(const_cast<RefCountedContended*>(this)->lock_exclusive().get()));
delete static_cast<const T*>(this);
return true;
} else if (new_ref_count == 1) {
AK::call_one_ref_left_if_present(static_cast<T*>(const_cast<RefCountedContended*>(this)->lock_exclusive().get()));
}
return false;
}
template<typename Callback>
decltype(auto) with_shared(Callback callback) const
{
auto lock = lock_shared();
return callback(*lock);
}
template<typename Callback>
decltype(auto) with_exclusive(Callback callback)
{
auto lock = lock_exclusive();
return callback(*lock);
}
template<typename Callback>
void for_each_shared(Callback callback) const
{
with_shared([&](const auto& value) {
for (auto& item : value)
callback(item);
});
}
template<typename Callback>
void for_each_exclusive(Callback callback)
{
with_exclusive([&](auto& value) {
for (auto& item : value)
callback(item);
});
}
};
}