k_synchronization_object.cpp 5.8 KB

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