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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 // WARNINGS: | 68 // WARNINGS: |
69 // - This implementation of LazyInstance is NOT THREAD-SAFE by default. See | 69 // - This implementation of LazyInstance IS THREAD-SAFE by default. See |
70 // ThreadSafeInitOnceTrait declared below for that. | 70 // SingleThreadInitOnceTrait if you don't care about thread safety. |
71 // - Lazy initialization comes with a cost. Make sure that you don't use it on | 71 // - Lazy initialization comes with a cost. Make sure that you don't use it on |
72 // critical path. Consider adding your initialization code to a function | 72 // critical path. Consider adding your initialization code to a function |
73 // which is explicitly called once. | 73 // which is explicitly called once. |
74 // | 74 // |
75 // Notes for advanced users: | 75 // Notes for advanced users: |
76 // LazyInstance can actually be used in two different ways: | 76 // LazyInstance can actually be used in two different ways: |
77 // | 77 // |
78 // - "Static mode" which is the default mode since it is the most efficient | 78 // - "Static mode" which is the default mode since it is the most efficient |
79 // (no extra heap allocation). In this mode, the instance is statically | 79 // (no extra heap allocation). In this mode, the instance is statically |
80 // allocated (stored in the global data section at compile time). | 80 // allocated (stored in the global data section at compile time). |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 mutable OnceType once_; | 220 mutable OnceType once_; |
221 // Note that the previous field, OnceType, is an AtomicWord which guarantees | 221 // Note that the previous field, OnceType, is an AtomicWord which guarantees |
222 // 4-byte alignment of the storage field below. If compiling with GCC (>4.2), | 222 // 4-byte alignment of the storage field below. If compiling with GCC (>4.2), |
223 // the LAZY_ALIGN macro above will guarantee correctness for any alignment. | 223 // the LAZY_ALIGN macro above will guarantee correctness for any alignment. |
224 mutable StorageType storage_; | 224 mutable StorageType storage_; |
225 }; | 225 }; |
226 | 226 |
227 | 227 |
228 template <typename T, | 228 template <typename T, |
229 typename CreateTrait = DefaultConstructTrait<T>, | 229 typename CreateTrait = DefaultConstructTrait<T>, |
230 typename InitOnceTrait = SingleThreadInitOnceTrait, | 230 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
231 typename DestroyTrait = LeakyInstanceTrait<T> > | 231 typename DestroyTrait = LeakyInstanceTrait<T> > |
232 struct LazyStaticInstance { | 232 struct LazyStaticInstance { |
233 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, | 233 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, |
234 CreateTrait, InitOnceTrait, DestroyTrait> type; | 234 CreateTrait, InitOnceTrait, DestroyTrait> type; |
235 }; | 235 }; |
236 | 236 |
237 | 237 |
238 template <typename T, | 238 template <typename T, |
239 typename CreateTrait = DefaultConstructTrait<T>, | 239 typename CreateTrait = DefaultConstructTrait<T>, |
240 typename InitOnceTrait = SingleThreadInitOnceTrait, | 240 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
241 typename DestroyTrait = LeakyInstanceTrait<T> > | 241 typename DestroyTrait = LeakyInstanceTrait<T> > |
242 struct LazyInstance { | 242 struct LazyInstance { |
243 // A LazyInstance is a LazyStaticInstance. | 243 // A LazyInstance is a LazyStaticInstance. |
244 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait, | 244 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait, |
245 DestroyTrait>::type type; | 245 DestroyTrait>::type type; |
246 }; | 246 }; |
247 | 247 |
248 | 248 |
249 template <typename T, | 249 template <typename T, |
250 typename CreateTrait = DefaultCreateTrait<T>, | 250 typename CreateTrait = DefaultCreateTrait<T>, |
251 typename InitOnceTrait = SingleThreadInitOnceTrait, | 251 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
252 typename DestroyTrait = LeakyInstanceTrait<T> > | 252 typename DestroyTrait = LeakyInstanceTrait<T> > |
253 struct LazyDynamicInstance { | 253 struct LazyDynamicInstance { |
254 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, | 254 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, |
255 CreateTrait, InitOnceTrait, DestroyTrait> type; | 255 CreateTrait, InitOnceTrait, DestroyTrait> type; |
256 }; | 256 }; |
257 | 257 |
258 } } // namespace v8::internal | 258 } } // namespace v8::internal |
259 | 259 |
260 #endif // V8_LAZY_INSTANCE_H_ | 260 #endif // V8_LAZY_INSTANCE_H_ |
OLD | NEW |