summaryrefslogtreecommitdiff
path: root/Kernel
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-08-16 21:50:28 +0200
committerAndreas Kling <kling@serenityos.org>2021-08-17 01:21:47 +0200
commitc410f08c2b0fad3aee0ba42c05bf4c0542d4a59d (patch)
tree6e9df5d8f2dae6d3e53c9f3e81b6e9eac57d58f4 /Kernel
parent641083f3b89b7a7a5928b0eedb1a236949dcdc42 (diff)
downloadserenity-c410f08c2b0fad3aee0ba42c05bf4c0542d4a59d.zip
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>.
Diffstat (limited to 'Kernel')
-rw-r--r--Kernel/Library/ListedRefCounted.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/Kernel/Library/ListedRefCounted.h b/Kernel/Library/ListedRefCounted.h
new file mode 100644
index 0000000000..e0fdae1edc
--- /dev/null
+++ b/Kernel/Library/ListedRefCounted.h
@@ -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;
+ }
+};
+
+}