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