aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/thread_name.cpp
blob: 372e14462e60a1c0484256ecf90f7f54c70e9ad0 (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
#include "thread_name.h"

#include <util/generic/string.h>
#include <util/system/thread.h>

#include <algorithm>
#include <cstring>

namespace NYT {

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

TString TThreadName::ToString() const
{
    // Buffer is zero terminated.
    return Buffer.data();
}

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

// This function uses cached TThread::CurrentThreadName() result
TThreadName GetCurrentThreadName()
{
    static thread_local TThreadName threadName;
    if (threadName.Length == 0) {
        if (auto name = TThread::CurrentThreadName()) {
            auto length = std::min<int>(TThreadName::BufferCapacity - 1, name.length());
            threadName.Length = length;
            ::memcpy(threadName.Buffer.data(), name.data(), length);
        }
    }
    return threadName;
}

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

} // namespace NYT