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

#include <library/cpp/yt/misc/tls.h>

#include <atomic>

#include <util/system/compiler.h>

namespace NYT {

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

YT_DECLARE_THREAD_LOCAL(TSequentialThreadId, CachedSequentialThreadId);
extern std::atomic<TSequentialThreadId> SequentialThreadIdGenerator;

inline TSequentialThreadId GetSequentialThreadId()
{
    auto& cachedSequentialThreadId = CachedSequentialThreadId();
    if (Y_UNLIKELY(cachedSequentialThreadId == InvalidSequentialThreadId)) {
        cachedSequentialThreadId = ++SequentialThreadIdGenerator;
    }
    return cachedSequentialThreadId;
}

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

} // namespace NYT