[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 1 | // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 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 |
| 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 |
| 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. |
| 11 | // When the instance is constructed it is registered with AtExitManager. The |
| 12 | // destructor will be called on program exit. |
| 13 | // |
| 14 | // LazyInstance is completely thread safe, assuming that you create it safely. |
| 15 | // The class was designed to be POD initialized, so it shouldn't require a |
| 16 | // static constructor. It really only makes sense to declare a LazyInstance as |
| 17 | // a global variable using the base::LinkerInitialized constructor. |
| 18 | // |
| 19 | // LazyInstance is similar to Singleton, except it does not have the singleton |
| 20 | // property. You can have multiple LazyInstance's of the same type, and each |
| 21 | // will manage a unique instance. It also preallocates the space for Type, as |
| 22 | // to avoid allocating the Type instance on the heap. This may help with the |
| 23 | // performance of creating the instance, and reducing heap fragmentation. This |
| 24 | // requires that Type be a complete type so we can determine the size. |
| 25 | // |
| 26 | // Example usage: |
[email protected] | de1f44a | 2009-01-13 12:34:53 | [diff] [blame] | 27 | // static LazyInstance<MyClass> my_instance(base::LINKER_INITIALIZED); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 28 | // void SomeMethod() { |
| 29 | // my_instance.Get().SomeMethod(); // MyClass::SomeMethod() |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 30 | // |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 31 | // MyClass* ptr = my_instance.Pointer(); |
| 32 | // ptr->DoDoDo(); // MyClass::DoDoDo |
| 33 | // } |
| 34 | |
| 35 | #ifndef BASE_LAZY_INSTANCE_H_ |
| 36 | #define BASE_LAZY_INSTANCE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame^] | 37 | #pragma once |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 38 | |
| 39 | #include "base/atomicops.h" |
| 40 | #include "base/basictypes.h" |
[email protected] | ee85751 | 2010-05-14 08:24:42 | [diff] [blame] | 41 | #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 42 | |
| 43 | namespace base { |
| 44 | |
| 45 | template <typename Type> |
| 46 | struct DefaultLazyInstanceTraits { |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 47 | static Type* New(void* instance) { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 48 | // Use placement new to initialize our instance in our preallocated space. |
| 49 | // The parenthesis is very important here to force POD type initialization. |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 50 | return new (instance) Type(); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 51 | } |
| 52 | static void Delete(void* instance) { |
| 53 | // Explicitly call the destructor. |
| 54 | reinterpret_cast<Type*>(instance)->~Type(); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // We pull out some of the functionality into a non-templated base, so that we |
| 59 | // can implement the more complicated pieces out of line in the .cc file. |
| 60 | class LazyInstanceHelper { |
| 61 | protected: |
| 62 | enum { |
| 63 | STATE_EMPTY = 0, |
| 64 | STATE_CREATING = 1, |
| 65 | STATE_CREATED = 2 |
| 66 | }; |
| 67 | |
| 68 | explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } |
[email protected] | 62ea451 | 2008-09-08 15:20:57 | [diff] [blame] | 69 | // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 70 | // static initializer to register the empty destructor with atexit(). |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 71 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 72 | // Check if instance needs to be created. If so return true otherwise |
| 73 | // if another thread has beat us, wait for instance to be created and |
| 74 | // return false. |
| 75 | bool NeedsInstance(); |
| 76 | |
| 77 | // After creating an instance, call this to register the dtor to be called |
| 78 | // at program exit and to update the state to STATE_CREATED. |
| 79 | void CompleteInstance(void* instance, void (*dtor)(void*)); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 80 | |
| 81 | base::subtle::Atomic32 state_; |
[email protected] | 3d531478 | 2008-09-08 14:47:24 | [diff] [blame] | 82 | |
| 83 | private: |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 84 | // Resets state of |helper| to STATE_EMPTY so that it can be reused. |
| 85 | // Not thread safe. |
| 86 | static void ResetState(void* helper); |
| 87 | |
[email protected] | 3d531478 | 2008-09-08 14:47:24 | [diff] [blame] | 88 | DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
| 92 | class LazyInstance : public LazyInstanceHelper { |
| 93 | public: |
| 94 | explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } |
[email protected] | 62ea451 | 2008-09-08 15:20:57 | [diff] [blame] | 95 | // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 96 | // static initializer to register the empty destructor with atexit(). |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 97 | |
| 98 | Type& Get() { |
| 99 | return *Pointer(); |
| 100 | } |
| 101 | |
| 102 | Type* Pointer() { |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 103 | // We will hopefully have fast access when the instance is already created. |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 104 | if ((base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) && |
| 105 | NeedsInstance()) { |
| 106 | // Create the instance in the space provided by |buf_|. |
| 107 | instance_ = Traits::New(buf_); |
| 108 | CompleteInstance(instance_, Traits::Delete); |
| 109 | } |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 110 | |
[email protected] | 001b694 | 2009-06-26 11:28:03 | [diff] [blame] | 111 | // This annotation helps race detectors recognize correct lock-less |
| 112 | // synchronization between different threads calling Pointer(). |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 113 | // We suggest dynamic race detection tool that "Traits::New" above |
| 114 | // and CompleteInstance(...) happens before "return instance_" below. |
| 115 | // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). |
[email protected] | 001b694 | 2009-06-26 11:28:03 | [diff] [blame] | 116 | ANNOTATE_HAPPENS_AFTER(&state_); |
| 117 | |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 118 | return instance_; |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private: |
| 122 | int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
[email protected] | c1aeaac | 2010-03-12 15:28:48 | [diff] [blame] | 123 | Type *instance_; |
[email protected] | 30039e6 | 2008-09-08 14:11:13 | [diff] [blame] | 124 | |
| 125 | DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 126 | }; |
| 127 | |
| 128 | } // namespace base |
| 129 | |
| 130 | #endif // BASE_LAZY_INSTANCE_H_ |