| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 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 | 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 17 matching lines...) Expand all Loading... |
| 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 | 37 |
| 38 #include "base/at_exit.h" |
| 38 #include "base/atomicops.h" | 39 #include "base/atomicops.h" |
| 39 #include "base/basictypes.h" | 40 #include "base/basictypes.h" |
| 40 #include "base/dynamic_annotations.h" | 41 #include "base/dynamic_annotations.h" |
| 42 #include "base/platform_thread.h" |
| 41 | 43 |
| 42 namespace base { | 44 namespace base { |
| 43 | 45 |
| 44 template <typename Type> | 46 template <typename Type> |
| 45 struct DefaultLazyInstanceTraits { | 47 struct DefaultLazyInstanceTraits { |
| 46 static void New(void* instance) { | 48 static Type* New(void* instance) { |
| 47 // Use placement new to initialize our instance in our preallocated space. | 49 // Use placement new to initialize our instance in our preallocated space. |
| 48 // The parenthesis is very important here to force POD type initialization. | 50 // The parenthesis is very important here to force POD type initialization. |
| 49 new (instance) Type(); | 51 return new (instance) Type(); |
| 50 } | 52 } |
| 51 static void Delete(void* instance) { | 53 static void Delete(void* instance) { |
| 52 // Explicitly call the destructor. | 54 // Explicitly call the destructor. |
| 53 reinterpret_cast<Type*>(instance)->~Type(); | 55 reinterpret_cast<Type*>(instance)->~Type(); |
| 54 } | 56 } |
| 55 }; | 57 }; |
| 56 | 58 |
| 57 // We pull out some of the functionality into a non-templated base, so that we | 59 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
| 58 // can implement the more complicated pieces out of line in the .cc file. | 60 class LazyInstance { |
| 59 class LazyInstanceHelper { | 61 public: |
| 60 protected: | 62 explicit LazyInstance(LinkerInitialized x) { } |
| 61 enum { | |
| 62 STATE_EMPTY = 0, | |
| 63 STATE_CREATING = 1, | |
| 64 STATE_CREATED = 2 | |
| 65 }; | |
| 66 | |
| 67 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } | |
| 68 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 63 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 69 // static initializer to register the empty destructor with atexit(). | 64 // static initializer to register the empty destructor with atexit(). |
| 70 | 65 |
| 71 // Make sure that instance is created, creating or waiting for it to be | |
| 72 // created if neccessary. Constructs with |ctor| in the space provided by | |
| 73 // |instance| and registers dtor for destruction at program exit. | |
| 74 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); | |
| 75 | |
| 76 base::subtle::Atomic32 state_; | |
| 77 | |
| 78 private: | |
| 79 // Resets state of |helper| to STATE_EMPTY so that it can be reused. | |
| 80 // Not thread safe. | |
| 81 static void ResetState(void* helper); | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); | |
| 84 }; | |
| 85 | |
| 86 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | |
| 87 class LazyInstance : public LazyInstanceHelper { | |
| 88 public: | |
| 89 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } | |
| 90 // Declaring a destructor (even if it's empty) will cause MSVC to register a | |
| 91 // static initializer to register the empty destructor with atexit(). | |
| 92 | |
| 93 Type& Get() { | 66 Type& Get() { |
| 94 return *Pointer(); | 67 return *Pointer(); |
| 95 } | 68 } |
| 96 | 69 |
| 97 Type* Pointer() { | 70 Type* Pointer() { |
| 98 Type* instance = reinterpret_cast<Type*>(&buf_); | |
| 99 | |
| 100 // We will hopefully have fast access when the instance is already created. | 71 // We will hopefully have fast access when the instance is already created. |
| 101 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) | 72 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
| 102 EnsureInstance(instance, Traits::New, Traits::Delete); | 73 EnsureInstance(); |
| 103 | 74 |
| 104 // This annotation helps race detectors recognize correct lock-less | 75 // This annotation helps race detectors recognize correct lock-less |
| 105 // synchronization between different threads calling Pointer(). | 76 // synchronization between different threads calling Pointer(). |
| 106 // We suggest dynamic race detection tool that | 77 // We suggest dynamic race detection tool that |
| 107 // "ctor(instance)" in EnsureInstance(...) happens before | 78 // "Traits::New(buf_)" in EnsureInstance() happens before |
| 108 // "return instance" in Pointer(). | 79 // "return instance_" in Pointer(). |
| 109 // See the corresponding HAPPENS_BEFORE in EnsureInstance(...). | 80 // See the corresponding HAPPENS_BEFORE in EnsureInstance(). |
| 110 ANNOTATE_HAPPENS_AFTER(&state_); | 81 ANNOTATE_HAPPENS_AFTER(&state_); |
| 111 | 82 |
| 112 return instance; | 83 return instance_; |
| 113 } | 84 } |
| 114 | 85 |
| 115 private: | 86 private: |
| 87 enum { |
| 88 STATE_EMPTY = 0, |
| 89 STATE_CREATING = 1, |
| 90 STATE_CREATED = 2 |
| 91 }; |
| 92 |
| 93 // Make sure that instance is created, creating or waiting for it to be |
| 94 // created if neccessary. Constructs with Traits::New in the space provided |
| 95 // by buf_ and registers Traits::Delete for destruction at program exit. |
| 96 void EnsureInstance() { |
| 97 // Try to create the instance, if we're the first, will go from EMPTY |
| 98 // to CREATING, otherwise we've already been beaten here. |
| 99 if (base::subtle::Acquire_CompareAndSwap( |
| 100 &state_, STATE_EMPTY, STATE_CREATING) == STATE_EMPTY) { |
| 101 // Create the instance in the space provided by |buf_|. |
| 102 instance_ = Traits::New(buf_); |
| 103 |
| 104 // See the comment to the corresponding HAPPENS_AFTER in Pointer(). |
| 105 ANNOTATE_HAPPENS_BEFORE(&state_); |
| 106 |
| 107 // Instance is created, go from CREATING to CREATED. |
| 108 base::subtle::Release_Store(&state_, STATE_CREATED); |
| 109 |
| 110 // Allow reusing the LazyInstance (reset it to the initial state). This |
| 111 // makes possible calling all AtExit callbacks between tests. Assumes that |
| 112 // no other threads execute when AtExit callbacks are processed. |
| 113 base::AtExitManager::RegisterCallback(&ResetState, this); |
| 114 |
| 115 // Make sure that the lazily instantiated object will get destroyed at exi
t. |
| 116 base::AtExitManager::RegisterCallback(Traits::Delete, instance_); |
| 117 } else { |
| 118 // It's either in the process of being created, or already created. Spin. |
| 119 while (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) |
| 120 PlatformThread::YieldCurrentThread(); |
| 121 } |
| 122 } |
| 123 |
| 124 // Resets state of |lazyinstance| to STATE_EMPTY so that it can be reused. |
| 125 // Not thread safe. |
| 126 static void ResetState(void* lazyinstance) { |
| 127 reinterpret_cast<LazyInstance*>(lazyinstance)->state_ = STATE_EMPTY; |
| 128 } |
| 129 |
| 130 base::subtle::Atomic32 state_; |
| 116 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 131 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
| 132 Type* instance_; |
| 117 | 133 |
| 118 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 134 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 119 }; | 135 }; |
| 120 | 136 |
| 121 } // namespace base | 137 } // namespace base |
| 122 | 138 |
| 123 #endif // BASE_LAZY_INSTANCE_H_ | 139 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |