wait_object.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // The list of waiting threads must not contain threads that are not waiting to be awakened.
  32. ASSERT_MSG(thread->status == ThreadStatus::WaitSynchAny ||
  33. thread->status == ThreadStatus::WaitSynchAll ||
  34. thread->status == ThreadStatus::WaitHLEEvent,
  35. "Inconsistent thread statuses in waiting_threads");
  36. if (thread->current_priority >= candidate_priority)
  37. continue;
  38. if (ShouldWait(thread.get()))
  39. continue;
  40. // A thread is ready to run if it's either in ThreadStatus::WaitSynchAny or
  41. // in ThreadStatus::WaitSynchAll and the rest of the objects it is waiting on are ready.
  42. bool ready_to_run = true;
  43. if (thread->status == ThreadStatus::WaitSynchAll) {
  44. ready_to_run = std::none_of(thread->wait_objects.begin(), thread->wait_objects.end(),
  45. [&thread](const SharedPtr<WaitObject>& object) {
  46. return object->ShouldWait(thread.get());
  47. });
  48. }
  49. if (ready_to_run) {
  50. candidate = thread.get();
  51. candidate_priority = thread->current_priority;
  52. }
  53. }
  54. return candidate;
  55. }
  56. void WaitObject::WakeupWaitingThread(SharedPtr<Thread> thread) {
  57. ASSERT(!ShouldWait(thread.get()));
  58. if (!thread)
  59. return;
  60. if (!thread->IsSleepingOnWaitAll()) {
  61. Acquire(thread.get());
  62. } else {
  63. for (auto& object : thread->wait_objects) {
  64. ASSERT(!object->ShouldWait(thread.get()));
  65. object->Acquire(thread.get());
  66. }
  67. }
  68. std::size_t index = thread->GetWaitObjectIndex(this);
  69. for (auto& object : thread->wait_objects)
  70. object->RemoveWaitingThread(thread.get());
  71. thread->wait_objects.clear();
  72. thread->CancelWakeupTimer();
  73. bool resume = true;
  74. if (thread->wakeup_callback)
  75. resume = thread->wakeup_callback(ThreadWakeupReason::Signal, thread, this, index);
  76. if (resume)
  77. thread->ResumeFromWait();
  78. }
  79. void WaitObject::WakeupAllWaitingThreads() {
  80. while (auto thread = GetHighestPriorityReadyThread()) {
  81. WakeupWaitingThread(thread);
  82. }
  83. }
  84. const std::vector<SharedPtr<Thread>>& WaitObject::GetWaitingThreads() const {
  85. return waiting_threads;
  86. }
  87. } // namespace Kernel