Semaphore.h 5.4 KB

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