OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 #include "base/base_export.h" | 42 #include "base/base_export.h" |
43 #include "base/basictypes.h" | 43 #include "base/basictypes.h" |
44 #include "base/logging.h" | 44 #include "base/logging.h" |
45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | 45 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
46 #include "base/threading/thread_restrictions.h" | 46 #include "base/threading/thread_restrictions.h" |
47 | 47 |
48 namespace base { | 48 namespace base { |
49 | 49 |
50 template <typename Type> | 50 template <typename Type> |
51 struct DefaultLazyInstanceTraits { | 51 struct DefaultLazyInstanceTraits { |
52 static const bool kRegisterOnExit = true; | |
52 static const bool kAllowedToAccessOnNonjoinableThread = false; | 53 static const bool kAllowedToAccessOnNonjoinableThread = false; |
53 | 54 |
54 static Type* New(void* instance) { | 55 static Type* New(void* instance) { |
55 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) % sizeof(instance), 0u) | 56 DCHECK_EQ(reinterpret_cast<uintptr_t>(instance) % sizeof(instance), 0u) |
56 << ": Bad boy, the buffer passed to placement new is not aligned!\n" | 57 << ": Bad boy, the buffer passed to placement new is not aligned!\n" |
57 "This may break some stuff like SSE-based optimizations assuming the " | 58 "This may break some stuff like SSE-based optimizations assuming the " |
58 "<Type> objects are word aligned."; | 59 "<Type> objects are word aligned."; |
59 // Use placement new to initialize our instance in our preallocated space. | 60 // Use placement new to initialize our instance in our preallocated space. |
60 // The parenthesis is very important here to force POD type initialization. | 61 // The parenthesis is very important here to force POD type initialization. |
61 return new (instance) Type(); | 62 return new (instance) Type(); |
62 } | 63 } |
63 static void Delete(void* instance) { | 64 static void Delete(Type* instance) { |
64 // Explicitly call the destructor. | 65 // Explicitly call the destructor. |
65 reinterpret_cast<Type*>(instance)->~Type(); | 66 instance->~Type(); |
66 } | 67 } |
67 }; | 68 }; |
68 | 69 |
69 template <typename Type> | 70 template <typename Type> |
70 struct LeakyLazyInstanceTraits { | 71 struct LeakyLazyInstanceTraits { |
72 static const bool kRegisterOnExit = false; | |
71 static const bool kAllowedToAccessOnNonjoinableThread = true; | 73 static const bool kAllowedToAccessOnNonjoinableThread = true; |
72 | 74 |
73 static Type* New(void* instance) { | 75 static Type* New(void* instance) { |
74 return DefaultLazyInstanceTraits<Type>::New(instance); | 76 return DefaultLazyInstanceTraits<Type>::New(instance); |
75 } | 77 } |
76 // Rather than define an empty Delete function, we make Delete itself | 78 static void Delete(Type* instance) { |
77 // a null pointer. This allows us to completely sidestep registering | 79 } |
78 // this object with an AtExitManager, which allows you to use | |
79 // LeakyLazyInstanceTraits in contexts where you don't have an | |
80 // AtExitManager. | |
81 static void (*Delete)(void* instance); | |
82 }; | 80 }; |
83 | 81 |
84 template <typename Type> | |
85 void (*LeakyLazyInstanceTraits<Type>::Delete)(void* instance) = NULL; | |
86 | |
87 // We pull out some of the functionality into a non-templated base, so that we | 82 // We pull out some of the functionality into a non-templated base, so that we |
88 // can implement the more complicated pieces out of line in the .cc file. | 83 // can implement the more complicated pieces out of line in the .cc file. |
89 class BASE_EXPORT LazyInstanceHelper { | 84 class BASE_EXPORT LazyInstanceHelper { |
90 protected: | 85 protected: |
91 enum { | 86 enum { |
92 STATE_EMPTY = 0, | 87 STATE_EMPTY = 0, |
93 STATE_CREATING = 1, | 88 STATE_CREATING = 1, |
94 STATE_CREATED = 2 | 89 STATE_CREATED = 2 |
95 }; | 90 }; |
96 | 91 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 // Since a thread sees state_ != STATE_CREATED at most once, | 140 // Since a thread sees state_ != STATE_CREATED at most once, |
146 // the load is taken out of NeedsInstance() as a fast-path. | 141 // the load is taken out of NeedsInstance() as a fast-path. |
147 // The load has acquire memory ordering as a thread which sees | 142 // The load has acquire memory ordering as a thread which sees |
148 // state_ == STATE_CREATED needs to acquire visibility over | 143 // state_ == STATE_CREATED needs to acquire visibility over |
149 // the associated data (buf_). Pairing Release_Store is in | 144 // the associated data (buf_). Pairing Release_Store is in |
150 // CompleteInstance(). | 145 // CompleteInstance(). |
151 if ((base::subtle::Acquire_Load(&state_) != STATE_CREATED) && | 146 if ((base::subtle::Acquire_Load(&state_) != STATE_CREATED) && |
152 NeedsInstance()) { | 147 NeedsInstance()) { |
153 // Create the instance in the space provided by |buf_|. | 148 // Create the instance in the space provided by |buf_|. |
154 instance_ = Traits::New(buf_); | 149 instance_ = Traits::New(buf_); |
155 // Traits::Delete will be null for LeakyLazyInstanceTraits | 150 CompleteInstance(this, Traits::kRegisterOnExit ? OnExit : NULL); |
156 void (*dtor)(void*) = Traits::Delete; | |
157 CompleteInstance(this, (dtor == NULL) ? NULL : OnExit); | |
158 } | 151 } |
159 | 152 |
160 // This annotation helps race detectors recognize correct lock-less | 153 // This annotation helps race detectors recognize correct lock-less |
161 // synchronization between different threads calling Pointer(). | 154 // synchronization between different threads calling Pointer(). |
162 // We suggest dynamic race detection tool that "Traits::New" above | 155 // We suggest dynamic race detection tool that "Traits::New" above |
163 // and CompleteInstance(...) happens before "return instance_" below. | 156 // and CompleteInstance(...) happens before "return instance_" below. |
164 // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). | 157 // See the corresponding HAPPENS_BEFORE in CompleteInstance(...). |
165 ANNOTATE_HAPPENS_AFTER(&state_); | 158 ANNOTATE_HAPPENS_AFTER(&state_); |
166 return instance_; | 159 return instance_; |
167 } | 160 } |
168 | 161 |
169 bool operator==(Type* p) { | 162 bool operator==(Type* p) { |
170 switch (base::subtle::NoBarrier_Load(&state_)) { | 163 switch (base::subtle::NoBarrier_Load(&state_)) { |
171 case STATE_EMPTY: | 164 case STATE_EMPTY: |
172 return p == NULL; | 165 return p == NULL; |
173 case STATE_CREATING: | 166 case STATE_CREATING: |
174 return static_cast<int8*>(static_cast<void*>(p)) == buf_; | 167 return static_cast<int8*>(static_cast<void*>(p)) == buf_; |
175 case STATE_CREATED: | 168 case STATE_CREATED: |
176 return p == instance_; | 169 return p == instance_; |
177 default: | 170 default: |
178 return false; | 171 return false; |
179 } | 172 } |
180 } | 173 } |
181 | 174 |
182 private: | 175 private: |
183 // Adapter function for use with AtExit. This should be called single | 176 // Adapter function for use with AtExit. This should be called single |
184 // threaded, so don't use atomic operations. | 177 // threaded, so don't synchronize across threads. |
joth
2011/10/25 14:22:57
this comment looked incorrect, as it is using an a
willchan no longer on Chromium
2011/10/25 15:57:25
I think what's happening here is we *shouldn't* ne
| |
185 // Calling OnExit while the instance is in use by other threads is a mistake. | 178 // Calling OnExit while the instance is in use by other threads is a mistake. |
186 static void OnExit(void* lazy_instance) { | 179 static void OnExit(void* lazy_instance) { |
187 LazyInstance<Type, Traits>* me = | 180 LazyInstance<Type, Traits>* me = |
188 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); | 181 reinterpret_cast<LazyInstance<Type, Traits>*>(lazy_instance); |
189 Traits::Delete(me->instance_); | 182 Traits::Delete(me->instance_); |
190 me->instance_ = NULL; | 183 me->instance_ = NULL; |
191 base::subtle::Release_Store(&me->state_, STATE_EMPTY); | 184 base::subtle::Release_Store(&me->state_, STATE_EMPTY); |
192 } | 185 } |
193 | 186 |
194 Type *instance_; | 187 Type *instance_; |
195 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. | 188 int8 buf_[sizeof(Type)]; // Preallocate the space for the Type instance. |
196 | 189 |
197 DISALLOW_COPY_AND_ASSIGN(LazyInstance); | 190 DISALLOW_COPY_AND_ASSIGN(LazyInstance); |
198 }; | 191 }; |
199 | 192 |
200 } // namespace base | 193 } // namespace base |
201 | 194 |
202 #endif // BASE_LAZY_INSTANCE_H_ | 195 #endif // BASE_LAZY_INSTANCE_H_ |
OLD | NEW |