thread.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 <functional>
  6. #include <string>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "core/arm/arm_interface.h"
  10. #include "core/hle/kernel/object.h"
  11. #include "core/hle/kernel/wait_object.h"
  12. #include "core/hle/result.h"
  13. namespace Kernel {
  14. class KernelCore;
  15. class Process;
  16. class Scheduler;
  17. enum ThreadPriority : u32 {
  18. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  19. THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps
  20. THREADPRIO_DEFAULT = 44, ///< Default thread priority for userland apps
  21. THREADPRIO_LOWEST = 63, ///< Lowest thread priority
  22. THREADPRIO_COUNT = 64, ///< Total number of possible thread priorities.
  23. };
  24. enum ThreadProcessorId : s32 {
  25. /// Indicates that no particular processor core is preferred.
  26. THREADPROCESSORID_DONT_CARE = -1,
  27. /// Run thread on the ideal core specified by the process.
  28. THREADPROCESSORID_IDEAL = -2,
  29. /// Indicates that the preferred processor ID shouldn't be updated in
  30. /// a core mask setting operation.
  31. THREADPROCESSORID_DONT_UPDATE = -3,
  32. THREADPROCESSORID_0 = 0, ///< Run thread on core 0
  33. THREADPROCESSORID_1 = 1, ///< Run thread on core 1
  34. THREADPROCESSORID_2 = 2, ///< Run thread on core 2
  35. THREADPROCESSORID_3 = 3, ///< Run thread on core 3
  36. THREADPROCESSORID_MAX = 4, ///< Processor ID must be less than this
  37. /// Allowed CPU mask
  38. THREADPROCESSORID_DEFAULT_MASK = (1 << THREADPROCESSORID_0) | (1 << THREADPROCESSORID_1) |
  39. (1 << THREADPROCESSORID_2) | (1 << THREADPROCESSORID_3)
  40. };
  41. enum class ThreadStatus {
  42. Running, ///< Currently running
  43. Ready, ///< Ready to run
  44. Paused, ///< Paused by SetThreadActivity or debug
  45. WaitHLEEvent, ///< Waiting for hle event to finish
  46. WaitSleep, ///< Waiting due to a SleepThread SVC
  47. WaitIPC, ///< Waiting for the reply from an IPC request
  48. WaitSynch, ///< Waiting due to WaitSynchronization
  49. WaitMutex, ///< Waiting due to an ArbitrateLock svc
  50. WaitCondVar, ///< Waiting due to an WaitProcessWideKey svc
  51. WaitArb, ///< Waiting due to a SignalToAddress/WaitForAddress svc
  52. Dormant, ///< Created but not yet made ready
  53. Dead ///< Run to completion, or forcefully terminated
  54. };
  55. enum class ThreadWakeupReason {
  56. Signal, // The thread was woken up by WakeupAllWaitingThreads due to an object signal.
  57. Timeout // The thread was woken up due to a wait timeout.
  58. };
  59. enum class ThreadActivity : u32 {
  60. Normal = 0,
  61. Paused = 1,
  62. };
  63. enum class ThreadSchedStatus : u32 {
  64. None = 0,
  65. Paused = 1,
  66. Runnable = 2,
  67. Exited = 3,
  68. };
  69. enum ThreadSchedFlags : u32 {
  70. ProcessPauseFlag = 1 << 4,
  71. ThreadPauseFlag = 1 << 5,
  72. ProcessDebugPauseFlag = 1 << 6,
  73. KernelInitPauseFlag = 1 << 8,
  74. };
  75. enum ThreadSchedMasks : u32 {
  76. LowMask = 0x000f,
  77. HighMask = 0xfff0,
  78. ForcePauseMask = 0x0070,
  79. };
  80. class Thread final : public WaitObject {
  81. public:
  82. using MutexWaitingThreads = std::vector<SharedPtr<Thread>>;
  83. using ThreadContext = Core::ARM_Interface::ThreadContext;
  84. using ThreadWaitObjects = std::vector<SharedPtr<WaitObject>>;
  85. using WakeupCallback = std::function<bool(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  86. SharedPtr<WaitObject> object, std::size_t index)>;
  87. /**
  88. * Creates and returns a new thread. The new thread is immediately scheduled
  89. * @param kernel The kernel instance this thread will be created under.
  90. * @param name The friendly name desired for the thread
  91. * @param entry_point The address at which the thread should start execution
  92. * @param priority The thread's priority
  93. * @param arg User data to pass to the thread
  94. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  95. * @param stack_top The address of the thread's stack top
  96. * @param owner_process The parent process for the thread
  97. * @return A shared pointer to the newly created thread
  98. */
  99. static ResultVal<SharedPtr<Thread>> Create(KernelCore& kernel, std::string name,
  100. VAddr entry_point, u32 priority, u64 arg,
  101. s32 processor_id, VAddr stack_top,
  102. Process& owner_process);
  103. std::string GetName() const override {
  104. return name;
  105. }
  106. void SetName(std::string new_name) {
  107. name = std::move(new_name);
  108. }
  109. std::string GetTypeName() const override {
  110. return "Thread";
  111. }
  112. static constexpr HandleType HANDLE_TYPE = HandleType::Thread;
  113. HandleType GetHandleType() const override {
  114. return HANDLE_TYPE;
  115. }
  116. bool ShouldWait(const Thread* thread) const override;
  117. void Acquire(Thread* thread) override;
  118. /**
  119. * Gets the thread's current priority
  120. * @return The current thread's priority
  121. */
  122. u32 GetPriority() const {
  123. return current_priority;
  124. }
  125. /**
  126. * Gets the thread's nominal priority.
  127. * @return The current thread's nominal priority.
  128. */
  129. u32 GetNominalPriority() const {
  130. return nominal_priority;
  131. }
  132. /**
  133. * Sets the thread's current priority
  134. * @param priority The new priority
  135. */
  136. void SetPriority(u32 priority);
  137. /// Adds a thread to the list of threads that are waiting for a lock held by this thread.
  138. void AddMutexWaiter(SharedPtr<Thread> thread);
  139. /// Removes a thread from the list of threads that are waiting for a lock held by this thread.
  140. void RemoveMutexWaiter(SharedPtr<Thread> thread);
  141. /// Recalculates the current priority taking into account priority inheritance.
  142. void UpdatePriority();
  143. /// Changes the core that the thread is running or scheduled to run on.
  144. void ChangeCore(u32 core, u64 mask);
  145. /**
  146. * Gets the thread's thread ID
  147. * @return The thread's ID
  148. */
  149. u64 GetThreadID() const {
  150. return thread_id;
  151. }
  152. /// Resumes a thread from waiting
  153. void ResumeFromWait();
  154. /// Cancels a waiting operation that this thread may or may not be within.
  155. ///
  156. /// When the thread is within a waiting state, this will set the thread's
  157. /// waiting result to signal a canceled wait. The function will then resume
  158. /// this thread.
  159. ///
  160. void CancelWait();
  161. /**
  162. * Schedules an event to wake up the specified thread after the specified delay
  163. * @param nanoseconds The time this thread will be allowed to sleep for
  164. */
  165. void WakeAfterDelay(s64 nanoseconds);
  166. /// Cancel any outstanding wakeup events for this thread
  167. void CancelWakeupTimer();
  168. /**
  169. * Sets the result after the thread awakens (from svcWaitSynchronization)
  170. * @param result Value to set to the returned result
  171. */
  172. void SetWaitSynchronizationResult(ResultCode result);
  173. /**
  174. * Sets the output parameter value after the thread awakens (from svcWaitSynchronization)
  175. * @param output Value to set to the output parameter
  176. */
  177. void SetWaitSynchronizationOutput(s32 output);
  178. /**
  179. * Retrieves the index that this particular object occupies in the list of objects
  180. * that the thread passed to WaitSynchronization, starting the search from the last element.
  181. *
  182. * It is used to set the output index of WaitSynchronization when the thread is awakened.
  183. *
  184. * When a thread wakes up due to an object signal, the kernel will use the index of the last
  185. * matching object in the wait objects list in case of having multiple instances of the same
  186. * object in the list.
  187. *
  188. * @param object Object to query the index of.
  189. */
  190. s32 GetWaitObjectIndex(const WaitObject* object) const;
  191. /**
  192. * Stops a thread, invalidating it from further use
  193. */
  194. void Stop();
  195. /*
  196. * Returns the Thread Local Storage address of the current thread
  197. * @returns VAddr of the thread's TLS
  198. */
  199. VAddr GetTLSAddress() const {
  200. return tls_address;
  201. }
  202. /*
  203. * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
  204. * @returns The value of the TPIDR_EL0 register.
  205. */
  206. u64 GetTPIDR_EL0() const {
  207. return tpidr_el0;
  208. }
  209. /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
  210. void SetTPIDR_EL0(u64 value) {
  211. tpidr_el0 = value;
  212. }
  213. /*
  214. * Returns the address of the current thread's command buffer, located in the TLS.
  215. * @returns VAddr of the thread's command buffer.
  216. */
  217. VAddr GetCommandBufferAddress() const;
  218. /// Returns whether this thread is waiting on objects from a WaitSynchronization call.
  219. bool IsSleepingOnWait() const {
  220. return status == ThreadStatus::WaitSynch;
  221. }
  222. ThreadContext& GetContext() {
  223. return context;
  224. }
  225. const ThreadContext& GetContext() const {
  226. return context;
  227. }
  228. ThreadStatus GetStatus() const {
  229. return status;
  230. }
  231. void SetStatus(ThreadStatus new_status);
  232. u64 GetLastRunningTicks() const {
  233. return last_running_ticks;
  234. }
  235. u64 GetTotalCPUTimeTicks() const {
  236. return total_cpu_time_ticks;
  237. }
  238. void UpdateCPUTimeTicks(u64 ticks) {
  239. total_cpu_time_ticks += ticks;
  240. }
  241. s32 GetProcessorID() const {
  242. return processor_id;
  243. }
  244. void SetProcessorID(s32 new_core) {
  245. processor_id = new_core;
  246. }
  247. Process* GetOwnerProcess() {
  248. return owner_process;
  249. }
  250. const Process* GetOwnerProcess() const {
  251. return owner_process;
  252. }
  253. const ThreadWaitObjects& GetWaitObjects() const {
  254. return wait_objects;
  255. }
  256. void SetWaitObjects(ThreadWaitObjects objects) {
  257. wait_objects = std::move(objects);
  258. }
  259. void ClearWaitObjects() {
  260. wait_objects.clear();
  261. }
  262. /// Determines whether all the objects this thread is waiting on are ready.
  263. bool AllWaitObjectsReady() const;
  264. const MutexWaitingThreads& GetMutexWaitingThreads() const {
  265. return wait_mutex_threads;
  266. }
  267. Thread* GetLockOwner() const {
  268. return lock_owner.get();
  269. }
  270. void SetLockOwner(SharedPtr<Thread> owner) {
  271. lock_owner = std::move(owner);
  272. }
  273. VAddr GetCondVarWaitAddress() const {
  274. return condvar_wait_address;
  275. }
  276. void SetCondVarWaitAddress(VAddr address) {
  277. condvar_wait_address = address;
  278. }
  279. VAddr GetMutexWaitAddress() const {
  280. return mutex_wait_address;
  281. }
  282. void SetMutexWaitAddress(VAddr address) {
  283. mutex_wait_address = address;
  284. }
  285. Handle GetWaitHandle() const {
  286. return wait_handle;
  287. }
  288. void SetWaitHandle(Handle handle) {
  289. wait_handle = handle;
  290. }
  291. VAddr GetArbiterWaitAddress() const {
  292. return arb_wait_address;
  293. }
  294. void SetArbiterWaitAddress(VAddr address) {
  295. arb_wait_address = address;
  296. }
  297. bool HasWakeupCallback() const {
  298. return wakeup_callback != nullptr;
  299. }
  300. void SetWakeupCallback(WakeupCallback callback) {
  301. wakeup_callback = std::move(callback);
  302. }
  303. void InvalidateWakeupCallback() {
  304. SetWakeupCallback(nullptr);
  305. }
  306. /**
  307. * Invokes the thread's wakeup callback.
  308. *
  309. * @pre A valid wakeup callback has been set. Violating this precondition
  310. * will cause an assertion to trigger.
  311. */
  312. bool InvokeWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread,
  313. SharedPtr<WaitObject> object, std::size_t index);
  314. u32 GetIdealCore() const {
  315. return ideal_core;
  316. }
  317. u64 GetAffinityMask() const {
  318. return affinity_mask;
  319. }
  320. ThreadActivity GetActivity() const {
  321. return activity;
  322. }
  323. void SetActivity(ThreadActivity value);
  324. /// Sleeps this thread for the given amount of nanoseconds.
  325. void Sleep(s64 nanoseconds);
  326. /// Yields this thread without rebalancing loads.
  327. bool YieldSimple();
  328. /// Yields this thread and does a load rebalancing.
  329. bool YieldAndBalanceLoad();
  330. /// Yields this thread and if the core is left idle, loads are rebalanced
  331. bool YieldAndWaitForLoadBalancing();
  332. void IncrementYieldCount() {
  333. yield_count++;
  334. }
  335. u64 GetYieldCount() const {
  336. return yield_count;
  337. }
  338. ThreadSchedStatus GetSchedulingStatus() const {
  339. return static_cast<ThreadSchedStatus>(scheduling_state & ThreadSchedMasks::LowMask);
  340. }
  341. bool IsRunning() const {
  342. return is_running;
  343. }
  344. void SetIsRunning(bool value) {
  345. is_running = value;
  346. }
  347. private:
  348. explicit Thread(KernelCore& kernel);
  349. ~Thread() override;
  350. void SetSchedulingStatus(ThreadSchedStatus new_status);
  351. void SetCurrentPriority(u32 new_priority);
  352. ResultCode SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask);
  353. void AdjustSchedulingOnStatus(u32 old_flags);
  354. void AdjustSchedulingOnPriority(u32 old_priority);
  355. void AdjustSchedulingOnAffinity(u64 old_affinity_mask, s32 old_core);
  356. Core::ARM_Interface::ThreadContext context{};
  357. u64 thread_id = 0;
  358. ThreadStatus status = ThreadStatus::Dormant;
  359. VAddr entry_point = 0;
  360. VAddr stack_top = 0;
  361. /// Nominal thread priority, as set by the emulated application.
  362. /// The nominal priority is the thread priority without priority
  363. /// inheritance taken into account.
  364. u32 nominal_priority = 0;
  365. /// Current thread priority. This may change over the course of the
  366. /// thread's lifetime in order to facilitate priority inheritance.
  367. u32 current_priority = 0;
  368. u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks.
  369. u64 last_running_ticks = 0; ///< CPU tick when thread was last running
  370. u64 yield_count = 0; ///< Number of innecessaries yields occured.
  371. s32 processor_id = 0;
  372. VAddr tls_address = 0; ///< Virtual address of the Thread Local Storage of the thread
  373. u64 tpidr_el0 = 0; ///< TPIDR_EL0 read/write system register.
  374. /// Process that owns this thread
  375. Process* owner_process;
  376. /// Objects that the thread is waiting on, in the same order as they were
  377. /// passed to WaitSynchronization.
  378. ThreadWaitObjects wait_objects;
  379. /// List of threads that are waiting for a mutex that is held by this thread.
  380. MutexWaitingThreads wait_mutex_threads;
  381. /// Thread that owns the lock that this thread is waiting for.
  382. SharedPtr<Thread> lock_owner;
  383. /// If waiting on a ConditionVariable, this is the ConditionVariable address
  384. VAddr condvar_wait_address = 0;
  385. /// If waiting on a Mutex, this is the mutex address
  386. VAddr mutex_wait_address = 0;
  387. /// The handle used to wait for the mutex.
  388. Handle wait_handle = 0;
  389. /// If waiting for an AddressArbiter, this is the address being waited on.
  390. VAddr arb_wait_address{0};
  391. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  392. Handle callback_handle = 0;
  393. /// Callback that will be invoked when the thread is resumed from a waiting state. If the thread
  394. /// was waiting via WaitSynchronization then the object will be the last object that became
  395. /// available. In case of a timeout, the object will be nullptr.
  396. WakeupCallback wakeup_callback;
  397. Scheduler* scheduler = nullptr;
  398. u32 ideal_core{0xFFFFFFFF};
  399. u64 affinity_mask{0x1};
  400. ThreadActivity activity = ThreadActivity::Normal;
  401. s32 ideal_core_override = -1;
  402. u64 affinity_mask_override = 0x1;
  403. u32 affinity_override_count = 0;
  404. u32 scheduling_state = 0;
  405. bool is_running = false;
  406. std::string name;
  407. };
  408. /**
  409. * Gets the current thread
  410. */
  411. Thread* GetCurrentThread();
  412. } // namespace Kernel