Baselib_HighCapacitySemaphore.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. // Baselib_HighCapacitySemaphore
  3. // This semaphore is similar to Baselib_Semaphore but allows for a far greater token count for a price of a bit slower performance.
  4. // This semaphore is usable for counting resources.
  5. // This is the max number of tokens guaranteed to be held by the semaphore at
  6. // any given point in time. Tokens submitted that exceed this value may silently be discarded.
  7. static const int64_t Baselib_HighCapacitySemaphore_MaxGuaranteedCount = UINT64_C(1) << 61;
  8. #if PLATFORM_FUTEX_NATIVE_SUPPORT
  9. #include "Internal/Baselib_HighCapacitySemaphore_FutexBased.inl.h"
  10. #else
  11. #include "Internal/Baselib_HighCapacitySemaphore_SemaphoreBased.inl.h"
  12. #endif
  13. // Creates a counting semaphore synchronization primitive.
  14. //
  15. // If there are not enough system resources to create a semaphore, process abort is triggered.
  16. //
  17. // For optimal performance, the returned Baselib_HighCapacitySemaphore should be stored at a cache aligned memory location.
  18. //
  19. // \returns A struct representing a semaphore instance. Use Baselib_HighCapacitySemaphore_Free to free the semaphore.
  20. BASELIB_INLINE_API Baselib_HighCapacitySemaphore Baselib_HighCapacitySemaphore_Create(void);
  21. // Wait for semaphore token to become available
  22. //
  23. // This function is guaranteed to emit an acquire barrier.
  24. // Returns if token was consumed or was woken up by Baselib_HighCapacitySemaphore_ResetAndReleaseWaitingThreads.
  25. BASELIB_INLINE_API void Baselib_HighCapacitySemaphore_Acquire(Baselib_HighCapacitySemaphore* semaphore);
  26. // Try to consume a token and return immediately.
  27. //
  28. // When successful this function is guaranteed to emit an acquire barrier.
  29. //
  30. // \returns true if token was consumed. false if not.
  31. BASELIB_INLINE_API bool Baselib_HighCapacitySemaphore_TryAcquire(Baselib_HighCapacitySemaphore* semaphore);
  32. // Wait for semaphore token to become available
  33. //
  34. // When successful this function is guaranteed to emit an acquire barrier.
  35. //
  36. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  37. // while Acquire may enter the kernel and cause a context switch.
  38. //
  39. // Timeout passed to this function may be subject to system clock resolution.
  40. // 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.
  41. //
  42. // \param timeout Time to wait for token to become available.
  43. //
  44. // \returns true if token was consumed or was woken up by Baselib_HighCapacitySemaphore_ResetAndReleaseWaitingThreads. false if timeout was reached.
  45. BASELIB_INLINE_API bool Baselib_HighCapacitySemaphore_TryTimedAcquire(Baselib_HighCapacitySemaphore* semaphore, const uint32_t timeoutInMilliseconds);
  46. // Submit tokens to the semaphore.
  47. //
  48. // When successful this function is guaranteed to emit a release barrier.
  49. //
  50. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  51. // If count reach `Baselib_HighCapacitySemaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  52. BASELIB_INLINE_API void Baselib_HighCapacitySemaphore_Release(Baselib_HighCapacitySemaphore* semaphore, const uint32_t count);
  53. // If threads are waiting on Baselib_HighCapacitySemaphore_Acquire / Baselib_HighCapacitySemaphore_TryTimedAcquire,
  54. // releases enough tokens to wake them up. Otherwise consumes all available tokens.
  55. //
  56. // When successful this function is guaranteed to emit a release barrier.
  57. //
  58. // \returns number of released threads.
  59. BASELIB_INLINE_API uint64_t Baselib_HighCapacitySemaphore_ResetAndReleaseWaitingThreads(Baselib_HighCapacitySemaphore* semaphore);
  60. // Reclaim resources and memory held by the semaphore.
  61. //
  62. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  63. // Calling this function with a nullptr result in a no-op
  64. BASELIB_INLINE_API void Baselib_HighCapacitySemaphore_Free(Baselib_HighCapacitySemaphore* semaphore);