k_auto_object.h 9.8 KB

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