thread.h 8.6 KB

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