k_auto_object.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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() { \
  21. return ::Kernel::ClassToken<CLASS>; \
  22. } \
  23. \
  24. public: \
  25. YUZU_NON_COPYABLE(CLASS); \
  26. YUZU_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_) : 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. this->Destroy();
  140. this->UnregisterWithKernel();
  141. }
  142. }
  143. const std::string& GetName() const {
  144. return name;
  145. }
  146. private:
  147. void RegisterWithKernel();
  148. void UnregisterWithKernel();
  149. protected:
  150. KernelCore& kernel;
  151. std::string name;
  152. private:
  153. std::atomic<u32> m_ref_count{};
  154. };
  155. class KAutoObjectWithListContainer;
  156. class KAutoObjectWithList : public KAutoObject, public boost::intrusive::set_base_hook<> {
  157. public:
  158. explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_) {}
  159. static int Compare(const KAutoObjectWithList& lhs, const KAutoObjectWithList& rhs) {
  160. const u64 lid = lhs.GetId();
  161. const u64 rid = rhs.GetId();
  162. if (lid < rid) {
  163. return -1;
  164. } else if (lid > rid) {
  165. return 1;
  166. } else {
  167. return 0;
  168. }
  169. }
  170. friend bool operator<(const KAutoObjectWithList& left, const KAutoObjectWithList& right) {
  171. return &left < &right;
  172. }
  173. public:
  174. virtual u64 GetId() const {
  175. return reinterpret_cast<u64>(this);
  176. }
  177. virtual const std::string& GetName() const {
  178. return name;
  179. }
  180. private:
  181. friend class KAutoObjectWithListContainer;
  182. };
  183. template <typename T>
  184. class KScopedAutoObject {
  185. public:
  186. YUZU_NON_COPYABLE(KScopedAutoObject);
  187. constexpr KScopedAutoObject() = default;
  188. constexpr KScopedAutoObject(T* o) : m_obj(o) {
  189. if (m_obj != nullptr) {
  190. m_obj->Open();
  191. }
  192. }
  193. ~KScopedAutoObject() {
  194. if (m_obj != nullptr) {
  195. m_obj->Close();
  196. }
  197. m_obj = nullptr;
  198. }
  199. template <typename U>
  200. requires(std::derived_from<T, U> ||
  201. std::derived_from<U, T>) constexpr KScopedAutoObject(KScopedAutoObject<U>&& rhs) {
  202. if constexpr (std::derived_from<U, T>) {
  203. // Upcast.
  204. m_obj = rhs.m_obj;
  205. rhs.m_obj = nullptr;
  206. } else {
  207. // Downcast.
  208. T* derived = nullptr;
  209. if (rhs.m_obj != nullptr) {
  210. derived = rhs.m_obj->template DynamicCast<T*>();
  211. if (derived == nullptr) {
  212. rhs.m_obj->Close();
  213. }
  214. }
  215. m_obj = derived;
  216. rhs.m_obj = nullptr;
  217. }
  218. }
  219. constexpr KScopedAutoObject<T>& operator=(KScopedAutoObject<T>&& rhs) {
  220. rhs.Swap(*this);
  221. return *this;
  222. }
  223. constexpr T* operator->() {
  224. return m_obj;
  225. }
  226. constexpr T& operator*() {
  227. return *m_obj;
  228. }
  229. constexpr void Reset(T* o) {
  230. KScopedAutoObject(o).Swap(*this);
  231. }
  232. constexpr T* GetPointerUnsafe() {
  233. return m_obj;
  234. }
  235. constexpr T* GetPointerUnsafe() const {
  236. return m_obj;
  237. }
  238. constexpr T* ReleasePointerUnsafe() {
  239. T* ret = m_obj;
  240. m_obj = nullptr;
  241. return ret;
  242. }
  243. constexpr bool IsNull() const {
  244. return m_obj == nullptr;
  245. }
  246. constexpr bool IsNotNull() const {
  247. return m_obj != nullptr;
  248. }
  249. private:
  250. template <typename U>
  251. friend class KScopedAutoObject;
  252. private:
  253. T* m_obj{};
  254. private:
  255. constexpr void Swap(KScopedAutoObject& rhs) noexcept {
  256. std::swap(m_obj, rhs.m_obj);
  257. }
  258. };
  259. } // namespace Kernel