Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(267)

Side by Side Diff: src/lazy-instance.h

Issue 10020040: Merged r11194, r11198, r11201, r11214, r11225, r11233, r11240 into 3.9 branch. (Closed) Base URL: https://v8.googlecode.com/svn/branches/3.9
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/isolate.cc ('k') | src/mips/lithium-mips.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 19 matching lines...) Expand all
97 100
98 101
99 template <typename T> 102 template <typename T>
100 struct LeakyInstanceTrait { 103 struct LeakyInstanceTrait {
101 static void Destroy(T* /* instance */) {} 104 static void Destroy(T* /* instance */) {}
102 }; 105 };
103 106
104 107
105 // Traits that define how an instance is allocated and accessed. 108 // Traits that define how an instance is allocated and accessed.
106 109
110 // TODO(kalmard): __alignof__ is only defined for GCC > 4.2. Fix alignment issue
111 // on MIPS with other compilers.
112 #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
113 #define LAZY_ALIGN(x) __attribute__((aligned(__alignof__(x))))
114 #else
115 #define LAZY_ALIGN(x)
116 #endif
117
107 template <typename T> 118 template <typename T>
108 struct StaticallyAllocatedInstanceTrait { 119 struct StaticallyAllocatedInstanceTrait {
109 typedef char StorageType[sizeof(T)]; 120 typedef char StorageType[sizeof(T)] LAZY_ALIGN(T);
110 121
111 static T* MutableInstance(StorageType* storage) { 122 static T* MutableInstance(StorageType* storage) {
112 return reinterpret_cast<T*>(storage); 123 return reinterpret_cast<T*>(storage);
113 } 124 }
114 125
115 template <typename ConstructTrait> 126 template <typename ConstructTrait>
116 static void InitStorageUsingTrait(StorageType* storage) { 127 static void InitStorageUsingTrait(StorageType* storage) {
117 ConstructTrait::Construct(MutableInstance(storage)); 128 ConstructTrait::Construct(MutableInstance(storage));
118 } 129 }
119 }; 130 };
120 131
132 #undef LAZY_ALIGN
133
121 134
122 template <typename T> 135 template <typename T>
123 struct DynamicallyAllocatedInstanceTrait { 136 struct DynamicallyAllocatedInstanceTrait {
124 typedef T* StorageType; 137 typedef T* StorageType;
125 138
126 static T* MutableInstance(StorageType* storage) { 139 static T* MutableInstance(StorageType* storage) {
127 return *storage; 140 return *storage;
128 } 141 }
129 142
130 template <typename CreateTrait> 143 template <typename CreateTrait>
(...skipping 13 matching lines...) Expand all
144 157
145 158
146 template <typename T> 159 template <typename T>
147 struct DefaultCreateTrait { 160 struct DefaultCreateTrait {
148 static T* Create() { 161 static T* Create() {
149 return new T(); 162 return new T();
150 } 163 }
151 }; 164 };
152 165
153 166
167 struct ThreadSafeInitOnceTrait {
168 template <typename Function, typename Storage>
169 static void Init(OnceType* once, Function function, Storage storage) {
170 CallOnce(once, function, storage);
171 }
172 };
173
174
175 // Initialization trait for users who don't care about thread-safety.
176 struct SingleThreadInitOnceTrait {
177 template <typename Function, typename Storage>
178 static void Init(OnceType* once, Function function, Storage storage) {
179 if (*once == ONCE_STATE_UNINITIALIZED) {
180 function(storage);
181 *once = ONCE_STATE_DONE;
182 }
183 }
184 };
185
186
154 // TODO(pliard): Handle instances destruction (using global destructors). 187 // TODO(pliard): Handle instances destruction (using global destructors).
155 template <typename T, typename AllocationTrait, typename CreateTrait, 188 template <typename T, typename AllocationTrait, typename CreateTrait,
156 typename DestroyTrait /* not used yet. */ > 189 typename InitOnceTrait, typename DestroyTrait /* not used yet. */>
157 struct LazyInstanceImpl { 190 struct LazyInstanceImpl {
158 public: 191 public:
159 typedef typename AllocationTrait::StorageType StorageType; 192 typedef typename AllocationTrait::StorageType StorageType;
160 193
161 private: 194 private:
162 static void InitInstance(StorageType* storage) { 195 static void InitInstance(StorageType* storage) {
163 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage); 196 AllocationTrait::template InitStorageUsingTrait<CreateTrait>(storage);
164 } 197 }
165 198
166 void Init() const { 199 void Init() const {
167 CallOnce(&once_, &InitInstance, &storage_); 200 InitOnceTrait::Init(
201 &once_,
202 // Casts to void* are needed here to avoid breaking strict aliasing
203 // rules.
204 reinterpret_cast<void(*)(void*)>(&InitInstance), // NOLINT
205 reinterpret_cast<void*>(&storage_));
168 } 206 }
169 207
170 public: 208 public:
171 T* Pointer() { 209 T* Pointer() {
172 Init(); 210 Init();
173 return AllocationTrait::MutableInstance(&storage_); 211 return AllocationTrait::MutableInstance(&storage_);
174 } 212 }
175 213
176 const T& Get() const { 214 const T& Get() const {
177 Init(); 215 Init();
178 return *AllocationTrait::MutableInstance(&storage_); 216 return *AllocationTrait::MutableInstance(&storage_);
179 } 217 }
180 218
181 mutable OnceType once_; 219 mutable OnceType once_;
182 // Note that the previous field, OnceType, is an AtomicWord which guarantees 220 // Note that the previous field, OnceType, is an AtomicWord which guarantees
183 // the correct alignment of the storage field below. 221 // 4-byte alignment of the storage field below. If compiling with GCC (>4.2),
222 // the LAZY_ALIGN macro above will guarantee correctness for any alignment.
184 mutable StorageType storage_; 223 mutable StorageType storage_;
185 }; 224 };
186 225
187 226
188 template <typename T, 227 template <typename T,
189 typename CreateTrait = DefaultConstructTrait<T>, 228 typename CreateTrait = DefaultConstructTrait<T>,
229 typename InitOnceTrait = SingleThreadInitOnceTrait,
190 typename DestroyTrait = LeakyInstanceTrait<T> > 230 typename DestroyTrait = LeakyInstanceTrait<T> >
191 struct LazyStaticInstance { 231 struct LazyStaticInstance {
192 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>, CreateTrait, 232 typedef LazyInstanceImpl<T, StaticallyAllocatedInstanceTrait<T>,
193 DestroyTrait> type; 233 CreateTrait, InitOnceTrait, DestroyTrait> type;
194 }; 234 };
195 235
196 236
197 template <typename T, 237 template <typename T,
198 typename CreateTrait = DefaultConstructTrait<T>, 238 typename CreateTrait = DefaultConstructTrait<T>,
239 typename InitOnceTrait = SingleThreadInitOnceTrait,
199 typename DestroyTrait = LeakyInstanceTrait<T> > 240 typename DestroyTrait = LeakyInstanceTrait<T> >
200 struct LazyInstance { 241 struct LazyInstance {
201 // A LazyInstance is a LazyStaticInstance. 242 // A LazyInstance is a LazyStaticInstance.
202 typedef typename LazyStaticInstance<T, CreateTrait, DestroyTrait>::type type; 243 typedef typename LazyStaticInstance<T, CreateTrait, InitOnceTrait,
244 DestroyTrait>::type type;
203 }; 245 };
204 246
205 247
206 template <typename T, 248 template <typename T,
207 typename CreateTrait = DefaultConstructTrait<T>, 249 typename CreateTrait = DefaultConstructTrait<T>,
250 typename InitOnceTrait = SingleThreadInitOnceTrait,
208 typename DestroyTrait = LeakyInstanceTrait<T> > 251 typename DestroyTrait = LeakyInstanceTrait<T> >
209 struct LazyDynamicInstance { 252 struct LazyDynamicInstance {
210 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>, CreateTrait, 253 typedef LazyInstanceImpl<T, DynamicallyAllocatedInstanceTrait<T>,
211 DestroyTrait> type; 254 CreateTrait, InitOnceTrait, DestroyTrait> type;
212 }; 255 };
213 256
214 } } // namespace v8::internal 257 } } // namespace v8::internal
215 258
216 #endif // V8_LAZY_INSTANCE_H_ 259 #endif // V8_LAZY_INSTANCE_H_
OLDNEW
« no previous file with comments | « src/isolate.cc ('k') | src/mips/lithium-mips.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698