k_auto_object.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 "common/assert.h"
  7. #include "common/common_funcs.h"
  8. #include "common/common_types.h"
  9. #include "common/intrusive_red_black_tree.h"
  10. #include "core/hle/kernel/k_class_token.h"
  11. namespace Kernel {
  12. class KernelCore;
  13. class KProcess;
  14. using Handle = u32;
  15. #define KERNEL_AUTOOBJECT_TRAITS(CLASS, BASE_CLASS) \
  16. NON_COPYABLE(CLASS); \
  17. 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() { \
  40. return GetStaticTypeName(); \
  41. } \
  42. \
  43. private:
  44. class KAutoObject {
  45. protected:
  46. class TypeObj {
  47. private:
  48. const char* m_name;
  49. ClassTokenType m_class_token;
  50. public:
  51. constexpr explicit TypeObj(const char* n, ClassTokenType tok)
  52. : m_name(n), m_class_token(tok) { // ...
  53. }
  54. constexpr const char* GetName() const {
  55. return m_name;
  56. }
  57. constexpr ClassTokenType GetClassToken() const {
  58. return m_class_token;
  59. }
  60. constexpr bool operator==(const TypeObj& rhs) {
  61. return this->GetClassToken() == rhs.GetClassToken();
  62. }
  63. constexpr bool operator!=(const TypeObj& rhs) {
  64. return this->GetClassToken() != rhs.GetClassToken();
  65. }
  66. constexpr bool IsDerivedFrom(const TypeObj& rhs) {
  67. return (this->GetClassToken() | rhs.GetClassToken()) == this->GetClassToken();
  68. }
  69. };
  70. private:
  71. KERNEL_AUTOOBJECT_TRAITS(KAutoObject, KAutoObject);
  72. private:
  73. std::atomic<u32> m_ref_count{};
  74. protected:
  75. KernelCore& kernel;
  76. std::string name;
  77. public:
  78. static KAutoObject* Create(KAutoObject* ptr);
  79. public:
  80. explicit KAutoObject(KernelCore& kernel_) : kernel(kernel_) {}
  81. virtual ~KAutoObject() {}
  82. // Destroy is responsible for destroying the auto object's resources when ref_count hits zero.
  83. virtual void Destroy() {
  84. UNIMPLEMENTED();
  85. }
  86. // Finalize is responsible for cleaning up resource, but does not destroy the object.
  87. virtual void Finalize() {}
  88. virtual KProcess* GetOwner() const {
  89. return nullptr;
  90. }
  91. u32 GetReferenceCount() const {
  92. return m_ref_count.load();
  93. }
  94. bool IsDerivedFrom(const TypeObj& rhs) const {
  95. return this->GetTypeObj().IsDerivedFrom(rhs);
  96. }
  97. bool IsDerivedFrom(const KAutoObject& rhs) const {
  98. return this->IsDerivedFrom(rhs.GetTypeObj());
  99. }
  100. template <typename Derived>
  101. Derived DynamicCast() {
  102. static_assert(std::is_pointer<Derived>::value);
  103. using DerivedType = typename std::remove_pointer<Derived>::type;
  104. if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
  105. return static_cast<Derived>(this);
  106. } else {
  107. return nullptr;
  108. }
  109. }
  110. template <typename Derived>
  111. const Derived DynamicCast() const {
  112. static_assert(std::is_pointer<Derived>::value);
  113. using DerivedType = typename std::remove_pointer<Derived>::type;
  114. if (this->IsDerivedFrom(DerivedType::GetStaticTypeObj())) {
  115. return static_cast<Derived>(this);
  116. } else {
  117. return nullptr;
  118. }
  119. }
  120. bool Open() {
  121. // Atomically increment the reference count, only if it's positive.
  122. u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
  123. do {
  124. if (cur_ref_count == 0) {
  125. return false;
  126. }
  127. ASSERT(cur_ref_count < cur_ref_count + 1);
  128. } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count + 1,
  129. std::memory_order_relaxed));
  130. return true;
  131. }
  132. void Close() {
  133. // Atomically decrement the reference count, not allowing it to become negative.
  134. u32 cur_ref_count = m_ref_count.load(std::memory_order_acquire);
  135. do {
  136. ASSERT(cur_ref_count > 0);
  137. } while (!m_ref_count.compare_exchange_weak(cur_ref_count, cur_ref_count - 1,
  138. std::memory_order_relaxed));
  139. // If ref count hits zero, destroy the object.
  140. if (cur_ref_count - 1 == 0) {
  141. this->Destroy();
  142. }
  143. }
  144. };
  145. class KAutoObjectWithListContainer;
  146. class KAutoObjectWithList : public KAutoObject {
  147. private:
  148. friend class KAutoObjectWithListContainer;
  149. private:
  150. Common::IntrusiveRedBlackTreeNode list_node;
  151. protected:
  152. KernelCore& kernel;
  153. public:
  154. explicit KAutoObjectWithList(KernelCore& kernel_) : KAutoObject(kernel_), kernel(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. public:
  167. virtual u64 GetId() const {
  168. return reinterpret_cast<u64>(this);
  169. }
  170. virtual const std::string& GetName() const {
  171. return name;
  172. }
  173. };
  174. template <typename T>
  175. class KScopedAutoObject {
  176. NON_COPYABLE(KScopedAutoObject);
  177. private:
  178. template <typename U>
  179. friend class KScopedAutoObject;
  180. private:
  181. T* m_obj{};
  182. private:
  183. constexpr void Swap(KScopedAutoObject& rhs) {
  184. std::swap(m_obj, rhs.m_obj);
  185. }
  186. public:
  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. };
  250. } // namespace Kernel