address_arbiter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 =
  32. [](size_t core_index, std::vector<SharedPtr<Thread>>& waiting_threads, VAddr arb_addr) {
  33. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  34. auto& thread_list = scheduler->GetThreadList();
  35. for (auto& thread : thread_list) {
  36. if (thread->arb_wait_address == arb_addr)
  37. waiting_threads.push_back(thread);
  38. }
  39. };
  40. // Retrieve all threads that are waiting for this address.
  41. std::vector<SharedPtr<Thread>> threads;
  42. RetrieveWaitingThreads(0, threads, address);
  43. RetrieveWaitingThreads(1, threads, address);
  44. RetrieveWaitingThreads(2, threads, address);
  45. RetrieveWaitingThreads(3, threads, address);
  46. // Sort them by priority, such that the highest priority ones come first.
  47. std::sort(threads.begin(), threads.end(),
  48. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  49. return lhs->current_priority < rhs->current_priority;
  50. });
  51. return threads;
  52. }
  53. // Wake up num_to_wake (or all) threads in a vector.
  54. static void WakeThreads(std::vector<SharedPtr<Thread>>& waiting_threads, s32 num_to_wake) {
  55. // Only process up to 'target' threads, unless 'target' is <= 0, in which case process
  56. // them all.
  57. size_t last = waiting_threads.size();
  58. if (num_to_wake > 0)
  59. last = num_to_wake;
  60. // Signal the waiting threads.
  61. for (size_t i = 0; i < last; i++) {
  62. ASSERT(waiting_threads[i]->status == ThreadStatus::WaitArb);
  63. waiting_threads[i]->SetWaitSynchronizationResult(RESULT_SUCCESS);
  64. waiting_threads[i]->arb_wait_address = 0;
  65. waiting_threads[i]->ResumeFromWait();
  66. }
  67. }
  68. // Signals an address being waited on.
  69. ResultCode SignalToAddress(VAddr address, s32 num_to_wake) {
  70. std::vector<SharedPtr<Thread>> waiting_threads = GetThreadsWaitingOnAddress(address);
  71. WakeThreads(waiting_threads, num_to_wake);
  72. return RESULT_SUCCESS;
  73. }
  74. // Signals an address being waited on and increments its value if equal to the value argument.
  75. ResultCode IncrementAndSignalToAddressIfEqual(VAddr address, s32 value, s32 num_to_wake) {
  76. // Ensure that we can write to the address.
  77. if (!Memory::IsValidVirtualAddress(address)) {
  78. return ERR_INVALID_ADDRESS_STATE;
  79. }
  80. if (static_cast<s32>(Memory::Read32(address)) == value) {
  81. Memory::Write32(address, static_cast<u32>(value + 1));
  82. } else {
  83. return ERR_INVALID_STATE;
  84. }
  85. return SignalToAddress(address, num_to_wake);
  86. }
  87. // Signals an address being waited on and modifies its value based on waiting thread count if equal
  88. // to the value argument.
  89. ResultCode ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr address, s32 value,
  90. s32 num_to_wake) {
  91. // Ensure that we can write to the address.
  92. if (!Memory::IsValidVirtualAddress(address)) {
  93. return ERR_INVALID_ADDRESS_STATE;
  94. }
  95. // Get threads waiting on the address.
  96. std::vector<SharedPtr<Thread>> waiting_threads = GetThreadsWaitingOnAddress(address);
  97. // Determine the modified value depending on the waiting count.
  98. s32 updated_value;
  99. if (waiting_threads.empty()) {
  100. updated_value = value - 1;
  101. } else if (num_to_wake <= 0 || waiting_threads.size() <= static_cast<u32>(num_to_wake)) {
  102. updated_value = value + 1;
  103. } else {
  104. updated_value = value;
  105. }
  106. if (static_cast<s32>(Memory::Read32(address)) == value) {
  107. Memory::Write32(address, static_cast<u32>(updated_value));
  108. } else {
  109. return ERR_INVALID_STATE;
  110. }
  111. WakeThreads(waiting_threads, num_to_wake);
  112. return RESULT_SUCCESS;
  113. }
  114. // Waits on an address if the value passed is less than the argument value, optionally decrementing.
  115. ResultCode WaitForAddressIfLessThan(VAddr address, s32 value, s64 timeout, bool should_decrement) {
  116. // Ensure that we can read the address.
  117. if (!Memory::IsValidVirtualAddress(address)) {
  118. return ERR_INVALID_ADDRESS_STATE;
  119. }
  120. s32 cur_value = static_cast<s32>(Memory::Read32(address));
  121. if (cur_value < value) {
  122. if (should_decrement) {
  123. Memory::Write32(address, static_cast<u32>(cur_value - 1));
  124. }
  125. } else {
  126. return ERR_INVALID_STATE;
  127. }
  128. // Short-circuit without rescheduling, if timeout is zero.
  129. if (timeout == 0) {
  130. return RESULT_TIMEOUT;
  131. }
  132. return WaitForAddress(address, timeout);
  133. }
  134. // Waits on an address if the value passed is equal to the argument value.
  135. ResultCode WaitForAddressIfEqual(VAddr address, s32 value, s64 timeout) {
  136. // Ensure that we can read the address.
  137. if (!Memory::IsValidVirtualAddress(address)) {
  138. return ERR_INVALID_ADDRESS_STATE;
  139. }
  140. // Only wait for the address if equal.
  141. if (static_cast<s32>(Memory::Read32(address)) != value) {
  142. return ERR_INVALID_STATE;
  143. }
  144. // Short-circuit without rescheduling, if timeout is zero.
  145. if (timeout == 0) {
  146. return RESULT_TIMEOUT;
  147. }
  148. return WaitForAddress(address, timeout);
  149. }
  150. } // namespace AddressArbiter
  151. } // namespace Kernel