thread.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  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 <utility>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "common/spin_lock.h"
  11. #include "core/arm/arm_interface.h"
  12. #include "core/hle/kernel/object.h"
  13. #include "core/hle/kernel/synchronization_object.h"
  14. #include "core/hle/result.h"
  15. namespace Common {
  16. class Fiber;
  17. }
  18. namespace Core {
  19. class ARM_Interface;
  20. class System;
  21. } // namespace Core
  22. namespace Kernel {
  23. class GlobalScheduler;
  24. class KernelCore;
  25. class Process;
  26. class Scheduler;
  27. enum ThreadPriority : u32 {
  28. THREADPRIO_HIGHEST = 0, ///< Highest thread priority
  29. THREADPRIO_MAX_CORE_MIGRATION = 2, ///< Highest priority for a core migration
  30. THREADPRIO_USERLAND_MAX = 24, ///< Highest thread priority for userland apps
  31. THREADPRIO_DEFAULT = 44, ///< Default thread priority for userland apps
  32. THREADPRIO_LOWEST = 63, ///< Lowest thread priority
  33. THREADPRIO_COUNT = 64, ///< Total number of possible thread priorities.
  34. };
  35. enum ThreadType : u32 {
  36. THREADTYPE_USER = 0x1,
  37. THREADTYPE_KERNEL = 0x2,
  38. THREADTYPE_HLE = 0x4,
  39. THREADTYPE_IDLE = 0x8,
  40. THREADTYPE_SUSPEND = 0x10,
  41. };
  42. enum ThreadProcessorId : s32 {
  43. /// Indicates that no particular processor core is preferred.
  44. THREADPROCESSORID_DONT_CARE = -1,
  45. /// Run thread on the ideal core specified by the process.
  46. THREADPROCESSORID_IDEAL = -2,
  47. /// Indicates that the preferred processor ID shouldn't be updated in
  48. /// a core mask setting operation.
  49. THREADPROCESSORID_DONT_UPDATE = -3,
  50. THREADPROCESSORID_0 = 0, ///< Run thread on core 0
  51. THREADPROCESSORID_1 = 1, ///< Run thread on core 1
  52. THREADPROCESSORID_2 = 2, ///< Run thread on core 2
  53. THREADPROCESSORID_3 = 3, ///< Run thread on core 3
  54. THREADPROCESSORID_MAX = 4, ///< Processor ID must be less than this
  55. /// Allowed CPU mask
  56. THREADPROCESSORID_DEFAULT_MASK = (1 << THREADPROCESSORID_0) | (1 << THREADPROCESSORID_1) |
  57. (1 << THREADPROCESSORID_2) | (1 << THREADPROCESSORID_3)
  58. };
  59. enum class ThreadStatus {
  60. Running, ///< Currently running
  61. Ready, ///< Ready to run
  62. Paused, ///< Paused by SetThreadActivity or debug
  63. WaitHLEEvent, ///< Waiting for hle event to finish
  64. WaitSleep, ///< Waiting due to a SleepThread SVC
  65. WaitIPC, ///< Waiting for the reply from an IPC request
  66. WaitSynch, ///< Waiting due to WaitSynchronization
  67. WaitMutex, ///< Waiting due to an ArbitrateLock svc
  68. WaitCondVar, ///< Waiting due to an WaitProcessWideKey svc
  69. WaitArb, ///< Waiting due to a SignalToAddress/WaitForAddress svc
  70. Dormant, ///< Created but not yet made ready
  71. Dead ///< Run to completion, or forcefully terminated
  72. };
  73. enum class ThreadWakeupReason {
  74. Signal, // The thread was woken up by WakeupAllWaitingThreads due to an object signal.
  75. Timeout // The thread was woken up due to a wait timeout.
  76. };
  77. enum class ThreadActivity : u32 {
  78. Normal = 0,
  79. Paused = 1,
  80. };
  81. enum class ThreadSchedStatus : u32 {
  82. None = 0,
  83. Paused = 1,
  84. Runnable = 2,
  85. Exited = 3,
  86. };
  87. enum class ThreadSchedFlags : u32 {
  88. ProcessPauseFlag = 1 << 4,
  89. ThreadPauseFlag = 1 << 5,
  90. ProcessDebugPauseFlag = 1 << 6,
  91. KernelInitPauseFlag = 1 << 8,
  92. };
  93. enum class ThreadSchedMasks : u32 {
  94. LowMask = 0x000f,
  95. HighMask = 0xfff0,
  96. ForcePauseMask = 0x0070,
  97. };
  98. class Thread final : public SynchronizationObject {
  99. public:
  100. explicit Thread(KernelCore& kernel);
  101. ~Thread() override;
  102. using MutexWaitingThreads = std::vector<std::shared_ptr<Thread>>;
  103. using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
  104. using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
  105. using ThreadSynchronizationObjects = std::vector<std::shared_ptr<SynchronizationObject>>;
  106. using HLECallback = std::function<bool(std::shared_ptr<Thread> thread)>;
  107. /**
  108. * Creates and returns a new thread. The new thread is immediately scheduled
  109. * @param system The instance of the whole system
  110. * @param name The friendly name desired for the thread
  111. * @param entry_point The address at which the thread should start execution
  112. * @param priority The thread's priority
  113. * @param arg User data to pass to the thread
  114. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  115. * @param stack_top The address of the thread's stack top
  116. * @param owner_process The parent process for the thread, if null, it's a kernel thread
  117. * @return A shared pointer to the newly created thread
  118. */
  119. static ResultVal<std::shared_ptr<Thread>> Create(Core::System& system, ThreadType type_flags,
  120. std::string name, VAddr entry_point,
  121. u32 priority, u64 arg, s32 processor_id,
  122. VAddr stack_top, Process* owner_process);
  123. /**
  124. * Creates and returns a new thread. The new thread is immediately scheduled
  125. * @param system The instance of the whole system
  126. * @param name The friendly name desired for the thread
  127. * @param entry_point The address at which the thread should start execution
  128. * @param priority The thread's priority
  129. * @param arg User data to pass to the thread
  130. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  131. * @param stack_top The address of the thread's stack top
  132. * @param owner_process The parent process for the thread, if null, it's a kernel thread
  133. * @param thread_start_func The function where the host context will start.
  134. * @param thread_start_parameter The parameter which will passed to host context on init
  135. * @return A shared pointer to the newly created thread
  136. */
  137. static ResultVal<std::shared_ptr<Thread>> Create(Core::System& system, ThreadType type_flags,
  138. std::string name, VAddr entry_point,
  139. u32 priority, u64 arg, s32 processor_id,
  140. VAddr stack_top, Process* owner_process,
  141. std::function<void(void*)>&& thread_start_func,
  142. void* thread_start_parameter);
  143. std::string GetName() const override {
  144. return name;
  145. }
  146. void SetName(std::string new_name) {
  147. name = std::move(new_name);
  148. }
  149. std::string GetTypeName() const override {
  150. return "Thread";
  151. }
  152. static constexpr HandleType HANDLE_TYPE = HandleType::Thread;
  153. HandleType GetHandleType() const override {
  154. return HANDLE_TYPE;
  155. }
  156. bool ShouldWait(const Thread* thread) const override;
  157. void Acquire(Thread* thread) override;
  158. bool IsSignaled() const override;
  159. /**
  160. * Gets the thread's current priority
  161. * @return The current thread's priority
  162. */
  163. u32 GetPriority() const {
  164. return current_priority;
  165. }
  166. /**
  167. * Gets the thread's nominal priority.
  168. * @return The current thread's nominal priority.
  169. */
  170. u32 GetNominalPriority() const {
  171. return nominal_priority;
  172. }
  173. /**
  174. * Sets the thread's current priority
  175. * @param priority The new priority
  176. */
  177. void SetPriority(u32 priority);
  178. /// Adds a thread to the list of threads that are waiting for a lock held by this thread.
  179. void AddMutexWaiter(std::shared_ptr<Thread> thread);
  180. /// Removes a thread from the list of threads that are waiting for a lock held by this thread.
  181. void RemoveMutexWaiter(std::shared_ptr<Thread> thread);
  182. /// Recalculates the current priority taking into account priority inheritance.
  183. void UpdatePriority();
  184. /// Changes the core that the thread is running or scheduled to run on.
  185. ResultCode SetCoreAndAffinityMask(s32 new_core, u64 new_affinity_mask);
  186. /**
  187. * Gets the thread's thread ID
  188. * @return The thread's ID
  189. */
  190. u64 GetThreadID() const {
  191. return thread_id;
  192. }
  193. /// Resumes a thread from waiting
  194. void ResumeFromWait();
  195. void OnWakeUp();
  196. ResultCode Start();
  197. /// Cancels a waiting operation that this thread may or may not be within.
  198. ///
  199. /// When the thread is within a waiting state, this will set the thread's
  200. /// waiting result to signal a canceled wait. The function will then resume
  201. /// this thread.
  202. ///
  203. void CancelWait();
  204. void SetSynchronizationResults(SynchronizationObject* object, ResultCode result);
  205. Core::ARM_Interface& ArmInterface();
  206. const Core::ARM_Interface& ArmInterface() const;
  207. SynchronizationObject* GetSignalingObject() const {
  208. return signaling_object;
  209. }
  210. ResultCode GetSignalingResult() const {
  211. return signaling_result;
  212. }
  213. /**
  214. * Retrieves the index that this particular object occupies in the list of objects
  215. * that the thread passed to WaitSynchronization, starting the search from the last element.
  216. *
  217. * It is used to set the output index of WaitSynchronization when the thread is awakened.
  218. *
  219. * When a thread wakes up due to an object signal, the kernel will use the index of the last
  220. * matching object in the wait objects list in case of having multiple instances of the same
  221. * object in the list.
  222. *
  223. * @param object Object to query the index of.
  224. */
  225. s32 GetSynchronizationObjectIndex(std::shared_ptr<SynchronizationObject> object) const;
  226. /**
  227. * Stops a thread, invalidating it from further use
  228. */
  229. void Stop();
  230. /*
  231. * Returns the Thread Local Storage address of the current thread
  232. * @returns VAddr of the thread's TLS
  233. */
  234. VAddr GetTLSAddress() const {
  235. return tls_address;
  236. }
  237. /*
  238. * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
  239. * @returns The value of the TPIDR_EL0 register.
  240. */
  241. u64 GetTPIDR_EL0() const {
  242. return tpidr_el0;
  243. }
  244. /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
  245. void SetTPIDR_EL0(u64 value) {
  246. tpidr_el0 = value;
  247. }
  248. /*
  249. * Returns the address of the current thread's command buffer, located in the TLS.
  250. * @returns VAddr of the thread's command buffer.
  251. */
  252. VAddr GetCommandBufferAddress() const;
  253. ThreadContext32& GetContext32() {
  254. return context_32;
  255. }
  256. const ThreadContext32& GetContext32() const {
  257. return context_32;
  258. }
  259. ThreadContext64& GetContext64() {
  260. return context_64;
  261. }
  262. const ThreadContext64& GetContext64() const {
  263. return context_64;
  264. }
  265. bool IsHLEThread() const {
  266. return (type & THREADTYPE_HLE) != 0;
  267. }
  268. bool IsSuspendThread() const {
  269. return (type & THREADTYPE_SUSPEND) != 0;
  270. }
  271. bool IsIdleThread() const {
  272. return (type & THREADTYPE_IDLE) != 0;
  273. }
  274. bool WasRunning() const {
  275. return was_running;
  276. }
  277. void SetWasRunning(bool value) {
  278. was_running = value;
  279. }
  280. std::shared_ptr<Common::Fiber>& GetHostContext();
  281. ThreadStatus GetStatus() const {
  282. return status;
  283. }
  284. void SetStatus(ThreadStatus new_status);
  285. u64 GetLastRunningTicks() const {
  286. return last_running_ticks;
  287. }
  288. u64 GetTotalCPUTimeTicks() const {
  289. return total_cpu_time_ticks;
  290. }
  291. void UpdateCPUTimeTicks(u64 ticks) {
  292. total_cpu_time_ticks += ticks;
  293. }
  294. s32 GetProcessorID() const {
  295. return processor_id;
  296. }
  297. void SetProcessorID(s32 new_core) {
  298. processor_id = new_core;
  299. }
  300. Process* GetOwnerProcess() {
  301. return owner_process;
  302. }
  303. const Process* GetOwnerProcess() const {
  304. return owner_process;
  305. }
  306. const ThreadSynchronizationObjects& GetSynchronizationObjects() const {
  307. return *wait_objects;
  308. }
  309. void SetSynchronizationObjects(ThreadSynchronizationObjects* objects) {
  310. wait_objects = objects;
  311. }
  312. void ClearSynchronizationObjects() {
  313. for (const auto& waiting_object : *wait_objects) {
  314. waiting_object->RemoveWaitingThread(SharedFrom(this));
  315. }
  316. wait_objects->clear();
  317. }
  318. /// Determines whether all the objects this thread is waiting on are ready.
  319. bool AllSynchronizationObjectsReady() const;
  320. const MutexWaitingThreads& GetMutexWaitingThreads() const {
  321. return wait_mutex_threads;
  322. }
  323. Thread* GetLockOwner() const {
  324. return lock_owner.get();
  325. }
  326. void SetLockOwner(std::shared_ptr<Thread> owner) {
  327. lock_owner = std::move(owner);
  328. }
  329. VAddr GetCondVarWaitAddress() const {
  330. return condvar_wait_address;
  331. }
  332. void SetCondVarWaitAddress(VAddr address) {
  333. condvar_wait_address = address;
  334. }
  335. VAddr GetMutexWaitAddress() const {
  336. return mutex_wait_address;
  337. }
  338. void SetMutexWaitAddress(VAddr address) {
  339. mutex_wait_address = address;
  340. }
  341. Handle GetWaitHandle() const {
  342. return wait_handle;
  343. }
  344. void SetWaitHandle(Handle handle) {
  345. wait_handle = handle;
  346. }
  347. VAddr GetArbiterWaitAddress() const {
  348. return arb_wait_address;
  349. }
  350. void SetArbiterWaitAddress(VAddr address) {
  351. arb_wait_address = address;
  352. }
  353. bool HasHLECallback() const {
  354. return hle_callback != nullptr;
  355. }
  356. void SetHLECallback(HLECallback callback) {
  357. hle_callback = std::move(callback);
  358. }
  359. void SetHLETimeEvent(Handle time_event) {
  360. hle_time_event = time_event;
  361. }
  362. void SetHLESyncObject(SynchronizationObject* object) {
  363. hle_object = object;
  364. }
  365. Handle GetHLETimeEvent() const {
  366. return hle_time_event;
  367. }
  368. SynchronizationObject* GetHLESyncObject() const {
  369. return hle_object;
  370. }
  371. void InvalidateHLECallback() {
  372. SetHLECallback(nullptr);
  373. }
  374. bool InvokeHLECallback(std::shared_ptr<Thread> thread);
  375. s32 GetIdealCore() const {
  376. return ideal_core;
  377. }
  378. u64 GetAffinityMask() const {
  379. return affinity_mask;
  380. }
  381. ResultCode SetActivity(ThreadActivity value);
  382. /// Sleeps this thread for the given amount of nanoseconds.
  383. ResultCode Sleep(s64 nanoseconds);
  384. /// Yields this thread without rebalancing loads.
  385. std::pair<ResultCode, bool> YieldSimple();
  386. /// Yields this thread and does a load rebalancing.
  387. std::pair<ResultCode, bool> YieldAndBalanceLoad();
  388. /// Yields this thread and if the core is left idle, loads are rebalanced
  389. std::pair<ResultCode, bool> YieldAndWaitForLoadBalancing();
  390. void IncrementYieldCount() {
  391. yield_count++;
  392. }
  393. u64 GetYieldCount() const {
  394. return yield_count;
  395. }
  396. ThreadSchedStatus GetSchedulingStatus() const {
  397. return static_cast<ThreadSchedStatus>(scheduling_state &
  398. static_cast<u32>(ThreadSchedMasks::LowMask));
  399. }
  400. bool IsRunnable() const {
  401. return scheduling_state == static_cast<u32>(ThreadSchedStatus::Runnable);
  402. }
  403. bool IsRunning() const {
  404. return is_running;
  405. }
  406. void SetIsRunning(bool value) {
  407. is_running = value;
  408. }
  409. bool IsSyncCancelled() const {
  410. return is_sync_cancelled;
  411. }
  412. void SetSyncCancelled(bool value) {
  413. is_sync_cancelled = value;
  414. }
  415. Handle GetGlobalHandle() const {
  416. return global_handle;
  417. }
  418. bool IsWaitingForArbitration() const {
  419. return waiting_for_arbitration;
  420. }
  421. void WaitForArbitration(bool set) {
  422. waiting_for_arbitration = set;
  423. }
  424. bool IsWaitingSync() const {
  425. return is_waiting_on_sync;
  426. }
  427. void SetWaitingSync(bool is_waiting) {
  428. is_waiting_on_sync = is_waiting;
  429. }
  430. bool IsPendingTermination() const {
  431. return will_be_terminated || GetSchedulingStatus() == ThreadSchedStatus::Exited;
  432. }
  433. bool IsPaused() const {
  434. return pausing_state != 0;
  435. }
  436. bool IsContinuousOnSVC() const {
  437. return is_continuous_on_svc;
  438. }
  439. void SetContinuousOnSVC(bool is_continuous) {
  440. is_continuous_on_svc = is_continuous;
  441. }
  442. bool IsPhantomMode() const {
  443. return is_phantom_mode;
  444. }
  445. void SetPhantomMode(bool phantom) {
  446. is_phantom_mode = phantom;
  447. }
  448. bool HasExited() const {
  449. return has_exited;
  450. }
  451. private:
  452. friend class GlobalScheduler;
  453. friend class Scheduler;
  454. void SetSchedulingStatus(ThreadSchedStatus new_status);
  455. void AddSchedulingFlag(ThreadSchedFlags flag);
  456. void RemoveSchedulingFlag(ThreadSchedFlags flag);
  457. void SetCurrentPriority(u32 new_priority);
  458. Common::SpinLock context_guard{};
  459. ThreadContext32 context_32{};
  460. ThreadContext64 context_64{};
  461. std::unique_ptr<Core::ARM_Interface> arm_interface{};
  462. std::shared_ptr<Common::Fiber> host_context{};
  463. u64 thread_id = 0;
  464. ThreadStatus status = ThreadStatus::Dormant;
  465. VAddr entry_point = 0;
  466. VAddr stack_top = 0;
  467. ThreadType type;
  468. /// Nominal thread priority, as set by the emulated application.
  469. /// The nominal priority is the thread priority without priority
  470. /// inheritance taken into account.
  471. u32 nominal_priority = 0;
  472. /// Current thread priority. This may change over the course of the
  473. /// thread's lifetime in order to facilitate priority inheritance.
  474. u32 current_priority = 0;
  475. u64 total_cpu_time_ticks = 0; ///< Total CPU running ticks.
  476. u64 last_running_ticks = 0; ///< CPU tick when thread was last running
  477. u64 yield_count = 0; ///< Number of redundant yields carried by this thread.
  478. ///< a redundant yield is one where no scheduling is changed
  479. s32 processor_id = 0;
  480. VAddr tls_address = 0; ///< Virtual address of the Thread Local Storage of the thread
  481. u64 tpidr_el0 = 0; ///< TPIDR_EL0 read/write system register.
  482. /// Process that owns this thread
  483. Process* owner_process;
  484. /// Objects that the thread is waiting on, in the same order as they were
  485. /// passed to WaitSynchronization.
  486. ThreadSynchronizationObjects* wait_objects;
  487. SynchronizationObject* signaling_object;
  488. ResultCode signaling_result{RESULT_SUCCESS};
  489. /// List of threads that are waiting for a mutex that is held by this thread.
  490. MutexWaitingThreads wait_mutex_threads;
  491. /// Thread that owns the lock that this thread is waiting for.
  492. std::shared_ptr<Thread> lock_owner;
  493. /// If waiting on a ConditionVariable, this is the ConditionVariable address
  494. VAddr condvar_wait_address = 0;
  495. /// If waiting on a Mutex, this is the mutex address
  496. VAddr mutex_wait_address = 0;
  497. /// The handle used to wait for the mutex.
  498. Handle wait_handle = 0;
  499. /// If waiting for an AddressArbiter, this is the address being waited on.
  500. VAddr arb_wait_address{0};
  501. bool waiting_for_arbitration{};
  502. /// Handle used as userdata to reference this object when inserting into the CoreTiming queue.
  503. Handle global_handle = 0;
  504. /// Callback for HLE Events
  505. HLECallback hle_callback;
  506. Handle hle_time_event;
  507. SynchronizationObject* hle_object;
  508. Scheduler* scheduler = nullptr;
  509. s32 ideal_core = -1;
  510. u64 affinity_mask = 1;
  511. s32 ideal_core_override = -1;
  512. u64 affinity_mask_override = 0x1;
  513. u32 affinity_override_count = 0;
  514. u32 scheduling_state = 0;
  515. u32 pausing_state = 0;
  516. bool is_running = false;
  517. bool is_waiting_on_sync = false;
  518. bool is_sync_cancelled = false;
  519. bool is_continuous_on_svc = false;
  520. bool will_be_terminated = false;
  521. bool is_phantom_mode = false;
  522. bool has_exited = false;
  523. bool was_running = false;
  524. std::string name;
  525. };
  526. } // namespace Kernel