k_auto_object.h 9.4 KB

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