Baselib_ReentrantLock.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. // Baselib_ReentrantLock
  3. // In computer science, the reentrant mutex (recursive mutex, recursive lock) is particular type of mutual exclusion (mutex) device that may be locked multiple
  4. // times by the same process/thread, without causing a deadlock.
  5. // While any attempt to perform the "lock" operation on an ordinary mutex (lock) would either fail or block when the mutex is already locked, on a recursive
  6. // mutex this operation will succeed if and only if the locking thread is the one that already holds the lock. Typically, a recursive mutex tracks the number
  7. // of times it has been locked, and requires equally many unlock operations to be performed before other threads may lock it.
  8. //
  9. // "Reentrant mutex", Wikipedia: The Free Encyclopedia
  10. // https://en.wikipedia.org/w/index.php?title=Reentrant_mutex&oldid=818566928
  11. #include "Internal/Baselib_ReentrantLock.inl.h"
  12. // Creates a reentrant 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_ReentrantLock should be stored at a cache aligned memory location.
  17. //
  18. // \returns A struct representing a lock instance. Use Baselib_ReentrantLock_Free to free the lock.
  19. BASELIB_INLINE_API Baselib_ReentrantLock Baselib_ReentrantLock_Create(void);
  20. // Try to acquire lock and return immediately.
  21. // If lock is already acquired by the current thread this function increase the lock count so that an equal number of calls to Baselib_ReentrantLock_Release needs
  22. // to be made before the lock is released.
  23. //
  24. // When lock is acquired 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_ReentrantLock_TryAcquire(Baselib_ReentrantLock* lock);
  29. // Acquire lock.
  30. //
  31. // If lock is already acquired by the current thread this function increase the lock count so that an equal number of calls to Baselib_ReentrantLock_Release needs
  32. // to be made before the lock is released.
  33. // If lock is held by another thread, this function wait for lock to be released.
  34. //
  35. // This function is guaranteed to emit an acquire barrier.
  36. BASELIB_INLINE_API void Baselib_ReentrantLock_Acquire(Baselib_ReentrantLock* lock);
  37. // Acquire lock.
  38. // If lock is already acquired by the current thread this function increase the lock count so that an equal number of calls to Baselib_ReentrantLock_Release needs
  39. // to be made before the lock is released.
  40. // If lock is held by another thread, this function wait for timeoutInMilliseconds for lock to be released.
  41. //
  42. // When a lock is acquired this function is guaranteed to emit an acquire barrier.
  43. //
  44. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  45. // while Acquire may enter the kernel and cause a context switch.
  46. //
  47. // Timeout passed to this function may be subject to system clock resolution.
  48. // 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.
  49. //
  50. // \returns true if lock was acquired.
  51. COMPILER_WARN_UNUSED_RESULT
  52. BASELIB_INLINE_API bool Baselib_ReentrantLock_TryTimedAcquire(Baselib_ReentrantLock* lock, uint32_t timeoutInMilliseconds);
  53. // Release lock.
  54. // If lock count is still higher than zero after the release operation then lock remain in a locked state.
  55. // If lock count reach zero the lock is unlocked and made available to other threads
  56. //
  57. // When the lock is released this function is guaranteed to emit a release barrier.
  58. //
  59. // Calling this function from a thread that doesn't own the lock result triggers an assert in debug and causes undefined behavior in release builds.
  60. BASELIB_INLINE_API void Baselib_ReentrantLock_Release(Baselib_ReentrantLock* lock);
  61. // Reclaim resources and memory held by lock.
  62. //
  63. // If threads are waiting on the lock, calling free may trigger an assert and may cause process abort.
  64. // Calling this function with a nullptr result in a no-op
  65. BASELIB_INLINE_API void Baselib_ReentrantLock_Free(Baselib_ReentrantLock* lock);