address_arbiter.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/hle/kernel/errors.h"
  10. #include "core/hle/kernel/kernel.h"
  11. #include "core/hle/kernel/process.h"
  12. #include "core/hle/kernel/thread.h"
  13. #include "core/hle/result.h"
  14. #include "core/memory.h"
  15. namespace Kernel {
  16. namespace AddressArbiter {
  17. // Performs actual address waiting logic.
  18. static ResultCode WaitForAddress(VAddr address, s64 timeout) {
  19. SharedPtr<Thread> current_thread = GetCurrentThread();
  20. current_thread->arb_wait_address = address;
  21. current_thread->status = ThreadStatus::WaitArb;
  22. current_thread->wakeup_callback = nullptr;
  23. current_thread->WakeAfterDelay(timeout);
  24. Core::System::GetInstance().CpuCore(current_thread->processor_id).PrepareReschedule();
  25. return RESULT_TIMEOUT;
  26. }
  27. // Gets the threads waiting on an address.
  28. static void GetThreadsWaitingOnAddress(std::vector<SharedPtr<Thread>>& waiting_threads,
  29. VAddr address) {
  30. auto RetrieveWaitingThreads =
  31. [](size_t core_index, std::vector<SharedPtr<Thread>>& waiting_threads, VAddr arb_addr) {
  32. const auto& scheduler = Core::System::GetInstance().Scheduler(core_index);
  33. auto& thread_list = scheduler->GetThreadList();
  34. for (auto& thread : thread_list) {
  35. if (thread->arb_wait_address == arb_addr)
  36. waiting_threads.push_back(thread);
  37. }
  38. };
  39. // Retrieve a list of all threads that are waiting for this address.
  40. RetrieveWaitingThreads(0, waiting_threads, address);
  41. RetrieveWaitingThreads(1, waiting_threads, address);
  42. RetrieveWaitingThreads(2, waiting_threads, address);
  43. RetrieveWaitingThreads(3, waiting_threads, address);
  44. // Sort them by priority, such that the highest priority ones come first.
  45. std::sort(waiting_threads.begin(), waiting_threads.end(),
  46. [](const SharedPtr<Thread>& lhs, const SharedPtr<Thread>& rhs) {
  47. return lhs->current_priority < rhs->current_priority;
  48. });
  49. }
  50. // Wake up num_to_wake (or all) threads in a vector.
  51. static void WakeThreads(std::vector<SharedPtr<Thread>>& waiting_threads, s32 num_to_wake) {
  52. // Only process up to 'target' threads, unless 'target' is <= 0, in which case process
  53. // them all.
  54. size_t last = waiting_threads.size();
  55. if (num_to_wake > 0)
  56. last = num_to_wake;
  57. // Signal the waiting threads.
  58. for (size_t i = 0; i < last; i++) {
  59. ASSERT(waiting_threads[i]->status == ThreadStatus::WaitArb);
  60. waiting_threads[i]->SetWaitSynchronizationResult(RESULT_SUCCESS);
  61. waiting_threads[i]->arb_wait_address = 0;
  62. waiting_threads[i]->ResumeFromWait();
  63. }
  64. }
  65. // Signals an address being waited on.
  66. ResultCode SignalToAddress(VAddr address, s32 num_to_wake) {
  67. // Get threads waiting on the address.
  68. std::vector<SharedPtr<Thread>> waiting_threads;
  69. GetThreadsWaitingOnAddress(waiting_threads, address);
  70. WakeThreads(waiting_threads, num_to_wake);
  71. return RESULT_SUCCESS;
  72. }
  73. // Signals an address being waited on and increments its value if equal to the value argument.
  74. ResultCode IncrementAndSignalToAddressIfEqual(VAddr address, s32 value, s32 num_to_wake) {
  75. // Ensure that we can write to the address.
  76. if (!Memory::IsValidVirtualAddress(address)) {
  77. return ERR_INVALID_ADDRESS_STATE;
  78. }
  79. if (static_cast<s32>(Memory::Read32(address)) == value) {
  80. Memory::Write32(address, static_cast<u32>(value + 1));
  81. } else {
  82. return ERR_INVALID_STATE;
  83. }
  84. return SignalToAddress(address, num_to_wake);
  85. }
  86. // Signals an address being waited on and modifies its value based on waiting thread count if equal
  87. // to the value argument.
  88. ResultCode ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr address, s32 value,
  89. s32 num_to_wake) {
  90. // Ensure that we can write to the address.
  91. if (!Memory::IsValidVirtualAddress(address)) {
  92. return ERR_INVALID_ADDRESS_STATE;
  93. }
  94. // Get threads waiting on the address.
  95. std::vector<SharedPtr<Thread>> waiting_threads;
  96. GetThreadsWaitingOnAddress(waiting_threads, address);
  97. // Determine the modified value depending on the waiting count.
  98. s32 updated_value;
  99. if (waiting_threads.size() == 0) {
  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