mutex.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include <utility>
  6. #include <vector>
  7. #include <boost/range/algorithm_ext/erase.hpp>
  8. #include "common/assert.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/thread.h"
  15. #include "core/hle/result.h"
  16. namespace Kernel {
  17. /// Returns the number of threads that are waiting for a mutex, and the highest priority one among
  18. /// those.
  19. static std::pair<SharedPtr<Thread>, u32> GetHighestPriorityMutexWaitingThread(
  20. const SharedPtr<Thread>& current_thread, VAddr mutex_addr) {
  21. SharedPtr<Thread> highest_priority_thread;
  22. u32 num_waiters = 0;
  23. for (auto& thread : current_thread->wait_mutex_threads) {
  24. if (thread->mutex_wait_address != mutex_addr)
  25. continue;
  26. ASSERT(thread->status == ThreadStatus::WaitMutex);
  27. ++num_waiters;
  28. if (highest_priority_thread == nullptr ||
  29. thread->GetPriority() < highest_priority_thread->GetPriority()) {
  30. highest_priority_thread = thread;
  31. }
  32. }
  33. return {highest_priority_thread, num_waiters};
  34. }
  35. /// Update the mutex owner field of all threads waiting on the mutex to point to the new owner.
  36. static void TransferMutexOwnership(VAddr mutex_addr, SharedPtr<Thread> current_thread,
  37. SharedPtr<Thread> new_owner) {
  38. auto threads = current_thread->wait_mutex_threads;
  39. for (auto& thread : threads) {
  40. if (thread->mutex_wait_address != mutex_addr)
  41. continue;
  42. ASSERT(thread->lock_owner == current_thread);
  43. current_thread->RemoveMutexWaiter(thread);
  44. if (new_owner != thread)
  45. new_owner->AddMutexWaiter(thread);
  46. }
  47. }
  48. ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
  49. Handle requesting_thread_handle) {
  50. // The mutex address must be 4-byte aligned
  51. if ((address % sizeof(u32)) != 0) {
  52. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidAddress);
  53. }
  54. SharedPtr<Thread> holding_thread = g_handle_table.Get<Thread>(holding_thread_handle);
  55. SharedPtr<Thread> requesting_thread = g_handle_table.Get<Thread>(requesting_thread_handle);
  56. // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another
  57. // thread.
  58. ASSERT(requesting_thread == GetCurrentThread());
  59. u32 addr_value = Memory::Read32(address);
  60. // If the mutex isn't being held, just return success.
  61. if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) {
  62. return RESULT_SUCCESS;
  63. }
  64. if (holding_thread == nullptr)
  65. return ERR_INVALID_HANDLE;
  66. // Wait until the mutex is released
  67. GetCurrentThread()->mutex_wait_address = address;
  68. GetCurrentThread()->wait_handle = requesting_thread_handle;
  69. GetCurrentThread()->status = ThreadStatus::WaitMutex;
  70. GetCurrentThread()->wakeup_callback = nullptr;
  71. // Update the lock holder thread's priority to prevent priority inversion.
  72. holding_thread->AddMutexWaiter(GetCurrentThread());
  73. Core::System::GetInstance().PrepareReschedule();
  74. return RESULT_SUCCESS;
  75. }
  76. ResultCode Mutex::Release(VAddr address) {
  77. // The mutex address must be 4-byte aligned
  78. if ((address % sizeof(u32)) != 0) {
  79. return ResultCode(ErrorModule::Kernel, ErrCodes::InvalidAddress);
  80. }
  81. auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(GetCurrentThread(), address);
  82. // There are no more threads waiting for the mutex, release it completely.
  83. if (thread == nullptr) {
  84. Memory::Write32(address, 0);
  85. return RESULT_SUCCESS;
  86. }
  87. // Transfer the ownership of the mutex from the previous owner to the new one.
  88. TransferMutexOwnership(address, GetCurrentThread(), thread);
  89. u32 mutex_value = thread->wait_handle;
  90. if (num_waiters >= 2) {
  91. // Notify the guest that there are still some threads waiting for the mutex
  92. mutex_value |= Mutex::MutexHasWaitersFlag;
  93. }
  94. // Grant the mutex to the next waiting thread and resume it.
  95. Memory::Write32(address, mutex_value);
  96. ASSERT(thread->status == ThreadStatus::WaitMutex);
  97. thread->ResumeFromWait();
  98. thread->lock_owner = nullptr;
  99. thread->condvar_wait_address = 0;
  100. thread->mutex_wait_address = 0;
  101. thread->wait_handle = 0;
  102. return RESULT_SUCCESS;
  103. }
  104. } // namespace Kernel