thread.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <string>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "core/core.h"
  9. #include "core/mem_map.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/result.h"
  12. enum ThreadPriority {
  13. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  14. THREADPRIO_DEFAULT = 16, ///< Default thread priority for userland apps
  15. THREADPRIO_LOW = 31, ///< Low range of thread priority for userland apps
  16. THREADPRIO_LOWEST = 63, ///< Thread priority max checked by svcCreateThread
  17. };
  18. enum ThreadProcessorId {
  19. THREADPROCESSORID_0 = 0xFFFFFFFE, ///< Enables core appcode
  20. THREADPROCESSORID_1 = 0xFFFFFFFD, ///< Enables core syscore
  21. THREADPROCESSORID_ALL = 0xFFFFFFFC, ///< Enables both cores
  22. };
  23. enum ThreadStatus {
  24. THREADSTATUS_RUNNING = 1,
  25. THREADSTATUS_READY = 2,
  26. THREADSTATUS_WAIT = 4,
  27. THREADSTATUS_SUSPEND = 8,
  28. THREADSTATUS_DORMANT = 16,
  29. THREADSTATUS_DEAD = 32,
  30. THREADSTATUS_WAITSUSPEND = THREADSTATUS_WAIT | THREADSTATUS_SUSPEND
  31. };
  32. enum WaitType {
  33. WAITTYPE_NONE,
  34. WAITTYPE_SLEEP,
  35. WAITTYPE_SEMA,
  36. WAITTYPE_EVENT,
  37. WAITTYPE_THREADEND,
  38. WAITTYPE_MUTEX,
  39. WAITTYPE_SYNCH,
  40. WAITTYPE_ARB,
  41. WAITTYPE_TIMER,
  42. };
  43. namespace Kernel {
  44. class Thread : public Kernel::Object {
  45. public:
  46. static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
  47. u32 arg, s32 processor_id, VAddr stack_top, u32 stack_size);
  48. std::string GetName() const override { return name; }
  49. std::string GetTypeName() const override { return "Thread"; }
  50. static const HandleType HANDLE_TYPE = HandleType::Thread;
  51. HandleType GetHandleType() const override { return HANDLE_TYPE; }
  52. inline bool IsRunning() const { return (status & THREADSTATUS_RUNNING) != 0; }
  53. inline bool IsStopped() const { return (status & THREADSTATUS_DORMANT) != 0; }
  54. inline bool IsReady() const { return (status & THREADSTATUS_READY) != 0; }
  55. inline bool IsWaiting() const { return (status & THREADSTATUS_WAIT) != 0; }
  56. inline bool IsSuspended() const { return (status & THREADSTATUS_SUSPEND) != 0; }
  57. inline bool IsIdle() const { return idle; }
  58. ResultVal<bool> WaitSynchronization() override;
  59. s32 GetPriority() const { return current_priority; }
  60. void SetPriority(s32 priority);
  61. u32 GetThreadId() const { return thread_id; }
  62. void Stop(const char* reason);
  63. /// Resumes a thread from waiting by marking it as "ready".
  64. void ResumeFromWait();
  65. Core::ThreadContext context;
  66. u32 thread_id;
  67. u32 status;
  68. u32 entry_point;
  69. u32 stack_top;
  70. u32 stack_size;
  71. s32 initial_priority;
  72. s32 current_priority;
  73. s32 processor_id;
  74. WaitType wait_type;
  75. Object* wait_object;
  76. VAddr wait_address;
  77. std::vector<SharedPtr<Thread>> waiting_threads;
  78. std::string name;
  79. /// Whether this thread is intended to never actually be executed, i.e. always idle
  80. bool idle = false;
  81. private:
  82. Thread() = default;
  83. };
  84. /// Sets up the primary application thread
  85. SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size);
  86. /// Reschedules to the next available thread (call after current thread is suspended)
  87. void Reschedule();
  88. /// Arbitrate the highest priority thread that is waiting
  89. Thread* ArbitrateHighestPriorityThread(Object* arbiter, u32 address);
  90. /// Arbitrate all threads currently waiting...
  91. void ArbitrateAllThreads(Object* arbiter, u32 address);
  92. /// Gets the current thread
  93. Thread* GetCurrentThread();
  94. /**
  95. * Puts the current thread in the wait state for the given type
  96. * @param wait_type Type of wait
  97. * @param wait_object Kernel object that we are waiting on, defaults to current thread
  98. */
  99. void WaitCurrentThread(WaitType wait_type, Object* wait_object = GetCurrentThread());
  100. /**
  101. * Schedules an event to wake up the specified thread after the specified delay.
  102. * @param thread The thread to wake after the delay.
  103. * @param nanoseconds The time this thread will be allowed to sleep for.
  104. */
  105. void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds);
  106. /**
  107. * Puts the current thread in the wait state for the given type
  108. * @param wait_type Type of wait
  109. * @param wait_object Kernel object that we are waiting on
  110. * @param wait_address Arbitration address used to resume from wait
  111. */
  112. void WaitCurrentThread(WaitType wait_type, Object* wait_object, VAddr wait_address);
  113. /**
  114. * Sets up the idle thread, this is a thread that is intended to never execute instructions,
  115. * only to advance the timing. It is scheduled when there are no other ready threads in the thread queue
  116. * and will try to yield on every call.
  117. * @returns The handle of the idle thread
  118. */
  119. Handle SetupIdleThread();
  120. /// Initialize threading
  121. void ThreadingInit();
  122. /// Shutdown threading
  123. void ThreadingShutdown();
  124. } // namespace