k_synchronization_object.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/kernel.h"
  11. #include "core/hle/kernel/svc_results.h"
  12. namespace Kernel {
  13. ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index,
  14. KSynchronizationObject** objects, const s32 num_objects,
  15. s64 timeout) {
  16. // Allocate space on stack for thread nodes.
  17. std::vector<ThreadListNode> thread_nodes(num_objects);
  18. // Prepare for wait.
  19. KThread* thread = kernel.CurrentScheduler()->GetCurrentThread();
  20. {
  21. // Setup the scheduling lock and sleep.
  22. KScopedSchedulerLockAndSleep slp{kernel, thread, timeout};
  23. // Check if any of the objects are already signaled.
  24. for (auto i = 0; i < num_objects; ++i) {
  25. ASSERT(objects[i] != nullptr);
  26. if (objects[i]->IsSignaled()) {
  27. *out_index = i;
  28. slp.CancelSleep();
  29. return RESULT_SUCCESS;
  30. }
  31. }
  32. // Check if the timeout is zero.
  33. if (timeout == 0) {
  34. slp.CancelSleep();
  35. return Svc::ResultTimedOut;
  36. }
  37. // Check if the thread should terminate.
  38. if (thread->IsTerminationRequested()) {
  39. slp.CancelSleep();
  40. return Svc::ResultTerminationRequested;
  41. }
  42. // Check if waiting was canceled.
  43. if (thread->IsWaitCancelled()) {
  44. slp.CancelSleep();
  45. thread->ClearWaitCancelled();
  46. return Svc::ResultCancelled;
  47. }
  48. // Add the waiters.
  49. for (auto i = 0; i < num_objects; ++i) {
  50. thread_nodes[i].thread = thread;
  51. thread_nodes[i].next = nullptr;
  52. if (objects[i]->thread_list_tail == nullptr) {
  53. objects[i]->thread_list_head = std::addressof(thread_nodes[i]);
  54. } else {
  55. objects[i]->thread_list_tail->next = std::addressof(thread_nodes[i]);
  56. }
  57. objects[i]->thread_list_tail = std::addressof(thread_nodes[i]);
  58. }
  59. // For debugging only
  60. thread->SetWaitObjectsForDebugging({objects, static_cast<std::size_t>(num_objects)});
  61. // Mark the thread as waiting.
  62. thread->SetCancellable();
  63. thread->SetSyncedObject(nullptr, Svc::ResultTimedOut);
  64. thread->SetState(ThreadState::Waiting);
  65. thread->SetWaitReasonForDebugging(ThreadWaitReasonForDebugging::Synchronization);
  66. }
  67. // The lock/sleep is done, so we should be able to get our result.
  68. // Thread is no longer cancellable.
  69. thread->ClearCancellable();
  70. // For debugging only
  71. thread->SetWaitObjectsForDebugging({});
  72. // Cancel the timer as needed.
  73. kernel.TimeManager().UnscheduleTimeEvent(thread);
  74. // Get the wait result.
  75. ResultCode wait_result{RESULT_SUCCESS};
  76. s32 sync_index = -1;
  77. {
  78. KScopedSchedulerLock lock(kernel);
  79. KSynchronizationObject* synced_obj;
  80. wait_result = thread->GetWaitResult(std::addressof(synced_obj));
  81. for (auto i = 0; i < num_objects; ++i) {
  82. // Unlink the object from the list.
  83. ThreadListNode* prev_ptr =
  84. reinterpret_cast<ThreadListNode*>(std::addressof(objects[i]->thread_list_head));
  85. ThreadListNode* prev_val = nullptr;
  86. ThreadListNode *prev, *tail_prev;
  87. do {
  88. prev = prev_ptr;
  89. prev_ptr = prev_ptr->next;
  90. tail_prev = prev_val;
  91. prev_val = prev_ptr;
  92. } while (prev_ptr != std::addressof(thread_nodes[i]));
  93. if (objects[i]->thread_list_tail == std::addressof(thread_nodes[i])) {
  94. objects[i]->thread_list_tail = tail_prev;
  95. }
  96. prev->next = thread_nodes[i].next;
  97. if (objects[i] == synced_obj) {
  98. sync_index = i;
  99. }
  100. }
  101. }
  102. // Set output.
  103. *out_index = sync_index;
  104. return wait_result;
  105. }
  106. KSynchronizationObject::KSynchronizationObject(KernelCore& kernel) : Object{kernel} {}
  107. KSynchronizationObject::KSynchronizationObject(KernelCore& kernel, std::string&& name)
  108. : Object{kernel, std::move(name)} {}
  109. KSynchronizationObject::~KSynchronizationObject() = default;
  110. void KSynchronizationObject::NotifyAvailable(ResultCode result) {
  111. KScopedSchedulerLock lock(kernel);
  112. // If we're not signaled, we've nothing to notify.
  113. if (!this->IsSignaled()) {
  114. return;
  115. }
  116. // Iterate over each thread.
  117. for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
  118. KThread* thread = cur_node->thread;
  119. if (thread->GetState() == ThreadState::Waiting) {
  120. thread->SetSyncedObject(this, result);
  121. thread->SetState(ThreadState::Runnable);
  122. }
  123. }
  124. }
  125. std::vector<KThread*> KSynchronizationObject::GetWaitingThreadsForDebugging() const {
  126. std::vector<KThread*> threads;
  127. // If debugging, dump the list of waiters.
  128. {
  129. KScopedSchedulerLock lock(kernel);
  130. for (auto* cur_node = thread_list_head; cur_node != nullptr; cur_node = cur_node->next) {
  131. threads.emplace_back(cur_node->thread);
  132. }
  133. }
  134. return threads;
  135. }
  136. } // namespace Kernel