1 // Copyright (c) 2011 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 // In order to make TLS destructors work, we need to keep function
14 // pointers to the destructor for each TLS that we allocate.
15 // We make this work by allocating a single OS-level TLS, which
16 // contains an array of slots for the application to use. In
17 // parallel, we also allocate an array of destructors, which we
18 // keep track of and call when threads terminate.
19
20 // tls_key_ is the one native TLS that we use. It stores our
21 // table.
22 long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES;
23
24 // tls_max_ is the high-water-mark of allocated thread local storage.
25 // We intentionally skip 0 so that it is not confused with an
26 // unallocated TLS slot.
27 long ThreadLocalStorage::tls_max_ = 1;
28
29 // An array of destructor function pointers for the slots. If
30 // a slot has a destructor, it will be stored in its corresponding
31 // entry in this array.
32 ThreadLocalStorage::TLSDestructorFunc
33 ThreadLocalStorage::tls_destructors_[kThreadLocalStorageSize];
34
Initialize()35 void** ThreadLocalStorage::Initialize() {
36 if (tls_key_ == TLS_OUT_OF_INDEXES) {
37 long value = TlsAlloc();
38 DCHECK(value != TLS_OUT_OF_INDEXES);
39
40 // Atomically test-and-set the tls_key. If the key is TLS_OUT_OF_INDEXES,
41 // go ahead and set it. Otherwise, do nothing, as another
42 // thread already did our dirty work.
43 if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) !=
44 TLS_OUT_OF_INDEXES) {
45 // We've been shortcut. Another thread replaced tls_key_ first so we need
46 // to destroy our index and use the one the other thread got first.
47 TlsFree(value);
48 }
49 }
50 DCHECK(!TlsGetValue(tls_key_));
51
52 // Create an array to store our data.
53 void** tls_data = new void*[kThreadLocalStorageSize];
54 memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize]));
55 TlsSetValue(tls_key_, tls_data);
56 return tls_data;
57 }
58
Slot(TLSDestructorFunc destructor)59 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor)
60 : initialized_(false),
61 slot_(0) {
62 Initialize(destructor);
63 }
64
Initialize(TLSDestructorFunc destructor)65 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) {
66 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_))
67 ThreadLocalStorage::Initialize();
68
69 // Grab a new slot.
70 slot_ = InterlockedIncrement(&tls_max_) - 1;
71 if (slot_ >= kThreadLocalStorageSize) {
72 NOTREACHED();
73 return false;
74 }
75
76 // Setup our destructor.
77 tls_destructors_[slot_] = destructor;
78 initialized_ = true;
79 return true;
80 }
81
Free()82 void ThreadLocalStorage::Slot::Free() {
83 // At this time, we don't reclaim old indices for TLS slots.
84 // So all we need to do is wipe the destructor.
85 tls_destructors_[slot_] = NULL;
86 initialized_ = false;
87 }
88
Get() const89 void* ThreadLocalStorage::Slot::Get() const {
90 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
91 if (!tls_data)
92 tls_data = ThreadLocalStorage::Initialize();
93 DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize);
94 return tls_data[slot_];
95 }
96
Set(void * value)97 void ThreadLocalStorage::Slot::Set(void* value) {
98 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
99 if (!tls_data)
100 tls_data = ThreadLocalStorage::Initialize();
101 DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize);
102 tls_data[slot_] = value;
103 }
104
ThreadExit()105 void ThreadLocalStorage::ThreadExit() {
106 if (tls_key_ == TLS_OUT_OF_INDEXES)
107 return;
108
109 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
110
111 // Maybe we have never initialized TLS for this thread.
112 if (!tls_data)
113 return;
114
115 for (int slot = 0; slot < tls_max_; slot++) {
116 if (tls_destructors_[slot] != NULL) {
117 void* value = tls_data[slot];
118 tls_destructors_[slot](value);
119 }
120 }
121
122 delete[] tls_data;
123
124 // In case there are other "onexit" handlers...
125 TlsSetValue(tls_key_, NULL);
126 }
127
128 } // namespace base
129
130 // Thread Termination Callbacks.
131 // Windows doesn't support a per-thread destructor with its
132 // TLS primitives. So, we build it manually by inserting a
133 // function to be called on each thread's exit.
134 // This magic is from http://www.codeproject.com/threads/tls.asp
135 // and it works for VC++ 7.0 and later.
136
137 // Force a reference to _tls_used to make the linker create the TLS directory
138 // if it's not already there. (e.g. if __declspec(thread) is not used).
139 // Force a reference to p_thread_callback_base to prevent whole program
140 // optimization from discarding the variable.
141 #ifdef _WIN64
142
143 #pragma comment(linker, "/INCLUDE:_tls_used")
144 #pragma comment(linker, "/INCLUDE:p_thread_callback_base")
145
146 #else // _WIN64
147
148 #pragma comment(linker, "/INCLUDE:__tls_used")
149 #pragma comment(linker, "/INCLUDE:_p_thread_callback_base")
150
151 #endif // _WIN64
152
153 // Static callback function to call with each thread termination.
OnThreadExit(PVOID module,DWORD reason,PVOID reserved)154 void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved) {
155 // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+
156 // and on W2K and W2K3. So don't assume it is sent.
157 if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
158 base::ThreadLocalStorage::ThreadExit();
159 }
160
161 // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
162 // called automatically by the OS loader code (not the CRT) when the module is
163 // loaded and on thread creation. They are NOT called if the module has been
164 // loaded by a LoadLibrary() call. It must have implicitly been loaded at
165 // process startup.
166 // By implicitly loaded, I mean that it is directly referenced by the main EXE
167 // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
168 // implicitly loaded.
169 //
170 // See VC\crt\src\tlssup.c for reference.
171
172 // extern "C" suppresses C++ name mangling so we know the symbol name for the
173 // linker /INCLUDE:symbol pragma above.
174 extern "C" {
175 // The linker must not discard p_thread_callback_base. (We force a reference
176 // to this variable with a linker /INCLUDE:symbol pragma to ensure that.) If
177 // this variable is discarded, the OnThreadExit function will never be called.
178 #ifdef _WIN64
179
180 // .CRT section is merged with .rdata on x64 so it must be constant data.
181 #pragma const_seg(".CRT$XLB")
182 // When defining a const variable, it must have external linkage to be sure the
183 // linker doesn't discard it.
184 extern const PIMAGE_TLS_CALLBACK p_thread_callback_base;
185 const PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
186
187 // Reset the default section.
188 #pragma const_seg()
189
190 #else // _WIN64
191
192 #pragma data_seg(".CRT$XLB")
193 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
194
195 // Reset the default section.
196 #pragma data_seg()
197
198 #endif // _WIN64
199 } // extern "C"
200