Baselib_CappedSemaphore.h 5.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. // Baselib_CappedSemaphore
  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. #if PLATFORM_FUTEX_NATIVE_SUPPORT
  15. #include "Internal/Baselib_CappedSemaphore_FutexBased.inl.h"
  16. #else
  17. #include "Internal/Baselib_CappedSemaphore_SemaphoreBased.inl.h"
  18. #endif
  19. // Creates a capped counting semaphore synchronization primitive.
  20. //
  21. // Cap is the number of tokens that can be held by the semaphore when there is no contention.
  22. // If there are not enough system resources to create a semaphore, process abort is triggered.
  23. // Use Baselib_CappedSemaphore_Free() to free the semaphore.
  24. //
  25. // For optimal performance, the returned Baselib_CappedSemaphore should be stored at a cache aligned memory location.
  26. //
  27. // \returns A struct representing a semaphore instance.
  28. BASELIB_INLINE_API Baselib_CappedSemaphore Baselib_CappedSemaphore_Create(uint16_t cap);
  29. // Creates a capped counting semaphore synchronization primitive in-place with memory provided by caller.
  30. //
  31. // Cap is the number of tokens that can be held by the semaphore when there is no contention.
  32. // If there are not enough system resources to create a semaphore, process abort is triggered.
  33. // Use Baselib_CappedSemaphore_FreeInplace() to free capped semaphore.
  34. //
  35. // For optimal performance, the returned Baselib_CappedSemaphore should be stored at a cache aligned memory location.
  36. BASELIB_INLINE_API void Baselib_CappedSemaphore_CreateInplace(Baselib_CappedSemaphore* semaphodateData, uint16_t cap);
  37. // Try to consume a token and return immediately.
  38. //
  39. // When successful this function is guaranteed to emit an acquire barrier.
  40. //
  41. // \returns true if token was consumed. false if not.
  42. BASELIB_INLINE_API bool Baselib_CappedSemaphore_TryAcquire(Baselib_CappedSemaphore* semaphore);
  43. // Wait for semaphore token to become available
  44. //
  45. // This function is guaranteed to emit an acquire barrier.
  46. BASELIB_INLINE_API void Baselib_CappedSemaphore_Acquire(Baselib_CappedSemaphore* semaphore);
  47. // Wait for semaphore token to become available
  48. //
  49. // When successful this function is guaranteed to emit an acquire barrier.
  50. //
  51. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  52. // while Acquire may enter the kernel and cause a context switch.
  53. //
  54. // Timeout passed to this function may be subject to system clock resolution.
  55. // 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.
  56. //
  57. // \param timeoutInMilliseconds Time to wait for token to become available in milliseconds.
  58. //
  59. // \returns true if token was consumed. false if timeout was reached.
  60. BASELIB_INLINE_API bool Baselib_CappedSemaphore_TryTimedAcquire(Baselib_CappedSemaphore* semaphore, const uint32_t timeoutInMilliseconds);
  61. // Submit tokens to the semaphore.
  62. //
  63. // If threads are waiting an equal amount of tokens are consumed before this function return.
  64. //
  65. // When successful this function is guaranteed to emit a release barrier.
  66. //
  67. // \returns number of submitted tokens.
  68. BASELIB_INLINE_API uint16_t Baselib_CappedSemaphore_Release(Baselib_CappedSemaphore* semaphore, const uint16_t count);
  69. // Sets the semaphore token count to zero and release all waiting threads.
  70. //
  71. // When successful this function is guaranteed to emit a release barrier.
  72. //
  73. // \returns number of released threads.
  74. BASELIB_INLINE_API uint32_t Baselib_CappedSemaphore_ResetAndReleaseWaitingThreads(Baselib_CappedSemaphore* semaphore);
  75. // Reclaim resources and memory held by the semaphore.
  76. //
  77. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  78. // Calling this function with a nullptr result in a no-op.
  79. BASELIB_INLINE_API void Baselib_CappedSemaphore_Free(Baselib_CappedSemaphore* semaphore);
  80. // Reclaim resources and memory held by the semaphore. Caller is responsible for freeing memory pointed to by the semaphore.
  81. //
  82. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  83. // Calling this function with a nullptr result in a no-op.
  84. BASELIB_INLINE_API void Baselib_CappedSemaphore_FreeInplace(Baselib_CappedSemaphore* semaphore);