k_auto_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project & 2024 suyu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <atomic>
  5. #include <string>
  6. #include <boost/intrusive/rbtree.hpp>
  7. #include "common/assert.h"
  8. #include "common/common_funcs.h"
  9. #include "common/common_types.h"
  10. #include "core/hle/kernel/k_class_token.h"
  11. namespace Kernel {
  12. class KernelCore;
  13. class KProcess;
  14. #define KERNEL_AUTOOBJECT_TRAITS_IMPL(CLASS, BASE_CLASS, ATTRIBUTE) \
  15. \
  16. private: \
  17. friend class ::Kernel::KClassTokenGenerator; \
  18. static constexpr inline auto ObjectType = ::Kernel::KClassTokenGenerator::ObjectType::CLASS; \
  19. static constexpr inline const char* const TypeName = #CLASS; \
  20. static constexpr inline ClassTokenType ClassToken() { \
  21. return ::Kernel::ClassToken<CLASS>; \
  22. } \
  23. \
  24. public: \
  25. SUYU_NON_COPYABLE(CLASS); \
  26. SUYU_NON_MOVEABLE(CLASS); \
  27. \
  28. using BaseClass = BASE_CLASS; \
  29. static constexpr TypeObj GetStaticTypeObj() { \
  30. constexpr ClassTokenType Token = ClassToken(); \
  31. return TypeObj(TypeName, Token); \
  32. } \
  33. static constexpr const char* GetStaticTypeName() { \
  34. return TypeName; \
  35. } \
  36. virtual TypeObj GetTypeObj() ATTRIBUTE { \
  37. return GetStaticTypeObj(); \
  38. } \
  39. virtual const char* GetTypeName() ATTRIBUTE { \
  40. return GetStaticTypeName(); \
  41. } \
  42. \
  43. private: \
  44. constexpr bool operator!=(const TypeObj& rhs)
  45. #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \
  46. KERNEL_AUTOOBJECT_TRAITS_IMPL(CLASS, BASE_CLASS, const override)
  47. class KAutoObject {
  48. protected:
  49. class TypeObj {
  50. public:
  51. constexpr explicit TypeObj(const char* n, ClassTokenType tok)
  52. : m_name(n), m_class_token(tok) {}
  53. constexpr const char* GetName() const {
  54. return m_name;
  55. }
  56. constexpr ClassTokenType GetClassToken() const {
  57. return m_class_token;
  58. }
  59. constexpr bool operator==(const TypeObj& rhs) const {
  60. return this->GetClassToken() == rhs.GetClassToken();
  61. }
  62. constexpr bool operator!=(const TypeObj& rhs) const {
  63. return this->GetClassToken() != rhs.GetClassToken();
  64. }
  65. constexpr bool IsDerivedFrom(const TypeObj& rhs) const {
  66. return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken();
  67. }
  68. private:
  69. const char* m_name;
  70. ClassTokenType m_class_token;
  71. };
  72. private:
  73. KERNEL_AUTOOBJECT_TRAITS_IMPL(KAutoObject, KAutoObject, const);
  74. public:
  75. explicit KAutoObject(KernelCore& kernel) : m_kernel(kernel) {
  76. RegisterWithKernel();
  77. }
  78. virtual ~KAutoObject() = default;
  79. static KAutoObject* Create(KAutoObject* ptr);
  80. // Destroy is responsible for destroying the auto object's resources when ref_count hits zero.
  81. virtual void Destroy() {
  82. UNIMPLEMENTED();
  83. }
  84. // Finalize is responsible for cleaning up resource, but does not destroy the object.
  85. virtual void Finalize() {}
  86. virtual KProcess* GetOwner() const {
  87. return nullptr;
  88. }
  89. u32 GetReferenceCount() const {
  90. return m_ref_count.load();
  91. }
  92. bool IsDerivedFrom(const TypeObj& rhs) const {
  93. return this->GetTypeObj().IsDerivedFrom(rhs);
  94. }
  95. bool IsDerivedFrom(const KAutoObject& rhs) const {
  96. return this->IsDerivedFrom(rhs.GetTypeObj());
  97. }
  98. template <typename Derived>
  99. Derived DynamicCast() {
  100. static_assert(std::is_pointer_v<Derived>);
  101. using DerivedType = std::remove_pointer_t<Derived>;
  102. if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
  103. return static_cast<Derived>(this);
  104. } else {
  105. return nullptr;
  106. }
  107. }
  108. template <typename Derived>
  109. const Derived DynamicCast() const {
  110. static_assert(std::is_pointer_v<Derived>);
  111. using DerivedType = std::remove_pointer_t<Derived>;
  112. if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
  113. return static_cast<Derived>(this);
  114. } else {
  115. return nullptr;
  116. }
  117. }
  118. bool Open() {
  119. // Atomically increment the reference count, only if it's positive.
  120. u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
  121. do {
  122. if (cur_ref_count == 0) {
  123. return false;
  124. }
  125. ASSERT(cur_ref_count < cur_ref_count + 1);
  126. } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count + 1,
  127. std::memory_order_relaxed));
  128. return true;
  129. }
  130. void Close() {
  131. // Atomically decrement the reference count, not allowing it to become negative.
  132. u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
  133. do {
  134. ASSERT(cur_ref_count > 0);
  135. } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1,
  136. std::memory_order_acq_rel));
  137. // If ref count hits zero, destroy the object.
  138. if (cur_ref_count - 1 == 0) {
  139. KernelCore& kernel = m_kernel;
  140. this->Destroy();
  141. KAutoObject::UnregisterWithKernel(kernel, this);
  142. }
  143. }
  144. private:
  145. void RegisterWithKernel();
  146. static void UnregisterWithKernel(KernelCore& kernel, KAutoObject* self);
  147. protected:
  148. KernelCore& m_kernel;
  149. private:
  150. std::atomic<u32> m_ref_count{};
  151. };
  152. class KAutoObjectWithListContainer;
  153. class KAutoObjectWithList : public KAutoObject, public boost::intrusive::set_base_hook<> {
  154. public:
  155. explicit KAutoObjectWithList(KernelCore& kernel) : KAutoObject(kernel) {}
  156. static int Compare(const KAutoObjectWithList& lhs, const KAutoObjectWithList& rhs) {
  157. const uintptr_t lid = reinterpret_cast<uintptr_t>(std::addressof(lhs));
  158. const uintptr_t rid = reinterpret_cast<uintptr_t>(std::addressof(rhs));
  159. if (lid < rid) {
  160. return -1;
  161. } else if (lid > rid) {
  162. return 1;
  163. } else {
  164. return 0;
  165. }
  166. }
  167. friend bool operator<(const KAutoObjectWithList& left, const KAutoObjectWithList& right) {
  168. return KAutoObjectWithList::Compare(left, right) < 0;
  169. }
  170. public:
  171. virtual u64 GetId() const {
  172. return reinterpret_cast<u64>(this);
  173. }
  174. private:
  175. friend class KAutoObjectWithListContainer;
  176. };
  177. template <typename T>
  178. class KScopedAutoObject {
  179. public:
  180. SUYU_NON_COPYABLE(KScopedAutoObject);
  181. constexpr KScopedAutoObject() = default;
  182. constexpr KScopedAutoObject(T* o) : m_obj(o) {
  183. if (m_obj != nullptr) {
  184. m_obj->Open();
  185. }
  186. }
  187. ~KScopedAutoObject() {
  188. if (m_obj != nullptr) {
  189. m_obj->Close();
  190. }
  191. m_obj = nullptr;
  192. }
  193. template <typename U>
  194. requires(std::derived_from<T, U> || std::derived_from<U, T>)
  195. constexpr KScopedAutoObject(KScopedAutoObject<U>&& rhs) {
  196. if constexpr (std::derived_from<U, T>) {
  197. // Upcast.
  198. m_obj = rhs.m_obj;
  199. rhs.m_obj = nullptr;
  200. } else {
  201. // Downcast.
  202. T* derived = nullptr;
  203. if (rhs.m_obj != nullptr) {
  204. derived = rhs.m_obj->template DynamicCast<T*>();
  205. if (derived == nullptr) {
  206. rhs.m_obj->Close();
  207. }
  208. }
  209. m_obj = derived;
  210. rhs.m_obj = nullptr;
  211. }
  212. }
  213. constexpr KScopedAutoObject<T>& operator=(KScopedAutoObject<T>&& rhs) {
  214. rhs.Swap(*this);
  215. return *this;
  216. }
  217. constexpr T* operator->() {
  218. return m_obj;
  219. }
  220. constexpr T& operator*() {
  221. return *m_obj;
  222. }
  223. constexpr void Reset(T* o) {
  224. KScopedAutoObject(o).Swap(*this);
  225. }
  226. constexpr T* GetPointerUnsafe() {
  227. return m_obj;
  228. }
  229. constexpr T* GetPointerUnsafe() const {
  230. return m_obj;
  231. }
  232. constexpr T* ReleasePointerUnsafe() {
  233. T* ret = m_obj;
  234. m_obj = nullptr;
  235. return ret;
  236. }
  237. constexpr bool IsNull() const {
  238. return m_obj == nullptr;
  239. }
  240. constexpr bool IsNotNull() const {
  241. return m_obj != nullptr;
  242. }
  243. private:
  244. template <typename U>
  245. friend class KScopedAutoObject;
  246. private:
  247. T* m_obj{};
  248. private:
  249. constexpr void Swap(KScopedAutoObject& rhs) noexcept {
  250. std::swap(m_obj, rhs.m_obj);
  251. }
  252. };
  253. } // namespace Kernel