CappedSemaphore.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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) : m_CappedSemaphoreData(Baselib_CappedSemaphore_Create(cap))
  35. {
  36. }
  37. // Reclaim resources and memory held by the semaphore.
  38. //
  39. // If threads are waiting on the semaphore, destructor will trigger an assert and may cause process abort.
  40. ~CappedSemaphore()
  41. {
  42. Baselib_CappedSemaphore_Free(&m_CappedSemaphoreData);
  43. }
  44. // Wait for semaphore token to become available
  45. //
  46. // This function is guaranteed to emit an acquire barrier.
  47. inline void Acquire()
  48. {
  49. return Baselib_CappedSemaphore_Acquire(&m_CappedSemaphoreData);
  50. }
  51. // Try to consume a token and return immediately.
  52. //
  53. // When successful this function is guaranteed to emit an acquire barrier.
  54. //
  55. // Return: true if token was consumed. false if not.
  56. inline bool TryAcquire()
  57. {
  58. return Baselib_CappedSemaphore_TryAcquire(&m_CappedSemaphoreData);
  59. }
  60. // Wait for semaphore token to become available
  61. //
  62. // When successful this function is guaranteed to emit an acquire barrier.
  63. //
  64. // TryAcquire with a zero timeout differs from TryAcquire() in that TryAcquire() is guaranteed to be a user space operation
  65. // while Acquire with a zero timeout may enter the kernel and cause a context switch.
  66. //
  67. // Timeout passed to this function may be subject to system clock resolution.
  68. // 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.
  69. //
  70. // Arguments:
  71. // - timeout: Time to wait for token to become available.
  72. //
  73. // Return: true if token was consumed. false if timeout was reached.
  74. inline bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
  75. {
  76. return Baselib_CappedSemaphore_TryTimedAcquire(&m_CappedSemaphoreData, timeoutInMilliseconds.count());
  77. }
  78. // Submit tokens to the semaphore.
  79. // If threads are waiting an equal amount of tokens are consumed before this function return.
  80. //
  81. // When successful this function is guaranteed to emit a release barrier.
  82. //
  83. // \returns number of submitted tokens.
  84. inline uint16_t Release(const uint16_t count)
  85. {
  86. return Baselib_CappedSemaphore_Release(&m_CappedSemaphoreData, count);
  87. }
  88. // Sets the semaphore token count to zero and release all waiting threads.
  89. //
  90. // When successful this function is guaranteed to emit a release barrier.
  91. //
  92. // Return: number of released threads.
  93. inline uint32_t ResetAndReleaseWaitingThreads()
  94. {
  95. return Baselib_CappedSemaphore_ResetAndReleaseWaitingThreads(&m_CappedSemaphoreData);
  96. }
  97. private:
  98. Baselib_CappedSemaphore m_CappedSemaphoreData;
  99. };
  100. }
  101. }