diff options
author | Ruslan Kovalev <ruslan.a.kovalev@gmail.com> | 2022-02-10 16:46:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:45 +0300 |
commit | 9123176b341b6f2658cff5132482b8237c1416c8 (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/cpp/codecs/tls_cache.h | |
parent | 59e19371de37995fcb36beb16cd6ec030af960bc (diff) | |
download | ydb-9123176b341b6f2658cff5132482b8237c1416c8.tar.gz |
Restoring authorship annotation for Ruslan Kovalev <ruslan.a.kovalev@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/codecs/tls_cache.h')
-rw-r--r-- | library/cpp/codecs/tls_cache.h | 184 |
1 files changed, 92 insertions, 92 deletions
diff --git a/library/cpp/codecs/tls_cache.h b/library/cpp/codecs/tls_cache.h index fa166729c5..0184e4bb6c 100644 --- a/library/cpp/codecs/tls_cache.h +++ b/library/cpp/codecs/tls_cache.h @@ -1,100 +1,100 @@ -#pragma once - -#include <util/generic/buffer.h> -#include <util/generic/deque.h> -#include <util/generic/noncopyable.h> -#include <util/generic/strbuf.h> -#include <util/system/tls.h> -#include <util/thread/singleton.h> - -namespace NCodecs { - template <class TItem> - struct TClear { - void operator()(TItem& item) const { - item.Clear(); - } - }; - +#pragma once + +#include <util/generic/buffer.h> +#include <util/generic/deque.h> +#include <util/generic/noncopyable.h> +#include <util/generic/strbuf.h> +#include <util/system/tls.h> +#include <util/thread/singleton.h> + +namespace NCodecs { + template <class TItem> + struct TClear { + void operator()(TItem& item) const { + item.Clear(); + } + }; + template <class TItem, class TCleaner = TClear<TItem>> - class TTlsCache { - using TSelf = TTlsCache<TItem, TCleaner>; - + class TTlsCache { + using TSelf = TTlsCache<TItem, TCleaner>; + struct TItemHolder: public TIntrusiveListItem<TItemHolder> { - TItemHolder(TSelf& factory) - : Factory(factory) + TItemHolder(TSelf& factory) + : Factory(factory) { } - - void Release() { - Factory.Release(*this); - } - - TSelf& Factory; - TItem Item; - }; - - class TItemGuard { - public: - explicit TItemGuard(TSelf& fact) - : Holder(fact.Acquire()) + + void Release() { + Factory.Release(*this); + } + + TSelf& Factory; + TItem Item; + }; + + class TItemGuard { + public: + explicit TItemGuard(TSelf& fact) + : Holder(fact.Acquire()) { } - + TItemGuard(TItemGuard&& other) noexcept { - *this = std::move(other); - } - + *this = std::move(other); + } + TItemGuard& operator=(TItemGuard&& other) noexcept { - if (&other != this) { - std::swap(Holder, other.Holder); - } - return *this; - } - - ~TItemGuard() { - if (Holder) { - Holder->Release(); - } - } - - TItem& Get() & { - Y_ASSERT(Holder); - return Holder->Item; - } - - TItem& Get() && = delete; - - private: - TItemHolder* Holder = nullptr; - }; - - public: - TItemGuard Item() { - return TItemGuard(*this); - } - - static TSelf& TlsInstance() { - return *FastTlsSingleton<TSelf>(); - } - - private: - TItemHolder* Acquire() { - if (Free.Empty()) { - return new TItemHolder(*this); - } else { - return Free.PopBack(); - } - } - - void Release(TItemHolder& item) { - Cleaner(item.Item); - Free.PushBack(&item); - } - - private: - TIntrusiveListWithAutoDelete<TItemHolder, TDelete> Free; - TCleaner Cleaner; - }; - - using TBufferTlsCache = TTlsCache<TBuffer>; -} + if (&other != this) { + std::swap(Holder, other.Holder); + } + return *this; + } + + ~TItemGuard() { + if (Holder) { + Holder->Release(); + } + } + + TItem& Get() & { + Y_ASSERT(Holder); + return Holder->Item; + } + + TItem& Get() && = delete; + + private: + TItemHolder* Holder = nullptr; + }; + + public: + TItemGuard Item() { + return TItemGuard(*this); + } + + static TSelf& TlsInstance() { + return *FastTlsSingleton<TSelf>(); + } + + private: + TItemHolder* Acquire() { + if (Free.Empty()) { + return new TItemHolder(*this); + } else { + return Free.PopBack(); + } + } + + void Release(TItemHolder& item) { + Cleaner(item.Item); + Free.PushBack(&item); + } + + private: + TIntrusiveListWithAutoDelete<TItemHolder, TDelete> Free; + TCleaner Cleaner; + }; + + using TBufferTlsCache = TTlsCache<TBuffer>; +} |