OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_D8_H_ | 5 #ifndef V8_D8_H_ |
6 #define V8_D8_H_ | 6 #define V8_D8_H_ |
7 | 7 |
| 8 #include <memory> |
8 #include <string> | 9 #include <string> |
| 10 #include <unordered_set> |
| 11 #include <vector> |
9 | 12 |
10 #include "src/allocation.h" | 13 #include "src/allocation.h" |
| 14 #include "src/base/functional.h" |
11 #include "src/base/hashmap.h" | 15 #include "src/base/hashmap.h" |
12 #include "src/base/platform/time.h" | 16 #include "src/base/platform/time.h" |
13 #include "src/list.h" | 17 #include "src/list.h" |
14 #include "src/utils.h" | 18 #include "src/utils.h" |
15 | 19 |
16 #include "src/base/once.h" | 20 #include "src/base/once.h" |
17 | 21 |
18 | 22 |
19 namespace v8 { | 23 namespace v8 { |
20 | 24 |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 base::Thread* thread_; | 140 base::Thread* thread_; |
137 | 141 |
138 void ExitShell(int exit_code); | 142 void ExitShell(int exit_code); |
139 Local<String> ReadFile(Isolate* isolate, const char* name); | 143 Local<String> ReadFile(Isolate* isolate, const char* name); |
140 | 144 |
141 const char** argv_; | 145 const char** argv_; |
142 int begin_offset_; | 146 int begin_offset_; |
143 int end_offset_; | 147 int end_offset_; |
144 }; | 148 }; |
145 | 149 |
146 enum SerializationTag { | |
147 kSerializationTagUndefined, | |
148 kSerializationTagNull, | |
149 kSerializationTagTrue, | |
150 kSerializationTagFalse, | |
151 kSerializationTagNumber, | |
152 kSerializationTagString, | |
153 kSerializationTagArray, | |
154 kSerializationTagObject, | |
155 kSerializationTagArrayBuffer, | |
156 kSerializationTagTransferredArrayBuffer, | |
157 kSerializationTagTransferredSharedArrayBuffer, | |
158 }; | |
159 | |
160 | 150 |
161 class SerializationData { | 151 class SerializationData { |
162 public: | 152 public: |
163 SerializationData() {} | 153 SerializationData() : data_(nullptr), size_(0) {} |
164 ~SerializationData(); | 154 ~SerializationData(); |
165 | 155 |
166 void WriteTag(SerializationTag tag); | 156 uint8_t* data() { return data_.get(); } |
167 void WriteMemory(const void* p, int length); | 157 size_t size() { return size_; } |
168 void WriteArrayBufferContents(const ArrayBuffer::Contents& contents); | 158 const std::vector<ArrayBuffer::Contents>& array_buffer_contents() { |
169 void WriteSharedArrayBufferContents( | 159 return array_buffer_contents_; |
170 const SharedArrayBuffer::Contents& contents); | 160 } |
171 | 161 const std::vector<SharedArrayBuffer::Contents>& |
172 template <typename T> | 162 shared_array_buffer_contents() { |
173 void Write(const T& data) { | 163 return shared_array_buffer_contents_; |
174 WriteMemory(&data, sizeof(data)); | |
175 } | 164 } |
176 | 165 |
177 SerializationTag ReadTag(int* offset) const; | 166 void ClearTransferredArrayBuffers(); |
178 void ReadMemory(void* p, int length, int* offset) const; | |
179 void ReadArrayBufferContents(ArrayBuffer::Contents* contents, | |
180 int* offset) const; | |
181 void ReadSharedArrayBufferContents(SharedArrayBuffer::Contents* contents, | |
182 int* offset) const; | |
183 | |
184 template <typename T> | |
185 T Read(int* offset) const { | |
186 T value; | |
187 ReadMemory(&value, sizeof(value), offset); | |
188 return value; | |
189 } | |
190 | 167 |
191 private: | 168 private: |
192 i::List<uint8_t> data_; | 169 struct DataDeleter { |
193 i::List<ArrayBuffer::Contents> array_buffer_contents_; | 170 void operator()(uint8_t* p) const { free(p); } |
194 i::List<SharedArrayBuffer::Contents> shared_array_buffer_contents_; | 171 }; |
| 172 |
| 173 std::unique_ptr<uint8_t, DataDeleter> data_; |
| 174 size_t size_; |
| 175 std::vector<ArrayBuffer::Contents> array_buffer_contents_; |
| 176 std::vector<SharedArrayBuffer::Contents> shared_array_buffer_contents_; |
| 177 |
| 178 private: |
| 179 friend class Serializer; |
| 180 |
| 181 DISALLOW_COPY_AND_ASSIGN(SerializationData); |
195 }; | 182 }; |
196 | 183 |
197 | 184 |
198 class SerializationDataQueue { | 185 class SerializationDataQueue { |
199 public: | 186 public: |
200 void Enqueue(SerializationData* data); | 187 void Enqueue(std::unique_ptr<SerializationData> data); |
201 bool Dequeue(SerializationData** data); | 188 bool Dequeue(std::unique_ptr<SerializationData>* data); |
202 bool IsEmpty(); | 189 bool IsEmpty(); |
203 void Clear(); | 190 void Clear(); |
204 | 191 |
205 private: | 192 private: |
206 base::Mutex mutex_; | 193 base::Mutex mutex_; |
207 i::List<SerializationData*> data_; | 194 std::vector<std::unique_ptr<SerializationData>> data_; |
208 }; | 195 }; |
209 | 196 |
210 | 197 |
211 class Worker { | 198 class Worker { |
212 public: | 199 public: |
213 Worker(); | 200 Worker(); |
214 ~Worker(); | 201 ~Worker(); |
215 | 202 |
216 // Run the given script on this Worker. This function should only be called | 203 // Run the given script on this Worker. This function should only be called |
217 // once, and should only be called by the thread that created the Worker. | 204 // once, and should only be called by the thread that created the Worker. |
218 void StartExecuteInThread(const char* script); | 205 void StartExecuteInThread(const char* script); |
219 // Post a message to the worker's incoming message queue. The worker will | 206 // Post a message to the worker's incoming message queue. The worker will |
220 // take ownership of the SerializationData. | 207 // take ownership of the SerializationData. |
221 // This function should only be called by the thread that created the Worker. | 208 // This function should only be called by the thread that created the Worker. |
222 void PostMessage(SerializationData* data); | 209 void PostMessage(std::unique_ptr<SerializationData> data); |
223 // Synchronously retrieve messages from the worker's outgoing message queue. | 210 // Synchronously retrieve messages from the worker's outgoing message queue. |
224 // If there is no message in the queue, block until a message is available. | 211 // If there is no message in the queue, block until a message is available. |
225 // If there are no messages in the queue and the worker is no longer running, | 212 // If there are no messages in the queue and the worker is no longer running, |
226 // return nullptr. | 213 // return nullptr. |
227 // This function should only be called by the thread that created the Worker. | 214 // This function should only be called by the thread that created the Worker. |
228 SerializationData* GetMessage(); | 215 std::unique_ptr<SerializationData> GetMessage(); |
229 // Terminate the worker's event loop. Messages from the worker that have been | 216 // Terminate the worker's event loop. Messages from the worker that have been |
230 // queued can still be read via GetMessage(). | 217 // queued can still be read via GetMessage(). |
231 // This function can be called by any thread. | 218 // This function can be called by any thread. |
232 void Terminate(); | 219 void Terminate(); |
233 // Terminate and join the thread. | 220 // Terminate and join the thread. |
234 // This function can be called by any thread. | 221 // This function can be called by any thread. |
235 void WaitForThread(); | 222 void WaitForThread(); |
236 | 223 |
237 private: | 224 private: |
238 class WorkerThread : public base::Thread { | 225 class WorkerThread : public base::Thread { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 static void ReportException(Isolate* isolate, TryCatch* try_catch); | 315 static void ReportException(Isolate* isolate, TryCatch* try_catch); |
329 static Local<String> ReadFile(Isolate* isolate, const char* name); | 316 static Local<String> ReadFile(Isolate* isolate, const char* name); |
330 static Local<Context> CreateEvaluationContext(Isolate* isolate); | 317 static Local<Context> CreateEvaluationContext(Isolate* isolate); |
331 static int RunMain(Isolate* isolate, int argc, char* argv[], bool last_run); | 318 static int RunMain(Isolate* isolate, int argc, char* argv[], bool last_run); |
332 static int Main(int argc, char* argv[]); | 319 static int Main(int argc, char* argv[]); |
333 static void Exit(int exit_code); | 320 static void Exit(int exit_code); |
334 static void OnExit(Isolate* isolate); | 321 static void OnExit(Isolate* isolate); |
335 static void CollectGarbage(Isolate* isolate); | 322 static void CollectGarbage(Isolate* isolate); |
336 static void EmptyMessageQueues(Isolate* isolate); | 323 static void EmptyMessageQueues(Isolate* isolate); |
337 | 324 |
338 // TODO(binji): stupid implementation for now. Is there an easy way to hash an | 325 static std::unique_ptr<SerializationData> SerializeValue( |
339 // object for use in base::HashMap? By pointer? | 326 Isolate* isolate, Local<Value> value, Local<Value> transfer); |
340 typedef i::List<Local<Object>> ObjectList; | 327 static MaybeLocal<Value> DeserializeValue( |
341 static bool SerializeValue(Isolate* isolate, Local<Value> value, | 328 Isolate* isolate, std::unique_ptr<SerializationData> data); |
342 const ObjectList& to_transfer, | |
343 ObjectList* seen_objects, | |
344 SerializationData* out_data); | |
345 static MaybeLocal<Value> DeserializeValue(Isolate* isolate, | |
346 const SerializationData& data, | |
347 int* offset); | |
348 static void CleanupWorkers(); | 329 static void CleanupWorkers(); |
349 static int* LookupCounter(const char* name); | 330 static int* LookupCounter(const char* name); |
350 static void* CreateHistogram(const char* name, | 331 static void* CreateHistogram(const char* name, |
351 int min, | 332 int min, |
352 int max, | 333 int max, |
353 size_t buckets); | 334 size_t buckets); |
354 static void AddHistogramSample(void* histogram, int sample); | 335 static void AddHistogramSample(void* histogram, int sample); |
355 static void MapCounters(v8::Isolate* isolate, const char* name); | 336 static void MapCounters(v8::Isolate* isolate, const char* name); |
356 | 337 |
357 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args); | 338 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 static Global<Function> stringify_function_; | 417 static Global<Function> stringify_function_; |
437 static CounterMap* counter_map_; | 418 static CounterMap* counter_map_; |
438 // We statically allocate a set of local counters to be used if we | 419 // We statically allocate a set of local counters to be used if we |
439 // don't want to store the stats in a memory-mapped file | 420 // don't want to store the stats in a memory-mapped file |
440 static CounterCollection local_counters_; | 421 static CounterCollection local_counters_; |
441 static CounterCollection* counters_; | 422 static CounterCollection* counters_; |
442 static base::OS::MemoryMappedFile* counters_file_; | 423 static base::OS::MemoryMappedFile* counters_file_; |
443 static base::LazyMutex context_mutex_; | 424 static base::LazyMutex context_mutex_; |
444 static const base::TimeTicks kInitialTicks; | 425 static const base::TimeTicks kInitialTicks; |
445 | 426 |
| 427 struct SharedArrayBufferContentsHash { |
| 428 size_t operator()(const v8::SharedArrayBuffer::Contents& contents) const { |
| 429 return base::hash_combine(contents.Data(), contents.ByteLength()); |
| 430 } |
| 431 }; |
| 432 |
| 433 struct SharedArrayBufferContentsIsEqual { |
| 434 bool operator()(const SharedArrayBuffer::Contents& a, |
| 435 const SharedArrayBuffer::Contents& b) const { |
| 436 return a.Data() == b.Data() && a.ByteLength() == b.ByteLength(); |
| 437 } |
| 438 }; |
| 439 |
446 static base::LazyMutex workers_mutex_; | 440 static base::LazyMutex workers_mutex_; |
447 static bool allow_new_workers_; | 441 static bool allow_new_workers_; |
448 static i::List<Worker*> workers_; | 442 static i::List<Worker*> workers_; |
449 static i::List<SharedArrayBuffer::Contents> externalized_shared_contents_; | 443 static std::unordered_set<SharedArrayBuffer::Contents, |
| 444 SharedArrayBufferContentsHash, |
| 445 SharedArrayBufferContentsIsEqual> |
| 446 externalized_shared_contents_; |
450 | 447 |
451 static void WriteIgnitionDispatchCountersFile(v8::Isolate* isolate); | 448 static void WriteIgnitionDispatchCountersFile(v8::Isolate* isolate); |
452 static Counter* GetCounter(const char* name, bool is_histogram); | 449 static Counter* GetCounter(const char* name, bool is_histogram); |
453 static Local<String> Stringify(Isolate* isolate, Local<Value> value); | 450 static Local<String> Stringify(Isolate* isolate, Local<Value> value); |
454 static void Initialize(Isolate* isolate); | 451 static void Initialize(Isolate* isolate); |
455 static void RunShell(Isolate* isolate); | 452 static void RunShell(Isolate* isolate); |
456 static bool SetOptions(int argc, char* argv[]); | 453 static bool SetOptions(int argc, char* argv[]); |
457 static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate); | 454 static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate); |
458 static MaybeLocal<Context> CreateRealm( | 455 static MaybeLocal<Context> CreateRealm( |
459 const v8::FunctionCallbackInfo<v8::Value>& args); | 456 const v8::FunctionCallbackInfo<v8::Value>& args); |
460 static MaybeLocal<Module> FetchModuleTree(v8::Local<v8::Context> context, | 457 static MaybeLocal<Module> FetchModuleTree(v8::Local<v8::Context> context, |
461 const std::string& file_name); | 458 const std::string& file_name); |
462 }; | 459 }; |
463 | 460 |
464 | 461 |
465 } // namespace v8 | 462 } // namespace v8 |
466 | 463 |
467 | 464 |
468 #endif // V8_D8_H_ | 465 #endif // V8_D8_H_ |
OLD | NEW |