aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/unittests/spin_wait_ut.cpp
blob: 8469634f3463936122a4776bc55c41f930363df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/yt/threading/spin_wait.h>
#include <library/cpp/yt/threading/spin_wait_hook.h>

#include <thread>
#include <mutex>

namespace NYT::NThreading {
namespace {

////////////////////////////////////////////////////////////////////////////////

bool SpinWaitSlowPathHookInvoked;

void SpinWaitSlowPathHook(
    TCpuDuration cpuDelay,
    const TSourceLocation& /*location*/,
    ESpinLockActivityKind /*activityKind*/)
{
    SpinWaitSlowPathHookInvoked = true;
    auto delay = CpuDurationToDuration(cpuDelay);
    EXPECT_GE(delay, TDuration::Seconds(1));
    EXPECT_LE(delay, TDuration::Seconds(5));
}

TEST(TSpinWaitTest, SlowPathHook)
{
    static std::once_flag registerFlag;
    std::call_once(
        registerFlag,
        [] {
            RegisterSpinWaitSlowPathHook(SpinWaitSlowPathHook);
        });
    SpinWaitSlowPathHookInvoked = false;
    {
        TSpinWait spinWait(__LOCATION__, ESpinLockActivityKind::ReadWrite);
        for (int i = 0; i < 1'000'000; ++i) {
            spinWait.Wait();
        }
    }
    EXPECT_TRUE(SpinWaitSlowPathHookInvoked);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace
} // namespace NYT::NThreading