OLD | NEW |
---|---|
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 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES; | 22 long ThreadLocalStorage::tls_key_ = TLS_OUT_OF_INDEXES; |
23 | 23 |
24 // tls_max_ is the high-water-mark of allocated thread local storage. | 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 | 25 // We intentionally skip 0 so that it is not confused with an |
26 // unallocated TLS slot. | 26 // unallocated TLS slot. |
27 long ThreadLocalStorage::tls_max_ = 1; | 27 long ThreadLocalStorage::tls_max_ = 1; |
28 | 28 |
29 // An array of destructor function pointers for the slots. If | 29 // An array of destructor function pointers for the slots. If |
30 // a slot has a destructor, it will be stored in its corresponding | 30 // a slot has a destructor, it will be stored in its corresponding |
31 // entry in this array. | 31 // entry in this array. |
32 ThreadLocalStorage::TLSDestructorFunc | 32 subtle::AtomicWord |
33 ThreadLocalStorage::tls_destructors_[kThreadLocalStorageSize]; | 33 ThreadLocalStorage::tls_destructors_[kThreadLocalStorageSize]; |
34 | 34 |
35 void** ThreadLocalStorage::Initialize() { | 35 void** ThreadLocalStorage::Initialize() { |
36 if (tls_key_ == TLS_OUT_OF_INDEXES) { | 36 if (tls_key_ == TLS_OUT_OF_INDEXES) { |
37 long value = TlsAlloc(); | 37 long value = TlsAlloc(); |
38 DCHECK(value != TLS_OUT_OF_INDEXES); | 38 DCHECK(value != TLS_OUT_OF_INDEXES); |
39 | 39 |
40 // Atomically test-and-set the tls_key. If the key is TLS_OUT_OF_INDEXES, | 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 | 41 // go ahead and set it. Otherwise, do nothing, as another |
42 // thread already did our dirty work. | 42 // thread already did our dirty work. |
43 if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) != | 43 if (InterlockedCompareExchange(&tls_key_, value, TLS_OUT_OF_INDEXES) != |
44 TLS_OUT_OF_INDEXES) { | 44 TLS_OUT_OF_INDEXES) { |
45 // We've been shortcut. Another thread replaced tls_key_ first so we need | 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. | 46 // to destroy our index and use the one the other thread got first. |
47 TlsFree(value); | 47 TlsFree(value); |
48 } | 48 } |
49 } | 49 } |
50 DCHECK(!TlsGetValue(tls_key_)); | 50 DCHECK(!TlsGetValue(tls_key_)); |
51 | 51 |
52 // Create an array to store our data. | 52 // Use use a stack allocated vector, so that we don't have dependence |
53 // on our allocator until our service is in place. (i.e., don't | |
54 // even call new until after we're setup) | |
willchan no longer on Chromium
2011/11/28 20:58:28
You should explain that this is because an allocat
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
55 void* stack_allocated_tls_data[kThreadLocalStorageSize]; | |
56 memset(stack_allocated_tls_data, 0, sizeof(stack_allocated_tls_data)); | |
57 // Ensure that any rentrant calls change the temp version. | |
58 TlsSetValue(tls_key_, stack_allocated_tls_data); | |
59 | |
60 // Allocate an array to store our data. | |
53 void** tls_data = new void*[kThreadLocalStorageSize]; | 61 void** tls_data = new void*[kThreadLocalStorageSize]; |
54 memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize])); | 62 memcpy(tls_data, stack_allocated_tls_data, sizeof(stack_allocated_tls_data)); |
55 TlsSetValue(tls_key_, tls_data); | 63 TlsSetValue(tls_key_, tls_data); |
56 return tls_data; | 64 return tls_data; |
57 } | 65 } |
58 | 66 |
59 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) | 67 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) |
60 : initialized_(false), | 68 : initialized_(false), |
61 slot_(0) { | 69 slot_(0) { |
62 Initialize(destructor); | 70 Initialize(destructor); |
63 } | 71 } |
64 | 72 |
65 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { | 73 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { |
66 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) | 74 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) |
67 ThreadLocalStorage::Initialize(); | 75 ThreadLocalStorage::Initialize(); |
68 | 76 |
69 // Grab a new slot. | 77 // Grab a new slot. |
70 slot_ = InterlockedIncrement(&tls_max_) - 1; | 78 slot_ = InterlockedIncrement(&tls_max_) - 1; |
79 DCHECK_GT(slot_, 0); | |
71 if (slot_ >= kThreadLocalStorageSize) { | 80 if (slot_ >= kThreadLocalStorageSize) { |
72 NOTREACHED(); | 81 NOTREACHED(); |
73 return false; | 82 return false; |
74 } | 83 } |
75 | 84 |
76 // Setup our destructor. | 85 // Setup our destructor. Make sure that any thread that is dostroyed can see |
willchan no longer on Chromium
2011/11/28 21:04:32
s/dostroyed/destroyed/
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
77 tls_destructors_[slot_] = destructor; | 86 // this new setting. |
87 subtle::NoBarrier_Store(&tls_destructors_[slot_], | |
willchan no longer on Chromium
2011/11/28 21:04:32
It's not obvious to me that the NoBarrier variants
jar (doing other things)
2011/11/29 02:32:45
As per our discussion, I added a pile of comments
| |
88 reinterpret_cast<subtle::AtomicWord>(destructor)); | |
78 initialized_ = true; | 89 initialized_ = true; |
79 return true; | 90 return true; |
80 } | 91 } |
81 | 92 |
82 void ThreadLocalStorage::Slot::Free() { | 93 void ThreadLocalStorage::Slot::Free() { |
83 // At this time, we don't reclaim old indices for TLS slots. | 94 // At this time, we don't reclaim old indices for TLS slots. |
84 // So all we need to do is wipe the destructor. | 95 // So all we need to do is wipe the destructor. |
85 tls_destructors_[slot_] = NULL; | 96 DCHECK_GT(slot_, 0); |
97 DCHECK_LT(slot_, kThreadLocalStorageSize); | |
98 subtle::NoBarrier_Store(&tls_destructors_[slot_], NULL); | |
99 slot_ = 0; | |
86 initialized_ = false; | 100 initialized_ = false; |
87 } | 101 } |
88 | 102 |
89 void* ThreadLocalStorage::Slot::Get() const { | 103 void* ThreadLocalStorage::Slot::Get() const { |
90 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 104 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
91 if (!tls_data) | 105 if (!tls_data) |
92 tls_data = ThreadLocalStorage::Initialize(); | 106 tls_data = ThreadLocalStorage::Initialize(); |
93 DCHECK_GE(slot_, 0); | 107 DCHECK_GT(slot_, 0); |
94 DCHECK_LT(slot_, kThreadLocalStorageSize); | 108 DCHECK_LT(slot_, kThreadLocalStorageSize); |
95 return tls_data[slot_]; | 109 return tls_data[slot_]; |
96 } | 110 } |
97 | 111 |
98 void ThreadLocalStorage::Slot::Set(void* value) { | 112 void ThreadLocalStorage::Slot::Set(void* value) { |
99 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 113 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
100 if (!tls_data) | 114 if (!tls_data) |
101 tls_data = ThreadLocalStorage::Initialize(); | 115 tls_data = ThreadLocalStorage::Initialize(); |
102 DCHECK_GE(slot_, 0); | 116 DCHECK_GT(slot_, 0); |
103 DCHECK_LT(slot_, kThreadLocalStorageSize); | 117 DCHECK_LT(slot_, kThreadLocalStorageSize); |
104 tls_data[slot_] = value; | 118 tls_data[slot_] = value; |
105 } | 119 } |
106 | 120 |
107 void ThreadLocalStorage::ThreadExit() { | 121 void ThreadLocalStorage::ThreadExit() { |
108 if (tls_key_ == TLS_OUT_OF_INDEXES) | 122 if (tls_key_ == TLS_OUT_OF_INDEXES) |
109 return; | 123 return; |
110 | 124 |
111 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 125 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
112 | |
113 // Maybe we have never initialized TLS for this thread. | 126 // Maybe we have never initialized TLS for this thread. |
114 if (!tls_data) | 127 if (!tls_data) |
115 return; | 128 return; |
116 | 129 |
117 for (int slot = 0; slot < tls_max_; slot++) { | 130 // Switch to using a stack allocated vector, so that we don't have dependence |
118 if (tls_destructors_[slot] != NULL) { | 131 // on our allocator after we have called all tls_destructors_. (i.e., don't |
119 void* value = tls_data[slot]; | 132 // even call delete[] after we're done with destructors.) |
willchan no longer on Chromium
2011/11/28 20:58:28
You should again explain that this is because the
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
120 tls_destructors_[slot](value); | 133 void* stack_allocated_tls_data[kThreadLocalStorageSize]; |
134 memcpy(stack_allocated_tls_data, tls_data, sizeof(stack_allocated_tls_data)); | |
135 // Ensure that any rentrant calls change the temp version. | |
136 TlsSetValue(tls_key_, stack_allocated_tls_data); | |
137 delete[] tls_data; // Our last dependence on an allocator. | |
138 | |
139 int remaining_attempts = PTHREAD_DESTRUCTOR_ITERATIONS; | |
140 bool need_to_scan_destructors = true; | |
141 while (need_to_scan_destructors) { | |
142 need_to_scan_destructors = false; | |
143 // Try to destroy the first-created-slot (which is slot 1) in our last | |
144 // destructor call. That user was able to function, and define a slot with | |
145 // no other services running, so perhaps it is a basic service (like an | |
146 // allocator) and should also be destroyed last. | |
147 for (int slot = tls_max_ - 1; slot > 0; --slot) { | |
148 void* value = stack_allocated_tls_data[slot]; | |
149 if (value == NULL) | |
150 continue; | |
151 TLSDestructorFunc destructor = | |
152 reinterpret_cast<ThreadLocalStorage::TLSDestructorFunc>( | |
153 subtle::NoBarrier_Load(&tls_destructors_[slot])); | |
154 if (destructor == NULL) | |
155 continue; | |
156 stack_allocated_tls_data[slot] = NULL; // pre-clear the slot. | |
157 destructor(value); | |
158 // Any destructor might have called a different service, which then set | |
159 // a different slot to a non-NULL value. Hence we need to check | |
160 // the whole vector again. This is a pthread standard. | |
161 need_to_scan_destructors = true; | |
162 } | |
163 if (--remaining_attempts <= 0) { | |
164 NOTREACHED(); // Destructors might not have been called. | |
165 break; | |
121 } | 166 } |
122 } | 167 } |
123 | 168 |
124 delete[] tls_data; | 169 // Remove our stack allocated vector. |
125 | |
126 // In case there are other "onexit" handlers... | |
127 TlsSetValue(tls_key_, NULL); | 170 TlsSetValue(tls_key_, NULL); |
128 } | 171 } |
129 | 172 |
130 } // namespace base | 173 } // namespace base |
131 | 174 |
132 // Thread Termination Callbacks. | 175 // Thread Termination Callbacks. |
133 // Windows doesn't support a per-thread destructor with its | 176 // Windows doesn't support a per-thread destructor with its |
134 // TLS primitives. So, we build it manually by inserting a | 177 // TLS primitives. So, we build it manually by inserting a |
135 // function to be called on each thread's exit. | 178 // function to be called on each thread's exit. |
136 // This magic is from http://www.codeproject.com/threads/tls.asp | 179 // This magic is from http://www.codeproject.com/threads/tls.asp |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
192 #else // _WIN64 | 235 #else // _WIN64 |
193 | 236 |
194 #pragma data_seg(".CRT$XLB") | 237 #pragma data_seg(".CRT$XLB") |
195 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; | 238 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; |
196 | 239 |
197 // Reset the default section. | 240 // Reset the default section. |
198 #pragma data_seg() | 241 #pragma data_seg() |
199 | 242 |
200 #endif // _WIN64 | 243 #endif // _WIN64 |
201 } // extern "C" | 244 } // extern "C" |
OLD | NEW |