HighCapacitySemaphore.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "../C/Baselib_HighCapacitySemaphore.h"
  3. #include "Time.h"
  4. namespace baselib
  5. {
  6. BASELIB_CPP_INTERFACE
  7. {
  8. // baselib::HighCapacitySemaphore is similar to baselib::Semaphore but allows for far greater token count.
  9. // It is suitable to be used as resource counting semaphore.
  10. class HighCapacitySemaphore
  11. {
  12. public:
  13. // non-copyable
  14. HighCapacitySemaphore(const HighCapacitySemaphore& other) = delete;
  15. HighCapacitySemaphore& operator=(const HighCapacitySemaphore& other) = delete;
  16. // non-movable (strictly speaking not needed but listed to signal intent)
  17. HighCapacitySemaphore(HighCapacitySemaphore&& other) = delete;
  18. HighCapacitySemaphore& operator=(HighCapacitySemaphore&& other) = delete;
  19. // This is the max number of tokens guaranteed to be held by the semaphore at
  20. // any given point in time. Tokens submitted that exceed this value may silently
  21. // be discarded.
  22. enum : int64_t { MaxGuaranteedCount = Baselib_HighCapacitySemaphore_MaxGuaranteedCount };
  23. // Creates a counting semaphore synchronization primitive.
  24. // If there are not enough system resources to create a semaphore, process abort is triggered.
  25. HighCapacitySemaphore() : m_SemaphoreData(Baselib_HighCapacitySemaphore_Create())
  26. {
  27. }
  28. // Reclaim resources and memory held by the semaphore.
  29. //
  30. // If threads are waiting on the semaphore, destructor will trigger an assert and may cause process abort.
  31. ~HighCapacitySemaphore()
  32. {
  33. Baselib_HighCapacitySemaphore_Free(&m_SemaphoreData);
  34. }
  35. // Wait for semaphore token to become available
  36. //
  37. // This function is guaranteed to emit an acquire barrier.
  38. inline void Acquire()
  39. {
  40. return Baselib_HighCapacitySemaphore_Acquire(&m_SemaphoreData);
  41. }
  42. // Try to consume a token and return immediately.
  43. //
  44. // When successful this function is guaranteed to emit an acquire barrier.
  45. //
  46. // Return: true if token was consumed. false if not.
  47. inline bool TryAcquire()
  48. {
  49. return Baselib_HighCapacitySemaphore_TryAcquire(&m_SemaphoreData);
  50. }
  51. // Wait for semaphore token to become available
  52. //
  53. // When successful this function is guaranteed to emit an acquire barrier.
  54. //
  55. // TryAcquire with a zero timeout differs from TryAcquire() in that TryAcquire() is guaranteed to be a user space operation
  56. // while Acquire with a zero timeout may enter the kernel and cause a context switch.
  57. //
  58. // Timeout passed to this function may be subject to system clock resolution.
  59. // If the system clock has a resolution of e.g. 16ms that means this function may exit with a timeout error 16ms earlier than originally scheduled.
  60. //
  61. // Arguments:
  62. // - timeout: Time to wait for token to become available.
  63. //
  64. // Return: true if token was consumed. false if timeout was reached.
  65. inline bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
  66. {
  67. return Baselib_HighCapacitySemaphore_TryTimedAcquire(&m_SemaphoreData, timeoutInMilliseconds.count());
  68. }
  69. // Submit tokens to the semaphore.
  70. //
  71. // When successful this function is guaranteed to emit a release barrier.
  72. //
  73. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  74. // If count reach `Baselib_HighCapacitySemaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  75. inline void Release(uint32_t count)
  76. {
  77. return Baselib_HighCapacitySemaphore_Release(&m_SemaphoreData, count);
  78. }
  79. // Sets the semaphore token count to zero and release all waiting threads.
  80. //
  81. // When successful this function is guaranteed to emit a release barrier.
  82. //
  83. // Return: number of released threads.
  84. inline uint64_t ResetAndReleaseWaitingThreads()
  85. {
  86. return Baselib_HighCapacitySemaphore_ResetAndReleaseWaitingThreads(&m_SemaphoreData);
  87. }
  88. private:
  89. Baselib_HighCapacitySemaphore m_SemaphoreData;
  90. };
  91. }
  92. }