Baselib_SystemSemaphore.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. // Baselib_SystemSemaphore
  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. #ifdef __cplusplus
  15. BASELIB_C_INTERFACE
  16. {
  17. #endif
  18. typedef struct Baselib_SystemSemaphore_Handle { void* handle; } Baselib_SystemSemaphore_Handle;
  19. // This is the maximum number of tokens that can be made available on a semaphore
  20. enum { Baselib_SystemSemaphore_MaxCount = INT32_MAX };
  21. // Creates a counting semaphore synchronization primitive.
  22. //
  23. // If there are not enough system resources to create a semaphore, process abort is triggered.
  24. //
  25. // \returns A handle to a semaphore instance. Use Baselib_SystemSemaphore_Free to free the semaphore.
  26. BASELIB_API Baselib_SystemSemaphore_Handle Baselib_SystemSemaphore_Create(void);
  27. // Wait for semaphore token to become available
  28. //
  29. BASELIB_API void Baselib_SystemSemaphore_Acquire(Baselib_SystemSemaphore_Handle semaphore);
  30. // Try to consume a token and return immediately.
  31. //
  32. // \returns true if token was consumed. false if not.
  33. BASELIB_API bool Baselib_SystemSemaphore_TryAcquire(Baselib_SystemSemaphore_Handle semaphore);
  34. // Wait for semaphore token to become available
  35. //
  36. // Timeout passed to this function may be subject to system clock resolution.
  37. // 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.
  38. //
  39. // \param timeout Time to wait for token to become available.
  40. //
  41. // \returns true if token was consumed. false if timeout was reached.
  42. BASELIB_API bool Baselib_SystemSemaphore_TryTimedAcquire(Baselib_SystemSemaphore_Handle semaphore, uint32_t timeoutInMilliseconds);
  43. // Submit tokens to the semaphore.
  44. //
  45. // Increase the number of available tokens on the semaphore by `count`. Any waiting threads will be notified there are new tokens available.
  46. // If count reach `Baselib_SystemSemaphore_MaxCount` this function silently discard any overflow.
  47. // Note that hitting max count may inflict a heavy performance penalty.
  48. BASELIB_API void Baselib_SystemSemaphore_Release(Baselib_SystemSemaphore_Handle semaphore, uint32_t count);
  49. // Reclaim resources and memory held by the semaphore.
  50. //
  51. // If threads are waiting on the semaphore, calling free may cause process abort.
  52. BASELIB_API void Baselib_SystemSemaphore_Free(Baselib_SystemSemaphore_Handle semaphore);
  53. #ifdef __cplusplus
  54. } // BASELIB_C_INTERFACE
  55. #endif