| 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 25 matching lines...) Expand all Loading... |
| 36 #define BASE_LAZY_INSTANCE_H_ | 36 #define BASE_LAZY_INSTANCE_H_ |
| 37 | 37 |
| 38 #include "base/atomicops.h" | 38 #include "base/atomicops.h" |
| 39 #include "base/basictypes.h" | 39 #include "base/basictypes.h" |
| 40 #include "base/dynamic_annotations.h" | 40 #include "base/dynamic_annotations.h" |
| 41 | 41 |
| 42 namespace base { | 42 namespace base { |
| 43 | 43 |
| 44 template <typename Type> | 44 template <typename Type> |
| 45 struct DefaultLazyInstanceTraits { | 45 struct DefaultLazyInstanceTraits { |
| 46 static void New(void* instance) { | 46 static Type* New(void* instance) { |
| 47 // Use placement new to initialize our instance in our preallocated space. | 47 // Use placement new to initialize our instance in our preallocated space. |
| 48 // The parenthesis is very important here to force POD type initialization. | 48 // The parenthesis is very important here to force POD type initialization. |
| 49 new (instance) Type(); | 49 return new (instance) Type(); |
| 50 } | 50 } |
| 51 static void Delete(void* instance) { | 51 static void Delete(void* instance) { |
| 52 // Explicitly call the destructor. | 52 // Explicitly call the destructor. |
| 53 reinterpret_cast<Type*>(instance)->~Type(); | 53 reinterpret_cast<Type*>(instance)->~Type(); |
| 54 } | 54 } |
| 55 }; | 55 }; |
| 56 | 56 |
| 57 // We pull out some of the functionality into a non-templated base, so that we | 57 // We pull out some of the functionality into a non-templated base, so that we |
| 58 // can implement the more complicated pieces out of line in the .cc file. | 58 // can implement the more complicated pieces out of line in the .cc file. |
| 59 class LazyInstanceHelper { | 59 class LazyInstanceHelper { |
| 60 protected: | 60 protected: |
| 61 enum { | 61 enum { |
| 62 STATE_EMPTY = 0, | 62 STATE_EMPTY = 0, |
| 63 STATE_CREATING = 1, | 63 STATE_CREATING = 1, |
| 64 STATE_CREATED = 2 | 64 STATE_CREATED = 2 |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } | 67 explicit LazyInstanceHelper(LinkerInitialized x) { /* state_ is 0 */ } |
| 68 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 68 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 69 // static initializer to register the empty destructor with atexit(). | 69 // static initializer to register the empty destructor with atexit(). |
| 70 | 70 |
| 71 // Make sure that instance is created, creating or waiting for it to be | 71 // Check if instance needs to be created. If so return true otherwise |
| 72 // created if neccessary. Constructs with |ctor| in the space provided by | 72 // if another thread has beat us, wait for instance to be created and |
| 73 // |instance| and registers dtor for destruction at program exit. | 73 // return false. |
| 74 void EnsureInstance(void* instance, void (*ctor)(void*), void (*dtor)(void*)); | 74 bool NeedsInstance(); |
| 75 |
| 76 // After creating an instance, call this to register the dtor to be called |
| 77 // at program exit and to update the state to STATE_CREATED. |
| 78 void CompleteInstance(void* instance, void (*dtor)(void*)); |
| 75 | 79 |
| 76 base::subtle::Atomic32 state_; | 80 base::subtle::Atomic32 state_; |
| 77 | 81 |
| 78 private: | 82 private: |
| 79 // Resets state of |helper| to STATE_EMPTY so that it can be reused. | 83 // Resets state of |helper| to STATE_EMPTY so that it can be reused. |
| 80 // Not thread safe. | 84 // Not thread safe. |
| 81 static void ResetState(void* helper); | 85 static void ResetState(void* helper); |
| 82 | 86 |
| 83 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); | 87 DISALLOW_COPY_AND_ASSIGN(LazyInstanceHelper); |
| 84 }; | 88 }; |
| 85 | 89 |
| 86 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > | 90 template <typename Type, typename Traits = DefaultLazyInstanceTraits<Type> > |
| 87 class LazyInstance : public LazyInstanceHelper { | 91 class LazyInstance : public LazyInstanceHelper { |
| 88 public: | 92 public: |
| 89 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } | 93 explicit LazyInstance(LinkerInitialized x) : LazyInstanceHelper(x) { } |
| 90 // Declaring a destructor (even if it's empty) will cause MSVC to register a | 94 // Declaring a destructor (even if it's empty) will cause MSVC to register a |
| 91 // static initializer to register the empty destructor with atexit(). | 95 // static initializer to register the empty destructor with atexit(). |
| 92 | 96 |
| 93 Type& Get() { | 97 Type& Get() { |
| 94 return *Pointer(); | 98 return *Pointer(); |
| 95 } | 99 } |
| 96 | 100 |
| 97 Type* Pointer() { | 101 Type* Pointer() { |
| 98 Type* instance = reinterpret_cast<Type*>(&buf_); | |
| 99 | |
| 100 // We will hopefully have fast access when the instance is already created. | 102 // We will hopefully have fast access when the instance is already created. |
| 101 if (base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) | 103 if ((base::subtle::NoBarrier_Load(&state_) != STATE_CREATED) && |
| 102 EnsureInstance(instance, Traits::New, Traits::Delete); | 104 NeedsInstance()) { |
| 105 // Create the instance in the space provided by |buf_|. |
| 106 instance_ = Traits::New(buf_); |
| 107 CompleteInstance(instance_, Traits::Delete); |
| 108 } |
| 103 | 109 |
| 104 // This annotation helps race detectors recognize correct lock-less | 110 // This annotation helps race detectors recognize correct lock-less |
| 105 // synchronization between different threads calling Pointer(). | 111 // synchronization between different threads calling Pointer(). |
| 106 // We suggest dynamic race detection tool that | 112 // We suggest dynamic race detection tool that "Traits::New" above |
| 107 // "ctor(instance)" in EnsureInstance(...) happens before | 113 // and CompleteInstance(...) happens before "return instance_" below. |
| 108 // "return instance" in Pointer(). | 114 // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). |
| 109 // See the corresponding HAPPENS_BEFORE in EnsureInstance(...). | |
| 110 ANNOTATE_HAPPENS_AFTER(&state_); | 115 ANNOTATE_HAPPENS_AFTER(&state_); |
| 111 | 116 |
| 112 return instance; | 117 return instance_; |
| 113 } | 118 } |
| 114 | 119 |
| 115 private: | 120 private: |
| 116 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 121 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
| 122 Type *instance_; |
| 117 | 123 |
| 118 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 124 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
| 119 }; | 125 }; |
| 120 | 126 |
| 121 } // namespace base | 127 } // namespace base |
| 122 | 128 |
| 123 #endif // BASE_LAZY_INSTANCE_H_ | 129 #endif // BASE_LAZY_INSTANCE_H_ |
| OLD | NEW |