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