wait_object.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <vector>
  7. #include "core/hle/kernel/object.h"
  8. namespace Kernel {
  9. class KernelCore;
  10. class Thread;
  11. /// Class that represents a Kernel object that a thread can be waiting on
  12. class WaitObject : public Object {
  13. public:
  14. explicit WaitObject(KernelCore& kernel);
  15. ~WaitObject() override;
  16. /**
  17. * Check if the specified thread should wait until the object is available
  18. * @param thread The thread about which we're deciding.
  19. * @return True if the current thread should wait due to this object being unavailable
  20. */
  21. virtual bool ShouldWait(const Thread* thread) const = 0;
  22. /// Acquire/lock the object for the specified thread if it is available
  23. virtual void Acquire(Thread* thread) = 0;
  24. /**
  25. * Add a thread to wait on this object
  26. * @param thread Pointer to thread to add
  27. */
  28. void AddWaitingThread(std::shared_ptr<Thread> thread);
  29. /**
  30. * Removes a thread from waiting on this object (e.g. if it was resumed already)
  31. * @param thread Pointer to thread to remove
  32. */
  33. void RemoveWaitingThread(std::shared_ptr<Thread> thread);
  34. /**
  35. * Wake up all threads waiting on this object that can be awoken, in priority order,
  36. * and set the synchronization result and output of the thread.
  37. */
  38. void WakeupAllWaitingThreads();
  39. /**
  40. * Wakes up a single thread waiting on this object.
  41. * @param thread Thread that is waiting on this object to wakeup.
  42. */
  43. void WakeupWaitingThread(std::shared_ptr<Thread> thread);
  44. /// Obtains the highest priority thread that is ready to run from this object's waiting list.
  45. std::shared_ptr<Thread> GetHighestPriorityReadyThread() const;
  46. /// Get a const reference to the waiting threads list for debug use
  47. const std::vector<std::shared_ptr<Thread>>& GetWaitingThreads() const;
  48. private:
  49. /// Threads waiting for this object to become available
  50. std::vector<std::shared_ptr<Thread>> waiting_threads;
  51. };
  52. // Specialization of DynamicObjectCast for WaitObjects
  53. template <>
  54. inline std::shared_ptr<WaitObject> DynamicObjectCast<WaitObject>(std::shared_ptr<Object> object) {
  55. if (object != nullptr && object->IsWaitable()) {
  56. return std::static_pointer_cast<WaitObject>(object);
  57. }
  58. return nullptr;
  59. }
  60. } // namespace Kernel