SemaphoreFactory.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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
  16. {
  17. friend class SemaphoreFactory;
  18. friend class MicroSemaphorePtrDeleter;
  19. template<typename, typename>
  20. friend class GenericPoolAllocator;
  21. public:
  22. MicroSemaphore(const MicroSemaphore&) = delete; // Non-copyable
  23. MicroSemaphore& operator=(const MicroSemaphore&) = delete; // Non-copyable
  24. const VkSemaphore& getHandle() const
  25. {
  26. ANKI_ASSERT(m_handle);
  27. return m_handle;
  28. }
  29. GrAllocator<U8> getAllocator() const;
  30. Atomic<U32>& getRefcount()
  31. {
  32. return m_refcount;
  33. }
  34. MicroFencePtr& getFence()
  35. {
  36. return m_fence;
  37. }
  38. void setFence(MicroFencePtr& fence)
  39. {
  40. m_fence = fence;
  41. }
  42. Bool clientWait(Second seconds);
  43. Bool isTimeline() const
  44. {
  45. return m_isTimeline;
  46. }
  47. /// Get the value of the semaphore after a signal.
  48. /// @note It's thread safe.
  49. U64 getNextSemaphoreValue()
  50. {
  51. ANKI_ASSERT(m_isTimeline);
  52. return m_timelineValue.fetchAdd(1) + 1;
  53. }
  54. /// Get the value of the semaphore to wait on.
  55. /// @note It's thread safe.
  56. U64 getSemaphoreValue() const
  57. {
  58. ANKI_ASSERT(m_isTimeline);
  59. return m_timelineValue.load();
  60. }
  61. private:
  62. VkSemaphore m_handle = VK_NULL_HANDLE;
  63. Atomic<U32> m_refcount = {0};
  64. SemaphoreFactory* m_factory = nullptr;
  65. /// Fence to find out when it's safe to reuse this semaphore.
  66. MicroFencePtr m_fence;
  67. Atomic<U64> m_timelineValue = {0};
  68. Bool m_isTimeline = false;
  69. MicroSemaphore(SemaphoreFactory* f, MicroFencePtr fence, Bool isTimeline);
  70. ~MicroSemaphore();
  71. };
  72. /// MicroSemaphorePtr deleter.
  73. class MicroSemaphorePtrDeleter
  74. {
  75. public:
  76. void operator()(MicroSemaphore* s);
  77. };
  78. /// MicroSemaphore smart pointer.
  79. using MicroSemaphorePtr = IntrusivePtr<MicroSemaphore, MicroSemaphorePtrDeleter>;
  80. /// Factory of semaphores.
  81. class SemaphoreFactory
  82. {
  83. friend class MicroSemaphore;
  84. friend class MicroSemaphorePtrDeleter;
  85. public:
  86. void init(GrAllocator<U8> alloc, VkDevice dev)
  87. {
  88. ANKI_ASSERT(dev);
  89. m_alloc = alloc;
  90. m_dev = dev;
  91. m_binaryRecycler.init(alloc);
  92. m_timelineRecycler.init(alloc);
  93. }
  94. void destroy()
  95. {
  96. m_binaryRecycler.destroy();
  97. m_timelineRecycler.destroy();
  98. }
  99. MicroSemaphorePtr newInstance(MicroFencePtr fence, Bool isTimeline);
  100. private:
  101. GrAllocator<U8> m_alloc;
  102. VkDevice m_dev = VK_NULL_HANDLE;
  103. MicroObjectRecycler<MicroSemaphore> m_binaryRecycler;
  104. MicroObjectRecycler<MicroSemaphore> m_timelineRecycler;
  105. };
  106. /// @}
  107. } // end namespace anki
  108. #include <AnKi/Gr/Vulkan/SemaphoreFactory.inl.h>