summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/cpu_id-inl.h
blob: e336ba5a2cea5e3a278cf7abdf7f29665afc2145 (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
#ifndef CPU_ID_INL_H_
#error "Direct inclusion of this file is not allowed, include cpu_id.h"
// For the sake of sane code completion.
#include "cpu_id.h"
#endif
#undef CPU_ID_INL_H_

#ifdef __linux__
#include <library/cpp/yt/rseq/rseq.h>
#endif

#include <util/system/compiler.h>

namespace NYT {

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

namespace NDetail {

int GetCurrentCpuIdSlow();

} // namespace NDetail

Y_FORCE_INLINE int GetCurrentCpuId()
{
#ifdef YT_RSEQ_AVAILABLE
    // Branch-free read of the rseq cpu_id: the offset always points at a readable
    // field. A data-dependent branch on the offset here would defeat load pipelining.
    auto cpuId = NRseq::ReadField<int>(NRseq::CpuIdFieldOffset);
    // Negative means this thread is not registered yet or rseq is unavailable.
    if (cpuId < 0) [[unlikely]] {
        return NDetail::GetCurrentCpuIdSlow();
    }
    return cpuId;
#else
    return NDetail::GetCurrentCpuIdSlow();
#endif
}

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

} // namespace NYT