wait_object.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include "common/assert.h"
  6. #include "common/logging/log.h"
  7. #include "core/hle/kernel/object.h"
  8. #include "core/hle/kernel/process.h"
  9. #include "core/hle/kernel/thread.h"
  10. #include "core/hle/kernel/timer.h"
  11. namespace Kernel {
  12. WaitObject::WaitObject(KernelCore& kernel) : Object{kernel} {}
  13. WaitObject::~WaitObject() = default;
  14. void WaitObject::AddWaitingThread(SharedPtr<Thread> thread) {
  15. auto itr = std::find(waiting_threads.begin(), waiting_threads.end(), thread);
  16. if (itr == waiting_threads.end())
  17. waiting_threads.push_back(std::move(thread));
  18. }
  19. void WaitObject::RemoveWaitingThread(Thread* thread) {
  20. auto itr = std::find(waiting_threads.begin(), waiting_threads.end(), thread);
  21. // If a thread passed multiple handles to the same object,
  22. // the kernel might attempt to remove the thread from the object's
  23. // waiting threads list multiple times.
  24. if (itr != waiting_threads.end())
  25. waiting_threads.erase(itr);
  26. }
  27. SharedPtr<Thread> WaitObject::GetHighestPriorityReadyThread() {
  28. Thread* candidate = nullptr;
  29. u32 candidate_priority = THREADPRIO_LOWEST + 1;
  30. for (const auto& thread : waiting_threads) {
  31. const ThreadStatus thread_status = thread->GetStatus();
  32. // The list of waiting threads must not contain threads that are not waiting to be awakened.
  33. ASSERT_MSG(thread_status == ThreadStatus::WaitSynchAny ||
  34. thread_status == ThreadStatus::WaitSynchAll ||
  35. thread_status == ThreadStatus::WaitHLEEvent,
  36. "Inconsistent thread statuses in waiting_threads");
  37. if (thread->GetPriority() >= candidate_priority)
  38. continue;
  39. if (ShouldWait(thread.get()))
  40. continue;
  41. // A thread is ready to run if it's either in ThreadStatus::WaitSynchAny or
  42. // in ThreadStatus::WaitSynchAll and the rest of the objects it is waiting on are ready.
  43. bool ready_to_run = true;
  44. if (thread_status == ThreadStatus::WaitSynchAll) {
  45. ready_to_run = thread->AllWaitObjectsReady();
  46. }
  47. if (ready_to_run) {
  48. candidate = thread.get();
  49. candidate_priority = thread->GetPriority();
  50. }
  51. }
  52. return candidate;
  53. }
  54. void WaitObject::WakeupWaitingThread(SharedPtr<Thread> thread) {
  55. ASSERT(!ShouldWait(thread.get()));
  56. if (!thread)
  57. return;
  58. if (!thread->IsSleepingOnWaitAll()) {
  59. Acquire(thread.get());
  60. } else {
  61. for (const auto& object : thread->GetWaitObjects()) {
  62. ASSERT(!object->ShouldWait(thread.get()));
  63. object->Acquire(thread.get());
  64. }
  65. }
  66. const std::size_t index = thread->GetWaitObjectIndex(this);
  67. for (const auto& object : thread->GetWaitObjects())
  68. object->RemoveWaitingThread(thread.get());
  69. thread->ClearWaitObjects();
  70. thread->CancelWakeupTimer();
  71. bool resume = true;
  72. if (thread->HasWakeupCallback())
  73. resume = thread->InvokeWakeupCallback(ThreadWakeupReason::Signal, thread, this, index);
  74. if (resume)
  75. thread->ResumeFromWait();
  76. }
  77. void WaitObject::WakeupAllWaitingThreads() {
  78. while (auto thread = GetHighestPriorityReadyThread()) {
  79. WakeupWaitingThread(thread);
  80. }
  81. }
  82. const std::vector<SharedPtr<Thread>>& WaitObject::GetWaitingThreads() const {
  83. return waiting_threads;
  84. }
  85. } // namespace Kernel