diff options
| author | kulikov <[email protected]> | 2025-11-18 15:48:30 +0300 |
|---|---|---|
| committer | kulikov <[email protected]> | 2025-11-18 16:08:27 +0300 |
| commit | 7b6a73defecf54640a9de7e0d9da969b2273295d (patch) | |
| tree | 6c6c9e6dfd329ad17bcc07e8d2ebc5d290ddd1bf /library/cpp | |
| parent | eef8c11b6e78580bdd3e0de90bf844f2cdc113e1 (diff) | |
Generic thread (yt fiber, etc) local storage
commit_hash:cc357d273b7ce5c911d194dd51fae5668877693c
Diffstat (limited to 'library/cpp')
| -rw-r--r-- | library/cpp/threading/thread_local/generic.cpp | 30 | ||||
| -rw-r--r-- | library/cpp/threading/thread_local/generic.h | 75 | ||||
| -rw-r--r-- | library/cpp/threading/thread_local/ya.make | 5 |
3 files changed, 109 insertions, 1 deletions
diff --git a/library/cpp/threading/thread_local/generic.cpp b/library/cpp/threading/thread_local/generic.cpp new file mode 100644 index 00000000000..cc1cef1ef92 --- /dev/null +++ b/library/cpp/threading/thread_local/generic.cpp @@ -0,0 +1,30 @@ +#include "generic.h" + +#include "thread_local.h" + +namespace { + class TThreadLocalStorage + : public NThreading::IGenericLocalStorage + { + public: + TData* GetData() const override { + return Data_.Get(); + } + private: + NThreading::TThreadLocalValue<TData> Data_; + }; + + NThreading::TGenericLocalStorageFactory genericLocalStorageFactory = []() { + return MakeHolder<TThreadLocalStorage>(); + }; +} + +namespace NThreading { + void SetGenericLocalStorageFactory(TGenericLocalStorageFactory factory) { + genericLocalStorageFactory = factory; + } + + THolder<IGenericLocalStorage> MakeGenericLocalStorage() { + return genericLocalStorageFactory(); + } +} diff --git a/library/cpp/threading/thread_local/generic.h b/library/cpp/threading/thread_local/generic.h new file mode 100644 index 00000000000..081562fa830 --- /dev/null +++ b/library/cpp/threading/thread_local/generic.h @@ -0,0 +1,75 @@ +#pragma once + +#include <util/generic/ptr.h> +#include <util/generic/vector.h> + +#include <functional> + +namespace NThreading { + + class IGenericLocalStorage { + public: + struct TTraits { + size_t Size = 0; + std::function<void(void*)> Constructor; + std::function<void(void*)> Destructor; + }; + + struct TData + : TNonCopyable + { + TVector<char> Memory; + std::function<void(void*)> Destructor; + public: + ~TData() { + if (Destructor) { + Destructor(&Memory[0]); + } + } + }; + public: + virtual ~IGenericLocalStorage() {}; + + void* GetMemory(const TTraits& traits) const { + TData* data = GetData(); + if (!data->Destructor) { + data->Destructor = traits.Destructor; + data->Memory.resize(traits.Size); + traits.Constructor(&data->Memory[0]); + } + return &data->Memory[0]; + } + private: + virtual TData* GetData() const = 0; + }; + + using TGenericLocalStorageFactory = std::function<THolder<IGenericLocalStorage>()>; + + void SetGenericLocalStorageFactory(TGenericLocalStorageFactory factory); + THolder<IGenericLocalStorage> MakeGenericLocalStorage(); + + template <typename T> + class TGenericLocalValue { + private: + static const auto& Traits() { + const static IGenericLocalStorage::TTraits traits = { + .Size = sizeof(T), + .Constructor = [](void* addr) { new (addr) T(); }, + .Destructor = [](void* addr) { static_cast<T*>(addr)->~T(); } + }; + + return traits; + }; + public: + T* Get() const { + return static_cast<T*>(Storage_->GetMemory(Traits())); + } + + T& GetRef() const { + return *Get(); + } + private: + THolder<IGenericLocalStorage> Storage_ = MakeGenericLocalStorage(); + }; +} + diff --git a/library/cpp/threading/thread_local/ya.make b/library/cpp/threading/thread_local/ya.make index 676381ab0f4..daf1c8c3645 100644 --- a/library/cpp/threading/thread_local/ya.make +++ b/library/cpp/threading/thread_local/ya.make @@ -7,6 +7,9 @@ PEERDIR( GENERATE_ENUM_SERIALIZATION(thread_local.h) -SRCS(thread_local.cpp) +SRCS( + thread_local.cpp + generic.cpp +) END() |
