address_arbiter.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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/address_arbiter.h"
  10. #include "core/hle/kernel/errors.h"
  11. #include "core/hle/kernel/scheduler.h"
  12. #include "core/hle/kernel/thread.h"
  13. #include "core/hle/result.h"
  14. #include "core/memory.h"
  15. namespace Kernel {
  16. // Wake up num_to_wake (or all) threads in a vector.
  17. void AddressArbiter::WakeThreads(const std::vector<std::shared_ptr<Thread>>& waiting_threads,
  18. s32 num_to_wake) {
  19. // Only process up to 'target' threads, unless 'target' is <= 0, in which case process
  20. // them all.
  21. std::size_t last = waiting_threads.size();
  22. if (num_to_wake > 0) {
  23. last = std::min(last, static_cast<std::size_t>(num_to_wake));
  24. }
  25. // Signal the waiting threads.
  26. for (std::size_t i = 0; i < last; i++) {
  27. ASSERT(waiting_threads[i]->GetStatus() == ThreadStatus::WaitArb);
  28. waiting_threads[i]->SetWaitSynchronizationResult(RESULT_SUCCESS);
  29. RemoveThread(waiting_threads[i]);
  30. waiting_threads[i]->SetArbiterWaitAddress(0);
  31. waiting_threads[i]->ResumeFromWait();
  32. system.PrepareReschedule(waiting_threads[i]->GetProcessorID());
  33. }
  34. }
  35. AddressArbiter::AddressArbiter(Core::System& system) : system{system} {}
  36. AddressArbiter::~AddressArbiter() = default;
  37. ResultCode AddressArbiter::SignalToAddress(VAddr address, SignalType type, s32 value,
  38. s32 num_to_wake) {
  39. switch (type) {
  40. case SignalType::Signal:
  41. return SignalToAddressOnly(address, num_to_wake);
  42. case SignalType::IncrementAndSignalIfEqual:
  43. return IncrementAndSignalToAddressIfEqual(address, value, num_to_wake);
  44. case SignalType::ModifyByWaitingCountAndSignalIfEqual:
  45. return ModifyByWaitingCountAndSignalToAddressIfEqual(address, value, num_to_wake);
  46. default:
  47. return ERR_INVALID_ENUM_VALUE;
  48. }
  49. }
  50. ResultCode AddressArbiter::SignalToAddressOnly(VAddr address, s32 num_to_wake) {
  51. const std::vector<std::shared_ptr<Thread>> waiting_threads =
  52. GetThreadsWaitingOnAddress(address);
  53. WakeThreads(waiting_threads, num_to_wake);
  54. return RESULT_SUCCESS;
  55. }
  56. ResultCode AddressArbiter::IncrementAndSignalToAddressIfEqual(VAddr address, s32 value,
  57. s32 num_to_wake) {
  58. auto& memory = system.Memory();
  59. // Ensure that we can write to the address.
  60. if (!memory.IsValidVirtualAddress(address)) {
  61. return ERR_INVALID_ADDRESS_STATE;
  62. }
  63. if (static_cast<s32>(memory.Read32(address)) != value) {
  64. return ERR_INVALID_STATE;
  65. }
  66. memory.Write32(address, static_cast<u32>(value + 1));
  67. return SignalToAddressOnly(address, num_to_wake);
  68. }
  69. ResultCode AddressArbiter::ModifyByWaitingCountAndSignalToAddressIfEqual(VAddr address, s32 value,
  70. s32 num_to_wake) {
  71. auto& memory = system.Memory();
  72. // Ensure that we can write to the address.
  73. if (!memory.IsValidVirtualAddress(address)) {
  74. return ERR_INVALID_ADDRESS_STATE;
  75. }
  76. // Get threads waiting on the address.
  77. const std::vector<std::shared_ptr<Thread>> waiting_threads =
  78. GetThreadsWaitingOnAddress(address);
  79. // Determine the modified value depending on the waiting count.
  80. s32 updated_value;
  81. if (num_to_wake <= 0) {
  82. if (waiting_threads.empty()) {
  83. updated_value = value + 1;
  84. } else {
  85. updated_value = value - 1;
  86. }
  87. } else {
  88. if (waiting_threads.empty()) {
  89. updated_value = value + 1;
  90. } else if (waiting_threads.size() <= static_cast<u32>(num_to_wake)) {
  91. updated_value = value - 1;
  92. } else {
  93. updated_value = value;
  94. }
  95. }
  96. if (static_cast<s32>(memory.Read32(address)) != value) {
  97. return ERR_INVALID_STATE;
  98. }
  99. memory.Write32(address, static_cast<u32>(updated_value));
  100. WakeThreads(waiting_threads, num_to_wake);
  101. return RESULT_SUCCESS;
  102. }
  103. ResultCode AddressArbiter::WaitForAddress(VAddr address, ArbitrationType type, s32 value,
  104. s64 timeout_ns) {
  105. switch (type) {
  106. case ArbitrationType::WaitIfLessThan:
  107. return WaitForAddressIfLessThan(address, value, timeout_ns, false);
  108. case ArbitrationType::DecrementAndWaitIfLessThan:
  109. return WaitForAddressIfLessThan(address, value, timeout_ns, true);
  110. case ArbitrationType::WaitIfEqual:
  111. return WaitForAddressIfEqual(address, value, timeout_ns);
  112. default:
  113. return ERR_INVALID_ENUM_VALUE;
  114. }
  115. }
  116. ResultCode AddressArbiter::WaitForAddressIfLessThan(VAddr address, s32 value, s64 timeout,
  117. bool should_decrement) {
  118. auto& memory = system.Memory();
  119. // Ensure that we can read the address.
  120. if (!memory.IsValidVirtualAddress(address)) {
  121. return ERR_INVALID_ADDRESS_STATE;
  122. }
  123. const s32 cur_value = static_cast<s32>(memory.Read32(address));
  124. if (cur_value >= value) {
  125. return ERR_INVALID_STATE;
  126. }
  127. if (should_decrement) {
  128. memory.Write32(address, static_cast<u32>(cur_value - 1));
  129. }
  130. // Short-circuit without rescheduling, if timeout is zero.
  131. if (timeout == 0) {
  132. return RESULT_TIMEOUT;
  133. }
  134. return WaitForAddressImpl(address, timeout);
  135. }
  136. ResultCode AddressArbiter::WaitForAddressIfEqual(VAddr address, s32 value, s64 timeout) {
  137. auto& memory = system.Memory();
  138. // Ensure that we can read the address.
  139. if (!memory.IsValidVirtualAddress(address)) {
  140. return ERR_INVALID_ADDRESS_STATE;
  141. }
  142. // Only wait for the address if equal.
  143. if (static_cast<s32>(memory.Read32(address)) != value) {
  144. return ERR_INVALID_STATE;
  145. }
  146. // Short-circuit without rescheduling if timeout is zero.
  147. if (timeout == 0) {
  148. return RESULT_TIMEOUT;
  149. }
  150. return WaitForAddressImpl(address, timeout);
  151. }
  152. ResultCode AddressArbiter::WaitForAddressImpl(VAddr address, s64 timeout) {
  153. Thread* current_thread = system.CurrentScheduler().GetCurrentThread();
  154. current_thread->SetArbiterWaitAddress(address);
  155. InsertThread(SharedFrom(current_thread));
  156. current_thread->SetStatus(ThreadStatus::WaitArb);
  157. current_thread->InvalidateWakeupCallback();
  158. current_thread->WakeAfterDelay(timeout);
  159. system.PrepareReschedule(current_thread->GetProcessorID());
  160. return RESULT_TIMEOUT;
  161. }
  162. void AddressArbiter::HandleWakeupThread(std::shared_ptr<Thread> thread) {
  163. ASSERT(thread->GetStatus() == ThreadStatus::WaitArb);
  164. RemoveThread(thread);
  165. thread->SetArbiterWaitAddress(0);
  166. }
  167. void AddressArbiter::InsertThread(std::shared_ptr<Thread> thread) {
  168. const VAddr arb_addr = thread->GetArbiterWaitAddress();
  169. std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
  170. auto it = thread_list.begin();
  171. while (it != thread_list.end()) {
  172. const std::shared_ptr<Thread>& current_thread = *it;
  173. if (current_thread->GetPriority() >= thread->GetPriority()) {
  174. thread_list.insert(it, thread);
  175. return;
  176. }
  177. ++it;
  178. }
  179. thread_list.push_back(std::move(thread));
  180. }
  181. void AddressArbiter::RemoveThread(std::shared_ptr<Thread> thread) {
  182. const VAddr arb_addr = thread->GetArbiterWaitAddress();
  183. std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[arb_addr];
  184. auto it = thread_list.begin();
  185. while (it != thread_list.end()) {
  186. const std::shared_ptr<Thread>& current_thread = *it;
  187. if (current_thread.get() == thread.get()) {
  188. thread_list.erase(it);
  189. return;
  190. }
  191. ++it;
  192. }
  193. UNREACHABLE();
  194. }
  195. std::vector<std::shared_ptr<Thread>> AddressArbiter::GetThreadsWaitingOnAddress(VAddr address) {
  196. std::vector<std::shared_ptr<Thread>> result;
  197. std::list<std::shared_ptr<Thread>>& thread_list = arb_threads[address];
  198. auto it = thread_list.begin();
  199. while (it != thread_list.end()) {
  200. std::shared_ptr<Thread> current_thread = *it;
  201. result.push_back(std::move(current_thread));
  202. ++it;
  203. }
  204. return result;
  205. }
  206. } // namespace Kernel