spin_wait_ut.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/threading/spin_wait.h>
  3. #include <library/cpp/yt/threading/spin_wait_hook.h>
  4. #include <thread>
  5. #include <mutex>
  6. namespace NYT::NThreading {
  7. namespace {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. bool SpinWaitSlowPathHookInvoked;
  10. void SpinWaitSlowPathHook(
  11. TCpuDuration cpuDelay,
  12. const TSourceLocation& /*location*/,
  13. ESpinLockActivityKind /*activityKind*/)
  14. {
  15. SpinWaitSlowPathHookInvoked = true;
  16. auto delay = CpuDurationToDuration(cpuDelay);
  17. EXPECT_GE(delay, TDuration::Seconds(1));
  18. EXPECT_LE(delay, TDuration::Seconds(5));
  19. }
  20. TEST(TSpinWaitTest, SlowPathHook)
  21. {
  22. static std::once_flag registerFlag;
  23. std::call_once(
  24. registerFlag,
  25. [] {
  26. RegisterSpinWaitSlowPathHook(SpinWaitSlowPathHook);
  27. });
  28. SpinWaitSlowPathHookInvoked = false;
  29. {
  30. TSpinWait spinWait(__LOCATION__, ESpinLockActivityKind::ReadWrite);
  31. for (int i = 0; i < 1'000'000; ++i) {
  32. spinWait.Wait();
  33. }
  34. }
  35. EXPECT_TRUE(SpinWaitSlowPathHookInvoked);
  36. }
  37. ////////////////////////////////////////////////////////////////////////////////
  38. } // namespace
  39. } // namespace NYT::NThreading