Chromium Code Reviews| Index: base/threading/thread_local_storage.cc |
| diff --git a/base/threading/thread_local_storage_win.cc b/base/threading/thread_local_storage.cc |
| similarity index 65% |
| copy from base/threading/thread_local_storage_win.cc |
| copy to base/threading/thread_local_storage.cc |
| index 0ae3cb4c8cd39471b9b532a5dc87e2b5d61647a8..1b42fe6a031f0a772934f7fd0328c1a3e35f5358 100644 |
| --- a/base/threading/thread_local_storage_win.cc |
| +++ b/base/threading/thread_local_storage.cc |
| @@ -4,10 +4,10 @@ |
| #include "base/threading/thread_local_storage.h" |
| -#include <windows.h> |
| - |
| +#include "base/atomicops.h" |
| #include "base/logging.h" |
| +using base::internal::PlatformThreadLocalStorage; |
| namespace { |
| // In order to make TLS destructors work, we need to keep function |
| @@ -18,7 +18,8 @@ namespace { |
| // keep track of and call when threads terminate. |
|
jar (doing other things)
2013/11/26 20:05:24
nit: Starting on line 13, better comment would be:
michaelbai
2013/11/27 00:29:26
Done.
|
| // g_native_tls_key is the one native TLS that we use. It stores our table. |
| -long g_native_tls_key = TLS_OUT_OF_INDEXES; |
| +PlatformThreadLocalStorage::TLSKey g_native_tls_key = |
| + PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES; |
| // g_last_used_tls_key is the high-water-mark of allocated thread local storage. |
| // Each allocation is an index into our g_tls_destructors[]. Each such index is |
| @@ -27,7 +28,7 @@ long g_native_tls_key = TLS_OUT_OF_INDEXES; |
| // instance of ThreadLocalStorage::Slot has been freed (i.e., destructor called, |
| // etc.). This reserved use of 0 is then stated as the initial value of |
| // g_last_used_tls_key, so that the first issued index will be 1. |
| -long g_last_used_tls_key = 0; |
| +base::subtle::Atomic32 g_last_used_tls_key = 0; |
| // The maximum number of 'slots' in our thread local storage stack. |
| const int kThreadLocalStorageSize = 64; |
| @@ -49,22 +50,35 @@ volatile base::ThreadLocalStorage::TLSDestructorFunc |
| g_tls_destructors[kThreadLocalStorageSize]; |
| void** ConstructTlsVector() { |
|
jar (doing other things)
2013/11/26 20:05:24
Please add a comment:
This function is called to
michaelbai
2013/11/27 00:29:26
Done.
|
| - if (g_native_tls_key == TLS_OUT_OF_INDEXES) { |
| - long value = TlsAlloc(); |
| - DCHECK(value != TLS_OUT_OF_INDEXES); |
| - |
| + if (g_native_tls_key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES) { |
|
jar (doing other things)
2013/11/26 20:05:24
I think you need an atomic access ofr g_native_tls
michaelbai
2013/11/27 00:29:26
Done.
|
| + PlatformThreadLocalStorage::TLSKey key; |
| + CHECK(PlatformThreadLocalStorage::AllocTLS(&key)); |
| + |
| + // The TLS_KEY_OUT_OF_INDEXES is used to find out whether the key is set or |
| + // not in NoBarrier_CompareAndSwap, but Posix doesn't have invalid key, we |
| + // define an almost impossible value be it. |
| + // If we really get TLS_KEY_OUT_OF_INDEXES as value of key, just alloc |
| + // another TLS slot. |
| + if (key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES) { |
| + PlatformThreadLocalStorage::TLSKey tmp = key; |
| + CHECK(PlatformThreadLocalStorage::AllocTLS(&key) && |
| + key != PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES); |
| + PlatformThreadLocalStorage::FreeTLS(tmp); |
|
jar (doing other things)
2013/11/26 20:05:24
This and line 78 are perhaps your only use of Free
michaelbai
2013/11/27 00:29:26
Right, I want it be clean, it looks weird if there
|
| + } |
| // Atomically test-and-set the tls_key. If the key is TLS_OUT_OF_INDEXES, |
|
jar (doing other things)
2013/11/26 20:05:24
This comment about TLS_OUT_OF_INDEXES seems to hav
michaelbai
2013/11/27 00:29:26
I changed TLS_OUT_OF_INDEXES to TLS_KEY_OUT_OF_IND
|
| // go ahead and set it. Otherwise, do nothing, as another |
| // thread already did our dirty work. |
| - if (TLS_OUT_OF_INDEXES != InterlockedCompareExchange( |
| - &g_native_tls_key, value, TLS_OUT_OF_INDEXES)) { |
| + if (PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES != |
| + base::subtle::NoBarrier_CompareAndSwap( |
| + reinterpret_cast<base::subtle::Atomic32*>(&g_native_tls_key), |
| + PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES, key)) { |
| // We've been shortcut. Another thread replaced g_native_tls_key first so |
| // we need to destroy our index and use the one the other thread got |
| // first. |
| - TlsFree(value); |
| + PlatformThreadLocalStorage::FreeTLS(key); |
| } |
| } |
| - DCHECK(!TlsGetValue(g_native_tls_key)); |
| + DCHECK(!PlatformThreadLocalStorage::GetTLSValue(g_native_tls_key)); |
|
jar (doing other things)
2013/11/26 20:05:24
I think that once you declare a value to be Atomic
michaelbai
2013/11/27 00:29:26
Thanks, Done.
|
| // Some allocators, such as TCMalloc, make use of thread local storage. |
| // As a result, any attempt to call new (or malloc) will lazily cause such a |
| @@ -77,22 +91,28 @@ void** ConstructTlsVector() { |
| void* stack_allocated_tls_data[kThreadLocalStorageSize]; |
| memset(stack_allocated_tls_data, 0, sizeof(stack_allocated_tls_data)); |
| // Ensure that any rentrant calls change the temp version. |
| - TlsSetValue(g_native_tls_key, stack_allocated_tls_data); |
| + PlatformThreadLocalStorage::SetTLSValue(g_native_tls_key, |
| + stack_allocated_tls_data); |
| // Allocate an array to store our data. |
| void** tls_data = new void*[kThreadLocalStorageSize]; |
| memcpy(tls_data, stack_allocated_tls_data, sizeof(stack_allocated_tls_data)); |
| - TlsSetValue(g_native_tls_key, tls_data); |
| + PlatformThreadLocalStorage::SetTLSValue(g_native_tls_key, tls_data); |
| return tls_data; |
| } |
| -// Called when we terminate a thread, this function calls any TLS destructors |
| -// that are pending for this thread. |
| -void WinThreadExit() { |
| - if (g_native_tls_key == TLS_OUT_OF_INDEXES) |
| +} // namespace |
| + |
| +namespace base { |
| + |
| +namespace internal { |
| + |
| +void PlatformThreadLocalStorage::OnThreadExit() { |
| + if (g_native_tls_key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES) |
| return; |
| - void** tls_data = static_cast<void**>(TlsGetValue(g_native_tls_key)); |
| + void** tls_data = static_cast<void**>(GetTLSValue(g_native_tls_key)); |
| + |
| // Maybe we have never initialized TLS for this thread. |
| if (!tls_data) |
| return; |
| @@ -109,7 +129,8 @@ void WinThreadExit() { |
| void* stack_allocated_tls_data[kThreadLocalStorageSize]; |
| memcpy(stack_allocated_tls_data, tls_data, sizeof(stack_allocated_tls_data)); |
| // Ensure that any re-entrant calls change the temp version. |
| - TlsSetValue(g_native_tls_key, stack_allocated_tls_data); |
| + PlatformThreadLocalStorage::SetTLSValue(g_native_tls_key, |
| + stack_allocated_tls_data); |
| delete[] tls_data; // Our last dependence on an allocator. |
| int remaining_attempts = kMaxDestructorIterations; |
| @@ -144,12 +165,11 @@ void WinThreadExit() { |
| } |
| // Remove our stack allocated vector. |
| - TlsSetValue(g_native_tls_key, NULL); |
| + PlatformThreadLocalStorage::SetTLSValue(g_native_tls_key, NULL); |
| } |
| -} // namespace |
| +} // namespace internal |
| -namespace base { |
| ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) { |
| initialized_ = false; |
| @@ -158,11 +178,13 @@ ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) { |
| } |
| bool ThreadLocalStorage::StaticSlot::Initialize(TLSDestructorFunc destructor) { |
| - if (g_native_tls_key == TLS_OUT_OF_INDEXES || !TlsGetValue(g_native_tls_key)) |
| + if (g_native_tls_key == PlatformThreadLocalStorage::TLS_KEY_OUT_OF_INDEXES || |
| + !PlatformThreadLocalStorage::GetTLSValue(g_native_tls_key)) |
| ConstructTlsVector(); |
| // Grab a new slot. |
| - slot_ = InterlockedIncrement(&g_last_used_tls_key); |
| + slot_ = base::subtle::NoBarrier_AtomicIncrement( |
| + reinterpret_cast<base::subtle::Atomic32*>(&g_last_used_tls_key), 1); |
| DCHECK_GT(slot_, 0); |
| if (slot_ >= kThreadLocalStorageSize) { |
| NOTREACHED(); |
| @@ -186,7 +208,8 @@ void ThreadLocalStorage::StaticSlot::Free() { |
| } |
| void* ThreadLocalStorage::StaticSlot::Get() const { |
| - void** tls_data = static_cast<void**>(TlsGetValue(g_native_tls_key)); |
| + void** tls_data = static_cast<void**>( |
| + PlatformThreadLocalStorage::GetTLSValue(g_native_tls_key)); |
| if (!tls_data) |
| tls_data = ConstructTlsVector(); |
| DCHECK_GT(slot_, 0); |
| @@ -195,7 +218,8 @@ void* ThreadLocalStorage::StaticSlot::Get() const { |
| } |
| void ThreadLocalStorage::StaticSlot::Set(void* value) { |
| - void** tls_data = static_cast<void**>(TlsGetValue(g_native_tls_key)); |
| + void** tls_data = static_cast<void**>( |
| + PlatformThreadLocalStorage::GetTLSValue(g_native_tls_key)); |
| if (!tls_data) |
| tls_data = ConstructTlsVector(); |
| DCHECK_GT(slot_, 0); |
| @@ -204,74 +228,3 @@ void ThreadLocalStorage::StaticSlot::Set(void* value) { |
| } |
| } // namespace base |
| - |
| -// Thread Termination Callbacks. |
| -// Windows doesn't support a per-thread destructor with its |
| -// TLS primitives. So, we build it manually by inserting a |
| -// function to be called on each thread's exit. |
| -// This magic is from http://www.codeproject.com/threads/tls.asp |
| -// and it works for VC++ 7.0 and later. |
| - |
| -// Force a reference to _tls_used to make the linker create the TLS directory |
| -// if it's not already there. (e.g. if __declspec(thread) is not used). |
| -// Force a reference to p_thread_callback_base to prevent whole program |
| -// optimization from discarding the variable. |
| -#ifdef _WIN64 |
| - |
| -#pragma comment(linker, "/INCLUDE:_tls_used") |
| -#pragma comment(linker, "/INCLUDE:p_thread_callback_base") |
| - |
| -#else // _WIN64 |
| - |
| -#pragma comment(linker, "/INCLUDE:__tls_used") |
| -#pragma comment(linker, "/INCLUDE:_p_thread_callback_base") |
| - |
| -#endif // _WIN64 |
| - |
| -// Static callback function to call with each thread termination. |
| -void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) { |
| - // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+ |
| - // and on W2K and W2K3. So don't assume it is sent. |
| - if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason) |
| - WinThreadExit(); |
| -} |
| - |
| -// .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are |
| -// called automatically by the OS loader code (not the CRT) when the module is |
| -// loaded and on thread creation. They are NOT called if the module has been |
| -// loaded by a LoadLibrary() call. It must have implicitly been loaded at |
| -// process startup. |
| -// By implicitly loaded, I mean that it is directly referenced by the main EXE |
| -// or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being |
| -// implicitly loaded. |
| -// |
| -// See VC\crt\src\tlssup.c for reference. |
| - |
| -// extern "C" suppresses C++ name mangling so we know the symbol name for the |
| -// linker /INCLUDE:symbol pragma above. |
| -extern "C" { |
| -// The linker must not discard p_thread_callback_base. (We force a reference |
| -// to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If |
| -// this variable is discarded, the OnThreadExit function will never be called. |
| -#ifdef _WIN64 |
| - |
| -// .CRT section is merged with .rdata on x64 so it must be constant data. |
| -#pragma const_seg(".CRT$XLB") |
| -// When defining a const variable, it must have external linkage to be sure the |
| -// linker doesn't discard it. |
| -extern const PIMAGE_TLS_CALLBACK p_thread_callback_base; |
| -const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; |
| - |
| -// Reset the default section. |
| -#pragma const_seg() |
| - |
| -#else // _WIN64 |
| - |
| -#pragma data_seg(".CRT$XLB") |
| -PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; |
| - |
| -// Reset the default section. |
| -#pragma data_seg() |
| - |
| -#endif // _WIN64 |
| -} // extern "C" |