summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/cpu_id.h
blob: 751183479b8ffb1c5ebb0b68ac57ed4f9b189b28 (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
#pragma once

namespace NYT {

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

/*!
 * Index of the CPU the calling thread currently runs on, in [0, NumberOfCpus).
 * On Linux/x86-64 and Linux/arm64 the fast path is a single thread-local read of
 * the kernel's rseq area; the first call per thread lazily registers rseq, and
 * reads fall back to sched_getcpu() when rseq is unavailable. On other platforms
 * it is sched_getcpu() (Linux) or 0.
 * It is a hint (the thread may migrate right after) -- for sharding, not correctness.
 *
 * The rseq backend lives in library/cpp/yt/rseq, with no third-party dependency.
 *
 * Fiber-TLS: the fast path is inlined and reads the thread pointer, so it must be
 * reached only through a non-inlinable, fiber-switch-free frame (a virtual call or
 * YT_PREVENT_TLS_CACHING; see library/cpp/yt/misc/tls.h). Otherwise the thread base
 * may be cached and reused after the fiber resumes on another OS thread, reading a
 * different -- possibly already-freed -- thread's area.
 */
int GetCurrentCpuId();

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

} // namespace NYT

#define CPU_ID_INL_H_
#include "cpu_id-inl.h"
#undef CPU_ID_INL_H_