Baselib_Semaphore.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. // Baselib_Semaphore
  3. // 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
  4. // 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
  5. // process synchronization in the multi processing environment. A trivial semaphore is a plain variable that is changed (for example, incremented or
  6. // decremented, or toggled) depending on programmer-defined conditions.
  7. //
  8. // 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
  9. // 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
  10. // resource becomes available.
  11. //
  12. // "Semaphore (programming)", Wikipedia: The Free Encyclopedia
  13. // https://en.wikipedia.org/w/index.php?title=Semaphore_(programming)&oldid=872408126
  14. // This is the max number of tokens guaranteed to be held by the semaphore at
  15. // any given point in time. Tokens submitted that exceed this value may silently be discarded.
  16. static const int32_t Baselib_Semaphore_MaxGuaranteedCount = UINT16_MAX;
  17. #if PLATFORM_FUTEX_NATIVE_SUPPORT
  18. #include "Internal/Baselib_Semaphore_FutexBased.inl.h"
  19. #else
  20. #include "Internal/Baselib_Semaphore_SemaphoreBased.inl.h"
  21. #endif
  22. // Creates a counting semaphore synchronization primitive.
  23. // Use Baselib_Semaphore_Free() to free semaphore.
  24. //
  25. // If there are not enough system resources to create a semaphore, process abort is triggered.
  26. //
  27. // For optimal performance, the returned Baselib_Semaphore should be stored at a cache aligned memory location.
  28. //
  29. // \returns A struct representing a semaphore instance. Use Baselib_Semaphore_Free to free the semaphore.
  30. BASELIB_INLINE_API Baselib_Semaphore Baselib_Semaphore_Create(void);
  31. // Creates a counting semaphore synchronization primitive in-place with memory provided by caller.
  32. // Use Baselib_Semaphore_FreeInplace() to free semaphore.
  33. //
  34. // If there are not enough system resources to create a semaphore, process abort is triggered.
  35. //
  36. // For optimal performance, created Baselib_Semaphore should be stored at a cache aligned memory location.
  37. BASELIB_INLINE_API void Baselib_Semaphore_CreateInplace(Baselib_Semaphore* semaphore);
  38. // Wait for semaphore token to become available
  39. //
  40. // This function is guaranteed to emit an acquire barrier.
  41. // Returns if token was consumed or was woken up by Baselib_Semaphore_ResetAndReleaseWaitingThreads.
  42. BASELIB_INLINE_API void Baselib_Semaphore_Acquire(Baselib_Semaphore* semaphore);
  43. // Try to consume a token and return immediately.
  44. //
  45. // When successful this function is guaranteed to emit an acquire barrier.
  46. //
  47. // \returns true if token was consumed. false if not.
  48. BASELIB_INLINE_API bool Baselib_Semaphore_TryAcquire(Baselib_Semaphore* semaphore);
  49. // Wait for semaphore token to become available
  50. //
  51. // When successful this function is guaranteed to emit an acquire barrier.
  52. //
  53. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  54. // while Acquire may enter the kernel and cause a context switch.
  55. //
  56. // Timeout passed to this function may be subject to system clock resolution.
  57. // 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.
  58. //
  59. // \param timeout Time to wait for token to become available.
  60. //
  61. // \returns true if token was consumed or was woken up by Baselib_Semaphore_ResetAndReleaseWaitingThreads. false if timeout was reached.
  62. BASELIB_INLINE_API bool Baselib_Semaphore_TryTimedAcquire(Baselib_Semaphore* semaphore, const uint32_t timeoutInMilliseconds);
  63. // Submit tokens to the semaphore.
  64. //
  65. // When successful this function is guaranteed to emit a release barrier.
  66. //
  67. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  68. // If count reach `Baselib_Semaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  69. BASELIB_INLINE_API void Baselib_Semaphore_Release(Baselib_Semaphore* semaphore, const uint16_t count);
  70. // If threads are waiting on Baselib_Semaphore_Acquire / Baselib_Semaphore_TryTimedAcquire,
  71. // releases enough tokens to wake them up. Otherwise consumes all available tokens.
  72. //
  73. // When successful this function is guaranteed to emit a release barrier.
  74. //
  75. // \returns number of released threads.
  76. BASELIB_INLINE_API uint32_t Baselib_Semaphore_ResetAndReleaseWaitingThreads(Baselib_Semaphore* semaphore);
  77. // Reclaim resources and memory held by the semaphore.
  78. //
  79. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  80. // Calling this function with a nullptr result in a no-op
  81. BASELIB_INLINE_API void Baselib_Semaphore_Free(Baselib_Semaphore* semaphore);
  82. // Reclaim resources held by the semaphore. Caller is responsible for freeing memory pointed to by semaphore
  83. //
  84. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  85. // Calling this function with a nullptr result in a no-op
  86. BASELIB_INLINE_API void Baselib_Semaphore_FreeInplace(Baselib_Semaphore* semaphore);