kernel.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. ConditionVariable,
  22. Timer,
  23. ResourceLimit,
  24. CodeSet,
  25. ClientPort,
  26. ServerPort,
  27. ClientSession,
  28. ServerSession,
  29. };
  30. enum {
  31. DEFAULT_STACK_SIZE = 0x10000,
  32. };
  33. enum class ResetType {
  34. OneShot,
  35. Sticky,
  36. Pulse,
  37. };
  38. class Object : NonCopyable {
  39. public:
  40. virtual ~Object() {}
  41. /// Returns a unique identifier for the object. For debugging purposes only.
  42. unsigned int GetObjectId() const {
  43. return object_id;
  44. }
  45. virtual std::string GetTypeName() const {
  46. return "[BAD KERNEL OBJECT TYPE]";
  47. }
  48. virtual std::string GetName() const {
  49. return "[UNKNOWN KERNEL OBJECT]";
  50. }
  51. virtual Kernel::HandleType GetHandleType() const = 0;
  52. /**
  53. * Check if a thread can wait on the object
  54. * @return True if a thread can wait on the object, otherwise false
  55. */
  56. bool IsWaitable() const {
  57. switch (GetHandleType()) {
  58. case HandleType::Event:
  59. case HandleType::Mutex:
  60. case HandleType::Thread:
  61. case HandleType::ConditionVariable:
  62. case HandleType::Timer:
  63. case HandleType::ServerPort:
  64. case HandleType::ServerSession:
  65. return true;
  66. case HandleType::Unknown:
  67. case HandleType::SharedMemory:
  68. case HandleType::Process:
  69. case HandleType::AddressArbiter:
  70. case HandleType::ResourceLimit:
  71. case HandleType::CodeSet:
  72. case HandleType::ClientPort:
  73. case HandleType::ClientSession:
  74. return false;
  75. }
  76. UNREACHABLE();
  77. }
  78. public:
  79. static unsigned int next_object_id;
  80. private:
  81. friend void intrusive_ptr_add_ref(Object*);
  82. friend void intrusive_ptr_release(Object*);
  83. unsigned int ref_count = 0;
  84. unsigned int object_id = next_object_id++;
  85. };
  86. // Special functions used by boost::instrusive_ptr to do automatic ref-counting
  87. inline void intrusive_ptr_add_ref(Object* object) {
  88. ++object->ref_count;
  89. }
  90. inline void intrusive_ptr_release(Object* object) {
  91. if (--object->ref_count == 0) {
  92. delete object;
  93. }
  94. }
  95. template <typename T>
  96. using SharedPtr = boost::intrusive_ptr<T>;
  97. /**
  98. * Attempts to downcast the given Object pointer to a pointer to T.
  99. * @return Derived pointer to the object, or `nullptr` if `object` isn't of type T.
  100. */
  101. template <typename T>
  102. inline SharedPtr<T> DynamicObjectCast(SharedPtr<Object> object) {
  103. if (object != nullptr && object->GetHandleType() == T::HANDLE_TYPE) {
  104. return boost::static_pointer_cast<T>(std::move(object));
  105. }
  106. return nullptr;
  107. }
  108. /// Initialize the kernel with the specified system mode.
  109. void Init(u32 system_mode);
  110. /// Shutdown the kernel
  111. void Shutdown();
  112. } // namespace Kernel