mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-11 01:06:01 +03:00
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:
parent
019ad8a507
commit
39ceefa5dd
Notes:
sideshowbarker
2024-07-18 07:20:40 +09:00
Author: https://github.com/boricj Commit: https://github.com/SerenityOS/serenity/commit/39ceefa5ddc Pull-request: https://github.com/SerenityOS/serenity/pull/8851 Reviewed-by: https://github.com/awesomekling ✅ Reviewed-by: https://github.com/bgianfo
17
Kernel/Locking/NonnullRefContendedPtr.h
Normal file
17
Kernel/Locking/NonnullRefContendedPtr.h
Normal 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>>;
|
||||
|
||||
}
|
17
Kernel/Locking/NonnullRefContendedPtrVector.h
Normal file
17
Kernel/Locking/NonnullRefContendedPtrVector.h
Normal 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>;
|
||||
|
||||
}
|
17
Kernel/Locking/RefContendedPtr.h
Normal file
17
Kernel/Locking/RefContendedPtr.h
Normal 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>>;
|
||||
|
||||
}
|
77
Kernel/Locking/RefCountedContended.h
Normal file
77
Kernel/Locking/RefCountedContended.h
Normal 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);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user