address_arbiter.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <vector>
  6. #include "common/assert.h"
  7. #include "common/common_types.h"
  8. #include "core/core.h"
  9. #include "core/core_cpu.h"
  10. #include "core/hle/kernel/errors.h"
  11. #include "core/hle/kernel/object.h"
  12. #include "core/hle/kernel/process.h"
  13. #include "core/hle/kernel/scheduler.h"
  14. #include "core/hle/kernel/thread.h"
  15. #include "core/hle/result.h"
  16. #include "core/memory.h"
  17. namespace Kernel {
  18. namespace AddressArbiter {
  19. // Performs actual address waiting logic.
  20. static ResultCode WaitForAddress(VAddr address, s64 timeout) {
  21. SharedPtr<Thread> current_thread = GetCurrentThread();
  22. current_thread->arb_wait_address = address;
  23. current_thread->status = ThreadStatus::WaitArb;
  24. current_thread->wakeup_callback = nullptr;
  25. current_thread->WakeAfterDelay(timeout);
  26. Core::System::GetInstance().CpuCore(current_thread->processor_id).PrepareReschedule();
  27. return RESULT_TIMEOUT;
  28. }
  29. // Gets the threads waiting on an address.
  30. static std::vector<SharedPtr<Thread>> GetThreadsWaitingOnAddress(VAddr address) {
  31. const auto RetrieveWaitingThreads = [](std::size_t core_index,
  32. std::vector<SharedPtr<Thread>>& waiting_threads,
  33. VAddr arb_addr) {
  34. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  35. auto& thread_list = scheduler->GetThreadList();
  36. for (auto& thread : thread_list) {
  37. if (thread->arb_wait_address == arb_addr)
  38. waiting_threads.push_back(thread);
  39. }
  40. };
  41. // Retrieve all threads that are waiting for this address.
  42. std::vector<SharedPtr<Thread>> threads;
  43. RetrieveWaitingThreads(0, threads, address);
  44. RetrieveWaitingThreads(1, threads, address);
  45. RetrieveWaitingThreads(2, threads, address);
  46. RetrieveWaitingThreads(3, threads, address);
  47. // Sort them by priority, such that the highest priority ones come first.
  48. std::sort(threads.begin(), threads.end(),
  49. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  50. return lhs->current_priority < rhs->current_priority;
  51. });
  52. return threads;
  53. }
  54. // Wake up num_to_wake (or all) threads in a vector.
  55. static void WakeThreads(std::vector<SharedPtr<Thread>>& waiting_threads, s32 num_to_wake) {
  56. // Only process up to 'target' threads, unless 'target' is <= 0, in which case process
  57. // them all.
  58. std::size_t last = waiting_threads.size();
  59. if (num_to_wake > 0)
  60. last = num_to_wake;
  61. // Signal the waiting threads.
  62. for (std::size_t i = 0; i < last; i++) {
  63. ASSERT(waiting_threads[i]->status == ThreadStatus::WaitArb);
  64. waiting_threads[i]->SetWaitSynchronizationResult(RESULT_SUCCESS);
  65. waiting_threads[i]->arb_wait_address = 0;
  66. waiting_threads[i]->ResumeFromWait();
  67. }
  68. }
  69. // Signals an address being waited on.
  70. ResultCode SignalToAddress(VAddr address, s32 num_to_wake) {
  71. std::vector<SharedPtr<Thread>> waiting_threads = GetThreadsWaitingOnAddress(address);
  72. WakeThreads(waiting_threads, num_to_wake);
  73. return RESULT_SUCCESS;
  74. }
  75. // Signals an address being waited on and increments its value if equal to the value argument.
  76. ResultCode IncrementAndSignalToAddressIfEqual(VAddr address, s32 value, s32 num_to_wake) {
  77. // Ensure that we can write to the address.
  78. if (!Memory::IsValidVirtualAddress(address)) {
  79. return ERR_INVALID_ADDRESS_STATE;
  80. }
  81. if (static_cast<s32>(Memory::Read32(address)) == value) {
  82. Memory::Write32(address, static_cast<u32>(value + 1));
  83. } else {
  84. return ERR_INVALID_STATE;
  85. }
  86. return SignalToAddress(address, num_to_wake);
  87. }
  88. // Signals an address being waited on and modifies its value based on waiting thread count if equal
  89. // to the value argument.
  90. ResultCode ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr address, s32 value,
  91. s32 num_to_wake) {
  92. // Ensure that we can write to the address.
  93. if (!Memory::IsValidVirtualAddress(address)) {
  94. return ERR_INVALID_ADDRESS_STATE;
  95. }
  96. // Get threads waiting on the address.
  97. std::vector<SharedPtr<Thread>> waiting_threads = GetThreadsWaitingOnAddress(address);
  98. // Determine the modified value depending on the waiting count.
  99. s32 updated_value;
  100. if (waiting_threads.empty()) {
  101. updated_value = value - 1;
  102. } else if (num_to_wake <= 0 || waiting_threads.size() <= static_cast<u32>(num_to_wake)) {
  103. updated_value = value + 1;
  104. } else {
  105. updated_value = value;
  106. }
  107. if (static_cast<s32>(Memory::Read32(address)) == value) {
  108. Memory::Write32(address, static_cast<u32>(updated_value));
  109. } else {
  110. return ERR_INVALID_STATE;
  111. }
  112. WakeThreads(waiting_threads, num_to_wake);
  113. return RESULT_SUCCESS;
  114. }
  115. // Waits on an address if the value passed is less than the argument value, optionally decrementing.
  116. ResultCode WaitForAddressIfLessThan(VAddr address, s32 value, s64 timeout, bool should_decrement) {
  117. // Ensure that we can read the address.
  118. if (!Memory::IsValidVirtualAddress(address)) {
  119. return ERR_INVALID_ADDRESS_STATE;
  120. }
  121. s32 cur_value = static_cast<s32>(Memory::Read32(address));
  122. if (cur_value < value) {
  123. if (should_decrement) {
  124. Memory::Write32(address, static_cast<u32>(cur_value - 1));
  125. }
  126. } else {
  127. return ERR_INVALID_STATE;
  128. }
  129. // Short-circuit without rescheduling, if timeout is zero.
  130. if (timeout == 0) {
  131. return RESULT_TIMEOUT;
  132. }
  133. return WaitForAddress(address, timeout);
  134. }
  135. // Waits on an address if the value passed is equal to the argument value.
  136. ResultCode WaitForAddressIfEqual(VAddr address, s32 value, s64 timeout) {
  137. // Ensure that we can read the address.
  138. if (!Memory::IsValidVirtualAddress(address)) {
  139. return ERR_INVALID_ADDRESS_STATE;
  140. }
  141. // Only wait for the address if equal.
  142. if (static_cast<s32>(Memory::Read32(address)) != value) {
  143. return ERR_INVALID_STATE;
  144. }
  145. // Short-circuit without rescheduling, if timeout is zero.
  146. if (timeout == 0) {
  147. return RESULT_TIMEOUT;
  148. }
  149. return WaitForAddress(address, timeout);
  150. }
  151. } // namespace AddressArbiter
  152. } // namespace Kernel