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

Side by Side Diff: base/lazy_instance.h

Issue 9186057: Add ALIGNAS and ALIGNOF macros to ensure proper alignment of StaticMemorySingletonTraits (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: willchan feedback Created 8 years, 10 months 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/compiler_specific.h ('k') | base/lazy_instance_unittest.cc » ('j') | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 // The LazyInstance<Type, Traits> class manages a single instance of Type, 5 // The LazyInstance<Type, Traits> class manages a single instance of Type,
6 // which will be lazily created on the first time it's accessed. This class is 6 // which will be lazily created on the first time it's accessed. This class is
7 // useful for places you would normally use a function-level static, but you 7 // useful for places you would normally use a function-level static, but you
8 // need to have guaranteed thread-safety. The Type constructor will only ever 8 // need to have guaranteed thread-safety. The Type constructor will only ever
9 // be called once, even if two threads are racing to create the object. Get() 9 // be called once, even if two threads are racing to create the object. Get()
10 // and Pointer() will always return the same, completely initialized instance. 10 // and Pointer() will always return the same, completely initialized instance.
(...skipping 24 matching lines...) Expand all
35 #ifndef BASE_LAZY_INSTANCE_H_ 35 #ifndef BASE_LAZY_INSTANCE_H_
36 #define BASE_LAZY_INSTANCE_H_ 36 #define BASE_LAZY_INSTANCE_H_
37 #pragma once 37 #pragma once
38 38
39 #include <new> // For placement new. 39 #include <new> // For placement new.
40 40
41 #include "base/atomicops.h" 41 #include "base/atomicops.h"
42 #include "base/base_export.h" 42 #include "base/base_export.h"
43 #include "base/basictypes.h" 43 #include "base/basictypes.h"
44 #include "base/logging.h" 44 #include "base/logging.h"
45 #include "base/memory/aligned_memory.h"
45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" 46 #include "base/third_party/dynamic_annotations/dynamic_annotations.h"
46 #include "base/threading/thread_restrictions.h" 47 #include "base/threading/thread_restrictions.h"
47 48
48 // LazyInstance uses its own struct initializer-list style static 49 // LazyInstance uses its own struct initializer-list style static
49 // initialization, as base's LINKER_INITIALIZED requires a constructor and on 50 // initialization, as base's LINKER_INITIALIZED requires a constructor and on
50 // some compilers (notably gcc 4.4) this still ends up needing runtime 51 // some compilers (notably gcc 4.4) this still ends up needing runtime
51 // initialization. 52 // initialization.
52 #define LAZY_INSTANCE_INITIALIZER {0} 53 #define LAZY_INSTANCE_INITIALIZER {0}
53 54
54 namespace base { 55 namespace base {
55 56
56 template <typename Type> 57 template <typename Type>
57 struct DefaultLazyInstanceTraits { 58 struct DefaultLazyInstanceTraits {
58 static const bool kRegisterOnExit = true; 59 static const bool kRegisterOnExit = true;
59 static const bool kAllowedToAccessOnNonjoinableThread = false; 60 static const bool kAllowedToAccessOnNonjoinableThread = false;
60 61
61 static Type* New(void* instance) { 62 static Type* New(void* instance) {
62 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) % sizeof(instance), 0u) 63 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u)
63 << ": Bad boy, the buffer passed to placement new is not aligned!\n" 64 << ": Bad boy, the buffer passed to placement new is not aligned!\n"
64 "This may break some stuff like SSE-based optimizations assuming the " 65 "This may break some stuff like SSE-based optimizations assuming the "
65 "<Type> objects are word aligned."; 66 "<Type> objects are word aligned.";
66 // Use placement new to initialize our instance in our preallocated space. 67 // Use placement new to initialize our instance in our preallocated space.
67 // The parenthesis is very important here to force POD type initialization. 68 // The parenthesis is very important here to force POD type initialization.
68 return new (instance) Type(); 69 return new (instance) Type();
69 } 70 }
70 static void Delete(Type* instance) { 71 static void Delete(Type* instance) {
71 // Explicitly call the destructor. 72 // Explicitly call the destructor.
72 instance->~Type(); 73 instance->~Type();
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
148 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating 149 // Since a thread sees private_instance_ == 0 or kLazyInstanceStateCreating
149 // at most once, the load is taken out of NeedsInstance() as a fast-path. 150 // at most once, the load is taken out of NeedsInstance() as a fast-path.
150 // The load has acquire memory ordering as a thread which sees 151 // The load has acquire memory ordering as a thread which sees
151 // private_instance_ > creating needs to acquire visibility over 152 // private_instance_ > creating needs to acquire visibility over
152 // the associated data (private_buf_). Pairing Release_Store is in 153 // the associated data (private_buf_). Pairing Release_Store is in
153 // CompleteLazyInstance(). 154 // CompleteLazyInstance().
154 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_); 155 subtle::AtomicWord value = subtle::Acquire_Load(&private_instance_);
155 if (!(value & kLazyInstanceCreatedMask) && 156 if (!(value & kLazyInstanceCreatedMask) &&
156 internal::NeedsLazyInstance(&private_instance_)) { 157 internal::NeedsLazyInstance(&private_instance_)) {
157 // Create the instance in the space provided by |private_buf_|. 158 // Create the instance in the space provided by |private_buf_|.
158 value = reinterpret_cast<subtle::AtomicWord>(Traits::New(private_buf_)); 159 value = reinterpret_cast<subtle::AtomicWord>(
160 Traits::New(private_buf_.void_data()));
159 internal::CompleteLazyInstance(&private_instance_, value, this, 161 internal::CompleteLazyInstance(&private_instance_, value, this,
160 Traits::kRegisterOnExit ? OnExit : NULL); 162 Traits::kRegisterOnExit ? OnExit : NULL);
161 } 163 }
162 164
163 // This annotation helps race detectors recognize correct lock-less 165 // This annotation helps race detectors recognize correct lock-less
164 // synchronization between different threads calling Pointer(). 166 // synchronization between different threads calling Pointer().
165 // We suggest dynamic race detection tool that "Traits::New" above 167 // We suggest dynamic race detection tool that "Traits::New" above
166 // and CompleteLazyInstance(...) happens before "return instance()" below. 168 // and CompleteLazyInstance(...) happens before "return instance()" below.
167 // See the corresponding HAPPENS_BEFORE in CompleteLazyInstance(...). 169 // See the corresponding HAPPENS_BEFORE in CompleteLazyInstance(...).
168 ANNOTATE_HAPPENS_AFTER(&private_instance_); 170 ANNOTATE_HAPPENS_AFTER(&private_instance_);
169 return instance(); 171 return instance();
170 } 172 }
171 173
172 bool operator==(Type* p) { 174 bool operator==(Type* p) {
173 switch (subtle::NoBarrier_Load(&private_instance_)) { 175 switch (subtle::NoBarrier_Load(&private_instance_)) {
174 case 0: 176 case 0:
175 return p == NULL; 177 return p == NULL;
176 case internal::kLazyInstanceStateCreating: 178 case internal::kLazyInstanceStateCreating:
177 return static_cast<int8*>(static_cast<void*>(p)) == private_buf_; 179 return static_cast<void*>(p) == private_buf_.void_data();
178 default: 180 default:
179 return p == instance(); 181 return p == instance();
180 } 182 }
181 } 183 }
182 184
183 // Effectively private: member data is only public to allow the linker to 185 // Effectively private: member data is only public to allow the linker to
184 // statically initialize it. DO NOT USE FROM OUTSIDE THIS CLASS. 186 // statically initialize it and to maintain a POD class. DO NOT USE FROM
187 // OUTSIDE THIS CLASS.
185 188
186 // Note this must use AtomicWord, not Atomic32, to ensure correct alignment
187 // of |private_buf_| on 64 bit architectures. (This member must be first to
188 // allow the syntax used in LAZY_INSTANCE_INITIALIZER to work correctly.)
189 subtle::AtomicWord private_instance_; 189 subtle::AtomicWord private_instance_;
190 int8 private_buf_[sizeof(Type)]; // Preallocated space for the Type instance. 190 // Preallocated space for the Type instance.
191 base::AlignedMemory<sizeof(Type), ALIGNOF(Type)> private_buf_;
191 192
192 private: 193 private:
193 Type* instance() { 194 Type* instance() {
194 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_)); 195 return reinterpret_cast<Type*>(subtle::NoBarrier_Load(&private_instance_));
195 } 196 }
196 197
197 // Adapter function for use with AtExit. This should be called single 198 // Adapter function for use with AtExit. This should be called single
198 // threaded, so don't synchronize across threads. 199 // threaded, so don't synchronize across threads.
199 // Calling OnExit while the instance is in use by other threads is a mistake. 200 // Calling OnExit while the instance is in use by other threads is a mistake.
200 static void OnExit(void* lazy_instance) { 201 static void OnExit(void* lazy_instance) {
201 LazyInstance<Type, Traits>* me = 202 LazyInstance<Type, Traits>* me =
202 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 203 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
203 Traits::Delete(me->instance()); 204 Traits::Delete(me->instance());
204 subtle::Release_Store(&me->private_instance_, 0); 205 subtle::NoBarrier_Store(&me->private_instance_, 0);
205 } 206 }
206 }; 207 };
207 208
208 } // namespace base 209 } // namespace base
209 210
210 #endif // BASE_LAZY_INSTANCE_H_ 211 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « base/compiler_specific.h ('k') | base/lazy_instance_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698