tls.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "tls.h"
  2. #include <util/generic/hash.h>
  3. #include <util/generic/intrlist.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/generic/vector.h>
  6. #include <atomic>
  7. #if defined(_unix_)
  8. #include <pthread.h>
  9. #endif
  10. using namespace NTls;
  11. namespace {
  12. static inline size_t AcquireKey() {
  13. static std::atomic<size_t> cur;
  14. return cur++;
  15. }
  16. class TGenericTlsBase {
  17. public:
  18. using TSmallKey = size_t;
  19. class TPerThreadStorage {
  20. public:
  21. struct TKey: public TNonCopyable {
  22. inline TKey(TDtor dtor)
  23. : Key(AcquireKey())
  24. , Dtor(dtor)
  25. {
  26. }
  27. TSmallKey Key;
  28. TDtor Dtor;
  29. };
  30. class TStoredValue: public TIntrusiveListItem<TStoredValue> {
  31. public:
  32. inline TStoredValue(const TKey* key)
  33. : Data_(nullptr)
  34. , Dtor_(key->Dtor)
  35. {
  36. }
  37. inline ~TStoredValue() {
  38. if (Dtor_ && Data_) {
  39. Dtor_(Data_);
  40. }
  41. }
  42. inline void Set(void* ptr) noexcept {
  43. Data_ = ptr;
  44. }
  45. inline void* Get() const noexcept {
  46. return Data_;
  47. }
  48. private:
  49. void* Data_;
  50. TDtor Dtor_;
  51. };
  52. inline TStoredValue* Value(const TKey* key) {
  53. TStoredValue*& ret = *ValuePtr((size_t)key->Key);
  54. if (!ret) {
  55. THolder<TStoredValue> sv(new TStoredValue(key));
  56. Storage_.PushFront(sv.Get());
  57. ret = sv.Release();
  58. }
  59. return ret;
  60. }
  61. inline TStoredValue** ValuePtr(size_t idx) {
  62. // do not grow vector too much
  63. if (idx < 10000) {
  64. if (idx >= Values_.size()) {
  65. Values_.resize(idx + 1);
  66. }
  67. return &Values_[idx];
  68. }
  69. return &FarValues_[idx];
  70. }
  71. private:
  72. TVector<TStoredValue*> Values_;
  73. THashMap<size_t, TStoredValue*> FarValues_;
  74. TIntrusiveListWithAutoDelete<TStoredValue, TDelete> Storage_;
  75. };
  76. inline TPerThreadStorage* MyStorage() {
  77. #if defined(Y_HAVE_FAST_POD_TLS)
  78. Y_POD_STATIC_THREAD(TPerThreadStorage*)
  79. my(nullptr);
  80. if (!my) {
  81. my = MyStorageSlow();
  82. }
  83. return my;
  84. #else
  85. return MyStorageSlow();
  86. #endif
  87. }
  88. virtual TPerThreadStorage* MyStorageSlow() = 0;
  89. virtual ~TGenericTlsBase() = default;
  90. };
  91. } // namespace
  92. #if defined(_unix_)
  93. namespace {
  94. class TMasterTls: public TGenericTlsBase {
  95. public:
  96. inline TMasterTls() {
  97. Y_ABORT_UNLESS(!pthread_key_create(&Key_, Dtor), "pthread_key_create failed");
  98. }
  99. inline ~TMasterTls() override {
  100. // explicitly call dtor for main thread
  101. Dtor(pthread_getspecific(Key_));
  102. Y_ABORT_UNLESS(!pthread_key_delete(Key_), "pthread_key_delete failed");
  103. }
  104. static inline TMasterTls* Instance() {
  105. return SingletonWithPriority<TMasterTls, 1>();
  106. }
  107. private:
  108. TPerThreadStorage* MyStorageSlow() override {
  109. void* ret = pthread_getspecific(Key_);
  110. if (!ret) {
  111. ret = new TPerThreadStorage();
  112. Y_ABORT_UNLESS(!pthread_setspecific(Key_, ret), "pthread_setspecific failed");
  113. }
  114. return (TPerThreadStorage*)ret;
  115. }
  116. static void Dtor(void* ptr) {
  117. delete (TPerThreadStorage*)ptr;
  118. }
  119. private:
  120. pthread_key_t Key_;
  121. };
  122. using TKeyDescriptor = TMasterTls::TPerThreadStorage::TKey;
  123. } // namespace
  124. class TKey::TImpl: public TKeyDescriptor {
  125. public:
  126. inline TImpl(TDtor dtor)
  127. : TKeyDescriptor(dtor)
  128. {
  129. }
  130. inline void* Get() const {
  131. return TMasterTls::Instance()->MyStorage()->Value(this)->Get();
  132. }
  133. inline void Set(void* val) const {
  134. TMasterTls::Instance()->MyStorage()->Value(this)->Set(val);
  135. }
  136. static inline void Cleanup() {
  137. }
  138. };
  139. #else
  140. namespace {
  141. class TGenericTls: public TGenericTlsBase {
  142. public:
  143. virtual TPerThreadStorage* MyStorageSlow() {
  144. auto lock = Guard(Lock_);
  145. {
  146. TPTSRef& ret = Datas_[TThread::CurrentThreadId()];
  147. if (!ret) {
  148. ret.Reset(new TPerThreadStorage());
  149. }
  150. return ret.Get();
  151. }
  152. }
  153. inline void Cleanup() noexcept {
  154. with_lock (Lock_) {
  155. Datas_.erase(TThread::CurrentThreadId());
  156. }
  157. }
  158. static inline TGenericTls* Instance() {
  159. return SingletonWithPriority<TGenericTls, 1>();
  160. }
  161. private:
  162. using TPTSRef = THolder<TPerThreadStorage>;
  163. TMutex Lock_;
  164. THashMap<TThread::TId, TPTSRef> Datas_;
  165. };
  166. } // namespace
  167. class TKey::TImpl {
  168. public:
  169. inline TImpl(TDtor dtor)
  170. : Key_(dtor)
  171. {
  172. }
  173. inline void* Get() {
  174. return TGenericTls::Instance()->MyStorage()->Value(&Key_)->Get();
  175. }
  176. inline void Set(void* ptr) {
  177. TGenericTls::Instance()->MyStorage()->Value(&Key_)->Set(ptr);
  178. }
  179. static inline void Cleanup() {
  180. TGenericTls::Instance()->Cleanup();
  181. }
  182. private:
  183. TGenericTls::TPerThreadStorage::TKey Key_;
  184. };
  185. #endif
  186. TKey::TKey(TDtor dtor)
  187. : Impl_(new TImpl(dtor))
  188. {
  189. }
  190. TKey::TKey(TKey&&) noexcept = default;
  191. TKey::~TKey() = default;
  192. void* TKey::Get() const {
  193. return Impl_->Get();
  194. }
  195. void TKey::Set(void* ptr) const {
  196. Impl_->Set(ptr);
  197. }
  198. void TKey::Cleanup() noexcept {
  199. TImpl::Cleanup();
  200. }