aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/spin_wait.cpp
blob: 309fdb8b3a4176ff341b2b333b9e6fdf0097efa4 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "spin_wait.h"

#include <util/datetime/base.h>

#include <util/system/compiler.h>

#include <atomic>

namespace NYT::NThreading {

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

static constexpr int SpinIterationCount = 1000;

namespace {

TDuration SuggestSleepDelay(int iteration)
{
    static std::atomic<ui64> Rand;
    auto rand = Rand.load(std::memory_order_relaxed);
    rand = 0x5deece66dLL * rand + 0xb;   // numbers from nrand48()
    Rand.store(rand, std::memory_order_relaxed);

    constexpr ui64 MinDelayUs = 128;

    // Double delay every 8 iterations, up to 16x (2ms).
    if (iteration > 32) {
        iteration = 32;
    }
    ui64 delayUs = MinDelayUs << (iteration / 8);

    // Randomize in delay..2*delay range, for resulting 128us..4ms range.
    delayUs = delayUs | ((delayUs - 1) & rand);
    return TDuration::MicroSeconds(delayUs);
}

} // namespace

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

TSpinWait::TSpinWait(
    const TSourceLocation& location,
    ESpinLockActivityKind activityKind)
    : Location_(location)
    , ActivityKind_(activityKind)
{ }

TSpinWait::~TSpinWait()
{
    if (SlowPathStartInstant_ >= 0) {
        auto cpuDelay = GetCpuInstant() - SlowPathStartInstant_;
        InvokeSpinWaitSlowPathHooks(cpuDelay, Location_, ActivityKind_);
    }
}

void TSpinWait::Wait()
{
    if (Y_LIKELY(SpinIteration_++ < SpinIterationCount)) {
        return;
    }

    SpinIteration_ = 0;

    if (SlowPathStartInstant_ < 0) {
        SlowPathStartInstant_ = GetCpuInstant();
    }

    Sleep(SuggestSleepDelay(SleepIteration_++));
}

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

} // namespace NYT::NThreading