mutex.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <vector>
  6. #include <boost/range/algorithm_ext/erase.hpp>
  7. #include "common/assert.h"
  8. #include "core/core.h"
  9. #include "core/hle/kernel/errors.h"
  10. #include "core/hle/kernel/handle_table.h"
  11. #include "core/hle/kernel/kernel.h"
  12. #include "core/hle/kernel/mutex.h"
  13. #include "core/hle/kernel/object_address_table.h"
  14. #include "core/hle/kernel/thread.h"
  15. namespace Kernel {
  16. /// Returns the number of threads that are waiting for a mutex, and the highest priority one among
  17. /// those.
  18. static std::pair<SharedPtr<Thread>, u32> GetHighestPriorityMutexWaitingThread(
  19. SharedPtr<Thread> current_thread, VAddr mutex_addr) {
  20. SharedPtr<Thread> highest_priority_thread;
  21. u32 num_waiters = 0;
  22. for (auto& thread : current_thread->wait_mutex_threads) {
  23. if (thread->mutex_wait_address != mutex_addr)
  24. continue;
  25. ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX);
  26. ++num_waiters;
  27. if (highest_priority_thread == nullptr ||
  28. thread->GetPriority() < highest_priority_thread->GetPriority()) {
  29. highest_priority_thread = thread;
  30. }
  31. }
  32. return {highest_priority_thread, num_waiters};
  33. }
  34. /// Update the mutex owner field of all threads waiting on the mutex to point to the new owner.
  35. static void TransferMutexOwnership(VAddr mutex_addr, SharedPtr<Thread> current_thread,
  36. SharedPtr<Thread> new_owner) {
  37. auto threads = current_thread->wait_mutex_threads;
  38. for (auto& thread : threads) {
  39. if (thread->mutex_wait_address != mutex_addr)
  40. continue;
  41. ASSERT(thread->lock_owner == current_thread);
  42. current_thread->RemoveMutexWaiter(thread);
  43. if (new_owner != thread)
  44. new_owner->AddMutexWaiter(thread);
  45. }
  46. }
  47. ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
  48. Handle requesting_thread_handle) {
  49. // The mutex address must be 4-byte aligned
  50. if ((address % sizeof(u32)) != 0) {
  51. return ResultCode(ErrorModule::Kernel, ErrCodes::MisalignedAddress);
  52. }
  53. SharedPtr<Thread> holding_thread = g_handle_table.Get<Thread>(holding_thread_handle);
  54. SharedPtr<Thread> requesting_thread = g_handle_table.Get<Thread>(requesting_thread_handle);
  55. // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another
  56. // thread.
  57. ASSERT(requesting_thread == GetCurrentThread());
  58. u32 addr_value = Memory::Read32(address);
  59. // If the mutex isn't being held, just return success.
  60. if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) {
  61. return RESULT_SUCCESS;
  62. }
  63. if (holding_thread == nullptr)
  64. return ERR_INVALID_HANDLE;
  65. // Wait until the mutex is released
  66. GetCurrentThread()->mutex_wait_address = address;
  67. GetCurrentThread()->wait_handle = requesting_thread_handle;
  68. GetCurrentThread()->status = THREADSTATUS_WAIT_MUTEX;
  69. GetCurrentThread()->wakeup_callback = nullptr;
  70. // Update the lock holder thread's priority to prevent priority inversion.
  71. holding_thread->AddMutexWaiter(GetCurrentThread());
  72. Core::System::GetInstance().PrepareReschedule();
  73. return RESULT_SUCCESS;
  74. }
  75. ResultCode Mutex::Release(VAddr address) {
  76. // The mutex address must be 4-byte aligned
  77. if ((address % sizeof(u32)) != 0) {
  78. return ResultCode(ErrorModule::Kernel, ErrCodes::MisalignedAddress);
  79. }
  80. auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(GetCurrentThread(), address);
  81. // There are no more threads waiting for the mutex, release it completely.
  82. if (thread == nullptr) {
  83. ASSERT(GetCurrentThread()->wait_mutex_threads.empty());
  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_WAIT_MUTEX);
  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