Home | History | Annotate | Download | only in threading
      1 // Copyright (c) 2012 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 
      5 #include "base/threading/thread_local_storage.h"
      6 
      7 #include <windows.h>
      8 
      9 #include "base/logging.h"
     10 
     11 namespace base {
     12 
     13 namespace internal {
     14 
     15 bool PlatformThreadLocalStorage::AllocTLS(TLSKey* key) {
     16   TLSKey value = TlsAlloc();
     17   if (value != TLS_OUT_OF_INDEXES) {
     18     *key = value;
     19     return true;
     20   }
     21   return false;
     22 }
     23 
     24 void PlatformThreadLocalStorage::FreeTLS(TLSKey key) {
     25   BOOL ret = TlsFree(key);
     26   DCHECK(ret);
     27 }
     28 
     29 void* PlatformThreadLocalStorage::GetTLSValue(TLSKey key) {
     30   return TlsGetValue(key);
     31 }
     32 
     33 void PlatformThreadLocalStorage::SetTLSValue(TLSKey key, void* value) {
     34   BOOL ret = TlsSetValue(key, value);
     35   DCHECK(ret);
     36 }
     37 
     38 }  // namespace internal
     39 
     40 }  // namespace base
     41 
     42 // Thread Termination Callbacks.
     43 // Windows doesn't support a per-thread destructor with its
     44 // TLS primitives.  So, we build it manually by inserting a
     45 // function to be called on each thread's exit.
     46 // This magic is from http://www.codeproject.com/threads/tls.asp
     47 // and it works for VC++ 7.0 and later.
     48 
     49 // Force a reference to _tls_used to make the linker create the TLS directory
     50 // if it's not already there.  (e.g. if __declspec(thread) is not used).
     51 // Force a reference to p_thread_callback_base to prevent whole program
     52 // optimization from discarding the variable.
     53 #ifdef _WIN64
     54 
     55 #pragma comment(linker, "/INCLUDE:_tls_used")
     56 #pragma comment(linker, "/INCLUDE:p_thread_callback_base")
     57 
     58 #else  // _WIN64
     59 
     60 #pragma comment(linker, "/INCLUDE:__tls_used")
     61 #pragma comment(linker, "/INCLUDE:_p_thread_callback_base")
     62 
     63 #endif  // _WIN64
     64 
     65 // Static callback function to call with each thread termination.
     66 void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
     67   // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+
     68   // and on W2K and W2K3. So don't assume it is sent.
     69   if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
     70     base::internal::PlatformThreadLocalStorage::OnThreadExit();
     71 }
     72 
     73 // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
     74 // called automatically by the OS loader code (not the CRT) when the module is
     75 // loaded and on thread creation. They are NOT called if the module has been
     76 // loaded by a LoadLibrary() call. It must have implicitly been loaded at
     77 // process startup.
     78 // By implicitly loaded, I mean that it is directly referenced by the main EXE
     79 // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
     80 // implicitly loaded.
     81 //
     82 // See VC\crt\src\tlssup.c for reference.
     83 
     84 // extern "C" suppresses C++ name mangling so we know the symbol name for the
     85 // linker /INCLUDE:symbol pragma above.
     86 extern "C" {
     87 // The linker must not discard p_thread_callback_base.  (We force a reference
     88 // to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
     89 // this variable is discarded, the OnThreadExit function will never be called.
     90 #ifdef _WIN64
     91 
     92 // .CRT section is merged with .rdata on x64 so it must be constant data.
     93 #pragma const_seg(".CRT$XLB")
     94 // When defining a const variable, it must have external linkage to be sure the
     95 // linker doesn't discard it.
     96 extern const PIMAGE_TLS_CALLBACK p_thread_callback_base;
     97 const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
     98 
     99 // Reset the default section.
    100 #pragma const_seg()
    101 
    102 #else  // _WIN64
    103 
    104 #pragma data_seg(".CRT$XLB")
    105 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
    106 
    107 // Reset the default section.
    108 #pragma data_seg()
    109 
    110 #endif  // _WIN64
    111 }  // extern "C"
    112