mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 18:32:28 -05:00
Kernel: Add ListedRefCounted<T> template class
This class implements the emerging "ref-counted object that participates in a lock-protected list that requires safe removal on unref()" pattern as a base class that can be inherited in place of RefCounted<T>.
This commit is contained in:
parent
641083f3b8
commit
c410f08c2b
1 changed files with 30 additions and 0 deletions
30
Kernel/Library/ListedRefCounted.h
Normal file
30
Kernel/Library/ListedRefCounted.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/RefCounted.h>
|
||||
|
||||
namespace Kernel {
|
||||
|
||||
template<typename T>
|
||||
class ListedRefCounted : public RefCountedBase {
|
||||
public:
|
||||
bool unref() const
|
||||
{
|
||||
bool did_hit_zero = T::all_instances().with([&](auto& list) {
|
||||
if (deref_base())
|
||||
return false;
|
||||
list.remove(const_cast<T&>(static_cast<T const&>(*this)));
|
||||
return true;
|
||||
});
|
||||
if (did_hit_zero)
|
||||
delete const_cast<T*>(static_cast<T const*>(this));
|
||||
return did_hit_zero;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue