k_thread.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. // Copyright 2021 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <array>
  6. #include <span>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include <boost/intrusive/list.hpp>
  11. #include "common/common_types.h"
  12. #include "common/intrusive_red_black_tree.h"
  13. #include "common/spin_lock.h"
  14. #include "core/arm/arm_interface.h"
  15. #include "core/hle/kernel/k_affinity_mask.h"
  16. #include "core/hle/kernel/k_light_lock.h"
  17. #include "core/hle/kernel/k_synchronization_object.h"
  18. #include "core/hle/kernel/object.h"
  19. #include "core/hle/kernel/svc_common.h"
  20. #include "core/hle/kernel/svc_types.h"
  21. #include "core/hle/result.h"
  22. namespace Common {
  23. class Fiber;
  24. }
  25. namespace Core {
  26. class ARM_Interface;
  27. class System;
  28. } // namespace Core
  29. namespace Kernel {
  30. class GlobalSchedulerContext;
  31. class KernelCore;
  32. class Process;
  33. class KScheduler;
  34. class KThreadQueue;
  35. using KThreadFunction = VAddr;
  36. enum class ThreadType : u32 {
  37. Main = 0,
  38. Kernel = 1,
  39. HighPriority = 2,
  40. User = 3,
  41. };
  42. DECLARE_ENUM_FLAG_OPERATORS(ThreadType);
  43. enum class SuspendType : u32 {
  44. Process = 0,
  45. Thread = 1,
  46. Debug = 2,
  47. Backtrace = 3,
  48. Init = 4,
  49. Count,
  50. };
  51. enum class ThreadState : u16 {
  52. Initialized = 0,
  53. Waiting = 1,
  54. Runnable = 2,
  55. Terminated = 3,
  56. SuspendShift = 4,
  57. Mask = (1 << SuspendShift) - 1,
  58. ProcessSuspended = (1 << (0 + SuspendShift)),
  59. ThreadSuspended = (1 << (1 + SuspendShift)),
  60. DebugSuspended = (1 << (2 + SuspendShift)),
  61. BacktraceSuspended = (1 << (3 + SuspendShift)),
  62. InitSuspended = (1 << (4 + SuspendShift)),
  63. SuspendFlagMask = ((1 << 5) - 1) << SuspendShift,
  64. };
  65. DECLARE_ENUM_FLAG_OPERATORS(ThreadState);
  66. enum class DpcFlag : u32 {
  67. Terminating = (1 << 0),
  68. Terminated = (1 << 1),
  69. };
  70. enum class ThreadWaitReasonForDebugging : u32 {
  71. None, ///< Thread is not waiting
  72. Sleep, ///< Thread is waiting due to a SleepThread SVC
  73. IPC, ///< Thread is waiting for the reply from an IPC request
  74. Synchronization, ///< Thread is waiting due to a WaitSynchronization SVC
  75. ConditionVar, ///< Thread is waiting due to a WaitProcessWideKey SVC
  76. Arbitration, ///< Thread is waiting due to a SignalToAddress/WaitForAddress SVC
  77. Suspended, ///< Thread is waiting due to process suspension
  78. };
  79. [[nodiscard]] KThread* GetCurrentThreadPointer(KernelCore& kernel);
  80. [[nodiscard]] KThread& GetCurrentThread(KernelCore& kernel);
  81. [[nodiscard]] s32 GetCurrentCoreId(KernelCore& kernel);
  82. class KThread final : public KSynchronizationObject, public boost::intrusive::list_base_hook<> {
  83. friend class KScheduler;
  84. friend class Process;
  85. public:
  86. static constexpr s32 DefaultThreadPriority = 44;
  87. static constexpr s32 IdleThreadPriority = Svc::LowestThreadPriority + 1;
  88. explicit KThread(KernelCore& kernel);
  89. ~KThread() override;
  90. public:
  91. using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
  92. using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
  93. using WaiterList = boost::intrusive::list<KThread>;
  94. /**
  95. * Creates and returns a new thread. The new thread is immediately scheduled
  96. * @param system The instance of the whole system
  97. * @param name The friendly name desired for the thread
  98. * @param entry_point The address at which the thread should start execution
  99. * @param priority The thread's priority
  100. * @param arg User data to pass to the thread
  101. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  102. * @param stack_top The address of the thread's stack top
  103. * @param owner_process The parent process for the thread, if null, it's a kernel thread
  104. * @return A shared pointer to the newly created thread
  105. */
  106. [[nodiscard]] static ResultVal<std::shared_ptr<KThread>> Create(
  107. Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point,
  108. u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process);
  109. /**
  110. * Creates and returns a new thread. The new thread is immediately scheduled
  111. * @param system The instance of the whole system
  112. * @param name The friendly name desired for the thread
  113. * @param entry_point The address at which the thread should start execution
  114. * @param priority The thread's priority
  115. * @param arg User data to pass to the thread
  116. * @param processor_id The ID(s) of the processors on which the thread is desired to be run
  117. * @param stack_top The address of the thread's stack top
  118. * @param owner_process The parent process for the thread, if null, it's a kernel thread
  119. * @param thread_start_func The function where the host context will start.
  120. * @param thread_start_parameter The parameter which will passed to host context on init
  121. * @return A shared pointer to the newly created thread
  122. */
  123. [[nodiscard]] static ResultVal<std::shared_ptr<KThread>> Create(
  124. Core::System& system, ThreadType type_flags, std::string name, VAddr entry_point,
  125. u32 priority, u64 arg, s32 processor_id, VAddr stack_top, Process* owner_process,
  126. std::function<void(void*)>&& thread_start_func, void* thread_start_parameter);
  127. [[nodiscard]] std::string GetName() const override {
  128. return name;
  129. }
  130. void SetName(std::string new_name) {
  131. name = std::move(new_name);
  132. }
  133. [[nodiscard]] std::string GetTypeName() const override {
  134. return "Thread";
  135. }
  136. static constexpr HandleType HANDLE_TYPE = HandleType::Thread;
  137. [[nodiscard]] HandleType GetHandleType() const override {
  138. return HANDLE_TYPE;
  139. }
  140. /**
  141. * Gets the thread's current priority
  142. * @return The current thread's priority
  143. */
  144. [[nodiscard]] s32 GetPriority() const {
  145. return priority;
  146. }
  147. /**
  148. * Sets the thread's current priority.
  149. * @param priority The new priority.
  150. */
  151. void SetPriority(s32 value) {
  152. priority = value;
  153. }
  154. /**
  155. * Gets the thread's nominal priority.
  156. * @return The current thread's nominal priority.
  157. */
  158. [[nodiscard]] s32 GetBasePriority() const {
  159. return base_priority;
  160. }
  161. /**
  162. * Gets the thread's thread ID
  163. * @return The thread's ID
  164. */
  165. [[nodiscard]] u64 GetThreadID() const {
  166. return thread_id;
  167. }
  168. void ContinueIfHasKernelWaiters() {
  169. if (GetNumKernelWaiters() > 0) {
  170. Continue();
  171. }
  172. }
  173. void Wakeup();
  174. void SetBasePriority(s32 value);
  175. [[nodiscard]] ResultCode Run();
  176. void Exit();
  177. [[nodiscard]] u32 GetSuspendFlags() const {
  178. return suspend_allowed_flags & suspend_request_flags;
  179. }
  180. [[nodiscard]] bool IsSuspended() const {
  181. return GetSuspendFlags() != 0;
  182. }
  183. [[nodiscard]] bool IsSuspendRequested(SuspendType type) const {
  184. return (suspend_request_flags &
  185. (1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)))) !=
  186. 0;
  187. }
  188. [[nodiscard]] bool IsSuspendRequested() const {
  189. return suspend_request_flags != 0;
  190. }
  191. void RequestSuspend(SuspendType type);
  192. void Resume(SuspendType type);
  193. void TrySuspend();
  194. void Continue();
  195. void Suspend();
  196. void Finalize() override;
  197. bool IsSignaled() const override;
  198. void SetSyncedObject(KSynchronizationObject* obj, ResultCode wait_res) {
  199. synced_object = obj;
  200. wait_result = wait_res;
  201. }
  202. [[nodiscard]] ResultCode GetWaitResult(KSynchronizationObject** out) const {
  203. *out = synced_object;
  204. return wait_result;
  205. }
  206. /*
  207. * Returns the Thread Local Storage address of the current thread
  208. * @returns VAddr of the thread's TLS
  209. */
  210. [[nodiscard]] VAddr GetTLSAddress() const {
  211. return tls_address;
  212. }
  213. /*
  214. * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
  215. * @returns The value of the TPIDR_EL0 register.
  216. */
  217. [[nodiscard]] u64 GetTPIDR_EL0() const {
  218. return thread_context_64.tpidr;
  219. }
  220. /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
  221. void SetTPIDR_EL0(u64 value) {
  222. thread_context_64.tpidr = value;
  223. thread_context_32.tpidr = static_cast<u32>(value);
  224. }
  225. [[nodiscard]] ThreadContext32& GetContext32() {
  226. return thread_context_32;
  227. }
  228. [[nodiscard]] const ThreadContext32& GetContext32() const {
  229. return thread_context_32;
  230. }
  231. [[nodiscard]] ThreadContext64& GetContext64() {
  232. return thread_context_64;
  233. }
  234. [[nodiscard]] const ThreadContext64& GetContext64() const {
  235. return thread_context_64;
  236. }
  237. [[nodiscard]] std::shared_ptr<Common::Fiber>& GetHostContext();
  238. [[nodiscard]] ThreadState GetState() const {
  239. return thread_state & ThreadState::Mask;
  240. }
  241. [[nodiscard]] ThreadState GetRawState() const {
  242. return thread_state;
  243. }
  244. void SetState(ThreadState state);
  245. [[nodiscard]] s64 GetLastScheduledTick() const {
  246. return last_scheduled_tick;
  247. }
  248. void SetLastScheduledTick(s64 tick) {
  249. last_scheduled_tick = tick;
  250. }
  251. void AddCpuTime([[maybe_unused]] s32 core_id_, s64 amount) {
  252. cpu_time += amount;
  253. // TODO(bunnei): Debug kernels track per-core tick counts. Should we?
  254. }
  255. [[nodiscard]] s64 GetCpuTime() const {
  256. return cpu_time;
  257. }
  258. [[nodiscard]] s32 GetActiveCore() const {
  259. return core_id;
  260. }
  261. void SetActiveCore(s32 core) {
  262. core_id = core;
  263. }
  264. [[nodiscard]] s32 GetCurrentCore() const {
  265. return current_core_id;
  266. }
  267. void SetCurrentCore(s32 core) {
  268. current_core_id = core;
  269. }
  270. [[nodiscard]] Process* GetOwnerProcess() {
  271. return parent;
  272. }
  273. [[nodiscard]] const Process* GetOwnerProcess() const {
  274. return parent;
  275. }
  276. [[nodiscard]] bool IsUserThread() const {
  277. return parent != nullptr;
  278. }
  279. [[nodiscard]] KThread* GetLockOwner() const {
  280. return lock_owner;
  281. }
  282. void SetLockOwner(KThread* owner) {
  283. lock_owner = owner;
  284. }
  285. [[nodiscard]] const KAffinityMask& GetAffinityMask() const {
  286. return physical_affinity_mask;
  287. }
  288. [[nodiscard]] ResultCode GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
  289. [[nodiscard]] ResultCode GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
  290. [[nodiscard]] ResultCode SetCoreMask(s32 core_id, u64 v_affinity_mask);
  291. [[nodiscard]] ResultCode SetActivity(Svc::ThreadActivity activity);
  292. [[nodiscard]] ResultCode Sleep(s64 timeout);
  293. [[nodiscard]] s64 GetYieldScheduleCount() const {
  294. return schedule_count;
  295. }
  296. void SetYieldScheduleCount(s64 count) {
  297. schedule_count = count;
  298. }
  299. void WaitCancel();
  300. [[nodiscard]] bool IsWaitCancelled() const {
  301. return wait_cancelled;
  302. }
  303. [[nodiscard]] void ClearWaitCancelled() {
  304. wait_cancelled = false;
  305. }
  306. [[nodiscard]] bool IsCancellable() const {
  307. return cancellable;
  308. }
  309. void SetCancellable() {
  310. cancellable = true;
  311. }
  312. void ClearCancellable() {
  313. cancellable = false;
  314. }
  315. [[nodiscard]] bool IsTerminationRequested() const {
  316. return termination_requested || GetRawState() == ThreadState::Terminated;
  317. }
  318. struct StackParameters {
  319. u8 svc_permission[0x10];
  320. std::atomic<u8> dpc_flags;
  321. u8 current_svc_id;
  322. bool is_calling_svc;
  323. bool is_in_exception_handler;
  324. bool is_pinned;
  325. s32 disable_count;
  326. KThread* cur_thread;
  327. };
  328. [[nodiscard]] StackParameters& GetStackParameters() {
  329. return stack_parameters;
  330. }
  331. [[nodiscard]] const StackParameters& GetStackParameters() const {
  332. return stack_parameters;
  333. }
  334. class QueueEntry {
  335. public:
  336. constexpr QueueEntry() = default;
  337. constexpr void Initialize() {
  338. prev = nullptr;
  339. next = nullptr;
  340. }
  341. constexpr KThread* GetPrev() const {
  342. return prev;
  343. }
  344. constexpr KThread* GetNext() const {
  345. return next;
  346. }
  347. constexpr void SetPrev(KThread* thread) {
  348. prev = thread;
  349. }
  350. constexpr void SetNext(KThread* thread) {
  351. next = thread;
  352. }
  353. private:
  354. KThread* prev{};
  355. KThread* next{};
  356. };
  357. [[nodiscard]] QueueEntry& GetPriorityQueueEntry(s32 core) {
  358. return per_core_priority_queue_entry[core];
  359. }
  360. [[nodiscard]] const QueueEntry& GetPriorityQueueEntry(s32 core) const {
  361. return per_core_priority_queue_entry[core];
  362. }
  363. void SetSleepingQueue(KThreadQueue* q) {
  364. sleeping_queue = q;
  365. }
  366. [[nodiscard]] s32 GetDisableDispatchCount() const {
  367. return this->GetStackParameters().disable_count;
  368. }
  369. void DisableDispatch() {
  370. ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() >= 0);
  371. this->GetStackParameters().disable_count++;
  372. }
  373. void EnableDispatch() {
  374. ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() > 0);
  375. this->GetStackParameters().disable_count--;
  376. }
  377. void Pin();
  378. void Unpin();
  379. void SetInExceptionHandler() {
  380. this->GetStackParameters().is_in_exception_handler = true;
  381. }
  382. void ClearInExceptionHandler() {
  383. this->GetStackParameters().is_in_exception_handler = false;
  384. }
  385. [[nodiscard]] bool IsInExceptionHandler() const {
  386. return this->GetStackParameters().is_in_exception_handler;
  387. }
  388. void SetIsCallingSvc() {
  389. this->GetStackParameters().is_calling_svc = true;
  390. }
  391. void ClearIsCallingSvc() {
  392. this->GetStackParameters().is_calling_svc = false;
  393. }
  394. [[nodiscard]] bool IsCallingSvc() const {
  395. return this->GetStackParameters().is_calling_svc;
  396. }
  397. [[nodiscard]] u8 GetSvcId() const {
  398. return this->GetStackParameters().current_svc_id;
  399. }
  400. void RegisterDpc(DpcFlag flag) {
  401. this->GetStackParameters().dpc_flags |= static_cast<u8>(flag);
  402. }
  403. void ClearDpc(DpcFlag flag) {
  404. this->GetStackParameters().dpc_flags &= ~static_cast<u8>(flag);
  405. }
  406. [[nodiscard]] u8 GetDpc() const {
  407. return this->GetStackParameters().dpc_flags;
  408. }
  409. [[nodiscard]] bool HasDpc() const {
  410. return this->GetDpc() != 0;
  411. }
  412. void SetWaitReasonForDebugging(ThreadWaitReasonForDebugging reason) {
  413. wait_reason_for_debugging = reason;
  414. }
  415. [[nodiscard]] ThreadWaitReasonForDebugging GetWaitReasonForDebugging() const {
  416. return wait_reason_for_debugging;
  417. }
  418. [[nodiscard]] ThreadType GetThreadTypeForDebugging() const {
  419. return thread_type_for_debugging;
  420. }
  421. void SetWaitObjectsForDebugging(const std::span<KSynchronizationObject*>& objects) {
  422. wait_objects_for_debugging.clear();
  423. wait_objects_for_debugging.reserve(objects.size());
  424. for (const auto& object : objects) {
  425. wait_objects_for_debugging.emplace_back(object);
  426. }
  427. }
  428. [[nodiscard]] const std::vector<KSynchronizationObject*>& GetWaitObjectsForDebugging() const {
  429. return wait_objects_for_debugging;
  430. }
  431. void SetMutexWaitAddressForDebugging(VAddr address) {
  432. mutex_wait_address_for_debugging = address;
  433. }
  434. [[nodiscard]] VAddr GetMutexWaitAddressForDebugging() const {
  435. return mutex_wait_address_for_debugging;
  436. }
  437. [[nodiscard]] s32 GetIdealCoreForDebugging() const {
  438. return virtual_ideal_core_id;
  439. }
  440. void AddWaiter(KThread* thread);
  441. void RemoveWaiter(KThread* thread);
  442. [[nodiscard]] ResultCode GetThreadContext3(std::vector<u8>& out);
  443. [[nodiscard]] KThread* RemoveWaiterByKey(s32* out_num_waiters, VAddr key);
  444. [[nodiscard]] VAddr GetAddressKey() const {
  445. return address_key;
  446. }
  447. [[nodiscard]] u32 GetAddressKeyValue() const {
  448. return address_key_value;
  449. }
  450. void SetAddressKey(VAddr key) {
  451. address_key = key;
  452. }
  453. void SetAddressKey(VAddr key, u32 val) {
  454. address_key = key;
  455. address_key_value = val;
  456. }
  457. [[nodiscard]] bool HasWaiters() const {
  458. return !waiter_list.empty();
  459. }
  460. [[nodiscard]] s32 GetNumKernelWaiters() const {
  461. return num_kernel_waiters;
  462. }
  463. [[nodiscard]] u64 GetConditionVariableKey() const {
  464. return condvar_key;
  465. }
  466. [[nodiscard]] u64 GetAddressArbiterKey() const {
  467. return condvar_key;
  468. }
  469. private:
  470. static constexpr size_t PriorityInheritanceCountMax = 10;
  471. union SyncObjectBuffer {
  472. std::array<KSynchronizationObject*, Svc::ArgumentHandleCountMax> sync_objects{};
  473. std::array<Handle,
  474. Svc::ArgumentHandleCountMax*(sizeof(KSynchronizationObject*) / sizeof(Handle))>
  475. handles;
  476. constexpr SyncObjectBuffer() {}
  477. };
  478. static_assert(sizeof(SyncObjectBuffer::sync_objects) == sizeof(SyncObjectBuffer::handles));
  479. struct ConditionVariableComparator {
  480. struct LightCompareType {
  481. u64 cv_key{};
  482. s32 priority{};
  483. [[nodiscard]] constexpr u64 GetConditionVariableKey() const {
  484. return cv_key;
  485. }
  486. [[nodiscard]] constexpr s32 GetPriority() const {
  487. return priority;
  488. }
  489. };
  490. template <typename T>
  491. requires(
  492. std::same_as<T, KThread> ||
  493. std::same_as<T, LightCompareType>) static constexpr int Compare(const T& lhs,
  494. const KThread& rhs) {
  495. const u64 l_key = lhs.GetConditionVariableKey();
  496. const u64 r_key = rhs.GetConditionVariableKey();
  497. if (l_key < r_key) {
  498. // Sort first by key
  499. return -1;
  500. } else if (l_key == r_key && lhs.GetPriority() < rhs.GetPriority()) {
  501. // And then by priority.
  502. return -1;
  503. } else {
  504. return 1;
  505. }
  506. }
  507. };
  508. void AddWaiterImpl(KThread* thread);
  509. void RemoveWaiterImpl(KThread* thread);
  510. void StartTermination();
  511. [[nodiscard]] ResultCode Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top,
  512. s32 prio, s32 virt_core, Process* owner, ThreadType type);
  513. [[nodiscard]] static ResultCode InitializeThread(KThread* thread, KThreadFunction func,
  514. uintptr_t arg, VAddr user_stack_top, s32 prio,
  515. s32 core, Process* owner, ThreadType type);
  516. static void RestorePriority(KernelCore& kernel, KThread* thread);
  517. // For core KThread implementation
  518. ThreadContext32 thread_context_32{};
  519. ThreadContext64 thread_context_64{};
  520. Common::IntrusiveRedBlackTreeNode condvar_arbiter_tree_node{};
  521. s32 priority{};
  522. using ConditionVariableThreadTreeTraits =
  523. Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
  524. &KThread::condvar_arbiter_tree_node>;
  525. using ConditionVariableThreadTree =
  526. ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;
  527. ConditionVariableThreadTree* condvar_tree{};
  528. u64 condvar_key{};
  529. u64 virtual_affinity_mask{};
  530. KAffinityMask physical_affinity_mask{};
  531. u64 thread_id{};
  532. std::atomic<s64> cpu_time{};
  533. KSynchronizationObject* synced_object{};
  534. VAddr address_key{};
  535. Process* parent{};
  536. VAddr kernel_stack_top{};
  537. u32* light_ipc_data{};
  538. VAddr tls_address{};
  539. KLightLock activity_pause_lock;
  540. s64 schedule_count{};
  541. s64 last_scheduled_tick{};
  542. std::array<QueueEntry, Core::Hardware::NUM_CPU_CORES> per_core_priority_queue_entry{};
  543. KThreadQueue* sleeping_queue{};
  544. WaiterList waiter_list{};
  545. WaiterList pinned_waiter_list{};
  546. KThread* lock_owner{};
  547. u32 address_key_value{};
  548. u32 suspend_request_flags{};
  549. u32 suspend_allowed_flags{};
  550. ResultCode wait_result{RESULT_SUCCESS};
  551. s32 base_priority{};
  552. s32 physical_ideal_core_id{};
  553. s32 virtual_ideal_core_id{};
  554. s32 num_kernel_waiters{};
  555. s32 current_core_id{};
  556. s32 core_id{};
  557. KAffinityMask original_physical_affinity_mask{};
  558. s32 original_physical_ideal_core_id{};
  559. s32 num_core_migration_disables{};
  560. ThreadState thread_state{};
  561. std::atomic<bool> termination_requested{};
  562. bool wait_cancelled{};
  563. bool cancellable{};
  564. bool signaled{};
  565. bool initialized{};
  566. bool debug_attached{};
  567. s8 priority_inheritance_count{};
  568. bool resource_limit_release_hint{};
  569. StackParameters stack_parameters{};
  570. Common::SpinLock context_guard{};
  571. // For emulation
  572. std::shared_ptr<Common::Fiber> host_context{};
  573. // For debugging
  574. std::vector<KSynchronizationObject*> wait_objects_for_debugging;
  575. VAddr mutex_wait_address_for_debugging{};
  576. ThreadWaitReasonForDebugging wait_reason_for_debugging{};
  577. ThreadType thread_type_for_debugging{};
  578. std::string name;
  579. public:
  580. using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
  581. void SetConditionVariable(ConditionVariableThreadTree* tree, VAddr address, u64 cv_key,
  582. u32 value) {
  583. condvar_tree = tree;
  584. condvar_key = cv_key;
  585. address_key = address;
  586. address_key_value = value;
  587. }
  588. void ClearConditionVariable() {
  589. condvar_tree = nullptr;
  590. }
  591. [[nodiscard]] bool IsWaitingForConditionVariable() const {
  592. return condvar_tree != nullptr;
  593. }
  594. void SetAddressArbiter(ConditionVariableThreadTree* tree, u64 address) {
  595. condvar_tree = tree;
  596. condvar_key = address;
  597. }
  598. void ClearAddressArbiter() {
  599. condvar_tree = nullptr;
  600. }
  601. [[nodiscard]] bool IsWaitingForAddressArbiter() const {
  602. return condvar_tree != nullptr;
  603. }
  604. [[nodiscard]] ConditionVariableThreadTree* GetConditionVariableTree() const {
  605. return condvar_tree;
  606. }
  607. };
  608. } // namespace Kernel