Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: base/threading/thread_local_storage_win.cc

Issue 8702014: Make ThreadLocalStorage more posix pthread compliant (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/threading/thread_local_storage_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/threading/thread_local_storage.h" 5 #include "base/threading/thread_local_storage.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 10
11
11 namespace base { 12 namespace base {
12 13
14 namespace {
15 // The maximum number of 'slots' in our thread local storage stack.
16 const int kThreadLocalStorageSize = 64;
17
18 // The maximum number of times to try to clear slots by calling destructors.
19 // Use pthread naming convention for clarity.
20 const int kMaxDestructorIterations = kThreadLocalStorageSize;
21
22 // An array of destructor function pointers for the slots. If a slot has a
23 // destructor, it will be stored in its corresponding entry in this array.
24 // The elements are volatile to ensure that when the compiler reads the value
25 // to potentially call the destructor, it does so once, and that value is tested
26 // for null-ness and then used. Yes, that would be a weird de-optimization,
27 // but I can imagine some register machines where it was just as easy to
28 // re-fetch an array element, and I want to be sure a call to free the key
29 // (i.e., null out the destructor entry) that happens on a separate thread can't
30 // hurt the racy calls to the destructors on another thread.
31 volatile ThreadLocalStorage::TLSDestructorFunc
willchan no longer on Chromium 2011/11/29 18:52:14 I'm always super wary of use of volatile. Are you
jar (doing other things) 2011/11/29 19:53:22 I'm beyond super wary, and almost tend to think of
rvargas (doing something else) 2011/11/29 19:55:22 Given that this is Windows specific code (and assu
32 g_tls_destructors[kThreadLocalStorageSize];
33
34 } // namespace anonymous
35
13 // In order to make TLS destructors work, we need to keep function 36 // In order to make TLS destructors work, we need to keep function
14 // pointers to the destructor for each TLS that we allocate. 37 // pointers to the destructor for each TLS that we allocate.
15 // We make this work by allocating a single OS-level TLS, which 38 // We make this work by allocating a single OS-level TLS, which
16 // contains an array of slots for the application to use. In 39 // contains an array of slots for the application to use. In
17 // parallel, we also allocate an array of destructors, which we 40 // parallel, we also allocate an array of destructors, which we
18 // keep track of and call when threads terminate. 41 // keep track of and call when threads terminate.
19 42
20 // tls_key_ is the one native TLS that we use. It stores our 43 // tls_key_ is the one native TLS that we use. It stores our
21 // table. 44 // table.
22 long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES; 45 long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES;
23 46
24 // tls_max_ is the high-water-mark of allocated thread local storage. 47 // 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 48 // We intentionally skip 0 so that it is not confused with an
26 // unallocated TLS slot. 49 // unallocated TLS slot.
27 long ThreadLocalStorage::tls_max_ = 1; 50 long ThreadLocalStorage::tls_max_ = 1;
28 51
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
35 void** ThreadLocalStorage::Initialize() { 52 void** ThreadLocalStorage::Initialize() {
36 if (tls_key_ == TLS_OUT_OF_INDEXES) { 53 if (tls_key_ == TLS_OUT_OF_INDEXES) {
37 long value = TlsAlloc(); 54 long value = TlsAlloc();
38 DCHECK(value != TLS_OUT_OF_INDEXES); 55 DCHECK(value != TLS_OUT_OF_INDEXES);
39 56
40 // Atomically test-and-set the tls_key. If the key is TLS_OUT_OF_INDEXES, 57 // 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 58 // go ahead and set it. Otherwise, do nothing, as another
42 // thread already did our dirty work. 59 // thread already did our dirty work.
43 if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) != 60 if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) !=
44 TLS_OUT_OF_INDEXES) { 61 TLS_OUT_OF_INDEXES) {
45 // We've been shortcut. Another thread replaced tls_key_ first so we need 62 // 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. 63 // to destroy our index and use the one the other thread got first.
47 TlsFree(value); 64 TlsFree(value);
48 } 65 }
49 } 66 }
50 DCHECK(!TlsGetValue(tls_key_)); 67 DCHECK(!TlsGetValue(tls_key_));
51 68
52 // Create an array to store our data. 69 // Some allocators, such as TCMalloc, make use of thread local storage.
70 // As a result, any attempt to call new (or malloc) will lazily cause such a
71 // system to initialize, which will include registering for a TLS key. If we
72 // are not careful here, then that request to create a key will call new back,
73 // and we'll have an infinite loop. We avoid that as follows:
74 // Use a stack allocated vector, so that we don't have dependence on our
75 // allocator until our service is in place. (i.e., don't even call new until
76 // after we're setup)
77 void* stack_allocated_tls_data[kThreadLocalStorageSize];
78 memset(stack_allocated_tls_data, 0, sizeof(stack_allocated_tls_data));
79 // Ensure that any rentrant calls change the temp version.
80 TlsSetValue(tls_key_, stack_allocated_tls_data);
81
82 // Allocate an array to store our data.
53 void** tls_data = new void*[kThreadLocalStorageSize]; 83 void** tls_data = new void*[kThreadLocalStorageSize];
54 memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize])); 84 memcpy(tls_data, stack_allocated_tls_data, sizeof(stack_allocated_tls_data));
55 TlsSetValue(tls_key_, tls_data); 85 TlsSetValue(tls_key_, tls_data);
56 return tls_data; 86 return tls_data;
57 } 87 }
58 88
59 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) 89 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor)
60 : initialized_(false), 90 : initialized_(false),
61 slot_(0) { 91 slot_(0) {
62 Initialize(destructor); 92 Initialize(destructor);
63 } 93 }
64 94
65 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { 95 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) {
66 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) 96 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_))
67 ThreadLocalStorage::Initialize(); 97 ThreadLocalStorage::Initialize();
68 98
69 // Grab a new slot. 99 // Grab a new slot.
70 slot_ = InterlockedIncrement(&tls_max_) - 1; 100 slot_ = InterlockedIncrement(&tls_max_) - 1;
101 DCHECK_GT(slot_, 0);
71 if (slot_ >= kThreadLocalStorageSize) { 102 if (slot_ >= kThreadLocalStorageSize) {
72 NOTREACHED(); 103 NOTREACHED();
73 return false; 104 return false;
74 } 105 }
75 106
76 // Setup our destructor. 107 // Setup our destructor.
77 tls_destructors_[slot_] = destructor; 108 g_tls_destructors[slot_] = destructor;
78 initialized_ = true; 109 initialized_ = true;
79 return true; 110 return true;
80 } 111 }
81 112
82 void ThreadLocalStorage::Slot::Free() { 113 void ThreadLocalStorage::Slot::Free() {
83 // At this time, we don't reclaim old indices for TLS slots. 114 // At this time, we don't reclaim old indices for TLS slots.
84 // So all we need to do is wipe the destructor. 115 // So all we need to do is wipe the destructor.
85 tls_destructors_[slot_] = NULL; 116 DCHECK_GT(slot_, 0);
117 DCHECK_LT(slot_, kThreadLocalStorageSize);
118 g_tls_destructors[slot_] = NULL;
willchan no longer on Chromium 2011/11/29 18:52:14 extra horizontal whitespace?
jar (doing other things) 2011/11/29 19:53:22 Done.
119 slot_ = 0;
86 initialized_ = false; 120 initialized_ = false;
87 } 121 }
88 122
89 void* ThreadLocalStorage::Slot::Get() const { 123 void* ThreadLocalStorage::Slot::Get() const {
90 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); 124 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
91 if (!tls_data) 125 if (!tls_data)
92 tls_data = ThreadLocalStorage::Initialize(); 126 tls_data = ThreadLocalStorage::Initialize();
93 DCHECK_GE(slot_, 0); 127 DCHECK_GT(slot_, 0);
94 DCHECK_LT(slot_, kThreadLocalStorageSize); 128 DCHECK_LT(slot_, kThreadLocalStorageSize);
95 return tls_data[slot_]; 129 return tls_data[slot_];
96 } 130 }
97 131
98 void ThreadLocalStorage::Slot::Set(void* value) { 132 void ThreadLocalStorage::Slot::Set(void* value) {
99 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); 133 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
100 if (!tls_data) 134 if (!tls_data)
101 tls_data = ThreadLocalStorage::Initialize(); 135 tls_data = ThreadLocalStorage::Initialize();
102 DCHECK_GE(slot_, 0); 136 DCHECK_GT(slot_, 0);
103 DCHECK_LT(slot_, kThreadLocalStorageSize); 137 DCHECK_LT(slot_, kThreadLocalStorageSize);
104 tls_data[slot_] = value; 138 tls_data[slot_] = value;
105 } 139 }
106 140
107 void ThreadLocalStorage::ThreadExit() { 141 void ThreadLocalStorage::ThreadExit() {
108 if (tls_key_ == TLS_OUT_OF_INDEXES) 142 if (tls_key_ == TLS_OUT_OF_INDEXES)
109 return; 143 return;
110 144
111 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); 145 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_));
112
113 // Maybe we have never initialized TLS for this thread. 146 // Maybe we have never initialized TLS for this thread.
114 if (!tls_data) 147 if (!tls_data)
115 return; 148 return;
116 149
117 for (int slot = 0; slot < tls_max_; slot++) { 150 // Some allocators, such as TCMalloc, use TLS. As a result, when a thread
118 if (tls_destructors_[slot] != NULL) { 151 // terminates, one of the destructor calls we make may be to shut down an
119 void* value = tls_data[slot]; 152 // allocator. We have to be careful that after we've shutdown all of the
120 tls_destructors_[slot](value); 153 // known destructors (perchance including an allocator), that we don't call
154 // the allocator and cause it to resurrect itself (with no possibly destructor
155 // call to follow). We handle this problem as follows:
156 // Switch to using a stack allocated vector, so that we don't have dependence
157 // on our allocator after we have called all g_tls_destructors. (i.e., don't
158 // even call delete[] after we're done with destructors.)
159 void* stack_allocated_tls_data[kThreadLocalStorageSize];
160 memcpy(stack_allocated_tls_data, tls_data, sizeof(stack_allocated_tls_data));
161 // Ensure that any re-entrant calls change the temp version.
162 TlsSetValue(tls_key_, stack_allocated_tls_data);
163 delete[] tls_data; // Our last dependence on an allocator.
164
165 int remaining_attempts = kMaxDestructorIterations;
166 bool need_to_scan_destructors = true;
167 while (need_to_scan_destructors) {
168 need_to_scan_destructors = false;
169 // Try to destroy the first-created-slot (which is slot 1) in our last
170 // destructor call. That user was able to function, and define a slot with
171 // no other services running, so perhaps it is a basic service (like an
172 // allocator) and should also be destroyed last. If we get the order wrong,
173 // then we'll itterate several more times, so it is really not that
174 // critical (but it might help).
175 for (int slot = tls_max_ - 1; slot > 0; --slot) {
176 void* value = stack_allocated_tls_data[slot];
177 if (value == NULL)
178 continue;
179 TLSDestructorFunc destructor = g_tls_destructors[slot];
180 if (destructor == NULL)
181 continue;
182 stack_allocated_tls_data[slot] = NULL; // pre-clear the slot.
183 destructor(value);
184 // Any destructor might have called a different service, which then set
185 // a different slot to a non-NULL value. Hence we need to check
186 // the whole vector again. This is a pthread standard.
187 need_to_scan_destructors = true;
188 }
189 if (--remaining_attempts <= 0) {
190 NOTREACHED(); // Destructors might not have been called.
191 break;
121 } 192 }
122 } 193 }
123 194
124 delete[] tls_data; 195 // Remove our stack allocated vector.
125
126 // In case there are other "onexit" handlers...
127 TlsSetValue(tls_key_, NULL); 196 TlsSetValue(tls_key_, NULL);
128 } 197 }
129 198
130 } // namespace base 199 } // namespace base
131 200
132 // Thread Termination Callbacks. 201 // Thread Termination Callbacks.
133 // Windows doesn't support a per-thread destructor with its 202 // Windows doesn't support a per-thread destructor with its
134 // TLS primitives. So, we build it manually by inserting a 203 // TLS primitives. So, we build it manually by inserting a
135 // function to be called on each thread's exit. 204 // function to be called on each thread's exit.
136 // This magic is from http://www.codeproject.com/threads/tls.asp 205 // This magic is from http://www.codeproject.com/threads/tls.asp
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 #else // _WIN64 261 #else // _WIN64
193 262
194 #pragma data_seg(".CRT$XLB") 263 #pragma data_seg(".CRT$XLB")
195 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; 264 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit;
196 265
197 // Reset the default section. 266 // Reset the default section.
198 #pragma data_seg() 267 #pragma data_seg()
199 268
200 #endif // _WIN64 269 #endif // _WIN64
201 } // extern "C" 270 } // extern "C"
OLDNEW
« no previous file with comments | « base/threading/thread_local_storage_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698