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