CappedSemaphore.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "../C/Baselib_CappedSemaphore.h"
  3. #include "Time.h"
  4. namespace baselib
  5. {
  6. BASELIB_CPP_INTERFACE
  7. {
  8. // In computer science, a semaphore is a variable or abstract data type used to control access to a common resource by multiple processes in a concurrent
  9. // system such as a multitasking operating system. A semaphore is simply a variable. This variable is used to solve critical section problems and to achieve
  10. // process synchronization in the multi processing environment. A trivial semaphore is a plain variable that is changed (for example, incremented or
  11. // decremented, or toggled) depending on programmer-defined conditions.
  12. //
  13. // A useful way to think of a semaphore as used in the real-world system is as a record of how many units of a particular resource are available, coupled with
  14. // operations to adjust that record safely (i.e. to avoid race conditions) as units are required or become free, and, if necessary, wait until a unit of the
  15. // resource becomes available.
  16. //
  17. // "Semaphore (programming)", Wikipedia: The Free Encyclopedia
  18. // https://en.wikipedia.org/w/index.php?title=Semaphore_(programming)&oldid=872408126
  19. //
  20. // For optimal performance, baselib::CappedSemaphore should be stored at a cache aligned memory location.
  21. class CappedSemaphore
  22. {
  23. public:
  24. // non-copyable
  25. CappedSemaphore(const CappedSemaphore& other) = delete;
  26. CappedSemaphore& operator=(const CappedSemaphore& other) = delete;
  27. // non-movable (strictly speaking not needed but listed to signal intent)
  28. CappedSemaphore(CappedSemaphore&& other) = delete;
  29. CappedSemaphore& operator=(CappedSemaphore&& other) = delete;
  30. // Creates a capped counting semaphore synchronization primitive.
  31. // Cap is the number of tokens that can be held by the semaphore when there is no contention.
  32. //
  33. // If there are not enough system resources to create a semaphore, process abort is triggered.
  34. CappedSemaphore(const uint16_t cap)
  35. {
  36. Baselib_CappedSemaphore_CreateInplace(&m_CappedSemaphoreData, cap);
  37. }
  38. // Reclaim resources and memory held by the semaphore.
  39. //
  40. // If threads are waiting on the semaphore, destructor will trigger an assert and may cause process abort.
  41. ~CappedSemaphore()
  42. {
  43. Baselib_CappedSemaphore_FreeInplace(&m_CappedSemaphoreData);
  44. }
  45. // Wait for semaphore token to become available
  46. //
  47. // This function is guaranteed to emit an acquire barrier.
  48. inline void Acquire()
  49. {
  50. return Baselib_CappedSemaphore_Acquire(&m_CappedSemaphoreData);
  51. }
  52. // Try to consume a token and return immediately.
  53. //
  54. // When successful this function is guaranteed to emit an acquire barrier.
  55. //
  56. // Return: true if token was consumed. false if not.
  57. inline bool TryAcquire()
  58. {
  59. return Baselib_CappedSemaphore_TryAcquire(&m_CappedSemaphoreData);
  60. }
  61. // Wait for semaphore token to become available
  62. //
  63. // When successful this function is guaranteed to emit an acquire barrier.
  64. //
  65. // TryAcquire with a zero timeout differs from TryAcquire() in that TryAcquire() is guaranteed to be a user space operation
  66. // while Acquire with a zero timeout may enter the kernel and cause a context switch.
  67. //
  68. // Timeout passed to this function may be subject to system clock resolution.
  69. // 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.
  70. //
  71. // Arguments:
  72. // - timeout: Time to wait for token to become available.
  73. //
  74. // Return: true if token was consumed. false if timeout was reached.
  75. inline bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
  76. {
  77. return Baselib_CappedSemaphore_TryTimedAcquire(&m_CappedSemaphoreData, timeoutInMilliseconds.count());
  78. }
  79. // Submit tokens to the semaphore.
  80. // If threads are waiting an equal amount of tokens are consumed before this function return.
  81. //
  82. // When successful this function is guaranteed to emit a release barrier.
  83. //
  84. // \returns number of submitted tokens.
  85. inline uint16_t Release(const uint16_t count)
  86. {
  87. return Baselib_CappedSemaphore_Release(&m_CappedSemaphoreData, count);
  88. }
  89. // Sets the semaphore token count to zero and release all waiting threads.
  90. //
  91. // When successful this function is guaranteed to emit a release barrier.
  92. //
  93. // Return: number of released threads.
  94. inline uint32_t ResetAndReleaseWaitingThreads()
  95. {
  96. return Baselib_CappedSemaphore_ResetAndReleaseWaitingThreads(&m_CappedSemaphoreData);
  97. }
  98. private:
  99. Baselib_CappedSemaphore m_CappedSemaphoreData;
  100. };
  101. }
  102. }