k_auto_object.h 9.9 KB

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