Baselib_EventSemaphore.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. // Baselib_EventSemaphore
  3. // In computer science, an event (also called event semaphore) is a type of synchronization mechanism that is used to indicate to waiting processes when a
  4. // particular condition has become true.
  5. // An event is an abstract data type with a boolean state and the following operations:
  6. // * wait - when executed, causes the suspension of the executing process until the state of the event is set to true. If the state is already set to true has no effect.
  7. // * set - sets the event's state to true, release all waiting processes.
  8. // * clear - sets the event's state to false.
  9. //
  10. // "Event (synchronization primitive)", Wikipedia: The Free Encyclopedia
  11. // https://en.wikipedia.org/w/index.php?title=Event_(synchronization_primitive)&oldid=781517732
  12. #if PLATFORM_FUTEX_NATIVE_SUPPORT
  13. #include "Internal/Baselib_EventSemaphore_FutexBased.inl.h"
  14. #else
  15. #include "Internal/Baselib_EventSemaphore_SemaphoreBased.inl.h"
  16. #endif
  17. // Creates an event semaphore synchronization primitive. Initial state of event is unset.
  18. //
  19. // If there are not enough system resources to create a semaphore, process abort is triggered.
  20. //
  21. // For optimal performance, the returned Baselib_EventSemaphore should be stored at a cache aligned memory location.
  22. //
  23. // \returns A struct representing a semaphore instance. Use Baselib_EventSemaphore_Free to free the semaphore.
  24. BASELIB_INLINE_API Baselib_EventSemaphore Baselib_EventSemaphore_Create(void);
  25. // Try to acquire semaphore.
  26. //
  27. // When semaphore is acquired this function is guaranteed to emit an acquire barrier.
  28. //
  29. // \returns true if event is set, false other wise.
  30. COMPILER_WARN_UNUSED_RESULT
  31. BASELIB_INLINE_API bool Baselib_EventSemaphore_TryAcquire(Baselib_EventSemaphore* semaphore);
  32. // Acquire semaphore.
  33. //
  34. // This function is guaranteed to emit an acquire barrier.
  35. BASELIB_INLINE_API void Baselib_EventSemaphore_Acquire(Baselib_EventSemaphore* semaphore);
  36. // Try to acquire semaphore.
  37. //
  38. // If event is set this function return true, otherwise the thread will wait for event to be set or for release to be called.
  39. //
  40. // When semaphore is acquired 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. // \returns true if semaphore was acquired.
  49. COMPILER_WARN_UNUSED_RESULT
  50. BASELIB_INLINE_API bool Baselib_EventSemaphore_TryTimedAcquire(Baselib_EventSemaphore* semaphore, const uint32_t timeoutInMilliseconds);
  51. // Sets the event
  52. //
  53. // Setting the event will cause all waiting threads to wakeup. And will let all future acquiring threads through until Baselib_EventSemaphore_Reset is called.
  54. // It is guaranteed that any thread waiting previously on the EventSemaphore will be woken up, even if the semaphore is immediately reset. (no lock stealing)
  55. //
  56. // Guaranteed to emit a release barrier.
  57. BASELIB_INLINE_API void Baselib_EventSemaphore_Set(Baselib_EventSemaphore* semaphore);
  58. // Reset event
  59. //
  60. // Resetting the event will cause all future acquiring threads to enter a wait state.
  61. // Has no effect if the EventSemaphore is already in a reset state.
  62. //
  63. // Guaranteed to emit a release barrier.
  64. BASELIB_INLINE_API void Baselib_EventSemaphore_Reset(Baselib_EventSemaphore* semaphore);
  65. // Reset event and release all waiting threads
  66. //
  67. // Resetting the event will cause all future acquiring threads to enter a wait state.
  68. // If there were any threads waiting (i.e. the EventSemaphore was already in a release state) they will be released.
  69. //
  70. // Guaranteed to emit a release barrier.
  71. BASELIB_INLINE_API void Baselib_EventSemaphore_ResetAndReleaseWaitingThreads(Baselib_EventSemaphore* semaphore);
  72. // Reclaim resources and memory held by the semaphore.
  73. //
  74. // If threads are waiting on the semaphore, calling free may trigger an assert and may cause process abort.
  75. // Calling this function with a nullptr result in a no-op
  76. BASELIB_INLINE_API void Baselib_EventSemaphore_Free(Baselib_EventSemaphore* semaphore);