Baselib_Semaphore.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. //
  24. // If there are not enough system resources to create a semaphore, process abort is triggered.
  25. //
  26. // For optimal performance, the returned Baselib_Semaphore should be stored at a cache aligned memory location.
  27. //
  28. // \returns A struct representing a semaphore instance. Use Baselib_Semaphore_Free to free the semaphore.
  29. BASELIB_INLINE_API Baselib_Semaphore Baselib_Semaphore_Create(void);
  30. // Wait for semaphore token to become available
  31. //
  32. // This function is guaranteed to emit an acquire barrier.
  33. // Returns if token was consumed or was woken up by Baselib_Semaphore_ResetAndReleaseWaitingThreads.
  34. BASELIB_INLINE_API void Baselib_Semaphore_Acquire(Baselib_Semaphore* semaphore);
  35. // Try to consume a token and return immediately.
  36. //
  37. // When successful this function is guaranteed to emit an acquire barrier.
  38. //
  39. // \returns true if token was consumed. false if not.
  40. BASELIB_INLINE_API bool Baselib_Semaphore_TryAcquire(Baselib_Semaphore* semaphore);
  41. // Wait for semaphore token to become available
  42. //
  43. // When successful this function is guaranteed to emit an acquire barrier.
  44. //
  45. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  46. // while Acquire may enter the kernel and cause a context switch.
  47. //
  48. // Timeout passed to this function may be subject to system clock resolution.
  49. // 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.
  50. //
  51. // \param timeout Time to wait for token to become available.
  52. //
  53. // \returns true if token was consumed or was woken up by Baselib_Semaphore_ResetAndReleaseWaitingThreads. false if timeout was reached.
  54. BASELIB_INLINE_API bool Baselib_Semaphore_TryTimedAcquire(Baselib_Semaphore* semaphore, const uint32_t timeoutInMilliseconds);
  55. // Submit tokens to the semaphore.
  56. //
  57. // When successful this function is guaranteed to emit a release barrier.
  58. //
  59. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  60. // If count reach `Baselib_Semaphore_MaxGuaranteedCount` this function may silently discard any overflow.
  61. BASELIB_INLINE_API void Baselib_Semaphore_Release(Baselib_Semaphore* semaphore, const uint16_t count);
  62. // If threads are waiting on Baselib_Semaphore_Acquire / Baselib_Semaphore_TryTimedAcquire,
  63. // releases enough tokens to wake them up. Otherwise consumes all available tokens.
  64. //
  65. // When successful this function is guaranteed to emit a release barrier.
  66. //
  67. // \returns number of released threads.
  68. BASELIB_INLINE_API uint32_t Baselib_Semaphore_ResetAndReleaseWaitingThreads(Baselib_Semaphore* semaphore);
  69. // Reclaim resources and memory held by the semaphore.
  70. //
  71. // If threads are waiting on the semaphore, calling free will trigger an assert and may cause process abort.
  72. // Calling this function with a nullptr result in a no-op
  73. BASELIB_INLINE_API void Baselib_Semaphore_Free(Baselib_Semaphore* semaphore);