mutex.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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(VAddr mutex_addr) {
  19. auto& thread_list = Core::System::GetInstance().Scheduler().GetThreadList();
  20. SharedPtr<Thread> highest_priority_thread;
  21. u32 num_waiters = 0;
  22. for (auto& thread : thread_list) {
  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. ResultCode Mutex::TryAcquire(VAddr address, Handle holding_thread_handle,
  35. Handle requesting_thread_handle) {
  36. // The mutex address must be 4-byte aligned
  37. if ((address % sizeof(u32)) != 0) {
  38. return ResultCode(ErrorModule::Kernel, ErrCodes::MisalignedAddress);
  39. }
  40. SharedPtr<Thread> holding_thread = g_handle_table.Get<Thread>(holding_thread_handle);
  41. SharedPtr<Thread> requesting_thread = g_handle_table.Get<Thread>(requesting_thread_handle);
  42. // TODO(Subv): It is currently unknown if it is possible to lock a mutex in behalf of another
  43. // thread.
  44. ASSERT(requesting_thread == GetCurrentThread());
  45. u32 addr_value = Memory::Read32(address);
  46. // If the mutex isn't being held, just return success.
  47. if (addr_value != (holding_thread_handle | Mutex::MutexHasWaitersFlag)) {
  48. return RESULT_SUCCESS;
  49. }
  50. if (holding_thread == nullptr)
  51. return ERR_INVALID_HANDLE;
  52. // Wait until the mutex is released
  53. requesting_thread->mutex_wait_address = address;
  54. requesting_thread->wait_handle = requesting_thread_handle;
  55. requesting_thread->status = THREADSTATUS_WAIT_MUTEX;
  56. requesting_thread->wakeup_callback = nullptr;
  57. Core::System::GetInstance().PrepareReschedule();
  58. return RESULT_SUCCESS;
  59. }
  60. ResultCode Mutex::Release(VAddr address) {
  61. // The mutex address must be 4-byte aligned
  62. if ((address % sizeof(u32)) != 0) {
  63. return ResultCode(ErrorModule::Kernel, ErrCodes::MisalignedAddress);
  64. }
  65. auto [thread, num_waiters] = GetHighestPriorityMutexWaitingThread(address);
  66. // There are no more threads waiting for the mutex, release it completely.
  67. if (thread == nullptr) {
  68. Memory::Write32(address, 0);
  69. return RESULT_SUCCESS;
  70. }
  71. u32 mutex_value = thread->wait_handle;
  72. if (num_waiters >= 2) {
  73. // Notify the guest that there are still some threads waiting for the mutex
  74. mutex_value |= Mutex::MutexHasWaitersFlag;
  75. }
  76. // Grant the mutex to the next waiting thread and resume it.
  77. Memory::Write32(address, mutex_value);
  78. ASSERT(thread->status == THREADSTATUS_WAIT_MUTEX);
  79. thread->ResumeFromWait();
  80. thread->condvar_wait_address = 0;
  81. thread->mutex_wait_address = 0;
  82. thread->wait_handle = 0;
  83. return RESULT_SUCCESS;
  84. }
  85. } // namespace Kernel