HighCapacitySemaphore.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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()
  26. {
  27. Baselib_HighCapacitySemaphore_CreateInplace(&m_SemaphoreData);
  28. }
  29. // Reclaim resources and memory held by the semaphore.
  30. //
  31. // If threads are waiting on the semaphore, destructor will trigger an assert and may cause process abort.
  32. ~HighCapacitySemaphore()
  33. {
  34. Baselib_HighCapacitySemaphore_FreeInplace(&m_SemaphoreData);
  35. }
  36. // Wait for semaphore token to become available
  37. //
  38. // This function is guaranteed to emit an acquire barrier.
  39. inline void Acquire()
  40. {
  41. return Baselib_HighCapacitySemaphore_Acquire(&m_SemaphoreData);
  42. }
  43. // Try to consume a token and return immediately.
  44. //
  45. // When successful this function is guaranteed to emit an acquire barrier.
  46. //
  47. // Return: true if token was consumed. false if not.
  48. inline bool TryAcquire()
  49. {
  50. return Baselib_HighCapacitySemaphore_TryAcquire(&m_SemaphoreData);
  51. }
  52. // Wait for semaphore token to become available
  53. //
  54. // When successful this function is guaranteed to emit an acquire barrier.
  55. //
  56. // TryAcquire with a zero timeout differs from TryAcquire() in that TryAcquire() is guaranteed to be a user space operation
  57. // while Acquire with a zero timeout may enter the kernel and cause a context switch.
  58. //
  59. // Timeout passed to this function may be subject to system clock resolution.
  60. // 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.
  61. //
  62. // Arguments:
  63. // - timeout: Time to wait for token to become available.
  64. //
  65. // Return: true if token was consumed. false if timeout was reached.
  66. inline bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
  67. {
  68. return Baselib_HighCapacitySemaphore_TryTimedAcquire(&m_SemaphoreData, timeoutInMilliseconds.count());
  69. }
  70. // Submit tokens to the semaphore.
  71. //
  72. // When successful this function is guaranteed to emit a release barrier.
  73. //
  74. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  75. // If count reach `Baselib_HighCapacitySemaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  76. inline void Release(uint32_t count)
  77. {
  78. return Baselib_HighCapacitySemaphore_Release(&m_SemaphoreData, count);
  79. }
  80. // Sets the semaphore token count to zero and release all waiting threads.
  81. //
  82. // When successful this function is guaranteed to emit a release barrier.
  83. //
  84. // Return: number of released threads.
  85. inline uint64_t ResetAndReleaseWaitingThreads()
  86. {
  87. return Baselib_HighCapacitySemaphore_ResetAndReleaseWaitingThreads(&m_SemaphoreData);
  88. }
  89. private:
  90. Baselib_HighCapacitySemaphore m_SemaphoreData;
  91. };
  92. }
  93. }