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

Side by Side Diff: base/lazy_instance.h

Issue 1997153002: libchrome: Several upstreamable fixes from libchrome Base URL: https://chromium.googlesource.com/a/chromium/src.git@master
Patch Set: Also fix unit tests Created 4 years, 7 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
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 struct LeakyLazyInstanceTraits { 90 struct LeakyLazyInstanceTraits {
91 static const bool kRegisterOnExit = false; 91 static const bool kRegisterOnExit = false;
92 #ifndef NDEBUG 92 #ifndef NDEBUG
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 */) {}
danakj 2016/05/23 02:59:54 delete the name, it adds no value
Luis Héctor Chávez 2016/05/24 15:27:53 Done.
101 }
102 }; 101 };
103 102
104 // Our AtomicWord doubles as a spinlock, where a value of 103 // Our AtomicWord doubles as a spinlock, where a value of
105 // kBeingCreatedMarker means the spinlock is being held for creation. 104 // kBeingCreatedMarker means the spinlock is being held for creation.
106 static const subtle::AtomicWord kLazyInstanceStateCreating = 1; 105 static const subtle::AtomicWord kLazyInstanceStateCreating = 1;
107 106
108 // Check if instance needs to be created. If so return true otherwise 107 // Check if instance needs to be created. If so return true otherwise
109 // if another thread has beat us, wait for instance to be created and 108 // if another thread has beat us, wait for instance to be created and
110 // return false. 109 // return false.
111 BASE_EXPORT bool NeedsLazyInstance(subtle::AtomicWord* state); 110 BASE_EXPORT bool NeedsLazyInstance(subtle::AtomicWord* state);
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 LazyInstance<Type, Traits>* me = 197 LazyInstance<Type, Traits>* me =
199 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); 198 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance);
200 Traits::Delete(me->instance()); 199 Traits::Delete(me->instance());
201 subtle::NoBarrier_Store(&me->private_instance_, 0); 200 subtle::NoBarrier_Store(&me->private_instance_, 0);
202 } 201 }
203 }; 202 };
204 203
205 } // namespace base 204 } // namespace base
206 205
207 #endif // BASE_LAZY_INSTANCE_H_ 206 #endif // BASE_LAZY_INSTANCE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698