mutex.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <memory>
  5. #include <utility>
  6. #include <vector>
  7. #include "common/assert.h"
  8. #include "common/logging/log.h"
  9. #include "core/core.h"
  10. #include "core/hle/kernel/errors.h"
  11. #include "core/hle/kernel/handle_table.h"
  12. #include "core/hle/kernel/kernel.h"
  13. #include "core/hle/kernel/mutex.h"
  14. #include "core/hle/kernel/object.h"
  15. #include "core/hle/kernel/process.h"
  16. #include "core/hle/kernel/scheduler.h"
  17. #include "core/hle/kernel/thread.h"
  18. #include "core/hle/result.h"
  19. #include "core/memory.h"
  20. namespace Kernel {
  21. /// Returns the number of threads that are waiting for a mutex, and the highest priority one among
  22. /// those.
  23. static std::pair<std::shared_ptr<Thread>, u32> GetHighestPriorityMutexWaitingThread(
  24. const std::shared_ptr<Thread>& current_thread, VAddr mutex_addr) {
  25. std::shared_ptr<Thread> highest_priority_thread;
  26. u32 num_waiters = 0;
  27. for (const auto& thread : current_thread->GetMutexWaitingThreads()) {
  28. if (thread->GetMutexWaitAddress() != mutex_addr)
  29. continue;
  30. ASSERT(thread->GetStatus() == ThreadStatus::WaitMutex);
  31. ++num_waiters;
  32. if (highest_priority_thread == nullptr ||
  33. thread->GetPriority() < highest_priority_thread->GetPriority()) {
  34. highest_priority_thread = thread;
  35. }
  36. }
  37. return {highest_priority_thread, num_waiters};
  38. }
  39. /// Update the mutex owner field of all threads waiting on the mutex to point to the new owner.
  40. static void TransferMutexOwnership(VAddr mutex_addr, std::shared_ptr<Thread> current_thread,
  41. std::shared_ptr<Thread> new_owner) {
  42. const auto threads = current_thread->GetMutexWaitingThreads();
  43. for (const auto& thread : threads) {
  44. if (thread->GetMutexWaitAddress() != mutex_addr)
  45. continue;
  46. ASSERT(thread->GetLockOwner() == current_thread.get());
  47. current_thread->RemoveMutexWaiter(thread);
  48. if (new_owner != thread)
  49. new_owner->AddMutexWaiter(thread);
  50. }
  51. }
  52. Mutex::Mutex(Core::System& system) : system{system} {}
  53. Mutex::~Mutex() = default;
  54. ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
  55. Handle requesting_thread_handle) {
  56. // The mutex address must be 4-byte aligned
  57. if ((address % sizeof(u32)) != 0) {
  58. LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address);
  59. return ERR_INVALID_ADDRESS;
  60. }
  61. auto& kernel = system.Kernel();
  62. std::shared_ptr<Thread> current_thread =
  63. SharedFrom(kernel.CurrentScheduler().GetCurrentThread());
  64. {
  65. SchedulerLock lock(kernel);
  66. // The mutex address must be 4-byte aligned
  67. if ((address % sizeof(u32)) != 0) {
  68. return ERR_INVALID_ADDRESS;
  69. }
  70. const auto& handle_table = kernel.CurrentProcess()->GetHandleTable();
  71. std::shared_ptr<Thread> holding_thread = handle_table.Get<Thread>(holding_thread_handle);
  72. std::shared_ptr<Thread> requesting_thread =
  73. handle_table.Get<Thread>(requesting_thread_handle);
  74. // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of
  75. // another thread.
  76. ASSERT(requesting_thread == current_thread);
  77. current_thread->SetSynchronizationResults(nullptr, RESULT_SUCCESS);
  78. const u32 addr_value = system.Memory().Read32(address);
  79. // If the mutex isn't being held, just return success.
  80. if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) {
  81. return RESULT_SUCCESS;
  82. }
  83. if (holding_thread == nullptr) {
  84. return ERR_INVALID_HANDLE;
  85. }
  86. // Wait until the mutex is released
  87. current_thread->SetMutexWaitAddress(address);
  88. current_thread->SetWaitHandle(requesting_thread_handle);
  89. current_thread->SetStatus(ThreadStatus::WaitMutex);
  90. // Update the lock holder thread's priority to prevent priority inversion.
  91. holding_thread->AddMutexWaiter(current_thread);
  92. }
  93. {
  94. SchedulerLock lock(kernel);
  95. auto* owner = current_thread->GetLockOwner();
  96. if (owner != nullptr) {
  97. owner->RemoveMutexWaiter(current_thread);
  98. }
  99. }
  100. return current_thread->GetSignalingResult();
  101. }
  102. std::pair<ResultCode, std::shared_ptr<Thread>> Mutex::Unlock(std::shared_ptr<Thread> owner,
  103. VAddr address) {
  104. // The mutex address must be 4-byte aligned
  105. if ((address % sizeof(u32)) != 0) {
  106. LOG_ERROR(Kernel, "Address is not 4-byte aligned! address={:016X}", address);
  107. return {ERR_INVALID_ADDRESS, nullptr};
  108. }
  109. auto [new_owner, num_waiters] = GetHighestPriorityMutexWaitingThread(owner, address);
  110. if (new_owner == nullptr) {
  111. system.Memory().Write32(address, 0);
  112. return {RESULT_SUCCESS, nullptr};
  113. }
  114. // Transfer the ownership of the mutex from the previous owner to the new one.
  115. TransferMutexOwnership(address, owner, new_owner);
  116. u32 mutex_value = new_owner->GetWaitHandle();
  117. if (num_waiters >= 2) {
  118. // Notify the guest that there are still some threads waiting for the mutex
  119. mutex_value |= Mutex::MutexHasWaitersFlag;
  120. }
  121. new_owner->SetSynchronizationResults(nullptr, RESULT_SUCCESS);
  122. new_owner->ResumeFromWait();
  123. new_owner->SetLockOwner(nullptr);
  124. system.Memory().Write32(address, mutex_value);
  125. return {RESULT_SUCCESS, new_owner};
  126. }
  127. ResultCode Mutex::Release(VAddr address) {
  128. auto& kernel = system.Kernel();
  129. SchedulerLock lock(kernel);
  130. std::shared_ptr<Thread> current_thread =
  131. SharedFrom(kernel.CurrentScheduler().GetCurrentThread());
  132. auto [result, new_owner] = Unlock(current_thread, address);
  133. if (result != RESULT_SUCCESS && new_owner != nullptr) {
  134. new_owner->SetSynchronizationResults(nullptr, result);
  135. }
  136. return result;
  137. }
  138. } // namespace Kernel