wait_object.h 2.1 KB

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