Chromium Code Reviews| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 | 167 |
| 168 struct ThreadSafeInitOnceTrait { | 168 struct ThreadSafeInitOnceTrait { |
| 169 template <typename Function, typename Storage> | 169 template <typename Function, typename Storage> |
| 170 static void Init(OnceType* once, Function function, Storage storage) { | 170 static void Init(OnceType* once, Function function, Storage storage) { |
| 171 CallOnce(once, function, storage); | 171 CallOnce(once, function, storage); |
| 172 } | 172 } |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 | 175 |
| 176 // Initialization trait for users who don't care about thread-safety. | 176 // Initialization trait for users who don't care about thread-safety. |
| 177 // | |
| 178 // Thank you for making this the default, Satan. | |
|
Hannes Payer (out of office)
2014/04/16 07:56:56
Very entertaining, but we should probably not have
| |
| 177 struct SingleThreadInitOnceTrait { | 179 struct SingleThreadInitOnceTrait { |
| 178 template <typename Function, typename Storage> | 180 template <typename Function, typename Storage> |
| 179 static void Init(OnceType* once, Function function, Storage storage) { | 181 static void Init(OnceType* once, Function function, Storage storage) { |
| 180 if (*once == ONCE_STATE_UNINITIALIZED) { | 182 if (*once == ONCE_STATE_UNINITIALIZED) { |
| 181 function(storage); | 183 function(storage); |
| 182 *once = ONCE_STATE_DONE; | 184 *once = ONCE_STATE_DONE; |
| 183 } | 185 } |
| 184 } | 186 } |
| 185 }; | 187 }; |
| 186 | 188 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 mutable OnceType once_; | 222 mutable OnceType once_; |
| 221 // Note that the previous field, OnceType, is an AtomicWord which guarantees | 223 // 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), | 224 // 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. | 225 // the LAZY_ALIGN macro above will guarantee correctness for any alignment. |
| 224 mutable StorageType storage_; | 226 mutable StorageType storage_; |
| 225 }; | 227 }; |
| 226 | 228 |
| 227 | 229 |
| 228 template <typename T, | 230 template <typename T, |
| 229 typename CreateTrait = DefaultConstructTrait<T>, | 231 typename CreateTrait = DefaultConstructTrait<T>, |
| 230 typename InitOnceTrait = SingleThreadInitOnceTrait, | 232 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
| 231 typename DestroyTrait = LeakyInstanceTrait<T> > | 233 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 232 struct LazyStaticInstance { | 234 struct LazyStaticInstance { |
| 233 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, | 235 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, |
| 234 CreateTrait, InitOnceTrait, DestroyTrait> type; | 236 CreateTrait, InitOnceTrait, DestroyTrait> type; |
| 235 }; | 237 }; |
| 236 | 238 |
| 237 | 239 |
| 238 template <typename T, | 240 template <typename T, |
| 239 typename CreateTrait = DefaultConstructTrait<T>, | 241 typename CreateTrait = DefaultConstructTrait<T>, |
| 240 typename InitOnceTrait = SingleThreadInitOnceTrait, | 242 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
| 241 typename DestroyTrait = LeakyInstanceTrait<T> > | 243 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 242 struct LazyInstance { | 244 struct LazyInstance { |
| 243 // A LazyInstance is a LazyStaticInstance. | 245 // A LazyInstance is a LazyStaticInstance. |
| 244 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait, | 246 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait, |
| 245 DestroyTrait>::type type; | 247 DestroyTrait>::type type; |
| 246 }; | 248 }; |
| 247 | 249 |
| 248 | 250 |
| 249 template <typename T, | 251 template <typename T, |
| 250 typename CreateTrait = DefaultCreateTrait<T>, | 252 typename CreateTrait = DefaultCreateTrait<T>, |
| 251 typename InitOnceTrait = SingleThreadInitOnceTrait, | 253 typename InitOnceTrait = ThreadSafeInitOnceTrait, |
| 252 typename DestroyTrait = LeakyInstanceTrait<T> > | 254 typename DestroyTrait = LeakyInstanceTrait<T> > |
| 253 struct LazyDynamicInstance { | 255 struct LazyDynamicInstance { |
| 254 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, | 256 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, |
| 255 CreateTrait, InitOnceTrait, DestroyTrait> type; | 257 CreateTrait, InitOnceTrait, DestroyTrait> type; |
| 256 }; | 258 }; |
| 257 | 259 |
| 258 } } // namespace v8::internal | 260 } } // namespace v8::internal |
| 259 | 261 |
| 260 #endif // V8_LAZY_INSTANCE_H_ | 262 #endif // V8_LAZY_INSTANCE_H_ |
| OLD | NEW |