spin_wait_hook.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "spin_wait_hook.h"
  2. #include <library/cpp/yt/assert/assert.h>
  3. #include <array>
  4. #include <atomic>
  5. namespace NYT::NThreading {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. static constexpr int MaxSpinWaitSlowPathHooks = 8;
  8. static std::array<std::atomic<TSpinWaitSlowPathHook>, MaxSpinWaitSlowPathHooks> SpinWaitSlowPathHooks;
  9. static std::atomic<int> SpinWaitSlowPathHookCount;
  10. void RegisterSpinWaitSlowPathHook(TSpinWaitSlowPathHook hook)
  11. {
  12. int index = SpinWaitSlowPathHookCount++;
  13. YT_VERIFY(index < MaxSpinWaitSlowPathHooks);
  14. SpinWaitSlowPathHooks[index].store(hook);
  15. }
  16. void InvokeSpinWaitSlowPathHooks(
  17. TCpuDuration cpuDelay,
  18. const ::TSourceLocation& location,
  19. ESpinLockActivityKind activityKind)
  20. {
  21. for (const auto& atomicHook : SpinWaitSlowPathHooks) {
  22. if (auto hook = atomicHook.load()) {
  23. hook(cpuDelay, location, activityKind);
  24. }
  25. }
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////
  28. } // namespace NYT::NThreading