thread.h 16 KB

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