| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/threading/thread_local_storage.h" | 5 #include "base/threading/thread_local_storage.h" |
| 6 | 6 |
| 7 #include "base/atomicops.h" | 7 #include "base/atomicops.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 using base::internal::PlatformThreadLocalStorage; | 10 using base::internal::PlatformThreadLocalStorage; |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 // In order to make TLS destructors work, we need to keep around a function | 13 // In order to make TLS destructors work, we need to keep around a function |
| 14 // pointer to the destructor for each slot. We keep this array of pointers in a | 14 // pointer to the destructor for each slot. We keep this array of pointers in a |
| 15 // global (static) array. | 15 // global (static) array. |
| 16 // We use the single OS-level TLS slot (giving us one pointer per thread) to | 16 // We use the single OS-level TLS slot (giving us one pointer per thread) to |
| 17 // hold a pointer to a per-thread array (table) of slots that we allocate to | 17 // hold a pointer to a per-thread array (table) of slots that we allocate to |
| 18 // Chromium consumers. | 18 // Chromium consumers. |
| 19 | 19 |
| 20 // g_native_tls_key is the one native TLS that we use. It stores our table. | 20 // g_native_tls_key is the one native TLS that we use. It stores our table. |
| 21 base::subtle::AtomicWord g_native_tls_key = | 21 base::subtle::Atomic32 g_native_tls_key = |
| 22 PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES; | 22 PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES; |
| 23 | 23 |
| 24 // g_last_used_tls_key is the high-water-mark of allocated thread local storage. | 24 // g_last_used_tls_key is the high-water-mark of allocated thread local storage. |
| 25 // Each allocation is an index into our g_tls_destructors[]. Each such index is | 25 // Each allocation is an index into our g_tls_destructors[]. Each such index is |
| 26 // assigned to the instance variable slot_ in a ThreadLocalStorage::Slot | 26 // assigned to the instance variable slot_ in a ThreadLocalStorage::Slot |
| 27 // instance. We reserve the value slot_ == 0 to indicate that the corresponding | 27 // instance. We reserve the value slot_ == 0 to indicate that the corresponding |
| 28 // instance of ThreadLocalStorage::Slot has been freed (i.e., destructor called, | 28 // instance of ThreadLocalStorage::Slot has been freed (i.e., destructor called, |
| 29 // etc.). This reserved use of 0 is then stated as the initial value of | 29 // etc.). This reserved use of 0 is then stated as the initial value of |
| 30 // g_last_used_tls_key, so that the first issued index will be 1. | 30 // g_last_used_tls_key, so that the first issued index will be 1. |
| 31 base::subtle::Atomic32 g_last_used_tls_key = 0; | 31 base::subtle::Atomic32 g_last_used_tls_key = 0; |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 PlatformThreadLocalStorage::GetTLSValue( | 241 PlatformThreadLocalStorage::GetTLSValue( |
| 242 base::subtle::NoBarrier_Load(&g_native_tls_key))); | 242 base::subtle::NoBarrier_Load(&g_native_tls_key))); |
| 243 if (!tls_data) | 243 if (!tls_data) |
| 244 tls_data = ConstructTlsVector(); | 244 tls_data = ConstructTlsVector(); |
| 245 DCHECK_GT(slot_, 0); | 245 DCHECK_GT(slot_, 0); |
| 246 DCHECK_LT(slot_, kThreadLocalStorageSize); | 246 DCHECK_LT(slot_, kThreadLocalStorageSize); |
| 247 tls_data[slot_] = value; | 247 tls_data[slot_] = value; |
| 248 } | 248 } |
| 249 | 249 |
| 250 } // namespace base | 250 } // namespace base |
| OLD | NEW |