VkSemaphoreFactory.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Gr/Vulkan/VkCommon.h>
  7. #include <AnKi/Gr/BackendCommon/MicroObjectRecycler.h>
  8. namespace anki {
  9. /// @addtogroup vulkan
  10. /// @{
  11. /// Simple semaphore wrapper.
  12. class MicroSemaphore
  13. {
  14. friend class SemaphoreFactory;
  15. friend class MicroSemaphorePtrDeleter;
  16. ANKI_FRIEND_CALL_CONSTRUCTOR_AND_DESTRUCTOR
  17. public:
  18. MicroSemaphore(const MicroSemaphore&) = delete; // Non-copyable
  19. MicroSemaphore& operator=(const MicroSemaphore&) = delete; // Non-copyable
  20. const VkSemaphore& getHandle() const
  21. {
  22. ANKI_ASSERT(m_handle);
  23. return m_handle;
  24. }
  25. void retain() const
  26. {
  27. m_refcount.fetchAdd(1);
  28. }
  29. void release()
  30. {
  31. if(m_refcount.fetchSub(1) == 1)
  32. {
  33. releaseInternal();
  34. }
  35. }
  36. I32 getRefcount() const
  37. {
  38. return m_refcount.load();
  39. }
  40. Bool clientWait(Second seconds);
  41. Bool isTimeline() const
  42. {
  43. return m_isTimeline;
  44. }
  45. /// Get the value of the semaphore after a signal.
  46. /// @note It's thread safe.
  47. U64 getNextSemaphoreValue()
  48. {
  49. ANKI_ASSERT(m_isTimeline);
  50. return m_timelineValue.fetchAdd(1) + 1;
  51. }
  52. /// Get the value of the semaphore to wait on.
  53. /// @note It's thread safe.
  54. U64 getSemaphoreValue() const
  55. {
  56. ANKI_ASSERT(m_isTimeline);
  57. return m_timelineValue.load();
  58. }
  59. private:
  60. VkSemaphore m_handle = VK_NULL_HANDLE;
  61. mutable Atomic<I32> m_refcount = {0};
  62. Atomic<U64> m_timelineValue = {0};
  63. Bool m_isTimeline = false;
  64. MicroSemaphore(Bool isTimeline);
  65. ~MicroSemaphore();
  66. void releaseInternal();
  67. };
  68. /// MicroSemaphore smart pointer.
  69. using MicroSemaphorePtr = IntrusiveNoDelPtr<MicroSemaphore>;
  70. /// Factory of semaphores.
  71. class SemaphoreFactory : public MakeSingleton<SemaphoreFactory>
  72. {
  73. friend class MicroSemaphore;
  74. friend class MicroSemaphorePtrDeleter;
  75. public:
  76. ~SemaphoreFactory()
  77. {
  78. m_binaryRecycler.destroy();
  79. m_timelineRecycler.destroy();
  80. }
  81. MicroSemaphorePtr newInstance(Bool isTimeline, CString name);
  82. private:
  83. MicroObjectRecycler<MicroSemaphore> m_binaryRecycler;
  84. MicroObjectRecycler<MicroSemaphore> m_timelineRecycler;
  85. };
  86. /// @}
  87. } // end namespace anki