ipc/chromium/src/base/thread_local_storage_win.cc

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:5d0bd4abcd13
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 #ifdef _WIN64
133
134 // This makes the linker create the TLS directory if it's not already
135 // there. (e.g. if __declspec(thread) is not used).
136 #pragma comment(linker, "/INCLUDE:_tls_used")
137
138 #else // _WIN64
139
140 // This makes the linker create the TLS directory if it's not already
141 // there. (e.g. if __declspec(thread) is not used).
142 #pragma comment(linker, "/INCLUDE:__tls_used")
143
144 #endif // _WIN64
145
146 // Static callback function to call with each thread termination.
147 void NTAPI OnThreadExit(PVOID module, DWORD reason, PVOID reserved)
148 {
149 // On XP SP0 & SP1, the DLL_PROCESS_ATTACH is never seen. It is sent on SP2+
150 // and on W2K and W2K3. So don't assume it is sent.
151 if (DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason)
152 ThreadLocalStorage::ThreadExit();
153 }
154
155 // .CRT$XLA to .CRT$XLZ is an array of PIMAGE_TLS_CALLBACK pointers that are
156 // called automatically by the OS loader code (not the CRT) when the module is
157 // loaded and on thread creation. They are NOT called if the module has been
158 // loaded by a LoadLibrary() call. It must have implicitly been loaded at
159 // process startup.
160 // By implicitly loaded, I mean that it is directly referenced by the main EXE
161 // or by one of its dependent DLLs. Delay-loaded DLL doesn't count as being
162 // implicitly loaded.
163 //
164 // See VC\crt\src\tlssup.c for reference.
165 #ifdef _WIN64
166
167 // .CRT section is merged with .rdata on x64 so it must be constant data.
168 #pragma const_seg(".CRT$XLB")
169 // When defining a const variable, it must have external linkage to be sure the
170 // linker doesn't discard it. If this value is discarded, the OnThreadExit
171 // function will never be called.
172 extern const PIMAGE_TLS_CALLBACK p_thread_callback;
173 const PIMAGE_TLS_CALLBACK p_thread_callback = OnThreadExit;
174
175 // Reset the default section.
176 #pragma const_seg()
177
178 #else // _WIN64
179
180 #pragma data_seg(".CRT$XLB")
181 PIMAGE_TLS_CALLBACK p_thread_callback = OnThreadExit;
182
183 // Reset the default section.
184 #pragma data_seg()
185
186 #endif // _WIN64

mercurial