Skip to content

Commit

Permalink
feat: Cache result of gettid syscall
Browse files Browse the repository at this point in the history
SDB-8486
bnbajwa committed Jan 12, 2025
1 parent de279c3 commit aec96b5
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion toolbox/sys/Logger.cpp
Original file line number Diff line number Diff line change
@@ -38,7 +38,21 @@ const char* Labels[] = {"NONE", "CRIT", "ERROR", "WARN", "METRIC", "NOTICE", "IN
#if defined(__linux__)
inline pid_t gettid()
{
return syscall(SYS_gettid);
struct S {
pid_t tid = {};
bool init_done = false;
};

thread_local S s{};

// 'tid' cannot be set during initialisation of struct S -- because
// the C++ standard doesn't guarantee which thread initialises it.
if (!s.init_done) [[unlikely]] {
s.tid = syscall(SYS_gettid);
s.init_done = true;
}

return s.tid;
}
#else
inline pid_t gettid()

0 comments on commit aec96b5

Please sign in to comment.