k_synchronization_object.cpp 5.5 KB

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