thread_local_storage.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/threading/thread_local_storage.h"
  5. #include <atomic>
  6. #include "base/check_op.h"
  7. #include "base/compiler_specific.h"
  8. #include "base/memory/raw_ptr_exclusion.h"
  9. #include "base/notreached.h"
  10. #include "base/synchronization/lock.h"
  11. #include "build/build_config.h"
  12. #if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_X86_64)
  13. #include <pthread.h>
  14. #include <type_traits>
  15. #endif
  16. using base::internal::PlatformThreadLocalStorage;
  17. // Chrome Thread Local Storage (TLS)
  18. //
  19. // This TLS system allows Chrome to use a single OS level TLS slot process-wide,
  20. // and allows us to control the slot limits instead of being at the mercy of the
  21. // platform. To do this, Chrome TLS replicates an array commonly found in the OS
  22. // thread metadata.
  23. //
  24. // Overview:
  25. //
  26. // OS TLS Slots Per-Thread Per-Process Global
  27. // ...
  28. // [] Chrome TLS Array Chrome TLS Metadata
  29. // [] ----------> [][][][][ ][][][][] [][][][][ ][][][][]
  30. // [] | |
  31. // ... V V
  32. // Metadata Version Slot Information
  33. // Your Data!
  34. //
  35. // Using a single OS TLS slot, Chrome TLS allocates an array on demand for the
  36. // lifetime of each thread that requests Chrome TLS data. Each per-thread TLS
  37. // array matches the length of the per-process global metadata array.
  38. //
  39. // A per-process global TLS metadata array tracks information about each item in
  40. // the per-thread array:
  41. // * Status: Tracks if the slot is allocated or free to assign.
  42. // * Destructor: An optional destructor to call on thread destruction for that
  43. // specific slot.
  44. // * Version: Tracks the current version of the TLS slot. Each TLS slot
  45. // allocation is associated with a unique version number.
  46. //
  47. // Most OS TLS APIs guarantee that a newly allocated TLS slot is
  48. // initialized to 0 for all threads. The Chrome TLS system provides
  49. // this guarantee by tracking the version for each TLS slot here
  50. // on each per-thread Chrome TLS array entry. Threads that access
  51. // a slot with a mismatched version will receive 0 as their value.
  52. // The metadata version is incremented when the client frees a
  53. // slot. The per-thread metadata version is updated when a client
  54. // writes to the slot. This scheme allows for constant time
  55. // invalidation and avoids the need to iterate through each Chrome
  56. // TLS array to mark the slot as zero.
  57. //
  58. // Just like an OS TLS API, clients of the Chrome TLS are responsible for
  59. // managing any necessary lifetime of the data in their slots. The only
  60. // convenience provided is automatic destruction when a thread ends. If a client
  61. // frees a slot, that client is responsible for destroying the data in the slot.
  62. namespace {
  63. // In order to make TLS destructors work, we need to keep around a function
  64. // pointer to the destructor for each slot. We keep this array of pointers in a
  65. // global (static) array.
  66. // We use the single OS-level TLS slot (giving us one pointer per thread) to
  67. // hold a pointer to a per-thread array (table) of slots that we allocate to
  68. // Chromium consumers.
  69. // g_native_tls_key is the one native TLS that we use. It stores our table.
  70. std::atomic<PlatformThreadLocalStorage::TLSKey> g_native_tls_key{
  71. PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES};
  72. // The OS TLS slot has the following states. The TLS slot's lower 2 bits contain
  73. // the state, the upper bits the TlsVectorEntry*.
  74. // * kUninitialized: Any call to Slot::Get()/Set() will create the base
  75. // per-thread TLS state. kUninitialized must be null.
  76. // * kInUse: value has been created and is in use.
  77. // * kDestroying: Set when the thread is exiting prior to deleting any of the
  78. // values stored in the TlsVectorEntry*. This state is necessary so that
  79. // sequence/task checks won't be done while in the process of deleting the
  80. // tls entries (see comments in SequenceCheckerImpl for more details).
  81. // * kDestroyed: All of the values in the vector have been deallocated and
  82. // the TlsVectorEntry has been deleted.
  83. //
  84. // Final States:
  85. // * Windows: kDestroyed. Windows does not iterate through the OS TLS to clean
  86. // up the values.
  87. // * POSIX: kUninitialized. POSIX iterates through TLS until all slots contain
  88. // nullptr.
  89. //
  90. // More details on this design:
  91. // We need some type of thread-local state to indicate that the TLS system has
  92. // been destroyed. To do so, we leverage the multi-pass nature of destruction
  93. // of pthread_key.
  94. //
  95. // a) After destruction of TLS system, we set the pthread_key to a sentinel
  96. // kDestroyed.
  97. // b) All calls to Slot::Get() DCHECK that the state is not kDestroyed, and
  98. // any system which might potentially invoke Slot::Get() after destruction
  99. // of TLS must check ThreadLocalStorage::ThreadIsBeingDestroyed().
  100. // c) After a full pass of the pthread_keys, on the next invocation of
  101. // ConstructTlsVector(), we'll then set the key to nullptr.
  102. // d) At this stage, the TLS system is back in its uninitialized state.
  103. // e) If in the second pass of destruction of pthread_keys something were to
  104. // re-initialize TLS [this should never happen! Since the only code which
  105. // uses Chrome TLS is Chrome controlled, we should really be striving for
  106. // single-pass destruction], then TLS will be re-initialized and then go
  107. // through the 2-pass destruction system again. Everything should just
  108. // work (TM).
  109. // The state of the tls-entry.
  110. enum class TlsVectorState {
  111. kUninitialized = 0,
  112. // In the process of destroying the entries in the vector.
  113. kDestroying,
  114. // All of the entries and the vector has been destroyed.
  115. kDestroyed,
  116. // The vector has been initialized and is in use.
  117. kInUse,
  118. kMaxValue = kInUse
  119. };
  120. // Bit-mask used to store TlsVectorState.
  121. constexpr uintptr_t kVectorStateBitMask = 3;
  122. static_assert(static_cast<int>(TlsVectorState::kMaxValue) <=
  123. kVectorStateBitMask,
  124. "number of states must fit in header");
  125. static_assert(static_cast<int>(TlsVectorState::kUninitialized) == 0,
  126. "kUninitialized must be null");
  127. // The maximum number of slots in our thread local storage stack.
  128. constexpr size_t kThreadLocalStorageSize = 256;
  129. enum TlsStatus {
  130. FREE,
  131. IN_USE,
  132. };
  133. struct TlsMetadata {
  134. TlsStatus status;
  135. base::ThreadLocalStorage::TLSDestructorFunc destructor;
  136. // Incremented every time a slot is reused. Used to detect reuse of slots.
  137. uint32_t version;
  138. };
  139. struct TlsVectorEntry {
  140. // `data` is not a raw_ptr<...> for performance reasons (based on analysis of
  141. // sampling profiler data and tab_search:top100:2020).
  142. RAW_PTR_EXCLUSION void* data;
  143. uint32_t version;
  144. };
  145. // This lock isn't needed until after we've constructed the per-thread TLS
  146. // vector, so it's safe to use.
  147. base::Lock* GetTLSMetadataLock() {
  148. static auto* lock = new base::Lock();
  149. return lock;
  150. }
  151. TlsMetadata g_tls_metadata[kThreadLocalStorageSize];
  152. size_t g_last_assigned_slot = 0;
  153. // The maximum number of times to try to clear slots by calling destructors.
  154. // Use pthread naming convention for clarity.
  155. constexpr size_t kMaxDestructorIterations = kThreadLocalStorageSize;
  156. // Sets the value and state of the vector.
  157. void SetTlsVectorValue(PlatformThreadLocalStorage::TLSKey key,
  158. TlsVectorEntry* tls_data,
  159. TlsVectorState state) {
  160. DCHECK(tls_data || (state == TlsVectorState::kUninitialized) ||
  161. (state == TlsVectorState::kDestroyed));
  162. PlatformThreadLocalStorage::SetTLSValue(
  163. key, reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(tls_data) |
  164. static_cast<uintptr_t>(state)));
  165. }
  166. // Returns the tls vector and current state from the raw tls value.
  167. TlsVectorState GetTlsVectorStateAndValue(void* tls_value,
  168. TlsVectorEntry** entry = nullptr) {
  169. if (entry) {
  170. *entry = reinterpret_cast<TlsVectorEntry*>(
  171. reinterpret_cast<uintptr_t>(tls_value) & ~kVectorStateBitMask);
  172. }
  173. return static_cast<TlsVectorState>(reinterpret_cast<uintptr_t>(tls_value) &
  174. kVectorStateBitMask);
  175. }
  176. // Returns the tls vector and state using the tls key.
  177. TlsVectorState GetTlsVectorStateAndValue(PlatformThreadLocalStorage::TLSKey key,
  178. TlsVectorEntry** entry = nullptr) {
  179. // Only on x86_64, the implementation is not stable on ARM64. For instance, in
  180. // macOS 11, the TPIDRRO_EL0 registers holds the CPU index in the low bits,
  181. // which is not the case in macOS 12. See libsyscall/os/tsd.h in XNU
  182. // (_os_tsd_get_direct() is used by pthread_getspecific() internally).
  183. #if BUILDFLAG(IS_MAC) && defined(ARCH_CPU_X86_64)
  184. // On macOS, pthread_getspecific() is in libSystem, so a call to it has to go
  185. // through PLT. However, and contrary to some other platforms, *all* TLS keys
  186. // are in a static array in the thread structure. So they are *always* at a
  187. // fixed offset from the segment register holding the thread structure
  188. // address.
  189. //
  190. // We could use _pthread_getspecific_direct(), but it is not
  191. // exported. However, on all macOS versions we support, the TLS array is at
  192. // %gs. This is used in V8 and PartitionAlloc, and can also be seen by looking
  193. // at pthread_getspecific() disassembly:
  194. //
  195. // libsystem_pthread.dylib`pthread_getspecific:
  196. // libsystem_pthread.dylib[0x7ff800316099] <+0>: movq %gs:(,%rdi,8), %rax
  197. // libsystem_pthread.dylib[0x7ff8003160a2] <+9>: retq
  198. //
  199. // This function is essentially inlining the content of pthread_getspecific()
  200. // here.
  201. //
  202. // Note that this likely ends up being even faster than thread_local for
  203. // typical Chromium builds where the code is in a dynamic library. For the
  204. // static executable case, this is likely equivalent.
  205. static_assert(
  206. std::is_same<PlatformThreadLocalStorage::TLSKey, pthread_key_t>::value,
  207. "The special-case below assumes that the platform TLS implementation is "
  208. "pthread.");
  209. intptr_t platform_tls_value;
  210. asm("movq %%gs:(,%1,8), %0;" : "=r"(platform_tls_value) : "r"(key));
  211. return GetTlsVectorStateAndValue(reinterpret_cast<void*>(platform_tls_value),
  212. entry);
  213. #else
  214. return GetTlsVectorStateAndValue(PlatformThreadLocalStorage::GetTLSValue(key),
  215. entry);
  216. #endif
  217. }
  218. // This function is called to initialize our entire Chromium TLS system.
  219. // It may be called very early, and we need to complete most all of the setup
  220. // (initialization) before calling *any* memory allocator functions, which may
  221. // recursively depend on this initialization.
  222. // As a result, we use Atomics, and avoid anything (like a singleton) that might
  223. // require memory allocations.
  224. TlsVectorEntry* ConstructTlsVector() {
  225. PlatformThreadLocalStorage::TLSKey key =
  226. g_native_tls_key.load(std::memory_order_relaxed);
  227. if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES) {
  228. CHECK(PlatformThreadLocalStorage::AllocTLS(&key));
  229. // The TLS_KEY_OUT_OF_INDEXES is used to find out whether the key is set or
  230. // not in NoBarrier_CompareAndSwap, but Posix doesn't have invalid key, we
  231. // define an almost impossible value be it.
  232. // If we really get TLS_KEY_OUT_OF_INDEXES as value of key, just alloc
  233. // another TLS slot.
  234. if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES) {
  235. PlatformThreadLocalStorage::TLSKey tmp = key;
  236. CHECK(PlatformThreadLocalStorage::AllocTLS(&key) &&
  237. key != PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES);
  238. PlatformThreadLocalStorage::FreeTLS(tmp);
  239. }
  240. // Atomically test-and-set the tls_key. If the key is
  241. // TLS_KEY_OUT_OF_INDEXES, go ahead and set it. Otherwise, do nothing, as
  242. // another thread already did our dirty work.
  243. PlatformThreadLocalStorage::TLSKey old_key =
  244. PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES;
  245. if (!g_native_tls_key.compare_exchange_strong(old_key, key,
  246. std::memory_order_relaxed,
  247. std::memory_order_relaxed)) {
  248. // We've been shortcut. Another thread replaced g_native_tls_key first so
  249. // we need to destroy our index and use the one the other thread got
  250. // first.
  251. PlatformThreadLocalStorage::FreeTLS(key);
  252. key = g_native_tls_key.load(std::memory_order_relaxed);
  253. }
  254. }
  255. CHECK_EQ(GetTlsVectorStateAndValue(key), TlsVectorState::kUninitialized);
  256. // Some allocators, such as TCMalloc, make use of thread local storage. As a
  257. // result, any attempt to call new (or malloc) will lazily cause such a system
  258. // to initialize, which will include registering for a TLS key. If we are not
  259. // careful here, then that request to create a key will call new back, and
  260. // we'll have an infinite loop. We avoid that as follows: Use a stack
  261. // allocated vector, so that we don't have dependence on our allocator until
  262. // our service is in place. (i.e., don't even call new until after we're
  263. // setup)
  264. TlsVectorEntry stack_allocated_tls_data[kThreadLocalStorageSize];
  265. memset(stack_allocated_tls_data, 0, sizeof(stack_allocated_tls_data));
  266. // Ensure that any rentrant calls change the temp version.
  267. SetTlsVectorValue(key, stack_allocated_tls_data, TlsVectorState::kInUse);
  268. // Allocate an array to store our data.
  269. TlsVectorEntry* tls_data = new TlsVectorEntry[kThreadLocalStorageSize];
  270. memcpy(tls_data, stack_allocated_tls_data, sizeof(stack_allocated_tls_data));
  271. SetTlsVectorValue(key, tls_data, TlsVectorState::kInUse);
  272. return tls_data;
  273. }
  274. void OnThreadExitInternal(TlsVectorEntry* tls_data) {
  275. DCHECK(tls_data);
  276. // Some allocators, such as TCMalloc, use TLS. As a result, when a thread
  277. // terminates, one of the destructor calls we make may be to shut down an
  278. // allocator. We have to be careful that after we've shutdown all of the known
  279. // destructors (perchance including an allocator), that we don't call the
  280. // allocator and cause it to resurrect itself (with no possibly destructor
  281. // call to follow). We handle this problem as follows: Switch to using a stack
  282. // allocated vector, so that we don't have dependence on our allocator after
  283. // we have called all g_tls_metadata destructors. (i.e., don't even call
  284. // delete[] after we're done with destructors.)
  285. TlsVectorEntry stack_allocated_tls_data[kThreadLocalStorageSize];
  286. memcpy(stack_allocated_tls_data, tls_data, sizeof(stack_allocated_tls_data));
  287. // Ensure that any re-entrant calls change the temp version.
  288. PlatformThreadLocalStorage::TLSKey key =
  289. g_native_tls_key.load(std::memory_order_relaxed);
  290. SetTlsVectorValue(key, stack_allocated_tls_data, TlsVectorState::kDestroying);
  291. delete[] tls_data; // Our last dependence on an allocator.
  292. // Snapshot the TLS Metadata so we don't have to lock on every access.
  293. TlsMetadata tls_metadata[kThreadLocalStorageSize];
  294. {
  295. base::AutoLock auto_lock(*GetTLSMetadataLock());
  296. memcpy(tls_metadata, g_tls_metadata, sizeof(g_tls_metadata));
  297. }
  298. size_t remaining_attempts = kMaxDestructorIterations + 1;
  299. bool need_to_scan_destructors = true;
  300. while (need_to_scan_destructors) {
  301. need_to_scan_destructors = false;
  302. // Try to destroy the first-created-slot (which is slot 1) in our last
  303. // destructor call. That user was able to function, and define a slot with
  304. // no other services running, so perhaps it is a basic service (like an
  305. // allocator) and should also be destroyed last. If we get the order wrong,
  306. // then we'll iterate several more times, so it is really not that critical
  307. // (but it might help).
  308. for (size_t slot = 0; slot < kThreadLocalStorageSize; ++slot) {
  309. void* tls_value = stack_allocated_tls_data[slot].data;
  310. if (!tls_value || tls_metadata[slot].status == TlsStatus::FREE ||
  311. stack_allocated_tls_data[slot].version != tls_metadata[slot].version)
  312. continue;
  313. base::ThreadLocalStorage::TLSDestructorFunc destructor =
  314. tls_metadata[slot].destructor;
  315. if (!destructor)
  316. continue;
  317. stack_allocated_tls_data[slot].data = nullptr; // pre-clear the slot.
  318. destructor(tls_value);
  319. // Any destructor might have called a different service, which then set a
  320. // different slot to a non-null value. Hence we need to check the whole
  321. // vector again. This is a pthread standard.
  322. need_to_scan_destructors = true;
  323. }
  324. if (--remaining_attempts == 0) {
  325. NOTREACHED(); // Destructors might not have been called.
  326. break;
  327. }
  328. }
  329. // Remove our stack allocated vector.
  330. SetTlsVectorValue(key, nullptr, TlsVectorState::kDestroyed);
  331. }
  332. } // namespace
  333. namespace base {
  334. namespace internal {
  335. #if BUILDFLAG(IS_WIN)
  336. void PlatformThreadLocalStorage::OnThreadExit() {
  337. PlatformThreadLocalStorage::TLSKey key =
  338. g_native_tls_key.load(std::memory_order_relaxed);
  339. if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES)
  340. return;
  341. TlsVectorEntry* tls_vector = nullptr;
  342. const TlsVectorState state = GetTlsVectorStateAndValue(key, &tls_vector);
  343. // On Windows, thread destruction callbacks are only invoked once per module,
  344. // so there should be no way that this could be invoked twice.
  345. DCHECK_NE(state, TlsVectorState::kDestroyed);
  346. // Maybe we have never initialized TLS for this thread.
  347. if (state == TlsVectorState::kUninitialized)
  348. return;
  349. OnThreadExitInternal(tls_vector);
  350. }
  351. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  352. void PlatformThreadLocalStorage::OnThreadExit(void* value) {
  353. // On posix this function may be called twice. The first pass calls dtors and
  354. // sets state to kDestroyed. The second pass sets kDestroyed to
  355. // kUninitialized.
  356. TlsVectorEntry* tls_vector = nullptr;
  357. const TlsVectorState state = GetTlsVectorStateAndValue(value, &tls_vector);
  358. if (state == TlsVectorState::kDestroyed) {
  359. PlatformThreadLocalStorage::TLSKey key =
  360. g_native_tls_key.load(std::memory_order_relaxed);
  361. SetTlsVectorValue(key, nullptr, TlsVectorState::kUninitialized);
  362. return;
  363. }
  364. OnThreadExitInternal(tls_vector);
  365. }
  366. #endif // BUILDFLAG(IS_WIN)
  367. } // namespace internal
  368. // static
  369. bool ThreadLocalStorage::HasBeenDestroyed() {
  370. PlatformThreadLocalStorage::TLSKey key =
  371. g_native_tls_key.load(std::memory_order_relaxed);
  372. if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES)
  373. return false;
  374. const TlsVectorState state = GetTlsVectorStateAndValue(key);
  375. return state == TlsVectorState::kDestroying ||
  376. state == TlsVectorState::kDestroyed;
  377. }
  378. void ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) {
  379. PlatformThreadLocalStorage::TLSKey key =
  380. g_native_tls_key.load(std::memory_order_relaxed);
  381. if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES ||
  382. GetTlsVectorStateAndValue(key) == TlsVectorState::kUninitialized) {
  383. ConstructTlsVector();
  384. }
  385. // Grab a new slot.
  386. {
  387. base::AutoLock auto_lock(*GetTLSMetadataLock());
  388. for (size_t i = 0; i < kThreadLocalStorageSize; ++i) {
  389. // Tracking the last assigned slot is an attempt to find the next
  390. // available slot within one iteration. Under normal usage, slots remain
  391. // in use for the lifetime of the process (otherwise before we reclaimed
  392. // slots, we would have run out of slots). This makes it highly likely the
  393. // next slot is going to be a free slot.
  394. size_t slot_candidate =
  395. (g_last_assigned_slot + 1 + i) % kThreadLocalStorageSize;
  396. if (g_tls_metadata[slot_candidate].status == TlsStatus::FREE) {
  397. g_tls_metadata[slot_candidate].status = TlsStatus::IN_USE;
  398. g_tls_metadata[slot_candidate].destructor = destructor;
  399. g_last_assigned_slot = slot_candidate;
  400. DCHECK_EQ(kInvalidSlotValue, slot_);
  401. slot_ = slot_candidate;
  402. version_ = g_tls_metadata[slot_candidate].version;
  403. break;
  404. }
  405. }
  406. }
  407. CHECK_LT(slot_, kThreadLocalStorageSize);
  408. }
  409. void ThreadLocalStorage::Slot::Free() {
  410. DCHECK_LT(slot_, kThreadLocalStorageSize);
  411. {
  412. base::AutoLock auto_lock(*GetTLSMetadataLock());
  413. g_tls_metadata[slot_].status = TlsStatus::FREE;
  414. g_tls_metadata[slot_].destructor = nullptr;
  415. ++(g_tls_metadata[slot_].version);
  416. }
  417. slot_ = kInvalidSlotValue;
  418. }
  419. void* ThreadLocalStorage::Slot::Get() const {
  420. TlsVectorEntry* tls_data = nullptr;
  421. const TlsVectorState state = GetTlsVectorStateAndValue(
  422. g_native_tls_key.load(std::memory_order_relaxed), &tls_data);
  423. DCHECK_NE(state, TlsVectorState::kDestroyed);
  424. if (!tls_data)
  425. return nullptr;
  426. DCHECK_LT(slot_, kThreadLocalStorageSize);
  427. // Version mismatches means this slot was previously freed.
  428. if (tls_data[slot_].version != version_)
  429. return nullptr;
  430. return tls_data[slot_].data;
  431. }
  432. void ThreadLocalStorage::Slot::Set(void* value) {
  433. TlsVectorEntry* tls_data = nullptr;
  434. const TlsVectorState state = GetTlsVectorStateAndValue(
  435. g_native_tls_key.load(std::memory_order_relaxed), &tls_data);
  436. DCHECK_NE(state, TlsVectorState::kDestroyed);
  437. if (UNLIKELY(!tls_data)) {
  438. if (!value)
  439. return;
  440. tls_data = ConstructTlsVector();
  441. }
  442. DCHECK_LT(slot_, kThreadLocalStorageSize);
  443. tls_data[slot_].data = value;
  444. tls_data[slot_].version = version_;
  445. }
  446. ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) {
  447. Initialize(destructor);
  448. }
  449. ThreadLocalStorage::Slot::~Slot() {
  450. Free();
  451. }
  452. } // namespace base