thread.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <boost/container/flat_set.hpp>
  8. #include "common/common_types.h"
  9. #include "core/core.h"
  10. #include "core/hle/hle.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/result.h"
  13. enum ThreadPriority : s32 {
  14. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  15. THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps
  16. THREADPRIO_DEFAULT = 48, ///< Default thread priority for userland apps
  17. THREADPRIO_LOWEST = 63, ///< Lowest thread priority
  18. };
  19. enum ThreadProcessorId : s32 {
  20. THREADPROCESSORID_DEFAULT = -2, ///< Run thread on default core specified by exheader
  21. THREADPROCESSORID_ALL = -1, ///< Run thread on either core
  22. THREADPROCESSORID_0 = 0, ///< Run thread on core 0 (AppCore)
  23. THREADPROCESSORID_1 = 1, ///< Run thread on core 1 (SysCore)
  24. THREADPROCESSORID_MAX = 2, ///< Processor ID must be less than this
  25. };
  26. enum ThreadStatus {
  27. THREADSTATUS_RUNNING, ///< Currently running
  28. THREADSTATUS_READY, ///< Ready to run
  29. THREADSTATUS_WAIT_ARB, ///< Waiting on an address arbiter
  30. THREADSTATUS_WAIT_SLEEP, ///< Waiting due to a SleepThread SVC
  31. THREADSTATUS_WAIT_SYNCH, ///< Waiting due to a WaitSynchronization SVC
  32. THREADSTATUS_DORMANT, ///< Created but not yet made ready
  33. THREADSTATUS_DEAD ///< Run to completion, or forcefully terminated
  34. };
  35. namespace Kernel {
  36. class Mutex;
  37. class Process;
  38. class Thread final : public WaitObject {
  39. public:
  40. /**
  41. * Creates and returns a new thread. The new thread is immediately scheduled
  42. * @param name The friendly name desired for the thread
  43. * @param entry_point The address at which the thread should start execution
  44. * @param priority The thread's priority
  45. * @param arg User data to pass to the thread
  46. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  47. * @param stack_top The address of the thread's stack top
  48. * @return A shared pointer to the newly created thread
  49. */
  50. static ResultVal<SharedPtr<Thread>> Create(std::string name, VAddr entry_point, s32 priority,
  51. u32 arg, s32 processor_id, VAddr stack_top);
  52. std::string GetName() const override {
  53. return name;
  54. }
  55. std::string GetTypeName() const override {
  56. return "Thread";
  57. }
  58. static const HandleType HANDLE_TYPE = HandleType::Thread;
  59. HandleType GetHandleType() const override {
  60. return HANDLE_TYPE;
  61. }
  62. bool ShouldWait() override;
  63. void Acquire() override;
  64. /**
  65. * Gets the thread's current priority
  66. * @return The current thread's priority
  67. */
  68. s32 GetPriority() const {
  69. return current_priority;
  70. }
  71. /**
  72. * Sets the thread's current priority
  73. * @param priority The new priority
  74. */
  75. void SetPriority(s32 priority);
  76. /**
  77. * Temporarily boosts the thread's priority until the next time it is scheduled
  78. * @param priority The new priority
  79. */
  80. void BoostPriority(s32 priority);
  81. /**
  82. * Gets the thread's thread ID
  83. * @return The thread's ID
  84. */
  85. u32 GetThreadId() const {
  86. return thread_id;
  87. }
  88. /**
  89. * Resumes a thread from waiting
  90. */
  91. void ResumeFromWait();
  92. /**
  93. * Schedules an event to wake up the specified thread after the specified delay
  94. * @param nanoseconds The time this thread will be allowed to sleep for
  95. */
  96. void WakeAfterDelay(s64 nanoseconds);
  97. /**
  98. * Sets the result after the thread awakens (from either WaitSynchronization SVC)
  99. * @param result Value to set to the returned result
  100. */
  101. void SetWaitSynchronizationResult(ResultCode result);
  102. /**
  103. * Sets the output parameter value after the thread awakens (from WaitSynchronizationN SVC only)
  104. * @param output Value to set to the output parameter
  105. */
  106. void SetWaitSynchronizationOutput(s32 output);
  107. /**
  108. * Stops a thread, invalidating it from further use
  109. */
  110. void Stop();
  111. /*
  112. * Returns the Thread Local Storage address of the current thread
  113. * @returns VAddr of the thread's TLS
  114. */
  115. VAddr GetTLSAddress() const {
  116. return tls_address;
  117. }
  118. Core::ThreadContext context;
  119. u32 thread_id;
  120. u32 status;
  121. u32 entry_point;
  122. u32 stack_top;
  123. s32 nominal_priority; ///< Nominal thread priority, as set by the emulated application
  124. s32 current_priority; ///< Current thread priority, can be temporarily changed
  125. u64 last_running_ticks; ///< CPU tick when thread was last running
  126. s32 processor_id;
  127. VAddr tls_address; ///< Virtual address of the Thread Local Storage of the thread
  128. bool waitsynch_waited; ///< Set to true if the last svcWaitSynch call caused the thread to wait
  129. /// Mutexes currently held by this thread, which will be released when it exits.
  130. boost::container::flat_set<SharedPtr<Mutex>> held_mutexes;
  131. SharedPtr<Process> owner_process; ///< Process that owns this thread
  132. std::vector<SharedPtr<WaitObject>> wait_objects; ///< Objects that the thread is waiting on
  133. VAddr wait_address; ///< If waiting on an AddressArbiter, this is the arbitration address
  134. bool wait_all; ///< True if the thread is waiting on all objects before resuming
  135. bool wait_set_output; ///< True if the output parameter should be set on thread wakeup
  136. std::string name;
  137. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  138. Handle callback_handle;
  139. private:
  140. Thread();
  141. ~Thread() override;
  142. };
  143. /**
  144. * Sets up the primary application thread
  145. * @param entry_point The address at which the thread should start execution
  146. * @param priority The priority to give the main thread
  147. * @return A shared pointer to the main thread
  148. */
  149. SharedPtr<Thread> SetupMainThread(u32 entry_point, s32 priority);
  150. /**
  151. * Reschedules to the next available thread (call after current thread is suspended)
  152. */
  153. void Reschedule();
  154. /**
  155. * Arbitrate the highest priority thread that is waiting
  156. * @param address The address for which waiting threads should be arbitrated
  157. */
  158. Thread* ArbitrateHighestPriorityThread(u32 address);
  159. /**
  160. * Arbitrate all threads currently waiting.
  161. * @param address The address for which waiting threads should be arbitrated
  162. */
  163. void ArbitrateAllThreads(u32 address);
  164. /**
  165. * Gets the current thread
  166. */
  167. Thread* GetCurrentThread();
  168. /**
  169. * Waits the current thread on a sleep
  170. */
  171. void WaitCurrentThread_Sleep();
  172. /**
  173. * Waits the current thread from a WaitSynchronization call
  174. * @param wait_objects Kernel objects that we are waiting on
  175. * @param wait_set_output If true, set the output parameter on thread wakeup (for
  176. * WaitSynchronizationN only)
  177. * @param wait_all If true, wait on all objects before resuming (for WaitSynchronizationN only)
  178. */
  179. void WaitCurrentThread_WaitSynchronization(std::vector<SharedPtr<WaitObject>> wait_objects,
  180. bool wait_set_output, bool wait_all);
  181. /**
  182. * Waits the current thread from an ArbitrateAddress call
  183. * @param wait_address Arbitration address used to resume from wait
  184. */
  185. void WaitCurrentThread_ArbitrateAddress(VAddr wait_address);
  186. /**
  187. * Initialize threading
  188. */
  189. void ThreadingInit();
  190. /**
  191. * Shutdown threading
  192. */
  193. void ThreadingShutdown();
  194. /**
  195. * Get a const reference to the thread list for debug use
  196. */
  197. const std::vector<SharedPtr<Thread>>& GetThreadList();
  198. } // namespace