Chromium Code Reviews| 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 // Some allocators, such as TCMalloc, make use of thread local storage. |
| 53 // As a result, any attempt to call new (or malloc) will lazilly cause such a | |
|
willchan no longer on Chromium
2011/11/29 01:46:49
s/lazilly/lazily/g
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
| 54 // system to initialize, which will include registering for a TLS key. If we | |
| 55 // are not careful here, then that request to create a key will call new back, | |
| 56 // and we'll have an infinite loop. We avoid that as follows: | |
| 57 // Use use a stack allocated vector, so that we don't have dependence | |
|
willchan no longer on Chromium
2011/11/29 01:46:49
s/Use use/Use/g
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
| 58 // on our allocator until our service is in place. (i.e., don't | |
| 59 // even call new until after we're setup) | |
| 60 void* stack_allocated_tls_data[kThreadLocalStorageSize]; | |
| 61 memset(stack_allocated_tls_data, 0, sizeof(stack_allocated_tls_data)); | |
|
willchan no longer on Chromium
2011/11/29 01:46:49
s/sizeof/arraysize/
jar (doing other things)
2011/11/29 02:32:45
I think I want the size of the array. Array size
| |
| 62 // Ensure that any rentrant calls change the temp version. | |
|
willchan no longer on Chromium
2011/11/29 01:46:49
re-entrant
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
| 63 TlsSetValue(tls_key_, stack_allocated_tls_data); | |
| 64 | |
| 65 // Allocate an array to store our data. | |
| 53 void** tls_data = new void*[kThreadLocalStorageSize]; | 66 void** tls_data = new void*[kThreadLocalStorageSize]; |
| 54 memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize])); | 67 memcpy(tls_data, stack_allocated_tls_data, sizeof(stack_allocated_tls_data)); |
|
willchan no longer on Chromium
2011/11/29 01:46:49
s/sizeof/arraysize/
jar (doing other things)
2011/11/29 02:32:45
Again... I think I have the right form.
willchan no longer on Chromium
2011/11/29 02:47:04
Oops, you're right. Ignore me.
| |
| 55 TlsSetValue(tls_key_, tls_data); | 68 TlsSetValue(tls_key_, tls_data); |
| 56 return tls_data; | 69 return tls_data; |
| 57 } | 70 } |
| 58 | 71 |
| 59 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) | 72 ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) |
| 60 : initialized_(false), | 73 : initialized_(false), |
| 61 slot_(0) { | 74 slot_(0) { |
| 62 Initialize(destructor); | 75 Initialize(destructor); |
| 63 } | 76 } |
| 64 | 77 |
| 65 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { | 78 bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { |
| 66 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) | 79 if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) |
| 67 ThreadLocalStorage::Initialize(); | 80 ThreadLocalStorage::Initialize(); |
| 68 | 81 |
| 69 // Grab a new slot. | 82 // Grab a new slot. |
| 70 slot_ = InterlockedIncrement(&tls_max_) - 1; | 83 slot_ = InterlockedIncrement(&tls_max_) - 1; |
| 84 DCHECK_GT(slot_, 0); | |
| 71 if (slot_ >= kThreadLocalStorageSize) { | 85 if (slot_ >= kThreadLocalStorageSize) { |
| 72 NOTREACHED(); | 86 NOTREACHED(); |
| 73 return false; | 87 return false; |
| 74 } | 88 } |
| 75 | 89 |
| 76 // Setup our destructor. | 90 // Setup our destructor. Make sure that any thread that is dostroyed can see |
|
rvargas (doing something else)
2011/11/29 01:26:31
nit: typo
jar (doing other things)
2011/11/29 02:32:45
Done.
| |
| 77 tls_destructors_[slot_] = destructor; | 91 // this new setting. |
| 92 subtle::NoBarrier_Store(&tls_destructors_[slot_], | |
|
rvargas (doing something else)
2011/11/29 01:26:31
Isn't it more readable to do a regular assignment?
jar (doing other things)
2011/11/29 02:32:45
Yeah... but I think it is a bit wrong. Perhaps if
rvargas (doing something else)
2011/11/29 02:46:46
The way I'm reading this code (I may be wrong) is
| |
| 93 reinterpret_cast<subtle::AtomicWord>(destructor)); | |
| 78 initialized_ = true; | 94 initialized_ = true; |
| 79 return true; | 95 return true; |
| 80 } | 96 } |
| 81 | 97 |
| 82 void ThreadLocalStorage::Slot::Free() { | 98 void ThreadLocalStorage::Slot::Free() { |
| 83 // At this time, we don't reclaim old indices for TLS slots. | 99 // At this time, we don't reclaim old indices for TLS slots. |
| 84 // So all we need to do is wipe the destructor. | 100 // So all we need to do is wipe the destructor. |
| 85 tls_destructors_[slot_] = NULL; | 101 DCHECK_GT(slot_, 0); |
| 102 DCHECK_LT(slot_, kThreadLocalStorageSize); | |
| 103 subtle::NoBarrier_Store(&tls_destructors_[slot_], NULL); | |
| 104 slot_ = 0; | |
| 86 initialized_ = false; | 105 initialized_ = false; |
| 87 } | 106 } |
| 88 | 107 |
| 89 void* ThreadLocalStorage::Slot::Get() const { | 108 void* ThreadLocalStorage::Slot::Get() const { |
| 90 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 109 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| 91 if (!tls_data) | 110 if (!tls_data) |
| 92 tls_data = ThreadLocalStorage::Initialize(); | 111 tls_data = ThreadLocalStorage::Initialize(); |
| 93 DCHECK_GE(slot_, 0); | 112 DCHECK_GT(slot_, 0); |
| 94 DCHECK_LT(slot_, kThreadLocalStorageSize); | 113 DCHECK_LT(slot_, kThreadLocalStorageSize); |
| 95 return tls_data[slot_]; | 114 return tls_data[slot_]; |
| 96 } | 115 } |
| 97 | 116 |
| 98 void ThreadLocalStorage::Slot::Set(void* value) { | 117 void ThreadLocalStorage::Slot::Set(void* value) { |
| 99 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 118 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| 100 if (!tls_data) | 119 if (!tls_data) |
| 101 tls_data = ThreadLocalStorage::Initialize(); | 120 tls_data = ThreadLocalStorage::Initialize(); |
| 102 DCHECK_GE(slot_, 0); | 121 DCHECK_GT(slot_, 0); |
| 103 DCHECK_LT(slot_, kThreadLocalStorageSize); | 122 DCHECK_LT(slot_, kThreadLocalStorageSize); |
| 104 tls_data[slot_] = value; | 123 tls_data[slot_] = value; |
| 105 } | 124 } |
| 106 | 125 |
| 107 void ThreadLocalStorage::ThreadExit() { | 126 void ThreadLocalStorage::ThreadExit() { |
| 108 if (tls_key_ == TLS_OUT_OF_INDEXES) | 127 if (tls_key_ == TLS_OUT_OF_INDEXES) |
| 109 return; | 128 return; |
| 110 | 129 |
| 111 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); | 130 void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); |
| 112 | |
| 113 // Maybe we have never initialized TLS for this thread. | 131 // Maybe we have never initialized TLS for this thread. |
| 114 if (!tls_data) | 132 if (!tls_data) |
| 115 return; | 133 return; |
| 116 | 134 |
| 117 for (int slot = 0; slot < tls_max_; slot++) { | 135 // Some allocators, such as TCMalloc, use TLS. As a result, when a thread |
| 118 if (tls_destructors_[slot] != NULL) { | 136 // terminates, one of the destructor calls we make may be to shut down an |
| 119 void* value = tls_data[slot]; | 137 // allocator. We have to be careful that after we've shutdown all of the |
| 120 tls_destructors_[slot](value); | 138 // known destructors (perchance including an allocator), that we don't call |
| 139 // the allocator and cause it to resurrect itself (with no possibly destructor | |
| 140 // call to follow). We handle this problem as follows: | |
| 141 // Switch to using a stack allocated vector, so that we don't have dependence | |
| 142 // on our allocator after we have called all tls_destructors_. (i.e., don't | |
| 143 // even call delete[] after we're done with destructors.) | |
| 144 void* stack_allocated_tls_data[kThreadLocalStorageSize]; | |
| 145 memcpy(stack_allocated_tls_data, tls_data, sizeof(stack_allocated_tls_data)); | |
| 146 // Ensure that any rentrant calls change the temp version. | |
| 147 TlsSetValue(tls_key_, stack_allocated_tls_data); | |
| 148 delete[] tls_data; // Our last dependence on an allocator. | |
| 149 | |
| 150 int remaining_attempts = PTHREAD_DESTRUCTOR_ITERATIONS; | |
| 151 bool need_to_scan_destructors = true; | |
| 152 while (need_to_scan_destructors) { | |
| 153 need_to_scan_destructors = false; | |
| 154 // Try to destroy the first-created-slot (which is slot 1) in our last | |
| 155 // destructor call. That user was able to function, and define a slot with | |
| 156 // no other services running, so perhaps it is a basic service (like an | |
| 157 // allocator) and should also be destroyed last. | |
| 158 for (int slot = tls_max_ - 1; slot > 0; --slot) { | |
| 159 void* value = stack_allocated_tls_data[slot]; | |
| 160 if (value == NULL) | |
| 161 continue; | |
| 162 TLSDestructorFunc destructor = | |
| 163 reinterpret_cast<ThreadLocalStorage::TLSDestructorFunc>( | |
| 164 subtle::NoBarrier_Load(&tls_destructors_[slot])); | |
| 165 if (destructor == NULL) | |
| 166 continue; | |
| 167 stack_allocated_tls_data[slot] = NULL; // pre-clear the slot. | |
| 168 destructor(value); | |
| 169 // Any destructor might have called a different service, which then set | |
| 170 // a different slot to a non-NULL value. Hence we need to check | |
| 171 // the whole vector again. This is a pthread standard. | |
| 172 need_to_scan_destructors = true; | |
| 173 } | |
| 174 if (--remaining_attempts <= 0) { | |
| 175 NOTREACHED(); // Destructors might not have been called. | |
| 176 break; | |
| 121 } | 177 } |
| 122 } | 178 } |
| 123 | 179 |
| 124 delete[] tls_data; | 180 // Remove our stack allocated vector. |
| 125 | |
| 126 // In case there are other "onexit" handlers... | |
| 127 TlsSetValue(tls_key_, NULL); | 181 TlsSetValue(tls_key_, NULL); |
| 128 } | 182 } |
| 129 | 183 |
| 130 } // namespace base | 184 } // namespace base |
| 131 | 185 |
| 132 // Thread Termination Callbacks. | 186 // Thread Termination Callbacks. |
| 133 // Windows doesn't support a per-thread destructor with its | 187 // Windows doesn't support a per-thread destructor with its |
| 134 // TLS primitives. So, we build it manually by inserting a | 188 // TLS primitives. So, we build it manually by inserting a |
| 135 // function to be called on each thread's exit. | 189 // function to be called on each thread's exit. |
| 136 // This magic is from http://www.codeproject.com/threads/tls.asp | 190 // 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 | 246 #else // _WIN64 |
| 193 | 247 |
| 194 #pragma data_seg(".CRT$XLB") | 248 #pragma data_seg(".CRT$XLB") |
| 195 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; | 249 PIMAGE_TLS_CALLBACK p_thread_callback_base = OnThreadExit; |
| 196 | 250 |
| 197 // Reset the default section. | 251 // Reset the default section. |
| 198 #pragma data_seg() | 252 #pragma data_seg() |
| 199 | 253 |
| 200 #endif // _WIN64 | 254 #endif // _WIN64 |
| 201 } // extern "C" | 255 } // extern "C" |
| OLD | NEW |