kernel.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2014 Citra Emulator Project / PPSSPP Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <cstddef>
  6. #include <string>
  7. #include <utility>
  8. #include <boost/smart_ptr/intrusive_ptr.hpp>
  9. #include "common/assert.h"
  10. #include "common/common_types.h"
  11. namespace Kernel {
  12. using Handle = u32;
  13. enum class HandleType : u32 {
  14. Unknown,
  15. Event,
  16. Mutex,
  17. SharedMemory,
  18. Thread,
  19. Process,
  20. AddressArbiter,
  21. Semaphore,
  22. Timer,
  23. ResourceLimit,
  24. CodeSet,
  25. ClientPort,
  26. ServerPort,
  27. ClientSession,
  28. ServerSession,
  29. Domain,
  30. };
  31. enum {
  32. DEFAULT_STACK_SIZE = 0x4000,
  33. };
  34. enum class ResetType {
  35. OneShot,
  36. Sticky,
  37. Pulse,
  38. };
  39. class Object : NonCopyable {
  40. public:
  41. virtual ~Object() {}
  42. /// Returns a unique identifier for the object. For debugging purposes only.
  43. unsigned int GetObjectId() const {
  44. return object_id;
  45. }
  46. virtual std::string GetTypeName() const {
  47. return "[BAD KERNEL OBJECT TYPE]";
  48. }
  49. virtual std::string GetName() const {
  50. return "[UNKNOWN KERNEL OBJECT]";
  51. }
  52. virtual Kernel::HandleType GetHandleType() const = 0;
  53. /**
  54. * Check if a thread can wait on the object
  55. * @return True if a thread can wait on the object, otherwise false
  56. */
  57. bool IsWaitable() const {
  58. switch (GetHandleType()) {
  59. case HandleType::Event:
  60. case HandleType::Mutex:
  61. case HandleType::Thread:
  62. case HandleType::Semaphore:
  63. case HandleType::Timer:
  64. case HandleType::ServerPort:
  65. case HandleType::ServerSession:
  66. return true;
  67. case HandleType::Unknown:
  68. case HandleType::SharedMemory:
  69. case HandleType::Process:
  70. case HandleType::AddressArbiter:
  71. case HandleType::ResourceLimit:
  72. case HandleType::CodeSet:
  73. case HandleType::ClientPort:
  74. case HandleType::ClientSession:
  75. case HandleType::Domain:
  76. return false;
  77. }
  78. UNREACHABLE();
  79. }
  80. /**
  81. * Check if svcSendSyncRequest can be called on the object
  82. * @return True svcSendSyncRequest can be called on the object, otherwise false
  83. */
  84. bool IsSyncable() const {
  85. switch (GetHandleType()) {
  86. case HandleType::ClientSession:
  87. case HandleType::Domain:
  88. return true;
  89. }
  90. UNREACHABLE();
  91. }
  92. public:
  93. static unsigned int next_object_id;
  94. private:
  95. friend void intrusive_ptr_add_ref(Object*);
  96. friend void intrusive_ptr_release(Object*);
  97. unsigned int ref_count = 0;
  98. unsigned int object_id = next_object_id++;
  99. };
  100. // Special functions used by boost::instrusive_ptr to do automatic ref-counting
  101. inline void intrusive_ptr_add_ref(Object* object) {
  102. ++object->ref_count;
  103. }
  104. inline void intrusive_ptr_release(Object* object) {
  105. if (--object->ref_count == 0) {
  106. delete object;
  107. }
  108. }
  109. template <typename T>
  110. using SharedPtr = boost::intrusive_ptr<T>;
  111. /**
  112. * Attempts to downcast the given Object pointer to a pointer to T.
  113. * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
  114. */
  115. template <typename T>
  116. inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) {
  117. if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
  118. return boost::static_pointer_cast<T>(std::move(object));
  119. }
  120. return nullptr;
  121. }
  122. /// Initialize the kernel with the specified system mode.
  123. void Init(u32 system_mode);
  124. /// Shutdown the kernel
  125. void Shutdown();
  126. } // namespace Kernel