Chromium Code Reviews| Index: base/threading/thread_local_storage_win.cc |
| =================================================================== |
| --- base/threading/thread_local_storage_win.cc (revision 112058) |
| +++ base/threading/thread_local_storage_win.cc (working copy) |
| @@ -12,6 +12,22 @@ |
| namespace base { |
| namespace { |
| +// In order to make TLS destructors work, we need to keep function |
| +// pointers to the destructor for each TLS that we allocate. |
| +// We make this work by allocating a single OS-level TLS, which |
| +// contains an array of slots for the application to use. In |
| +// parallel, we also allocate an array of destructors, which we |
| +// keep track of and call when threads terminate. |
| + |
| +// windows_native_tls_key is the one native TLS that we use. It stores our |
| +// table. |
| +long windows_native_tls_key = TLS_OUT_OF_INDEXES; |
|
rvargas (doing something else)
2011/11/30 04:00:23
Shouldn't we move to g_windows_native... ?
jar (doing other things)
2011/11/30 07:31:30
Done.
|
| + |
| +// last_used_tls_key is the high-water-mark of allocated thread local storage. |
| +// We intentionally skip 0 (claiming it was used) so that it is not confused |
|
rvargas (doing something else)
2011/11/30 04:00:23
Remove the comment about skipping 0
jar (doing other things)
2011/11/30 07:31:30
This is a somewhat significant comment, even thoug
rvargas (doing something else)
2011/11/30 18:48:41
Right... I got confused by the change to 0 here (e
|
| +// with an unallocated TLS slot. |
| +long last_used_tls_key = 0; |
| + |
| // The maximum number of 'slots' in our thread local storage stack. |
| const int kThreadLocalStorageSize = 64; |
| @@ -33,38 +49,23 @@ |
| } // namespace anonymous |
| -// In order to make TLS destructors work, we need to keep function |
| -// pointers to the destructor for each TLS that we allocate. |
| -// We make this work by allocating a single OS-level TLS, which |
| -// contains an array of slots for the application to use. In |
| -// parallel, we also allocate an array of destructors, which we |
| -// keep track of and call when threads terminate. |
| - |
| -// tls_key_ is the one native TLS that we use. It stores our |
| -// table. |
| -long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES; |
| - |
| -// tls_max_ is the high-water-mark of allocated thread local storage. |
| -// We intentionally skip 0 so that it is not confused with an |
| -// unallocated TLS slot. |
| -long ThreadLocalStorage::tls_max_ = 1; |
| - |
| void** ThreadLocalStorage::Initialize() { |
| - if (tls_key_ == TLS_OUT_OF_INDEXES) { |
| + if (windows_native_tls_key == TLS_OUT_OF_INDEXES) { |
| long value = TlsAlloc(); |
| DCHECK(value != TLS_OUT_OF_INDEXES); |
| // Atomically test-and-set the tls_key. If the key is TLS_OUT_OF_INDEXES, |
| // go ahead and set it. Otherwise, do nothing, as another |
| // thread already did our dirty work. |
| - if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) != |
| - TLS_OUT_OF_INDEXES) { |
| - // We've been shortcut. Another thread replaced tls_key_ first so we need |
| - // to destroy our index and use the one the other thread got first. |
| + if (TLS_OUT_OF_INDEXES != InterlockedCompareExchange( |
| + &windows_native_tls_key, value, TLS_OUT_OF_INDEXES)) { |
|
rvargas (doing something else)
2011/11/30 04:00:23
nit: needs two extra spaces here
jar (doing other things)
2011/11/30 07:31:30
Done.
|
| + // We've been shortcut. Another thread replaced windows_native_tls_key |
| + // first so we need to destroy our index and use the one the other thread |
| + // got first. |
| TlsFree(value); |
| } |
| } |
| - DCHECK(!TlsGetValue(tls_key_)); |
| + DCHECK(!TlsGetValue(windows_native_tls_key)); |
| // 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,12 +78,12 @@ |
| 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(tls_key_, stack_allocated_tls_data); |
| + TlsSetValue(windows_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(tls_key_, tls_data); |
| + TlsSetValue(windows_native_tls_key, tls_data); |
| return tls_data; |
| } |
| @@ -93,11 +94,12 @@ |
| } |
| bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { |
| - if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) |
| + if (windows_native_tls_key == TLS_OUT_OF_INDEXES || |
| + !TlsGetValue(windows_native_tls_key)) |
| ThreadLocalStorage::Initialize(); |
| // Grab a new slot. |
| - slot_ = InterlockedIncrement(&tls_max_) - 1; |
| + slot_ = InterlockedIncrement(&last_used_tls_key); |
| DCHECK_GT(slot_, 0); |
| if (slot_ >= kThreadLocalStorageSize) { |
| NOTREACHED(); |
| @@ -121,7 +123,7 @@ |
| } |
| void* ThreadLocalStorage::Slot::Get() const { |
| - void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| + void** tls_data = static_cast<void**>(TlsGetValue(windows_native_tls_key)); |
| if (!tls_data) |
| tls_data = ThreadLocalStorage::Initialize(); |
| DCHECK_GT(slot_, 0); |
| @@ -130,7 +132,7 @@ |
| } |
| void ThreadLocalStorage::Slot::Set(void* value) { |
| - void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| + void** tls_data = static_cast<void**>(TlsGetValue(windows_native_tls_key)); |
| if (!tls_data) |
| tls_data = ThreadLocalStorage::Initialize(); |
| DCHECK_GT(slot_, 0); |
| @@ -139,10 +141,10 @@ |
| } |
| void ThreadLocalStorage::ThreadExit() { |
| - if (tls_key_ == TLS_OUT_OF_INDEXES) |
| + if (windows_native_tls_key == TLS_OUT_OF_INDEXES) |
| return; |
| - void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| + void** tls_data = static_cast<void**>(TlsGetValue(windows_native_tls_key)); |
| // Maybe we have never initialized TLS for this thread. |
| if (!tls_data) |
| return; |
| @@ -159,7 +161,7 @@ |
| 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(tls_key_, stack_allocated_tls_data); |
| + TlsSetValue(windows_native_tls_key, stack_allocated_tls_data); |
| delete[] tls_data; // Our last dependence on an allocator. |
| int remaining_attempts = kMaxDestructorIterations; |
| @@ -172,7 +174,7 @@ |
| // allocator) and should also be destroyed last. If we get the order wrong, |
| // then we'll itterate several more times, so it is really not that |
| // critical (but it might help). |
| - for (int slot = tls_max_ - 1; slot > 0; --slot) { |
| + for (int slot = last_used_tls_key; slot > 0; --slot) { |
| void* value = stack_allocated_tls_data[slot]; |
| if (value == NULL) |
| continue; |
| @@ -193,78 +195,7 @@ |
| } |
| // Remove our stack allocated vector. |
| - TlsSetValue(tls_key_, NULL); |
| + TlsSetValue(windows_native_tls_key, NULL); |
| } |
| } // 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) |
| - base::ThreadLocalStorage::ThreadExit(); |
| -} |
| - |
| -// .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" |