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