traceless_guard-inl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #ifndef TRACELESS_GUARD_INL_H_
  3. #error "Direct inclusion of this file is not allowed, include traceless_guard.h"
  4. #endif
  5. #undef TRACELESS_GUARD_INL_H_
  6. #include "rw_spin_lock.h"
  7. #include "spin_lock_count.h"
  8. #include <util/system/guard.h>
  9. #include <concepts>
  10. namespace NYT::NThreading::NPrivate {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. #ifdef NDEBUG
  13. template <class T, class TOps = TCommonLockOps<T>>
  14. using TTracelessGuard = TGuard<T, TOps>;
  15. template <class T, class TOps = TCommonLockOps<T>>
  16. using TTracelessInverseGuard = TInverseGuard<T, TOps>;
  17. template <class T, class TOps = TTryLockOps<T>>
  18. using TTracelessTryGuard = TTryGuard<T, TOps>;
  19. template <class T>
  20. using TTracelessReaderGuard = TReaderGuard<T>;
  21. template <class T>
  22. using TTracelessWriterGuard = TWriterGuard<T>;
  23. #else
  24. template <class TOps>
  25. struct TTracelessOps
  26. : public TOps
  27. {
  28. template <class T>
  29. static void Acquire(T* t) noexcept
  30. {
  31. TOps::Acquire(t);
  32. NDetail::RecordSpinLockReleased();
  33. }
  34. template <class T>
  35. static void Release(T* t) noexcept
  36. {
  37. NDetail::RecordSpinLockAcquired();
  38. TOps::Release(t);
  39. }
  40. template <class T>
  41. requires requires (T* t) { { TOps::TryAcquire(t) } -> std::same_as<bool>; }
  42. static bool TryAcquire(T* t) noexcept
  43. {
  44. bool isAcquired = TOps::TryAcquire(t);
  45. if (isAcquired) {
  46. NDetail::RecordSpinLockReleased();
  47. }
  48. return isAcquired;
  49. }
  50. };
  51. template <CTrackedSpinLock T, class TOps = TCommonLockOps<T>>
  52. using TTracelessGuard = TGuard<T, TTracelessOps<TOps>>;
  53. template <CTrackedSpinLock T, class TOps = TCommonLockOps<T>>
  54. using TTracelessInverseGuard = TInverseGuard<T, TTracelessOps<TOps>>;
  55. template <CTrackedSpinLock T, class TOps = TTryLockOps<T>>
  56. using TTracelessTryGuard = TTryGuard<T, TTracelessOps<TOps>>;
  57. template <CTrackedSpinLock T>
  58. using TTracelessReaderGuard = TGuard<T, TTracelessOps<TReaderSpinlockTraits<T>>>;
  59. template <CTrackedSpinLock T>
  60. using TTracelessWriterGuard = TGuard<T, TTracelessOps<TWriterSpinlockTraits<T>>>;
  61. #endif
  62. ////////////////////////////////////////////////////////////////////////////////
  63. template <CTrackedSpinLock T>
  64. TTracelessGuard<T> TracelessGuard(const T& mutex)
  65. {
  66. return {&mutex};
  67. }
  68. template <CTrackedSpinLock T>
  69. TTracelessTryGuard<T> TracelessTryGuard(const T& mutex)
  70. {
  71. return {&mutex};
  72. }
  73. template <CTrackedSpinLock T>
  74. TTracelessReaderGuard<T> TracelessReaderGuard(const T& mutex)
  75. {
  76. return {&mutex};
  77. }
  78. template <CTrackedSpinLock T>
  79. TTracelessWriterGuard<T> TracelessWriterGuard(const T& mutex)
  80. {
  81. return {&mutex};
  82. }
  83. ////////////////////////////////////////////////////////////////////////////////
  84. } // namespace NYT::NThreading::NPrivate