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