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

Side by Side Diff: base/lazy_instance.h

Issue 2163023002: Unify usage of logging/assert macros in base/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix base/android/build_info.cc compile Created 4 years, 4 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
« no previous file with comments | « base/i18n/icu_util.cc ('k') | base/logging.h » ('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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 // initialization, as base's LINKER_INITIALIZED requires a constructor and on 48 // initialization, as base's LINKER_INITIALIZED requires a constructor and on
49 // some compilers (notably gcc 4.4) this still ends up needing runtime 49 // some compilers (notably gcc 4.4) this still ends up needing runtime
50 // initialization. 50 // initialization.
51 #define LAZY_INSTANCE_INITIALIZER {0} 51 #define LAZY_INSTANCE_INITIALIZER {0}
52 52
53 namespace base { 53 namespace base {
54 54
55 template <typename Type> 55 template <typename Type>
56 struct DefaultLazyInstanceTraits { 56 struct DefaultLazyInstanceTraits {
57 static const bool kRegisterOnExit = true; 57 static const bool kRegisterOnExit = true;
58 #ifndef NDEBUG 58 #if DCHECK_IS_ON()
59 static const bool kAllowedToAccessOnNonjoinableThread = false; 59 static const bool kAllowedToAccessOnNonjoinableThread = false;
60 #endif 60 #endif
61 61
62 static Type* New(void* instance) { 62 static Type* New(void* instance) {
63 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u) 63 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) & (ALIGNOF(Type) - 1), 0u)
64 << ": 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"
65 "This may break some stuff like SSE-based optimizations assuming the " 65 "This may break some stuff like SSE-based optimizations assuming the "
66 "<Type> objects are word aligned."; 66 "<Type> objects are word aligned.";
67 // Use placement new to initialize our instance in our preallocated space. 67 // Use placement new to initialize our instance in our preallocated space.
68 // The parenthesis is very important here to force POD type initialization. 68 // The parenthesis is very important here to force POD type initialization.
(...skipping 13 matching lines...) Expand all
82 // base::LazyInstance<T>::Leaky my_leaky_lazy_instance; 82 // base::LazyInstance<T>::Leaky my_leaky_lazy_instance;
83 // instead of: 83 // instead of:
84 // base::LazyInstance<T, base::internal::LeakyLazyInstanceTraits<T> > 84 // base::LazyInstance<T, base::internal::LeakyLazyInstanceTraits<T> >
85 // my_leaky_lazy_instance; 85 // my_leaky_lazy_instance;
86 // (especially when T is MyLongTypeNameImplClientHolderFactory). 86 // (especially when T is MyLongTypeNameImplClientHolderFactory).
87 // Only use this internal::-qualified verbose form to extend this traits class 87 // Only use this internal::-qualified verbose form to extend this traits class
88 // (depending on its implementation details). 88 // (depending on its implementation details).
89 template <typename Type> 89 template <typename Type>
90 struct LeakyLazyInstanceTraits { 90 struct LeakyLazyInstanceTraits {
91 static const bool kRegisterOnExit = false; 91 static const bool kRegisterOnExit = false;
92 #ifndef NDEBUG 92 #if DCHECK_IS_ON()
93 static const bool kAllowedToAccessOnNonjoinableThread = true; 93 static const bool kAllowedToAccessOnNonjoinableThread = true;
94 #endif 94 #endif
95 95
96 static Type* New(void* instance) { 96 static Type* New(void* instance) {
97 ANNOTATE_SCOPED_MEMORY_LEAK; 97 ANNOTATE_SCOPED_MEMORY_LEAK;
98 return DefaultLazyInstanceTraits<Type>::New(instance); 98 return DefaultLazyInstanceTraits<Type>::New(instance);
99 } 99 }
100 static void Delete(Type* instance) { 100 static void Delete(Type* instance) {
101 } 101 }
102 }; 102 };
(...skipping 28 matching lines...) Expand all
131 131
132 // Convenience typedef to avoid having to repeat Type for leaky lazy 132 // Convenience typedef to avoid having to repeat Type for leaky lazy
133 // instances. 133 // instances.
134 typedef LazyInstance<Type, internal::LeakyLazyInstanceTraits<Type> > Leaky; 134 typedef LazyInstance<Type, internal::LeakyLazyInstanceTraits<Type> > Leaky;
135 135
136 Type& Get() { 136 Type& Get() {
137 return *Pointer(); 137 return *Pointer();
138 } 138 }
139 139
140 Type* Pointer() { 140 Type* Pointer() {
141 #ifndef NDEBUG 141 #if DCHECK_IS_ON()
142 // Avoid making TLS lookup on release builds. 142 // Avoid making TLS lookup on release builds.
143 if (!Traits::kAllowedToAccessOnNonjoinableThread) 143 if (!Traits::kAllowedToAccessOnNonjoinableThread)
144 ThreadRestrictions::AssertSingletonAllowed(); 144 ThreadRestrictions::AssertSingletonAllowed();
145 #endif 145 #endif
146 // If any bit in the created mask is true, the instance has already been 146 // If any bit in the created mask is true, the instance has already been
147 // fully constructed. 147 // fully constructed.
148 static const subtle::AtomicWord kLazyInstanceCreatedMask = 148 static const subtle::AtomicWord kLazyInstanceCreatedMask =
149 ~internal::kLazyInstanceStateCreating; 149 ~internal::kLazyInstanceStateCreating;
150 150
151 // We will hopefully have fast access when the instance is already created. 151 // We will hopefully have fast access when the instance is already created.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 LazyInstance<Type, Traits>* me = 198 LazyInstance<Type, Traits>* me =
199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
200 Traits::Delete(me->instance()); 200 Traits::Delete(me->instance());
201 subtle::NoBarrier_Store(&me->private_instance_, 0); 201 subtle::NoBarrier_Store(&me->private_instance_, 0);
202 } 202 }
203 }; 203 };
204 204
205 } // namespace base 205 } // namespace base
206 206
207 #endif // BASE_LAZY_INSTANCE_H_ 207 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « base/i18n/icu_util.cc ('k') | base/logging.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698