OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
144 | 144 |
145 | 145 |
146 template <typename T> | 146 template <typename T> |
147 struct DefaultCreateTrait { | 147 struct DefaultCreateTrait { |
148 static T* Create() { | 148 static T* Create() { |
149 return new T(); | 149 return new T(); |
150 } | 150 } |
151 }; | 151 }; |
152 | 152 |
153 | 153 |
154 template <typename T> | |
155 struct ThreadSafeInitOnceTrait { | |
156 template <typename Function, typename Storage> | |
157 static void Init(OnceType* once, Function function, Storage storage) { | |
158 CallOnce(once, function, storage); | |
159 } | |
160 }; | |
161 | |
162 | |
163 // Initialization trait for users who don't care about thread-safety. | |
164 template <typename T> | |
165 struct SingleThreadInitOnceTrait { | |
166 template <typename Function, typename Storage> | |
167 static void Init(OnceType* once, Function function, Storage storage) { | |
168 if (*once == ONCE_STATE_UNINITIALIZED) { | |
169 function(storage); | |
170 *once = ONCE_STATE_DONE; | |
171 } | |
172 } | |
173 }; | |
174 | |
175 | |
154 // TODO(pliard): Handle instances destruction (using global destructors). | 176 // TODO(pliard): Handle instances destruction (using global destructors). |
155 template <typename T, typename AllocationTrait, typename CreateTrait, | 177 template <typename T, typename AllocationTrait, typename CreateTrait, |
156 typename DestroyTrait /* not used yet. */ > | 178 template <class> class InitOnceTrait, |
179 typename DestroyTrait /* not used yet. */> | |
157 struct LazyInstanceImpl { | 180 struct LazyInstanceImpl { |
158 public: | 181 public: |
159 typedef typename AllocationTrait::StorageType StorageType; | 182 typedef typename AllocationTrait::StorageType StorageType; |
160 | 183 |
161 private: | 184 private: |
162 static void InitInstance(StorageType* storage) { | 185 static void InitInstance(StorageType* storage) { |
163 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage); | 186 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage); |
164 } | 187 } |
165 | 188 |
166 void Init() const { | 189 void Init() const { |
167 CallOnce(&once_, &InitInstance, &storage_); | 190 InitOnceTrait<T>::Init( |
191 &once_, | |
192 // Casts to void* are needed here to avoid breaking strict aliasing | |
193 // rules. | |
194 reinterpret_cast<void (*)(void*)>(&InitInstance), | |
195 reinterpret_cast<void*>(&storage_)); | |
168 } | 196 } |
169 | 197 |
170 public: | 198 public: |
171 T* Pointer() { | 199 T* Pointer() { |
172 Init(); | 200 Init(); |
173 return AllocationTrait::MutableInstance(&storage_); | 201 return AllocationTrait::MutableInstance(&storage_); |
174 } | 202 } |
175 | 203 |
176 const T& Get() const { | 204 const T& Get() const { |
177 Init(); | 205 Init(); |
178 return *AllocationTrait::MutableInstance(&storage_); | 206 return *AllocationTrait::MutableInstance(&storage_); |
179 } | 207 } |
180 | 208 |
181 mutable OnceType once_; | 209 mutable OnceType once_; |
182 // Note that the previous field, OnceType, is an AtomicWord which guarantees | 210 // Note that the previous field, OnceType, is an AtomicWord which guarantees |
183 // the correct alignment of the storage field below. | 211 // the correct alignment of the storage field below. |
184 mutable StorageType storage_; | 212 mutable StorageType storage_; |
185 }; | 213 }; |
186 | 214 |
187 | 215 |
188 template <typename T, | 216 template <typename T, |
189 typename CreateTrait = DefaultConstructTrait<T>, | 217 typename CreateTrait = DefaultConstructTrait<T>, |
218 template <class> class InitOnceTrait = ThreadSafeInitOnceTrait, | |
danno
2012/03/29 07:45:17
how about changing the default to SingleThreadInit
Philippe
2012/03/29 09:00:48
Right. I was just concerned about the potential ri
| |
190 typename DestroyTrait = LeakyInstanceTrait<T> > | 219 typename DestroyTrait = LeakyInstanceTrait<T> > |
191 struct LazyStaticInstance { | 220 struct LazyStaticInstance { |
192 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, CreateTrait, | 221 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, |
193 DestroyTrait> type; | 222 CreateTrait, InitOnceTrait, DestroyTrait> type; |
194 }; | 223 }; |
195 | 224 |
196 | 225 |
197 template <typename T, | 226 template <typename T, |
198 typename CreateTrait = DefaultConstructTrait<T>, | 227 typename CreateTrait = DefaultConstructTrait<T>, |
228 template <class> class InitOnceTrait = ThreadSafeInitOnceTrait, | |
danno
2012/03/29 07:45:17
Here too
Philippe
2012/03/29 09:00:48
Done.
| |
199 typename DestroyTrait = LeakyInstanceTrait<T> > | 229 typename DestroyTrait = LeakyInstanceTrait<T> > |
200 struct LazyInstance { | 230 struct LazyInstance { |
201 // A LazyInstance is a LazyStaticInstance. | 231 // A LazyInstance is a LazyStaticInstance. |
202 typedef typename LazyStaticInstance<T, CreateTrait, DestroyTrait>::type type; | 232 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait, |
233 DestroyTrait>::type type; | |
203 }; | 234 }; |
204 | 235 |
205 | 236 |
206 template <typename T, | 237 template <typename T, |
207 typename CreateTrait = DefaultConstructTrait<T>, | 238 typename CreateTrait = DefaultConstructTrait<T>, |
239 template <class> class InitOnceTrait = ThreadSafeInitOnceTrait, | |
danno
2012/03/29 07:45:17
Here too
Philippe
2012/03/29 09:00:48
Done.
| |
208 typename DestroyTrait = LeakyInstanceTrait<T> > | 240 typename DestroyTrait = LeakyInstanceTrait<T> > |
209 struct LazyDynamicInstance { | 241 struct LazyDynamicInstance { |
210 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, CreateTrait, | 242 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, |
211 DestroyTrait> type; | 243 CreateTrait, InitOnceTrait, DestroyTrait> type; |
212 }; | 244 }; |
213 | 245 |
214 } } // namespace v8::internal | 246 } } // namespace v8::internal |
215 | 247 |
216 #endif // V8_LAZY_INSTANCE_H_ | 248 #endif // V8_LAZY_INSTANCE_H_ |
OLD | NEW |