Baselib_Lock.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // Creates a lock synchronization primitive in-place with memory provided by caller.
  21. // Use Baselib_Lock_FreeInplace to free the lock
  22. //
  23. // If there are not enough system resources to create a lock, process abort is triggered.
  24. //
  25. // For optimal performance, the Baselib_Lock should be stored at a cache aligned memory location.
  26. BASELIB_INLINE_API void Baselib_Lock_CreateInplace(Baselib_Lock* lockData);
  27. // Try to acquire lock and return immediately.
  28. //
  29. // If lock is held, either by this or another thread, then lock is not acquired and function return false.
  30. //
  31. // If successful this function is guaranteed to emit an acquire barrier.
  32. //
  33. // \returns true if lock was acquired.
  34. COMPILER_WARN_UNUSED_RESULT
  35. BASELIB_INLINE_API bool Baselib_Lock_TryAcquire(Baselib_Lock* lock);
  36. // Acquire lock.
  37. //
  38. // If lock is held, either by this or another thread, then the function wait for lock to be released.
  39. //
  40. // This function is guaranteed to emit an acquire barrier.
  41. BASELIB_INLINE_API void Baselib_Lock_Acquire(Baselib_Lock* lock);
  42. // Try to acquire lock.
  43. //
  44. // If lock is held, either by this or another thread, then the function wait for timeoutInMilliseconds for lock to be released.
  45. //
  46. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  47. // while Acquire may enter the kernel and cause a context switch.
  48. //
  49. // When a lock is acquired this function is guaranteed to emit an acquire barrier.
  50. //
  51. // Timeout passed to this function may be subject to system clock resolution.
  52. // 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.
  53. //
  54. // \returns true if lock was acquired.
  55. COMPILER_WARN_UNUSED_RESULT
  56. BASELIB_INLINE_API bool Baselib_Lock_TryTimedAcquire(Baselib_Lock* lock, uint32_t timeoutInMilliseconds);
  57. // Release lock and make it available to other threads.
  58. //
  59. // This function can be called from any thread, not only the thread that acquired the lock.
  60. // If no lock was previously held calling this function result in a no-op.
  61. //
  62. // When the lock is released this function is guaranteed to emit a release barrier.
  63. BASELIB_INLINE_API void Baselib_Lock_Release(Baselib_Lock* lock);
  64. // Reclaim resources and memory held by lock.
  65. //
  66. // If threads are waiting on the lock, calling free may trigger an assert and may cause process abort.
  67. // Calling this function with a nullptr result in a no-op
  68. BASELIB_INLINE_API void Baselib_Lock_Free(Baselib_Lock* lock);
  69. // Reclaim resources and memory held by lock. Caller is responsible for freeing memory pointed to by lock.
  70. //
  71. // If threads are waiting on the lock, calling free may 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_Lock_FreeInplace(Baselib_Lock* lock);