thread.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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/synchronization_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 class ThreadSchedFlags : u32 {
  70. ProcessPauseFlag = 1 << 4,
  71. ThreadPauseFlag = 1 << 5,
  72. ProcessDebugPauseFlag = 1 << 6,
  73. KernelInitPauseFlag = 1 << 8,
  74. };
  75. enum class ThreadSchedMasks : u32 {
  76. LowMask = 0x000f,
  77. HighMask = 0xfff0,
  78. ForcePauseMask = 0x0070,
  79. };
  80. class Thread final : public SynchronizationObject {
  81. public:
  82. explicit Thread(KernelCore& kernel);
  83. ~Thread() override;
  84. using MutexWaitingThreads = std::vector<std::shared_ptr<Thread>>;
  85. using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
  86. using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
  87. using ThreadSynchronizationObjects = std::vector<std::shared_ptr<SynchronizationObject>>;
  88. using WakeupCallback =
  89. std::function<bool(ThreadWakeupReason reason, std::shared_ptr<Thread> thread,
  90. std::shared_ptr<SynchronizationObject> object, std::size_t index)>;
  91. /**
  92. * Creates and returns a new thread. The new thread is immediately scheduled
  93. * @param kernel The kernel instance this thread will be created under.
  94. * @param name The friendly name desired for the thread
  95. * @param entry_point The address at which the thread should start execution
  96. * @param priority The thread's priority
  97. * @param arg User data to pass to the thread
  98. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  99. * @param stack_top The address of the thread's stack top
  100. * @param owner_process The parent process for the thread
  101. * @return A shared pointer to the newly created thread
  102. */
  103. static ResultVal<std::shared_ptr<Thread>> Create(KernelCore& kernel, std::string name,
  104. VAddr entry_point, u32 priority, u64 arg,
  105. s32 processor_id, VAddr stack_top,
  106. Process& owner_process);
  107. std::string GetName() const override {
  108. return name;
  109. }
  110. void SetName(std::string new_name) {
  111. name = std::move(new_name);
  112. }
  113. std::string GetTypeName() const override {
  114. return "Thread";
  115. }
  116. static constexpr HandleType HANDLE_TYPE = HandleType::Thread;
  117. HandleType GetHandleType() const override {
  118. return HANDLE_TYPE;
  119. }
  120. bool ShouldWait(const Thread* thread) const override;
  121. void Acquire(Thread* thread) override;
  122. bool IsSignaled() const override;
  123. /**
  124. * Gets the thread's current priority
  125. * @return The current thread's priority
  126. */
  127. u32 GetPriority() const {
  128. return current_priority;
  129. }
  130. /**
  131. * Gets the thread's nominal priority.
  132. * @return The current thread's nominal priority.
  133. */
  134. u32 GetNominalPriority() const {
  135. return nominal_priority;
  136. }
  137. /**
  138. * Sets the thread's current priority
  139. * @param priority The new priority
  140. */
  141. void SetPriority(u32 priority);
  142. /// Adds a thread to the list of threads that are waiting for a lock held by this thread.
  143. void AddMutexWaiter(std::shared_ptr<Thread> thread);
  144. /// Removes a thread from the list of threads that are waiting for a lock held by this thread.
  145. void RemoveMutexWaiter(std::shared_ptr<Thread> thread);
  146. /// Recalculates the current priority taking into account priority inheritance.
  147. void UpdatePriority();
  148. /// Changes the core that the thread is running or scheduled to run on.
  149. void ChangeCore(u32 core, u64 mask);
  150. /**
  151. * Gets the thread's thread ID
  152. * @return The thread's ID
  153. */
  154. u64 GetThreadID() const {
  155. return thread_id;
  156. }
  157. /// Resumes a thread from waiting
  158. void ResumeFromWait();
  159. /// Cancels a waiting operation that this thread may or may not be within.
  160. ///
  161. /// When the thread is within a waiting state, this will set the thread's
  162. /// waiting result to signal a canceled wait. The function will then resume
  163. /// this thread.
  164. ///
  165. void CancelWait();
  166. /**
  167. * Schedules an event to wake up the specified thread after the specified delay
  168. * @param nanoseconds The time this thread will be allowed to sleep for
  169. */
  170. void WakeAfterDelay(s64 nanoseconds);
  171. /// Cancel any outstanding wakeup events for this thread
  172. void CancelWakeupTimer();
  173. /**
  174. * Sets the result after the thread awakens (from svcWaitSynchronization)
  175. * @param result Value to set to the returned result
  176. */
  177. void SetWaitSynchronizationResult(ResultCode result);
  178. /**
  179. * Sets the output parameter value after the thread awakens (from svcWaitSynchronization)
  180. * @param output Value to set to the output parameter
  181. */
  182. void SetWaitSynchronizationOutput(s32 output);
  183. /**
  184. * Retrieves the index that this particular object occupies in the list of objects
  185. * that the thread passed to WaitSynchronization, starting the search from the last element.
  186. *
  187. * It is used to set the output index of WaitSynchronization when the thread is awakened.
  188. *
  189. * When a thread wakes up due to an object signal, the kernel will use the index of the last
  190. * matching object in the wait objects list in case of having multiple instances of the same
  191. * object in the list.
  192. *
  193. * @param object Object to query the index of.
  194. */
  195. s32 GetSynchronizationObjectIndex(std::shared_ptr<SynchronizationObject> object) const;
  196. /**
  197. * Stops a thread, invalidating it from further use
  198. */
  199. void Stop();
  200. /*
  201. * Returns the Thread Local Storage address of the current thread
  202. * @returns VAddr of the thread's TLS
  203. */
  204. VAddr GetTLSAddress() const {
  205. return tls_address;
  206. }
  207. /*
  208. * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
  209. * @returns The value of the TPIDR_EL0 register.
  210. */
  211. u64 GetTPIDR_EL0() const {
  212. return tpidr_el0;
  213. }
  214. /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
  215. void SetTPIDR_EL0(u64 value) {
  216. tpidr_el0 = value;
  217. }
  218. /*
  219. * Returns the address of the current thread's command buffer, located in the TLS.
  220. * @returns VAddr of the thread's command buffer.
  221. */
  222. VAddr GetCommandBufferAddress() const;
  223. /// Returns whether this thread is waiting on objects from a WaitSynchronization call.
  224. bool IsSleepingOnWait() const {
  225. return status == ThreadStatus::WaitSynch;
  226. }
  227. ThreadContext32& GetContext32() {
  228. return context_32;
  229. }
  230. const ThreadContext32& GetContext32() const {
  231. return context_32;
  232. }
  233. ThreadContext64& GetContext64() {
  234. return context_64;
  235. }
  236. const ThreadContext64& GetContext64() const {
  237. return context_64;
  238. }
  239. ThreadStatus GetStatus() const {
  240. return status;
  241. }
  242. void SetStatus(ThreadStatus new_status);
  243. u64 GetLastRunningTicks() const {
  244. return last_running_ticks;
  245. }
  246. u64 GetTotalCPUTimeTicks() const {
  247. return total_cpu_time_ticks;
  248. }
  249. void UpdateCPUTimeTicks(u64 ticks) {
  250. total_cpu_time_ticks += ticks;
  251. }
  252. s32 GetProcessorID() const {
  253. return processor_id;
  254. }
  255. void SetProcessorID(s32 new_core) {
  256. processor_id = new_core;
  257. }
  258. Process* GetOwnerProcess() {
  259. return owner_process;
  260. }
  261. const Process* GetOwnerProcess() const {
  262. return owner_process;
  263. }
  264. const ThreadSynchronizationObjects& GetSynchronizationObjects() const {
  265. return wait_objects;
  266. }
  267. void SetSynchronizationObjects(ThreadSynchronizationObjects objects) {
  268. wait_objects = std::move(objects);
  269. }
  270. void ClearSynchronizationObjects() {
  271. for (const auto& waiting_object : wait_objects) {
  272. waiting_object->RemoveWaitingThread(SharedFrom(this));
  273. }
  274. wait_objects.clear();
  275. }
  276. /// Determines whether all the objects this thread is waiting on are ready.
  277. bool AllSynchronizationObjectsReady() const;
  278. const MutexWaitingThreads& GetMutexWaitingThreads() const {
  279. return wait_mutex_threads;
  280. }
  281. Thread* GetLockOwner() const {
  282. return lock_owner.get();
  283. }
  284. void SetLockOwner(std::shared_ptr<Thread> owner) {
  285. lock_owner = std::move(owner);
  286. }
  287. VAddr GetCondVarWaitAddress() const {
  288. return condvar_wait_address;
  289. }
  290. void SetCondVarWaitAddress(VAddr address) {
  291. condvar_wait_address = address;
  292. }
  293. VAddr GetMutexWaitAddress() const {
  294. return mutex_wait_address;
  295. }
  296. void SetMutexWaitAddress(VAddr address) {
  297. mutex_wait_address = address;
  298. }
  299. Handle GetWaitHandle() const {
  300. return wait_handle;
  301. }
  302. void SetWaitHandle(Handle handle) {
  303. wait_handle = handle;
  304. }
  305. VAddr GetArbiterWaitAddress() const {
  306. return arb_wait_address;
  307. }
  308. void SetArbiterWaitAddress(VAddr address) {
  309. arb_wait_address = address;
  310. }
  311. bool HasWakeupCallback() const {
  312. return wakeup_callback != nullptr;
  313. }
  314. void SetWakeupCallback(WakeupCallback callback) {
  315. wakeup_callback = std::move(callback);
  316. }
  317. void InvalidateWakeupCallback() {
  318. SetWakeupCallback(nullptr);
  319. }
  320. /**
  321. * Invokes the thread's wakeup callback.
  322. *
  323. * @pre A valid wakeup callback has been set. Violating this precondition
  324. * will cause an assertion to trigger.
  325. */
  326. bool InvokeWakeupCallback(ThreadWakeupReason reason, std::shared_ptr<Thread> thread,
  327. std::shared_ptr<SynchronizationObject> object, std::size_t index);
  328. u32 GetIdealCore() const {
  329. return ideal_core;
  330. }
  331. u64 GetAffinityMask() const {
  332. return affinity_mask;
  333. }
  334. ThreadActivity GetActivity() const {
  335. return activity;
  336. }
  337. void SetActivity(ThreadActivity value);
  338. /// Sleeps this thread for the given amount of nanoseconds.
  339. void Sleep(s64 nanoseconds);
  340. /// Yields this thread without rebalancing loads.
  341. bool YieldSimple();
  342. /// Yields this thread and does a load rebalancing.
  343. bool YieldAndBalanceLoad();
  344. /// Yields this thread and if the core is left idle, loads are rebalanced
  345. bool YieldAndWaitForLoadBalancing();
  346. void IncrementYieldCount() {
  347. yield_count++;
  348. }
  349. u64 GetYieldCount() const {
  350. return yield_count;
  351. }
  352. ThreadSchedStatus GetSchedulingStatus() const {
  353. return static_cast<ThreadSchedStatus>(scheduling_state &
  354. static_cast<u32>(ThreadSchedMasks::LowMask));
  355. }
  356. bool IsRunning() const {
  357. return is_running;
  358. }
  359. void SetIsRunning(bool value) {
  360. is_running = value;
  361. }
  362. bool IsSyncCancelled() const {
  363. return is_sync_cancelled;
  364. }
  365. void SetSyncCancelled(bool value) {
  366. is_sync_cancelled = value;
  367. }
  368. Handle GetGlobalHandle() const {
  369. return global_handle;
  370. }
  371. private:
  372. void SetSchedulingStatus(ThreadSchedStatus new_status);
  373. void SetCurrentPriority(u32 new_priority);
  374. ResultCode SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask);
  375. void AdjustSchedulingOnStatus(u32 old_flags);
  376. void AdjustSchedulingOnPriority(u32 old_priority);
  377. void AdjustSchedulingOnAffinity(u64 old_affinity_mask, s32 old_core);
  378. ThreadContext32 context_32{};
  379. ThreadContext64 context_64{};
  380. u64 thread_id = 0;
  381. ThreadStatus status = ThreadStatus::Dormant;
  382. VAddr entry_point = 0;
  383. VAddr stack_top = 0;
  384. /// Nominal thread priority, as set by the emulated application.
  385. /// The nominal priority is the thread priority without priority
  386. /// inheritance taken into account.
  387. u32 nominal_priority = 0;
  388. /// Current thread priority. This may change over the course of the
  389. /// thread's lifetime in order to facilitate priority inheritance.
  390. u32 current_priority = 0;
  391. u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks.
  392. u64 last_running_ticks = 0; ///< CPU tick when thread was last running
  393. u64 yield_count = 0; ///< Number of redundant yields carried by this thread.
  394. ///< a redundant yield is one where no scheduling is changed
  395. s32 processor_id = 0;
  396. VAddr tls_address = 0; ///< Virtual address of the Thread Local Storage of the thread
  397. u64 tpidr_el0 = 0; ///< TPIDR_EL0 read/write system register.
  398. /// Process that owns this thread
  399. Process* owner_process;
  400. /// Objects that the thread is waiting on, in the same order as they were
  401. /// passed to WaitSynchronization.
  402. ThreadSynchronizationObjects wait_objects;
  403. /// List of threads that are waiting for a mutex that is held by this thread.
  404. MutexWaitingThreads wait_mutex_threads;
  405. /// Thread that owns the lock that this thread is waiting for.
  406. std::shared_ptr<Thread> lock_owner;
  407. /// If waiting on a ConditionVariable, this is the ConditionVariable address
  408. VAddr condvar_wait_address = 0;
  409. /// If waiting on a Mutex, this is the mutex address
  410. VAddr mutex_wait_address = 0;
  411. /// The handle used to wait for the mutex.
  412. Handle wait_handle = 0;
  413. /// If waiting for an AddressArbiter, this is the address being waited on.
  414. VAddr arb_wait_address{0};
  415. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  416. Handle global_handle = 0;
  417. /// Callback that will be invoked when the thread is resumed from a waiting state. If the thread
  418. /// was waiting via WaitSynchronization then the object will be the last object that became
  419. /// available. In case of a timeout, the object will be nullptr.
  420. WakeupCallback wakeup_callback;
  421. Scheduler* scheduler = nullptr;
  422. u32 ideal_core{0xFFFFFFFF};
  423. u64 affinity_mask{0x1};
  424. ThreadActivity activity = ThreadActivity::Normal;
  425. s32 ideal_core_override = -1;
  426. u64 affinity_mask_override = 0x1;
  427. u32 affinity_override_count = 0;
  428. u32 scheduling_state = 0;
  429. bool is_running = false;
  430. bool is_sync_cancelled = false;
  431. std::string name;
  432. };
  433. /**
  434. * Gets the current thread
  435. */
  436. Thread* GetCurrentThread();
  437. } // namespace Kernel