Semaphore.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include "../C/Baselib_Semaphore.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::Semaphore should be stored at a cache aligned memory location.
  21. class Semaphore
  22. {
  23. public:
  24. // non-copyable
  25. Semaphore(const Semaphore& other) = delete;
  26. Semaphore& operator=(const Semaphore& other) = delete;
  27. // non-movable (strictly speaking not needed but listed to signal intent)
  28. Semaphore(Semaphore&& other) = delete;
  29. Semaphore& operator=(Semaphore&& other) = delete;
  30. // This is the max number of tokens guaranteed to be held by the semaphore at
  31. // any given point in time. Tokens submitted that exceed this value may silently
  32. // be discarded.
  33. enum { MaxGuaranteedCount = Baselib_Semaphore_MaxGuaranteedCount };
  34. // Creates a counting semaphore synchronization primitive.
  35. // If there are not enough system resources to create a semaphore, process abort is triggered.
  36. Semaphore() : m_SemaphoreData(Baselib_Semaphore_Create())
  37. {
  38. }
  39. // Reclaim resources and memory held by the semaphore.
  40. //
  41. // If threads are waiting on the semaphore, destructor will trigger an assert and may cause process abort.
  42. ~Semaphore()
  43. {
  44. Baselib_Semaphore_Free(&m_SemaphoreData);
  45. }
  46. // Wait for semaphore token to become available
  47. //
  48. // This function is guaranteed to emit an acquire barrier.
  49. inline void Acquire()
  50. {
  51. return Baselib_Semaphore_Acquire(&m_SemaphoreData);
  52. }
  53. // Try to consume a token and return immediately.
  54. //
  55. // When successful this function is guaranteed to emit an acquire barrier.
  56. //
  57. // Return: true if token was consumed. false if not.
  58. inline bool TryAcquire()
  59. {
  60. return Baselib_Semaphore_TryAcquire(&m_SemaphoreData);
  61. }
  62. // Wait for semaphore token to become available
  63. //
  64. // When successful this function is guaranteed to emit an acquire barrier.
  65. //
  66. // TryAcquire with a zero timeout differs from TryAcquire() in that TryAcquire() is guaranteed to be a user space operation
  67. // while Acquire with a zero timeout may enter the kernel and cause a context switch.
  68. //
  69. // Timeout passed to this function may be subject to system clock resolution.
  70. // 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.
  71. //
  72. // Arguments:
  73. // - timeout: Time to wait for token to become available.
  74. //
  75. // Return: true if token was consumed. false if timeout was reached.
  76. inline bool TryTimedAcquire(const timeout_ms timeoutInMilliseconds)
  77. {
  78. return Baselib_Semaphore_TryTimedAcquire(&m_SemaphoreData, timeoutInMilliseconds.count());
  79. }
  80. // Submit tokens to the semaphore.
  81. //
  82. // When successful this function is guaranteed to emit a release barrier.
  83. //
  84. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  85. // If count reach `Baselib_Semaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  86. inline void Release(uint16_t count)
  87. {
  88. return Baselib_Semaphore_Release(&m_SemaphoreData, count);
  89. }
  90. // Sets the semaphore token count to zero and release all waiting threads.
  91. //
  92. // When successful this function is guaranteed to emit a release barrier.
  93. //
  94. // Return: number of released threads.
  95. inline uint32_t ResetAndReleaseWaitingThreads()
  96. {
  97. return Baselib_Semaphore_ResetAndReleaseWaitingThreads(&m_SemaphoreData);
  98. }
  99. private:
  100. Baselib_Semaphore m_SemaphoreData;
  101. };
  102. }
  103. }