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

Side by Side Diff: base/lazy_instance.h

Issue 9192024: Add a convenience typedef LazyInstance<T>::Leaky to avoid repeating T. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 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
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 // 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 // Use placement new to initialize our instance in our preallocated space. 66 // Use placement new to initialize our instance in our preallocated space.
67 // The parenthesis is very important here to force POD type initialization. 67 // The parenthesis is very important here to force POD type initialization.
68 return new (instance) Type(); 68 return new (instance) Type();
69 } 69 }
70 static void Delete(Type* instance) { 70 static void Delete(Type* instance) {
71 // Explicitly call the destructor. 71 // Explicitly call the destructor.
72 instance->~Type(); 72 instance->~Type();
73 } 73 }
74 }; 74 };
75 75
76 // Use LazyInstance<T>::Leaky for a less-verbose call-site typedef.
wtc 2012/01/21 00:51:01 Nit: would be nice to give an example. Before I s
Ami GONE FROM CHROMIUM 2012/01/21 01:22:30 Done.
76 template <typename Type> 77 template <typename Type>
77 struct LeakyLazyInstanceTraits { 78 struct LeakyLazyInstanceTraits {
78 static const bool kRegisterOnExit = false; 79 static const bool kRegisterOnExit = false;
79 static const bool kAllowedToAccessOnNonjoinableThread = true; 80 static const bool kAllowedToAccessOnNonjoinableThread = true;
80 81
81 static Type* New(void* instance) { 82 static Type* New(void* instance) {
82 return DefaultLazyInstanceTraits<Type>::New(instance); 83 return DefaultLazyInstanceTraits<Type>::New(instance);
83 } 84 }
84 static void Delete(Type* instance) { 85 static void Delete(Type* instance) {
85 } 86 }
(...skipping 24 matching lines...) Expand all
110 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > 111 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> >
111 class LazyInstance { 112 class LazyInstance {
112 public: 113 public:
113 // Do not define a destructor, as doing so makes LazyInstance a 114 // Do not define a destructor, as doing so makes LazyInstance a
114 // non-POD-struct. We don't want that because then a static initializer will 115 // non-POD-struct. We don't want that because then a static initializer will
115 // be created to register the (empty) destructor with atexit() under MSVC, for 116 // be created to register the (empty) destructor with atexit() under MSVC, for
116 // example. We handle destruction of the contained Type class explicitly via 117 // example. We handle destruction of the contained Type class explicitly via
117 // the OnExit member function, where needed. 118 // the OnExit member function, where needed.
118 // ~LazyInstance() {} 119 // ~LazyInstance() {}
119 120
121 // Convenience typedef to avoid having to repeat Type for leaky lazy
122 // instances.
123 typedef LazyInstance<Type, LeakyLazyInstanceTraits<Type> > Leaky;
124
120 Type& Get() { 125 Type& Get() {
121 return *Pointer(); 126 return *Pointer();
122 } 127 }
123 128
124 Type* Pointer() { 129 Type* Pointer() {
125 #ifndef NDEBUG 130 #ifndef NDEBUG
126 // Avoid making TLS lookup on release builds. 131 // Avoid making TLS lookup on release builds.
127 if (!Traits::kAllowedToAccessOnNonjoinableThread) 132 if (!Traits::kAllowedToAccessOnNonjoinableThread)
128 ThreadRestrictions::AssertSingletonAllowed(); 133 ThreadRestrictions::AssertSingletonAllowed();
129 #endif 134 #endif
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 LazyInstance<Type, Traits>* me = 194 LazyInstance<Type, Traits>* me =
190 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 195 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
191 Traits::Delete(me->instance()); 196 Traits::Delete(me->instance());
192 subtle::Release_Store(&me->private_instance_, 0); 197 subtle::Release_Store(&me->private_instance_, 0);
193 } 198 }
194 }; 199 };
195 200
196 } // namespace base 201 } // namespace base
197 202
198 #endif // BASE_LAZY_INSTANCE_H_ 203 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « base/debug/trace_event.cc ('k') | base/lazy_instance_unittest.cc » ('j') | net/ocsp/nss_ocsp.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698