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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // providing your own trait: | 58 // providing your own trait: |
59 // Example usage: | 59 // Example usage: |
60 // struct MyCreateTrait { | 60 // struct MyCreateTrait { |
61 // static void Construct(MyClass* allocated_ptr) { | 61 // static void Construct(MyClass* allocated_ptr) { |
62 // new (allocated_ptr) MyClass(/* extra parameters... */); | 62 // new (allocated_ptr) MyClass(/* extra parameters... */); |
63 // } | 63 // } |
64 // }; | 64 // }; |
65 // static LazyInstance<MyClass, MyCreateTrait>::type my_instance = | 65 // static LazyInstance<MyClass, MyCreateTrait>::type my_instance = |
66 // LAZY_INSTANCE_INITIALIZER; | 66 // LAZY_INSTANCE_INITIALIZER; |
67 // | 67 // |
| 68 // WARNING: This implementation of LazyInstance is NOT thread-safe by default. |
| 69 // See ThreadSafeInitOnceTrait declared below for that. |
| 70 // |
68 // Notes for advanced users: | 71 // Notes for advanced users: |
69 // LazyInstance can actually be used in two different ways: | 72 // LazyInstance can actually be used in two different ways: |
70 // | 73 // |
71 // - "Static mode" which is the default mode since it is the most efficient | 74 // - "Static mode" which is the default mode since it is the most efficient |
72 // (no extra heap allocation). In this mode, the instance is statically | 75 // (no extra heap allocation). In this mode, the instance is statically |
73 // allocated (stored in the global data section at compile time). | 76 // allocated (stored in the global data section at compile time). |
74 // The macro LAZY_STATIC_INSTANCE_INITIALIZER (= LAZY_INSTANCE_INITIALIZER) | 77 // The macro LAZY_STATIC_INSTANCE_INITIALIZER (= LAZY_INSTANCE_INITIALIZER) |
75 // must be used to initialize static lazy instances. | 78 // must be used to initialize static lazy instances. |
76 // | 79 // |
77 // - "Dynamic mode". In this mode, the instance is dynamically allocated and | 80 // - "Dynamic mode". In this mode, the instance is dynamically allocated and |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 147 |
145 | 148 |
146 template <typename T> | 149 template <typename T> |
147 struct DefaultCreateTrait { | 150 struct DefaultCreateTrait { |
148 static T* Create() { | 151 static T* Create() { |
149 return new T(); | 152 return new T(); |
150 } | 153 } |
151 }; | 154 }; |
152 | 155 |
153 | 156 |
| 157 struct ThreadSafeInitOnceTrait { |
| 158 template <typename Function, typename Storage> |
| 159 static void Init(OnceType* once, Function function, Storage storage) { |
| 160 CallOnce(once, function, storage); |
| 161 } |
| 162 }; |
| 163 |
| 164 |
| 165 // Initialization trait for users who don't care about thread-safety. |
| 166 struct SingleThreadInitOnceTrait { |
| 167 template <typename Function, typename Storage> |
| 168 static void Init(OnceType* once, Function function, Storage storage) { |
| 169 if (*once == ONCE_STATE_UNINITIALIZED) { |
| 170 function(storage); |
| 171 *once = ONCE_STATE_DONE; |
| 172 } |
| 173 } |
| 174 }; |
| 175 |
| 176 |
154 // TODO(pliard): Handle instances destruction (using global destructors). | 177 // TODO(pliard): Handle instances destruction (using global destructors). |
155 template <typename T, typename AllocationTrait, typename CreateTrait, | 178 template <typename T, typename AllocationTrait, typename CreateTrait, |
156 typename DestroyTrait /* not used yet. */ > | 179 typename InitOnceTrait, 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::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 typename InitOnceTrait = SingleThreadInitOnceTrait, |
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 typename InitOnceTrait = SingleThreadInitOnceTrait, |
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 typename InitOnceTrait = SingleThreadInitOnceTrait, |
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 |