Browse Source

Use volatile TLS in library/cpp/yt

lukyan 1 year ago
parent
commit
d5b84ed65f

+ 9 - 5
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()

+ 4 - 2
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;

+ 5 - 5
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)
 {

+ 2 - 2
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)) { \

+ 3 - 1
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()
 {

+ 5 - 1
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());

+ 3 - 1
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()

+ 1 - 1
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()