SemaphoreFactory.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2009-2021, 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/FenceFactory.h>
  7. #include <AnKi/Gr/Vulkan/MicroObjectRecycler.h>
  8. namespace anki
  9. {
  10. // Forward
  11. class SemaphoreFactory;
  12. /// @addtogroup vulkan
  13. /// @{
  14. /// Simple semaphore wrapper.
  15. class MicroSemaphore : public NonCopyable
  16. {
  17. friend class SemaphoreFactory;
  18. friend class MicroSemaphorePtrDeleter;
  19. template<typename, typename>
  20. friend class GenericPoolAllocator;
  21. public:
  22. const VkSemaphore& getHandle() const
  23. {
  24. ANKI_ASSERT(m_handle);
  25. return m_handle;
  26. }
  27. GrAllocator<U8> getAllocator() const;
  28. Atomic<U32>& getRefcount()
  29. {
  30. return m_refcount;
  31. }
  32. MicroFencePtr& getFence()
  33. {
  34. return m_fence;
  35. }
  36. void setFence(MicroFencePtr& fence)
  37. {
  38. m_fence = fence;
  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. Atomic<U32> m_refcount = {0};
  62. SemaphoreFactory* m_factory = nullptr;
  63. /// Fence to find out when it's safe to reuse this semaphore.
  64. MicroFencePtr m_fence;
  65. Atomic<U64> m_timelineValue = {0};
  66. Bool m_isTimeline = false;
  67. MicroSemaphore(SemaphoreFactory* f, MicroFencePtr fence, Bool isTimeline);
  68. ~MicroSemaphore();
  69. };
  70. /// MicroSemaphorePtr deleter.
  71. class MicroSemaphorePtrDeleter
  72. {
  73. public:
  74. void operator()(MicroSemaphore* s);
  75. };
  76. /// MicroSemaphore smart pointer.
  77. using MicroSemaphorePtr = IntrusivePtr<MicroSemaphore, MicroSemaphorePtrDeleter>;
  78. /// Factory of semaphores.
  79. class SemaphoreFactory
  80. {
  81. friend class MicroSemaphore;
  82. friend class MicroSemaphorePtrDeleter;
  83. public:
  84. void init(GrAllocator<U8> alloc, VkDevice dev)
  85. {
  86. ANKI_ASSERT(dev);
  87. m_alloc = alloc;
  88. m_dev = dev;
  89. m_binaryRecycler.init(alloc);
  90. m_timelineRecycler.init(alloc);
  91. }
  92. void destroy()
  93. {
  94. m_binaryRecycler.destroy();
  95. m_timelineRecycler.destroy();
  96. }
  97. MicroSemaphorePtr newInstance(MicroFencePtr fence, Bool isTimeline);
  98. private:
  99. GrAllocator<U8> m_alloc;
  100. VkDevice m_dev = VK_NULL_HANDLE;
  101. MicroObjectRecycler<MicroSemaphore> m_binaryRecycler;
  102. MicroObjectRecycler<MicroSemaphore> m_timelineRecycler;
  103. };
  104. /// @}
  105. } // end namespace anki
  106. #include <AnKi/Gr/Vulkan/SemaphoreFactory.inl.h>