k_synchronization_object.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "common/assert.h"
  4. #include "common/common_types.h"
  5. #include "core/hle/kernel/k_scheduler.h"
  6. #include "core/hle/kernel/k_scoped_scheduler_lock_and_sleep.h"
  7. #include "core/hle/kernel/k_synchronization_object.h"
  8. #include "core/hle/kernel/k_thread.h"
  9. #include "core/hle/kernel/k_thread_queue.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/kernel/svc_results.h"
  12. namespace Kernel {
  13. namespace {
  14. class ThreadQueueImplForKSynchronizationObjectWait final : public KThreadQueueWithoutEndWait {
  15. public:
  16. ThreadQueueImplForKSynchronizationObjectWait(KernelCore& kernel_, KSynchronizationObject** o,
  17. KSynchronizationObject::ThreadListNode* n, s32 c)
  18. : KThreadQueueWithoutEndWait(kernel_), m_objects(o), m_nodes(n), m_count(c) {}
  19. void NotifyAvailable(KThread* waiting_thread, KSynchronizationObject* signaled_object,
  20. Result wait_result) override {
  21. // Determine the sync index, and unlink all nodes.
  22. s32 sync_index = -1;
  23. for (auto i = 0; i < m_count; ++i) {
  24. // Check if this is the signaled object.
  25. if (m_objects[i] == signaled_object && sync_index == -1) {
  26. sync_index = i;
  27. }
  28. // Unlink the current node from the current object.
  29. m_objects[i]->UnlinkNode(std::addressof(m_nodes[i]));
  30. }
  31. // Set the waiting thread's sync index.
  32. waiting_thread->SetSyncedIndex(sync_index);
  33. // Set the waiting thread as not cancellable.
  34. waiting_thread->ClearCancellable();
  35. // Invoke the base end wait handler.
  36. KThreadQueue::EndWait(waiting_thread, wait_result);
  37. }
  38. void CancelWait(KThread* waiting_thread, Result wait_result, bool cancel_timer_task) override {
  39. // Remove all nodes from our list.
  40. for (auto i = 0; i < m_count; ++i) {
  41. m_objects[i]->UnlinkNode(std::addressof(m_nodes[i]));
  42. }
  43. // Set the waiting thread as not cancellable.
  44. waiting_thread->ClearCancellable();
  45. // Invoke the base cancel wait handler.
  46. KThreadQueue::CancelWait(waiting_thread, wait_result, cancel_timer_task);
  47. }
  48. private:
  49. KSynchronizationObject** m_objects;
  50. KSynchronizationObject::ThreadListNode* m_nodes;
  51. s32 m_count;
  52. };
  53. } // namespace
  54. void KSynchronizationObject::Finalize() {
  55. this->OnFinalizeSynchronizationObject();
  56. KAutoObject::Finalize();
  57. }
  58. Result KSynchronizationObject::Wait(KernelCore& kernel_ctx, s32* out_index,
  59. KSynchronizationObject** objects, const s32 num_objects,
  60. s64 timeout) {
  61. // Allocate space on stack for thread nodes.
  62. std::vector<ThreadListNode> thread_nodes(num_objects);
  63. // Prepare for wait.
  64. KThread* thread = GetCurrentThreadPointer(kernel_ctx);
  65. ThreadQueueImplForKSynchronizationObjectWait wait_queue(kernel_ctx, objects,
  66. thread_nodes.data(), num_objects);
  67. {
  68. // Setup the scheduling lock and sleep.
  69. KScopedSchedulerLockAndSleep slp(kernel_ctx, thread, timeout);
  70. // Check if the thread should terminate.
  71. if (thread->IsTerminationRequested()) {
  72. slp.CancelSleep();
  73. return ResultTerminationRequested;
  74. }
  75. // Check if any of the objects are already signaled.
  76. for (auto i = 0; i < num_objects; ++i) {
  77. ASSERT(objects[i] != nullptr);
  78. if (objects[i]->IsSignaled()) {
  79. *out_index = i;
  80. slp.CancelSleep();
  81. return ResultSuccess;
  82. }
  83. }
  84. // Check if the timeout is zero.
  85. if (timeout == 0) {
  86. slp.CancelSleep();
  87. return ResultTimedOut;
  88. }
  89. // Check if waiting was canceled.
  90. if (thread->IsWaitCancelled()) {
  91. slp.CancelSleep();
  92. thread->ClearWaitCancelled();
  93. return ResultCancelled;
  94. }
  95. // Add the waiters.
  96. for (auto i = 0; i < num_objects; ++i) {
  97. thread_nodes[i].thread = thread;
  98. thread_nodes[i].next = nullptr;
  99. objects[i]->LinkNode(std::addressof(thread_nodes[i]));
  100. }
  101. // Mark the thread as cancellable.
  102. thread->SetCancellable();
  103. // Clear the thread's synced index.
  104. thread->SetSyncedIndex(-1);
  105. // Wait for an object to be signaled.
  106. thread->BeginWait(std::addressof(wait_queue));
  107. thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Synchronization);
  108. }
  109. // Set the output index.
  110. *out_index = thread->GetSyncedIndex();
  111. // Get the wait result.
  112. return thread->GetWaitResult();
  113. }
  114. KSynchronizationObject::KSynchronizationObject(KernelCore& kernel_)
  115. : KAutoObjectWithList{kernel_} {}
  116. KSynchronizationObject::~KSynchronizationObject() = default;
  117. void KSynchronizationObject::NotifyAvailable(Result result) {
  118. KScopedSchedulerLock sl(kernel);
  119. // If we're not signaled, we've nothing to notify.
  120. if (!this->IsSignaled()) {
  121. return;
  122. }
  123. // Iterate over each thread.
  124. for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
  125. cur_node->thread->NotifyAvailable(this, result);
  126. }
  127. }
  128. std::vector<KThread*> KSynchronizationObject::GetWaitingThreadsForDebugging() const {
  129. std::vector<KThread*> threads;
  130. // If debugging, dump the list of waiters.
  131. {
  132. KScopedSchedulerLock lock(kernel);
  133. for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
  134. threads.emplace_back(cur_node->thread);
  135. }
  136. }
  137. return threads;
  138. }
  139. } // namespace Kernel