aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorlukyan <lukyan@yandex-team.com>2023-12-05 19:07:41 +0300
committerlukyan <lukyan@yandex-team.com>2023-12-05 20:50:57 +0300
commitd5b84ed65f77eab0135b9d0ee025556f4507b62b (patch)
tree5c5db90ad950923a5554c4a0f5fac239b3f44c53
parentddadddf4718f23838ed4ffb8e6aa1f126301cf55 (diff)
downloadydb-d5b84ed65f77eab0135b9d0ee025556f4507b62b.tar.gz
Use volatile TLS in library/cpp/yt
-rw-r--r--library/cpp/yt/cpu_clock/clock.cpp14
-rw-r--r--library/cpp/yt/logging/logger-inl.h6
-rw-r--r--library/cpp/yt/logging/logger.cpp10
-rw-r--r--library/cpp/yt/logging/logger.h4
-rw-r--r--library/cpp/yt/memory/memory_tag.cpp4
-rw-r--r--library/cpp/yt/misc/thread_name.cpp6
-rw-r--r--library/cpp/yt/system/thread_id-inl.h4
-rw-r--r--library/cpp/yt/system/thread_id.cpp2
8 files changed, 32 insertions, 18 deletions
diff --git a/library/cpp/yt/cpu_clock/clock.cpp b/library/cpp/yt/cpu_clock/clock.cpp
index eda3f03bbc..0ed65e3b92 100644
--- a/library/cpp/yt/cpu_clock/clock.cpp
+++ b/library/cpp/yt/cpu_clock/clock.cpp
@@ -4,6 +4,8 @@
#include <library/cpp/yt/assert/assert.h>
+#include <library/cpp/yt/misc/tls.h>
+
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
@@ -31,14 +33,16 @@ double GetTicksToMicroseconds()
TCalibrationState GetCalibrationState(TCpuInstant cpuInstant)
{
- thread_local TCalibrationState State;
+ YT_THREAD_LOCAL(TCalibrationState) State;
+
+ auto& state = GetTlsRef(State);
- if (State.CpuInstant + CalibrationCpuPeriod < cpuInstant) {
- State.CpuInstant = cpuInstant;
- State.Instant = TInstant::Now();
+ if (state.CpuInstant + CalibrationCpuPeriod < cpuInstant) {
+ state.CpuInstant = cpuInstant;
+ state.Instant = TInstant::Now();
}
- return State;
+ return state;
}
TCalibrationState GetCalibrationState()
diff --git a/library/cpp/yt/logging/logger-inl.h b/library/cpp/yt/logging/logger-inl.h
index 6f489da82d..5e2b6795dc 100644
--- a/library/cpp/yt/logging/logger-inl.h
+++ b/library/cpp/yt/logging/logger-inl.h
@@ -8,6 +8,8 @@
#include <library/cpp/yt/yson_string/convert.h>
#include <library/cpp/yt/yson_string/string.h>
+#include <library/cpp/yt/misc/tls.h>
+
namespace NYT::NLogging {
////////////////////////////////////////////////////////////////////////////////
@@ -98,8 +100,8 @@ private:
TSharedMutableRef Buffer_;
- static thread_local TPerThreadCache* Cache_;
- static thread_local bool CacheDestroyed_;
+ static YT_THREAD_LOCAL(TPerThreadCache*) Cache_;
+ static YT_THREAD_LOCAL(bool) CacheDestroyed_;
static TPerThreadCache* GetCache();
static constexpr size_t ChunkSize = 128_KB - 64;
diff --git a/library/cpp/yt/logging/logger.cpp b/library/cpp/yt/logging/logger.cpp
index 4ee5c1a01b..c11457f836 100644
--- a/library/cpp/yt/logging/logger.cpp
+++ b/library/cpp/yt/logging/logger.cpp
@@ -93,8 +93,8 @@ TMessageStringBuilder::TPerThreadCache* TMessageStringBuilder::GetCache()
if (CacheDestroyed_) {
return nullptr;
}
- static thread_local TPerThreadCache Cache;
- Cache_ = &Cache;
+ static YT_THREAD_LOCAL(TPerThreadCache) Cache;
+ Cache_ = &GetTlsRef(Cache);
return Cache_;
}
@@ -103,8 +103,8 @@ TMessageStringBuilder::TPerThreadCache::~TPerThreadCache()
TMessageStringBuilder::DisablePerThreadCache();
}
-thread_local TMessageStringBuilder::TPerThreadCache* TMessageStringBuilder::Cache_;
-thread_local bool TMessageStringBuilder::CacheDestroyed_;
+YT_THREAD_LOCAL(TMessageStringBuilder::TPerThreadCache*) TMessageStringBuilder::Cache_;
+YT_THREAD_LOCAL(bool) TMessageStringBuilder::CacheDestroyed_;
} // namespace NDetail
@@ -126,7 +126,7 @@ Y_WEAK ILogManager* GetDefaultLogManager()
////////////////////////////////////////////////////////////////////////////////
-thread_local ELogLevel ThreadMinLogLevel = ELogLevel::Minimum;
+YT_THREAD_LOCAL(ELogLevel) ThreadMinLogLevel = ELogLevel::Minimum;
void SetThreadMinLogLevel(ELogLevel minLogLevel)
{
diff --git a/library/cpp/yt/logging/logger.h b/library/cpp/yt/logging/logger.h
index 0913b9ea08..082afcddd8 100644
--- a/library/cpp/yt/logging/logger.h
+++ b/library/cpp/yt/logging/logger.h
@@ -324,8 +324,8 @@ void LogStructuredEvent(
break; \
} \
\
- static thread_local i64 localByteCounter__; \
- static thread_local ui8 localMessageCounter__; \
+ static YT_THREAD_LOCAL(i64) localByteCounter__; \
+ static YT_THREAD_LOCAL(ui8) localMessageCounter__; \
\
localByteCounter__ += message__.MessageRef.Size(); \
if (Y_UNLIKELY(++localMessageCounter__ == 0)) { \
diff --git a/library/cpp/yt/memory/memory_tag.cpp b/library/cpp/yt/memory/memory_tag.cpp
index 391816240f..ebb0b43370 100644
--- a/library/cpp/yt/memory/memory_tag.cpp
+++ b/library/cpp/yt/memory/memory_tag.cpp
@@ -1,12 +1,14 @@
#include "memory_tag.h"
+#include <library/cpp/yt/misc/tls.h>
+
#include <util/system/compiler.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
-thread_local TMemoryTag CurrentMemoryTag;
+YT_THREAD_LOCAL(TMemoryTag) CurrentMemoryTag;
Y_WEAK TMemoryTag GetCurrentMemoryTag()
{
diff --git a/library/cpp/yt/misc/thread_name.cpp b/library/cpp/yt/misc/thread_name.cpp
index 63ba1ee227..d59043db51 100644
--- a/library/cpp/yt/misc/thread_name.cpp
+++ b/library/cpp/yt/misc/thread_name.cpp
@@ -1,5 +1,7 @@
#include "thread_name.h"
+#include <library/cpp/yt/misc/tls.h>
+
#include <util/generic/string.h>
#include <util/system/thread.h>
@@ -29,7 +31,9 @@ TThreadName::TThreadName(const TString& name)
// This function uses cached TThread::CurrentThreadName() result
TThreadName GetCurrentThreadName()
{
- static thread_local TThreadName threadName;
+ static YT_THREAD_LOCAL(TThreadName) ThreadName;
+ auto& threadName = GetTlsRef(ThreadName);
+
if (threadName.Length == 0) {
if (auto name = TThread::CurrentThreadName()) {
auto length = std::min<int>(TThreadName::BufferCapacity - 1, name.length());
diff --git a/library/cpp/yt/system/thread_id-inl.h b/library/cpp/yt/system/thread_id-inl.h
index 86d34ef8a0..73434145e3 100644
--- a/library/cpp/yt/system/thread_id-inl.h
+++ b/library/cpp/yt/system/thread_id-inl.h
@@ -4,6 +4,8 @@
#include "thread_id.h"
#endif
+#include <library/cpp/yt/misc/tls.h>
+
#include <atomic>
#include <util/system/compiler.h>
@@ -12,7 +14,7 @@ namespace NYT {
////////////////////////////////////////////////////////////////////////////////
-extern thread_local TSequentialThreadId CachedSequentialThreadId;
+extern YT_THREAD_LOCAL(TSequentialThreadId) CachedSequentialThreadId;
extern std::atomic<TSequentialThreadId> SequentialThreadIdGenerator;
inline TSequentialThreadId GetSequentialThreadId()
diff --git a/library/cpp/yt/system/thread_id.cpp b/library/cpp/yt/system/thread_id.cpp
index c4eef110b2..1ecf1fe9bd 100644
--- a/library/cpp/yt/system/thread_id.cpp
+++ b/library/cpp/yt/system/thread_id.cpp
@@ -6,7 +6,7 @@ namespace NYT {
////////////////////////////////////////////////////////////////////////////////
-thread_local TSequentialThreadId CachedSequentialThreadId = InvalidSequentialThreadId;
+YT_THREAD_LOCAL(TSequentialThreadId) CachedSequentialThreadId = InvalidSequentialThreadId;
std::atomic<TSequentialThreadId> SequentialThreadIdGenerator = InvalidSequentialThreadId;
TSystemThreadId GetSystemThreadId()