ListedRefCounted.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. namespace Kernel {
  9. // ListedRefCounted<T> is a slot-in replacement for RefCounted<T> to use in classes
  10. // that add themselves to a {Spinlock, Mutex}Protected<IntrusiveList> when constructed.
  11. // The custom unref() implementation here ensures that the list is locked during
  12. // unref(), and that the T is removed from the list before ~T() is invoked.
  13. enum class LockType {
  14. Spinlock,
  15. Mutex,
  16. };
  17. template<typename T, LockType Lock>
  18. class ListedRefCounted : public RefCountedBase {
  19. public:
  20. bool unref() const
  21. {
  22. auto const* that = static_cast<T const*>(this);
  23. auto callback = [&](auto& list) {
  24. auto new_ref_count = deref_base();
  25. if (new_ref_count == 0) {
  26. list.remove(const_cast<T&>(*that));
  27. if constexpr (requires { that->revoke_weak_ptrs(); }) {
  28. that->revoke_weak_ptrs();
  29. }
  30. }
  31. return new_ref_count;
  32. };
  33. RefCountType new_ref_count;
  34. if constexpr (Lock == LockType::Spinlock)
  35. new_ref_count = T::all_instances().with(callback);
  36. else if constexpr (Lock == LockType::Mutex)
  37. new_ref_count = T::all_instances().with_exclusive(callback);
  38. if (new_ref_count == 0) {
  39. if constexpr (requires { that->will_be_destroyed(); })
  40. that->will_be_destroyed();
  41. delete that;
  42. } else if (new_ref_count == 1) {
  43. if constexpr (requires { that->one_ref_left(); })
  44. that->one_ref_left();
  45. }
  46. return new_ref_count == 0;
  47. }
  48. };
  49. }