Baselib_CappedSemaphore.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. //
  24. // For optimal performance, the returned Baselib_CappedSemaphore should be stored at a cache aligned memory location.
  25. //
  26. // \returns A struct representing a semaphore instance. Use Baselib_CappedSemaphore_Free to free the semaphore.
  27. BASELIB_INLINE_API Baselib_CappedSemaphore Baselib_CappedSemaphore_Create(uint16_t cap);
  28. // Try to consume a token and return immediately.
  29. //
  30. // When successful this function is guaranteed to emit an acquire barrier.
  31. //
  32. // \returns true if token was consumed. false if not.
  33. BASELIB_INLINE_API bool Baselib_CappedSemaphore_TryAcquire(Baselib_CappedSemaphore* semaphore);
  34. // Wait for semaphore token to become available
  35. //
  36. // This function is guaranteed to emit an acquire barrier.
  37. BASELIB_INLINE_API void Baselib_CappedSemaphore_Acquire(Baselib_CappedSemaphore* semaphore);
  38. // Wait for semaphore token to become available
  39. //
  40. // When successful this function is guaranteed to emit an acquire barrier.
  41. //
  42. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  43. // while Acquire may enter the kernel and cause a context switch.
  44. //
  45. // Timeout passed to this function may be subject to system clock resolution.
  46. // 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.
  47. //
  48. // \param timeoutInMilliseconds Time to wait for token to become available in milliseconds.
  49. //
  50. // \returns true if token was consumed. false if timeout was reached.
  51. BASELIB_INLINE_API bool Baselib_CappedSemaphore_TryTimedAcquire(Baselib_CappedSemaphore* semaphore, const uint32_t timeoutInMilliseconds);
  52. // Submit tokens to the semaphore.
  53. //
  54. // If threads are waiting an equal amount of tokens are consumed before this function return.
  55. //
  56. // When successful this function is guaranteed to emit a release barrier.
  57. //
  58. // \returns number of submitted tokens.
  59. BASELIB_INLINE_API uint16_t Baselib_CappedSemaphore_Release(Baselib_CappedSemaphore* semaphore, const uint16_t count);
  60. // Sets the semaphore token count to zero and release all waiting threads.
  61. //
  62. // When successful this function is guaranteed to emit a release barrier.
  63. //
  64. // \returns number of released threads.
  65. BASELIB_INLINE_API uint32_t Baselib_CappedSemaphore_ResetAndReleaseWaitingThreads(Baselib_CappedSemaphore* semaphore);
  66. // Reclaim resources and memory held by the semaphore.
  67. //
  68. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  69. // Calling this function with a nullptr result in a no-op.
  70. BASELIB_INLINE_API void Baselib_CappedSemaphore_Free(Baselib_CappedSemaphore* semaphore);