synchronization_object.h 2.2 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 <atomic>
  6. #include <memory>
  7. #include <vector>
  8. #include "core/hle/kernel/object.h"
  9. namespace Kernel {
  10. class KernelCore;
  11. class Synchronization;
  12. class Thread;
  13. /// Class that represents a Kernel object that a thread can be waiting on
  14. class SynchronizationObject : public Object {
  15. public:
  16. explicit SynchronizationObject(KernelCore& kernel);
  17. ~SynchronizationObject() override;
  18. /**
  19. * Check if the specified thread should wait until the object is available
  20. * @param thread The thread about which we're deciding.
  21. * @return True if the current thread should wait due to this object being unavailable
  22. */
  23. virtual bool ShouldWait(const Thread* thread) const = 0;
  24. /// Acquire/lock the object for the specified thread if it is available
  25. virtual void Acquire(Thread* thread) = 0;
  26. /// Signal this object
  27. virtual void Signal();
  28. virtual bool IsSignaled() const {
  29. return is_signaled;
  30. }
  31. /**
  32. * Add a thread to wait on this object
  33. * @param thread Pointer to thread to add
  34. */
  35. void AddWaitingThread(std::shared_ptr<Thread> thread);
  36. /**
  37. * Removes a thread from waiting on this object (e.g. if it was resumed already)
  38. * @param thread Pointer to thread to remove
  39. */
  40. void RemoveWaitingThread(std::shared_ptr<Thread> thread);
  41. /// Get a const reference to the waiting threads list for debug use
  42. const std::vector<std::shared_ptr<Thread>>& GetWaitingThreads() const;
  43. void ClearWaitingThreads();
  44. protected:
  45. std::atomic_bool is_signaled{}; // Tells if this sync object is signaled
  46. private:
  47. /// Threads waiting for this object to become available
  48. std::vector<std::shared_ptr<Thread>> waiting_threads;
  49. };
  50. // Specialization of DynamicObjectCast for SynchronizationObjects
  51. template <>
  52. inline std::shared_ptr<SynchronizationObject> DynamicObjectCast<SynchronizationObject>(
  53. std::shared_ptr<Object> object) {
  54. if (object != nullptr && object->IsWaitable()) {
  55. return std::static_pointer_cast<SynchronizationObject>(object);
  56. }
  57. return nullptr;
  58. }
  59. } // namespace Kernel