k_thread.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 "core/arm/arm_interface.h"
  14. #include "core/hle/kernel/k_affinity_mask.h"
  15. #include "core/hle/kernel/k_light_lock.h"
  16. #include "core/hle/kernel/k_spin_lock.h"
  17. #include "core/hle/kernel/k_synchronization_object.h"
  18. #include "core/hle/kernel/slab_helpers.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 KProcess;
  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 KAutoObjectWithSlabHeapAndContainer<KThread, KSynchronizationObject>,
  83. public boost::intrusive::list_base_hook<> {
  84. KERNEL_AUTOOBJECT_TRAITS(KThread, KSynchronizationObject);
  85. private:
  86. friend class KScheduler;
  87. friend class KProcess;
  88. public:
  89. static constexpr s32 DefaultThreadPriority = 44;
  90. static constexpr s32 IdleThreadPriority = Svc::LowestThreadPriority + 1;
  91. explicit KThread(KernelCore& kernel);
  92. ~KThread() override;
  93. public:
  94. using ThreadContext32 = Core::ARM_Interface::ThreadContext32;
  95. using ThreadContext64 = Core::ARM_Interface::ThreadContext64;
  96. using WaiterList = boost::intrusive::list<KThread>;
  97. void SetName(std::string new_name) {
  98. name = std::move(new_name);
  99. }
  100. /**
  101. * Gets the thread's current priority
  102. * @return The current thread's priority
  103. */
  104. [[nodiscard]] s32 GetPriority() const {
  105. return priority;
  106. }
  107. /**
  108. * Sets the thread's current priority.
  109. * @param priority The new priority.
  110. */
  111. void SetPriority(s32 value) {
  112. priority = value;
  113. }
  114. /**
  115. * Gets the thread's nominal priority.
  116. * @return The current thread's nominal priority.
  117. */
  118. [[nodiscard]] s32 GetBasePriority() const {
  119. return base_priority;
  120. }
  121. /**
  122. * Gets the thread's thread ID
  123. * @return The thread's ID
  124. */
  125. [[nodiscard]] u64 GetThreadID() const {
  126. return thread_id;
  127. }
  128. void ContinueIfHasKernelWaiters() {
  129. if (GetNumKernelWaiters() > 0) {
  130. Continue();
  131. }
  132. }
  133. void Wakeup();
  134. void SetBasePriority(s32 value);
  135. [[nodiscard]] ResultCode Run();
  136. void Exit();
  137. [[nodiscard]] u32 GetSuspendFlags() const {
  138. return suspend_allowed_flags & suspend_request_flags;
  139. }
  140. [[nodiscard]] bool IsSuspended() const {
  141. return GetSuspendFlags() != 0;
  142. }
  143. [[nodiscard]] bool IsSuspendRequested(SuspendType type) const {
  144. return (suspend_request_flags &
  145. (1u << (static_cast<u32>(ThreadState::SuspendShift) + static_cast<u32>(type)))) !=
  146. 0;
  147. }
  148. [[nodiscard]] bool IsSuspendRequested() const {
  149. return suspend_request_flags != 0;
  150. }
  151. void RequestSuspend(SuspendType type);
  152. void Resume(SuspendType type);
  153. void TrySuspend();
  154. void Continue();
  155. void Suspend();
  156. void SetSyncedObject(KSynchronizationObject* obj, ResultCode wait_res) {
  157. synced_object = obj;
  158. wait_result = wait_res;
  159. }
  160. [[nodiscard]] ResultCode GetWaitResult(KSynchronizationObject** out) const {
  161. *out = synced_object;
  162. return wait_result;
  163. }
  164. /*
  165. * Returns the Thread Local Storage address of the current thread
  166. * @returns VAddr of the thread's TLS
  167. */
  168. [[nodiscard]] VAddr GetTLSAddress() const {
  169. return tls_address;
  170. }
  171. /*
  172. * Returns the value of the TPIDR_EL0 Read/Write system register for this thread.
  173. * @returns The value of the TPIDR_EL0 register.
  174. */
  175. [[nodiscard]] u64 GetTPIDR_EL0() const {
  176. return thread_context_64.tpidr;
  177. }
  178. /// Sets the value of the TPIDR_EL0 Read/Write system register for this thread.
  179. void SetTPIDR_EL0(u64 value) {
  180. thread_context_64.tpidr = value;
  181. thread_context_32.tpidr = static_cast<u32>(value);
  182. }
  183. [[nodiscard]] ThreadContext32& GetContext32() {
  184. return thread_context_32;
  185. }
  186. [[nodiscard]] const ThreadContext32& GetContext32() const {
  187. return thread_context_32;
  188. }
  189. [[nodiscard]] ThreadContext64& GetContext64() {
  190. return thread_context_64;
  191. }
  192. [[nodiscard]] const ThreadContext64& GetContext64() const {
  193. return thread_context_64;
  194. }
  195. [[nodiscard]] std::shared_ptr<Common::Fiber>& GetHostContext();
  196. [[nodiscard]] ThreadState GetState() const {
  197. return thread_state & ThreadState::Mask;
  198. }
  199. [[nodiscard]] ThreadState GetRawState() const {
  200. return thread_state;
  201. }
  202. void SetState(ThreadState state);
  203. [[nodiscard]] s64 GetLastScheduledTick() const {
  204. return last_scheduled_tick;
  205. }
  206. void SetLastScheduledTick(s64 tick) {
  207. last_scheduled_tick = tick;
  208. }
  209. void AddCpuTime([[maybe_unused]] s32 core_id_, s64 amount) {
  210. cpu_time += amount;
  211. // TODO(bunnei): Debug kernels track per-core tick counts. Should we?
  212. }
  213. [[nodiscard]] s64 GetCpuTime() const {
  214. return cpu_time;
  215. }
  216. [[nodiscard]] s32 GetActiveCore() const {
  217. return core_id;
  218. }
  219. void SetActiveCore(s32 core) {
  220. core_id = core;
  221. }
  222. [[nodiscard]] s32 GetCurrentCore() const {
  223. return current_core_id;
  224. }
  225. void SetCurrentCore(s32 core) {
  226. current_core_id = core;
  227. }
  228. [[nodiscard]] KProcess* GetOwnerProcess() {
  229. return parent;
  230. }
  231. [[nodiscard]] const KProcess* GetOwnerProcess() const {
  232. return parent;
  233. }
  234. [[nodiscard]] bool IsUserThread() const {
  235. return parent != nullptr;
  236. }
  237. [[nodiscard]] KThread* GetLockOwner() const {
  238. return lock_owner;
  239. }
  240. void SetLockOwner(KThread* owner) {
  241. lock_owner = owner;
  242. }
  243. [[nodiscard]] const KAffinityMask& GetAffinityMask() const {
  244. return physical_affinity_mask;
  245. }
  246. [[nodiscard]] ResultCode GetCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
  247. [[nodiscard]] ResultCode GetPhysicalCoreMask(s32* out_ideal_core, u64* out_affinity_mask);
  248. [[nodiscard]] ResultCode SetCoreMask(s32 core_id, u64 v_affinity_mask);
  249. [[nodiscard]] ResultCode SetActivity(Svc::ThreadActivity activity);
  250. [[nodiscard]] ResultCode Sleep(s64 timeout);
  251. [[nodiscard]] s64 GetYieldScheduleCount() const {
  252. return schedule_count;
  253. }
  254. void SetYieldScheduleCount(s64 count) {
  255. schedule_count = count;
  256. }
  257. void WaitCancel();
  258. [[nodiscard]] bool IsWaitCancelled() const {
  259. return wait_cancelled;
  260. }
  261. void ClearWaitCancelled() {
  262. wait_cancelled = false;
  263. }
  264. [[nodiscard]] bool IsCancellable() const {
  265. return cancellable;
  266. }
  267. void SetCancellable() {
  268. cancellable = true;
  269. }
  270. void ClearCancellable() {
  271. cancellable = false;
  272. }
  273. [[nodiscard]] bool IsTerminationRequested() const {
  274. return termination_requested || GetRawState() == ThreadState::Terminated;
  275. }
  276. [[nodiscard]] virtual u64 GetId() const override final {
  277. return this->GetThreadID();
  278. }
  279. [[nodiscard]] virtual bool IsInitialized() const override {
  280. return initialized;
  281. }
  282. [[nodiscard]] virtual uintptr_t GetPostDestroyArgument() const override {
  283. return reinterpret_cast<uintptr_t>(parent) | (resource_limit_release_hint ? 1 : 0);
  284. }
  285. virtual void Finalize() override;
  286. [[nodiscard]] virtual bool IsSignaled() const override;
  287. static void PostDestroy(uintptr_t arg);
  288. [[nodiscard]] static ResultCode InitializeDummyThread(KThread* thread);
  289. [[nodiscard]] static ResultCode InitializeIdleThread(Core::System& system, KThread* thread,
  290. s32 virt_core);
  291. [[nodiscard]] static ResultCode InitializeHighPriorityThread(Core::System& system,
  292. KThread* thread,
  293. KThreadFunction func,
  294. uintptr_t arg, s32 virt_core);
  295. [[nodiscard]] static ResultCode InitializeUserThread(Core::System& system, KThread* thread,
  296. KThreadFunction func, uintptr_t arg,
  297. VAddr user_stack_top, s32 prio,
  298. s32 virt_core, KProcess* owner);
  299. public:
  300. struct StackParameters {
  301. u8 svc_permission[0x10];
  302. std::atomic<u8> dpc_flags;
  303. u8 current_svc_id;
  304. bool is_calling_svc;
  305. bool is_in_exception_handler;
  306. bool is_pinned;
  307. s32 disable_count;
  308. KThread* cur_thread;
  309. };
  310. [[nodiscard]] StackParameters& GetStackParameters() {
  311. return stack_parameters;
  312. }
  313. [[nodiscard]] const StackParameters& GetStackParameters() const {
  314. return stack_parameters;
  315. }
  316. class QueueEntry {
  317. public:
  318. constexpr QueueEntry() = default;
  319. constexpr void Initialize() {
  320. prev = nullptr;
  321. next = nullptr;
  322. }
  323. constexpr KThread* GetPrev() const {
  324. return prev;
  325. }
  326. constexpr KThread* GetNext() const {
  327. return next;
  328. }
  329. constexpr void SetPrev(KThread* thread) {
  330. prev = thread;
  331. }
  332. constexpr void SetNext(KThread* thread) {
  333. next = thread;
  334. }
  335. private:
  336. KThread* prev{};
  337. KThread* next{};
  338. };
  339. [[nodiscard]] QueueEntry& GetPriorityQueueEntry(s32 core) {
  340. return per_core_priority_queue_entry[core];
  341. }
  342. [[nodiscard]] const QueueEntry& GetPriorityQueueEntry(s32 core) const {
  343. return per_core_priority_queue_entry[core];
  344. }
  345. void SetSleepingQueue(KThreadQueue* q) {
  346. sleeping_queue = q;
  347. }
  348. [[nodiscard]] s32 GetDisableDispatchCount() const {
  349. return this->GetStackParameters().disable_count;
  350. }
  351. void DisableDispatch() {
  352. ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() >= 0);
  353. this->GetStackParameters().disable_count++;
  354. }
  355. void EnableDispatch() {
  356. ASSERT(GetCurrentThread(kernel).GetDisableDispatchCount() > 0);
  357. this->GetStackParameters().disable_count--;
  358. }
  359. void Pin();
  360. void Unpin();
  361. void SetInExceptionHandler() {
  362. this->GetStackParameters().is_in_exception_handler = true;
  363. }
  364. void ClearInExceptionHandler() {
  365. this->GetStackParameters().is_in_exception_handler = false;
  366. }
  367. [[nodiscard]] bool IsInExceptionHandler() const {
  368. return this->GetStackParameters().is_in_exception_handler;
  369. }
  370. void SetIsCallingSvc() {
  371. this->GetStackParameters().is_calling_svc = true;
  372. }
  373. void ClearIsCallingSvc() {
  374. this->GetStackParameters().is_calling_svc = false;
  375. }
  376. [[nodiscard]] bool IsCallingSvc() const {
  377. return this->GetStackParameters().is_calling_svc;
  378. }
  379. [[nodiscard]] u8 GetSvcId() const {
  380. return this->GetStackParameters().current_svc_id;
  381. }
  382. void RegisterDpc(DpcFlag flag) {
  383. this->GetStackParameters().dpc_flags |= static_cast<u8>(flag);
  384. }
  385. void ClearDpc(DpcFlag flag) {
  386. this->GetStackParameters().dpc_flags &= ~static_cast<u8>(flag);
  387. }
  388. [[nodiscard]] u8 GetDpc() const {
  389. return this->GetStackParameters().dpc_flags;
  390. }
  391. [[nodiscard]] bool HasDpc() const {
  392. return this->GetDpc() != 0;
  393. }
  394. void SetWaitReasonForDebugging(ThreadWaitReasonForDebugging reason) {
  395. wait_reason_for_debugging = reason;
  396. }
  397. [[nodiscard]] ThreadWaitReasonForDebugging GetWaitReasonForDebugging() const {
  398. return wait_reason_for_debugging;
  399. }
  400. [[nodiscard]] ThreadType GetThreadTypeForDebugging() const {
  401. return thread_type_for_debugging;
  402. }
  403. void SetWaitObjectsForDebugging(const std::span<KSynchronizationObject*>& objects) {
  404. wait_objects_for_debugging.clear();
  405. wait_objects_for_debugging.reserve(objects.size());
  406. for (const auto& object : objects) {
  407. wait_objects_for_debugging.emplace_back(object);
  408. }
  409. }
  410. [[nodiscard]] const std::vector<KSynchronizationObject*>& GetWaitObjectsForDebugging() const {
  411. return wait_objects_for_debugging;
  412. }
  413. void SetMutexWaitAddressForDebugging(VAddr address) {
  414. mutex_wait_address_for_debugging = address;
  415. }
  416. [[nodiscard]] VAddr GetMutexWaitAddressForDebugging() const {
  417. return mutex_wait_address_for_debugging;
  418. }
  419. [[nodiscard]] s32 GetIdealCoreForDebugging() const {
  420. return virtual_ideal_core_id;
  421. }
  422. void AddWaiter(KThread* thread);
  423. void RemoveWaiter(KThread* thread);
  424. [[nodiscard]] ResultCode GetThreadContext3(std::vector<u8>& out);
  425. [[nodiscard]] KThread* RemoveWaiterByKey(s32* out_num_waiters, VAddr key);
  426. [[nodiscard]] VAddr GetAddressKey() const {
  427. return address_key;
  428. }
  429. [[nodiscard]] u32 GetAddressKeyValue() const {
  430. return address_key_value;
  431. }
  432. void SetAddressKey(VAddr key) {
  433. address_key = key;
  434. }
  435. void SetAddressKey(VAddr key, u32 val) {
  436. address_key = key;
  437. address_key_value = val;
  438. }
  439. [[nodiscard]] bool HasWaiters() const {
  440. return !waiter_list.empty();
  441. }
  442. [[nodiscard]] s32 GetNumKernelWaiters() const {
  443. return num_kernel_waiters;
  444. }
  445. [[nodiscard]] u64 GetConditionVariableKey() const {
  446. return condvar_key;
  447. }
  448. [[nodiscard]] u64 GetAddressArbiterKey() const {
  449. return condvar_key;
  450. }
  451. private:
  452. static constexpr size_t PriorityInheritanceCountMax = 10;
  453. union SyncObjectBuffer {
  454. std::array<KSynchronizationObject*, Svc::ArgumentHandleCountMax> sync_objects{};
  455. std::array<Handle,
  456. Svc::ArgumentHandleCountMax*(sizeof(KSynchronizationObject*) / sizeof(Handle))>
  457. handles;
  458. constexpr SyncObjectBuffer() {}
  459. };
  460. static_assert(sizeof(SyncObjectBuffer::sync_objects) == sizeof(SyncObjectBuffer::handles));
  461. struct ConditionVariableComparator {
  462. struct LightCompareType {
  463. u64 cv_key{};
  464. s32 priority{};
  465. [[nodiscard]] constexpr u64 GetConditionVariableKey() const {
  466. return cv_key;
  467. }
  468. [[nodiscard]] constexpr s32 GetPriority() const {
  469. return priority;
  470. }
  471. };
  472. template <typename T>
  473. requires(
  474. std::same_as<T, KThread> ||
  475. std::same_as<T, LightCompareType>) static constexpr int Compare(const T& lhs,
  476. const KThread& rhs) {
  477. const u64 l_key = lhs.GetConditionVariableKey();
  478. const u64 r_key = rhs.GetConditionVariableKey();
  479. if (l_key < r_key) {
  480. // Sort first by key
  481. return -1;
  482. } else if (l_key == r_key && lhs.GetPriority() < rhs.GetPriority()) {
  483. // And then by priority.
  484. return -1;
  485. } else {
  486. return 1;
  487. }
  488. }
  489. };
  490. void AddWaiterImpl(KThread* thread);
  491. void RemoveWaiterImpl(KThread* thread);
  492. void StartTermination();
  493. [[nodiscard]] ResultCode Initialize(KThreadFunction func, uintptr_t arg, VAddr user_stack_top,
  494. s32 prio, s32 virt_core, KProcess* owner, ThreadType type);
  495. [[nodiscard]] static ResultCode InitializeThread(KThread* thread, KThreadFunction func,
  496. uintptr_t arg, VAddr user_stack_top, s32 prio,
  497. s32 core, KProcess* owner, ThreadType type,
  498. std::function<void(void*)>&& init_func,
  499. void* init_func_parameter);
  500. static void RestorePriority(KernelCore& kernel, KThread* thread);
  501. // For core KThread implementation
  502. ThreadContext32 thread_context_32{};
  503. ThreadContext64 thread_context_64{};
  504. Common::IntrusiveRedBlackTreeNode condvar_arbiter_tree_node{};
  505. s32 priority{};
  506. using ConditionVariableThreadTreeTraits =
  507. Common::IntrusiveRedBlackTreeMemberTraitsDeferredAssert<
  508. &KThread::condvar_arbiter_tree_node>;
  509. using ConditionVariableThreadTree =
  510. ConditionVariableThreadTreeTraits::TreeType<ConditionVariableComparator>;
  511. ConditionVariableThreadTree* condvar_tree{};
  512. u64 condvar_key{};
  513. u64 virtual_affinity_mask{};
  514. KAffinityMask physical_affinity_mask{};
  515. u64 thread_id{};
  516. std::atomic<s64> cpu_time{};
  517. KSynchronizationObject* synced_object{};
  518. VAddr address_key{};
  519. KProcess* parent{};
  520. VAddr kernel_stack_top{};
  521. u32* light_ipc_data{};
  522. VAddr tls_address{};
  523. KLightLock activity_pause_lock;
  524. s64 schedule_count{};
  525. s64 last_scheduled_tick{};
  526. std::array<QueueEntry, Core::Hardware::NUM_CPU_CORES> per_core_priority_queue_entry{};
  527. KThreadQueue* sleeping_queue{};
  528. WaiterList waiter_list{};
  529. WaiterList pinned_waiter_list{};
  530. KThread* lock_owner{};
  531. u32 address_key_value{};
  532. u32 suspend_request_flags{};
  533. u32 suspend_allowed_flags{};
  534. ResultCode wait_result{RESULT_SUCCESS};
  535. s32 base_priority{};
  536. s32 physical_ideal_core_id{};
  537. s32 virtual_ideal_core_id{};
  538. s32 num_kernel_waiters{};
  539. s32 current_core_id{};
  540. s32 core_id{};
  541. KAffinityMask original_physical_affinity_mask{};
  542. s32 original_physical_ideal_core_id{};
  543. s32 num_core_migration_disables{};
  544. ThreadState thread_state{};
  545. std::atomic<bool> termination_requested{};
  546. bool wait_cancelled{};
  547. bool cancellable{};
  548. bool signaled{};
  549. bool initialized{};
  550. bool debug_attached{};
  551. s8 priority_inheritance_count{};
  552. bool resource_limit_release_hint{};
  553. StackParameters stack_parameters{};
  554. KSpinLock context_guard{};
  555. // For emulation
  556. std::shared_ptr<Common::Fiber> host_context{};
  557. // For debugging
  558. std::vector<KSynchronizationObject*> wait_objects_for_debugging;
  559. VAddr mutex_wait_address_for_debugging{};
  560. ThreadWaitReasonForDebugging wait_reason_for_debugging{};
  561. ThreadType thread_type_for_debugging{};
  562. public:
  563. using ConditionVariableThreadTreeType = ConditionVariableThreadTree;
  564. void SetConditionVariable(ConditionVariableThreadTree* tree, VAddr address, u64 cv_key,
  565. u32 value) {
  566. condvar_tree = tree;
  567. condvar_key = cv_key;
  568. address_key = address;
  569. address_key_value = value;
  570. }
  571. void ClearConditionVariable() {
  572. condvar_tree = nullptr;
  573. }
  574. [[nodiscard]] bool IsWaitingForConditionVariable() const {
  575. return condvar_tree != nullptr;
  576. }
  577. void SetAddressArbiter(ConditionVariableThreadTree* tree, u64 address) {
  578. condvar_tree = tree;
  579. condvar_key = address;
  580. }
  581. void ClearAddressArbiter() {
  582. condvar_tree = nullptr;
  583. }
  584. [[nodiscard]] bool IsWaitingForAddressArbiter() const {
  585. return condvar_tree != nullptr;
  586. }
  587. [[nodiscard]] ConditionVariableThreadTree* GetConditionVariableTree() const {
  588. return condvar_tree;
  589. }
  590. };
  591. } // namespace Kernel