Baselib_Lock.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #pragma once
  2. // In computer science, a lock or mutex (from mutual exclusion) is a synchronization mechanism for enforcing limits on access to a resource in an environment
  3. // where there are many threads of execution. A lock is designed to enforce a mutual exclusion concurrency control policy.
  4. //
  5. // "Lock (computer science)", Wikipedia: The Free Encyclopedia
  6. // https://en.wikipedia.org/w/index.php?title=Lock_(computer_science)&oldid=875674239
  7. #if PLATFORM_FUTEX_NATIVE_SUPPORT
  8. #include "Internal/Baselib_Lock_FutexBased.inl.h"
  9. #else
  10. #include "Internal/Baselib_Lock_SemaphoreBased.inl.h"
  11. #endif
  12. // Creates a lock synchronization primitive.
  13. //
  14. // If there are not enough system resources to create a lock, process abort is triggered.
  15. //
  16. // For optimal performance, the returned Baselib_Lock should be stored at a cache aligned memory location.
  17. //
  18. // \returns A struct representing a lock instance. Use Baselib_Lock_Free to free the lock.
  19. BASELIB_INLINE_API Baselib_Lock Baselib_Lock_Create(void);
  20. // Try to acquire lock and return immediately.
  21. //
  22. // If lock is held, either by this or another thread, then lock is not acquired and function return false.
  23. //
  24. // If successful this function is guaranteed to emit an acquire barrier.
  25. //
  26. // \returns true if lock was acquired.
  27. COMPILER_WARN_UNUSED_RESULT
  28. BASELIB_INLINE_API bool Baselib_Lock_TryAcquire(Baselib_Lock* lock);
  29. // Acquire lock.
  30. //
  31. // If lock is held, either by this or another thread, then the function wait for lock to be released.
  32. //
  33. // This function is guaranteed to emit an acquire barrier.
  34. BASELIB_INLINE_API void Baselib_Lock_Acquire(Baselib_Lock* lock);
  35. // Try to acquire lock.
  36. //
  37. // If lock is held, either by this or another thread, then the function wait for timeoutInMilliseconds for lock to be released.
  38. //
  39. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  40. // while Acquire may enter the kernel and cause a context switch.
  41. //
  42. // When a lock is acquired this function is guaranteed to emit an acquire barrier.
  43. //
  44. // Timeout passed to this function may be subject to system clock resolution.
  45. // 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.
  46. //
  47. // \returns true if lock was acquired.
  48. COMPILER_WARN_UNUSED_RESULT
  49. BASELIB_INLINE_API bool Baselib_Lock_TryTimedAcquire(Baselib_Lock* lock, uint32_t timeoutInMilliseconds);
  50. // Release lock and make it available to other threads.
  51. //
  52. // This function can be called from any thread, not only the thread that acquired the lock.
  53. // If no lock was previously held calling this function result in a no-op.
  54. //
  55. // When the lock is released this function is guaranteed to emit a release barrier.
  56. BASELIB_INLINE_API void Baselib_Lock_Release(Baselib_Lock* lock);
  57. // Reclaim resources and memory held by lock.
  58. //
  59. // If threads are waiting on the lock, calling free may trigger an assert and may cause process abort.
  60. // Calling this function with a nullptr result in a no-op
  61. BASELIB_INLINE_API void Baselib_Lock_Free(Baselib_Lock* lock);