| OLD | NEW |
| 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 16 matching lines...) Expand all Loading... |
| 27 // static LazyInstance<MyClass> my_instance = LAZY_INSTANCE_INITIALIZER; | 27 // static LazyInstance<MyClass> my_instance = LAZY_INSTANCE_INITIALIZER; |
| 28 // void SomeMethod() { | 28 // void SomeMethod() { |
| 29 // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() | 29 // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() |
| 30 // | 30 // |
| 31 // MyClass* ptr = my_instance.Pointer(); | 31 // MyClass* ptr = my_instance.Pointer(); |
| 32 // ptr->DoDoDo(); // MyClass::DoDoDo | 32 // ptr->DoDoDo(); // MyClass::DoDoDo |
| 33 // } | 33 // } |
| 34 | 34 |
| 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 | |
| 38 | 37 |
| 39 #include <new> // For placement new. | 38 #include <new> // For placement new. |
| 40 | 39 |
| 41 #include "base/atomicops.h" | 40 #include "base/atomicops.h" |
| 42 #include "base/base_export.h" | 41 #include "base/base_export.h" |
| 43 #include "base/basictypes.h" | 42 #include "base/basictypes.h" |
| 44 #include "base/logging.h" | 43 #include "base/logging.h" |
| 45 #include "base/memory/aligned_memory.h" | 44 #include "base/memory/aligned_memory.h" |
| 46 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 47 #include "base/threading/thread_restrictions.h" | 46 #include "base/threading/thread_restrictions.h" |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 LazyInstance<Type, Traits>* me = | 201 LazyInstance<Type, Traits>* me = |
| 203 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 202 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
| 204 Traits::Delete(me->instance()); | 203 Traits::Delete(me->instance()); |
| 205 subtle::NoBarrier_Store(&me->private_instance_, 0); | 204 subtle::NoBarrier_Store(&me->private_instance_, 0); |
| 206 } | 205 } |
| 207 }; | 206 }; |
| 208 | 207 |
| 209 } // namespace base | 208 } // namespace base |
| 210 | 209 |
| 211 #endif // BASE_LAZY_INSTANCE_H_ | 210 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |