1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:28:11 +00:00
serenity/Kernel/Library/ListedRefCounted.h
Idan Horowitz be91b4fe3e Kernel: Support Mutex Protected lists in ListedRefCounted
This will allow us to support Mutex Protected lists like the custodies
list as well.
2021-12-29 12:04:15 +01:00

50 lines
1.5 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
namespace Kernel {
// ListedRefCounted<T> is a slot-in replacement for RefCounted<T> to use in classes
// that add themselves to a {Spinlock, Mutex}Protected<IntrusiveList> when constructed.
// The custom unref() implementation here ensures that the list is locked during
// unref(), and that the T is removed from the list before ~T() is invoked.
enum class LockType {
Spinlock,
Mutex,
};
template<typename T, LockType Lock>
class ListedRefCounted : public RefCountedBase {
public:
bool unref() const
{
auto callback = [&](auto& list) {
auto new_ref_count = deref_base();
if (new_ref_count == 0)
list.remove(const_cast<T&>(static_cast<T const&>(*this)));
return new_ref_count;
};
RefCountType new_ref_count;
if constexpr (Lock == LockType::Spinlock)
new_ref_count = T::all_instances().with(callback);
else if constexpr (Lock == LockType::Mutex)
new_ref_count = T::all_instances().with_exclusive(callback);
if (new_ref_count == 0) {
call_will_be_destroyed_if_present(static_cast<const T*>(this));
delete const_cast<T*>(static_cast<T const*>(this));
} else if (new_ref_count == 1) {
call_one_ref_left_if_present(static_cast<T const*>(this));
}
return new_ref_count == 0;
}
};
}