summaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/thread_local/thread_local.h
diff options
context:
space:
mode:
authorkulikov <[email protected]>2025-11-28 00:32:45 +0300
committerkulikov <[email protected]>2025-11-28 00:47:44 +0300
commit909e306757b2da405daed2e0838e11b65e033c1a (patch)
treeede331912522813f6f0f60f594306feaa9589b30 /library/cpp/threading/thread_local/thread_local.h
parent2a1c1d7670e335bc967a309eda2e0beac81332d7 (diff)
Use generic thread-local value for kernel/groupattrs, crash context and eventlog scope
- fix StdThreadLocalImpl -- make static state refcounted, there is no guarantee that static fields will outlive threads or TThreadLocalValues; - replace thread local values to generic local storage value in some places; - call runtime Init and replace local values factory; - don't disable NCurrentThreadEventlog for ytxx runtime, it should now work; - ensure generic local storage values are allocated after runtime init; - use function-scope static variables to prevent issues with order of construction and destruction; - basesearch now almost works with coroutines (with NProfile disabled). commit_hash:7fde81591b0dbc3a53b8d1cb11bb96930a2e9a80
Diffstat (limited to 'library/cpp/threading/thread_local/thread_local.h')
-rw-r--r--library/cpp/threading/thread_local/thread_local.h32
1 files changed, 20 insertions, 12 deletions
diff --git a/library/cpp/threading/thread_local/thread_local.h b/library/cpp/threading/thread_local/thread_local.h
index af4d7a07cf2..8f8e9aa9517 100644
--- a/library/cpp/threading/thread_local/thread_local.h
+++ b/library/cpp/threading/thread_local/thread_local.h
@@ -129,8 +129,8 @@ template <typename T, size_t NumShards>
class TThreadLocalValue<T, EThreadLocalImpl::StdThreadLocal, NumShards> : private TNonCopyable {
public:
~TThreadLocalValue() {
- AllValues_.Destroy(ObjectId_);
- FlatIds.Put(ObjectId_);
+ StaticState_->AllValues.Destroy(ObjectId_);
+ StaticState_->FlatIds.Put(ObjectId_);
}
template <typename ...ConstructArgs>
@@ -156,23 +156,23 @@ public:
return &*v;
}
private:
- struct TAllValues;
+ struct TStaticState;
struct TPerThreadValues {
TAdaptiveLock Lock;
TDeque<TMaybe<T>> Storage;
public:
- explicit TPerThreadValues(TAllValues* allValues)
- : AllValues_(allValues)
+ explicit TPerThreadValues(TAtomicSharedPtr<TStaticState> staticState)
+ : StaticState_(std::move(staticState))
{
- AllValues_->Add(this);
+ StaticState_->AllValues.Add(this);
}
~TPerThreadValues() {
- AllValues_->Remove(this);
+ StaticState_->AllValues.Remove(this);
}
private:
- TAllValues* const AllValues_;
+ TAtomicSharedPtr<TStaticState> StaticState_;
};
class TFlatIdGenerator {
@@ -234,13 +234,21 @@ private:
TAdaptiveLock Lock_;
THashSet<TPerThreadValues*> Ptrs_;
};
+private:
+ struct TStaticState {
+ TFlatIdGenerator FlatIds;
+ TAllValues AllValues;
+ };
+ static TAtomicSharedPtr<TStaticState> GetStaticState() {
+ static TAtomicSharedPtr<TStaticState> state = MakeAtomicShared<TStaticState>();
+ return state;
+ }
private:
- static inline TFlatIdGenerator FlatIds;
- const ui32 ObjectId_ = FlatIds.Get();
+ TAtomicSharedPtr<TStaticState> StaticState_ = GetStaticState();
+ const ui32 ObjectId_ = StaticState_->FlatIds.Get();
- static inline TAllValues AllValues_;
- static inline thread_local TPerThreadValues Values_{&AllValues_};
+ static inline thread_local TPerThreadValues Values_{GetStaticState()};
};