Baselib_ReentrantLock.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // Use Baselib_ReentrantLock_Free to free the lock
  15. // If there are not enough system resources to create a lock, process abort is triggered.
  16. //
  17. // For optimal performance, the returned Baselib_ReentrantLock should be stored at a cache aligned memory location.
  18. //
  19. // \returns A struct representing a lock instance. Use Baselib_ReentrantLock_Free to free the lock.
  20. BASELIB_INLINE_API Baselib_ReentrantLock Baselib_ReentrantLock_Create(void);
  21. // Creates a reentrant lock synchronization primitive in-place with memory provided by caller.
  22. //
  23. // Use Baselib_ReentrantLock_FreeInplace to free the lock
  24. // If there are not enough system resources to create a lock, process abort is triggered.
  25. //
  26. // For optimal performance, the Baselib_ReentrantLock should be stored at a cache aligned memory location.
  27. BASELIB_INLINE_API void Baselib_ReentrantLock_CreateInplace(Baselib_ReentrantLock* lockData);
  28. // Try to acquire lock and return immediately.
  29. // 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
  30. // to be made before the lock is released.
  31. //
  32. // When lock is acquired this function is guaranteed to emit an acquire barrier.
  33. //
  34. // \returns true if lock was acquired.
  35. COMPILER_WARN_UNUSED_RESULT
  36. BASELIB_INLINE_API bool Baselib_ReentrantLock_TryAcquire(Baselib_ReentrantLock* lock);
  37. // Acquire lock.
  38. //
  39. // 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
  40. // to be made before the lock is released.
  41. // If lock is held by another thread, this function wait for lock to be released.
  42. //
  43. // This function is guaranteed to emit an acquire barrier.
  44. BASELIB_INLINE_API void Baselib_ReentrantLock_Acquire(Baselib_ReentrantLock* lock);
  45. // Acquire lock.
  46. // 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
  47. // to be made before the lock is released.
  48. // If lock is held by another thread, this function wait for timeoutInMilliseconds for lock to be released.
  49. //
  50. // When a lock is acquired this function is guaranteed to emit an acquire barrier.
  51. //
  52. // Acquire with a zero timeout differs from TryAcquire in that TryAcquire is guaranteed to be a user space operation
  53. // while Acquire may enter the kernel and cause a context switch.
  54. //
  55. // Timeout passed to this function may be subject to system clock resolution.
  56. // 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.
  57. //
  58. // \returns true if lock was acquired.
  59. COMPILER_WARN_UNUSED_RESULT
  60. BASELIB_INLINE_API bool Baselib_ReentrantLock_TryTimedAcquire(Baselib_ReentrantLock* lock, uint32_t timeoutInMilliseconds);
  61. // Release lock.
  62. // If lock count is still higher than zero after the release operation then lock remain in a locked state.
  63. // If lock count reach zero the lock is unlocked and made available to other threads
  64. //
  65. // When the lock is released this function is guaranteed to emit a release barrier.
  66. //
  67. // 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.
  68. BASELIB_INLINE_API void Baselib_ReentrantLock_Release(Baselib_ReentrantLock* lock);
  69. // Reclaim resources and memory held 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_ReentrantLock_Free(Baselib_ReentrantLock* lock);
  74. // Reclaim resources and memory held by lock. Caller is responsible for freeing memory pointed to by lock.
  75. //
  76. // If threads are waiting on the lock, calling free may trigger an assert and may cause process abort.
  77. // Calling this function with a nullptr result in a no-op
  78. BASELIB_INLINE_API void Baselib_ReentrantLock_FreeInplace(Baselib_ReentrantLock* lock);