address_arbiter.cpp 6.3 KB

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