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

Side by Side Diff: src/d8.h

Issue 2643723010: [d8] Use ValueSerializer for postMessage (instead of ad-hoc serializer) (Closed)
Patch Set: fix Created 3 years, 10 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
« no previous file with comments | « no previous file | src/d8.cc » ('j') | src/d8.cc » ('J')
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 // 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"
11 #include "src/base/hashmap.h" 14 #include "src/base/hashmap.h"
12 #include "src/base/platform/time.h" 15 #include "src/base/platform/time.h"
13 #include "src/list.h" 16 #include "src/list.h"
14 #include "src/utils.h" 17 #include "src/utils.h"
15 18
16 #include "src/base/once.h" 19 #include "src/base/once.h"
17 20
18 21
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 base::Thread* thread_; 139 base::Thread* thread_;
137 140
138 void ExitShell(int exit_code); 141 void ExitShell(int exit_code);
139 Local<String> ReadFile(Isolate* isolate, const char* name); 142 Local<String> ReadFile(Isolate* isolate, const char* name);
140 143
141 const char** argv_; 144 const char** argv_;
142 int begin_offset_; 145 int begin_offset_;
143 int end_offset_; 146 int end_offset_;
144 }; 147 };
145 148
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 149
161 class SerializationData { 150 class SerializationData {
162 public: 151 public:
163 SerializationData() {} 152 SerializationData() : data_(nullptr), size_(0) {}
164 ~SerializationData(); 153 ~SerializationData();
165 154
166 void WriteTag(SerializationTag tag); 155 uint8_t* data() { return data_.get(); }
167 void WriteMemory(const void* p, int length); 156 size_t size() { return size_; }
168 void WriteArrayBufferContents(const ArrayBuffer::Contents& contents); 157 const std::vector<ArrayBuffer::Contents>& array_buffer_contents() {
169 void WriteSharedArrayBufferContents( 158 return array_buffer_contents_;
170 const SharedArrayBuffer::Contents& contents); 159 }
171 160 const std::vector<SharedArrayBuffer::Contents>&
172 template <typename T> 161 shared_array_buffer_contents() {
173 void Write(const T& data) { 162 return shared_array_buffer_contents_;
174 WriteMemory(&data, sizeof(data));
175 } 163 }
176 164
177 SerializationTag ReadTag(int* offset) const; 165 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 166
191 private: 167 private:
192 i::List<uint8_t> data_; 168 struct DataDeleter {
193 i::List<ArrayBuffer::Contents> array_buffer_contents_; 169 void operator()(uint8_t* p) const { free(p); }
194 i::List<SharedArrayBuffer::Contents> shared_array_buffer_contents_; 170 };
171
172 std::unique_ptr<uint8_t, DataDeleter> data_;
173 size_t size_;
174 std::vector<ArrayBuffer::Contents> array_buffer_contents_;
175 std::vector<SharedArrayBuffer::Contents> shared_array_buffer_contents_;
176
177 private:
178 friend class Serializer;
179
180 DISALLOW_COPY_AND_ASSIGN(SerializationData);
195 }; 181 };
196 182
197 183
198 class SerializationDataQueue { 184 class SerializationDataQueue {
199 public: 185 public:
200 void Enqueue(SerializationData* data); 186 void Enqueue(std::unique_ptr<SerializationData> data);
201 bool Dequeue(SerializationData** data); 187 bool Dequeue(std::unique_ptr<SerializationData>* data);
202 bool IsEmpty(); 188 bool IsEmpty();
203 void Clear(); 189 void Clear();
204 190
205 private: 191 private:
206 base::Mutex mutex_; 192 base::Mutex mutex_;
207 i::List<SerializationData*> data_; 193 std::vector<std::unique_ptr<SerializationData>> data_;
208 }; 194 };
209 195
210 196
211 class Worker { 197 class Worker {
212 public: 198 public:
213 Worker(); 199 Worker();
214 ~Worker(); 200 ~Worker();
215 201
216 // Run the given script on this Worker. This function should only be called 202 // 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. 203 // once, and should only be called by the thread that created the Worker.
218 void StartExecuteInThread(const char* script); 204 void StartExecuteInThread(const char* script);
219 // Post a message to the worker's incoming message queue. The worker will 205 // Post a message to the worker's incoming message queue. The worker will
220 // take ownership of the SerializationData. 206 // take ownership of the SerializationData.
221 // This function should only be called by the thread that created the Worker. 207 // This function should only be called by the thread that created the Worker.
222 void PostMessage(SerializationData* data); 208 void PostMessage(std::unique_ptr<SerializationData> data);
223 // Synchronously retrieve messages from the worker's outgoing message queue. 209 // 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. 210 // 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, 211 // If there are no messages in the queue and the worker is no longer running,
226 // return nullptr. 212 // return nullptr.
227 // This function should only be called by the thread that created the Worker. 213 // This function should only be called by the thread that created the Worker.
228 SerializationData* GetMessage(); 214 std::unique_ptr<SerializationData> GetMessage();
229 // Terminate the worker's event loop. Messages from the worker that have been 215 // Terminate the worker's event loop. Messages from the worker that have been
230 // queued can still be read via GetMessage(). 216 // queued can still be read via GetMessage().
231 // This function can be called by any thread. 217 // This function can be called by any thread.
232 void Terminate(); 218 void Terminate();
233 // Terminate and join the thread. 219 // Terminate and join the thread.
234 // This function can be called by any thread. 220 // This function can be called by any thread.
235 void WaitForThread(); 221 void WaitForThread();
236 222
237 private: 223 private:
238 class WorkerThread : public base::Thread { 224 class WorkerThread : public base::Thread {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 static void ReportException(Isolate* isolate, TryCatch* try_catch); 314 static void ReportException(Isolate* isolate, TryCatch* try_catch);
329 static Local<String> ReadFile(Isolate* isolate, const char* name); 315 static Local<String> ReadFile(Isolate* isolate, const char* name);
330 static Local<Context> CreateEvaluationContext(Isolate* isolate); 316 static Local<Context> CreateEvaluationContext(Isolate* isolate);
331 static int RunMain(Isolate* isolate, int argc, char* argv[], bool last_run); 317 static int RunMain(Isolate* isolate, int argc, char* argv[], bool last_run);
332 static int Main(int argc, char* argv[]); 318 static int Main(int argc, char* argv[]);
333 static void Exit(int exit_code); 319 static void Exit(int exit_code);
334 static void OnExit(Isolate* isolate); 320 static void OnExit(Isolate* isolate);
335 static void CollectGarbage(Isolate* isolate); 321 static void CollectGarbage(Isolate* isolate);
336 static void EmptyMessageQueues(Isolate* isolate); 322 static void EmptyMessageQueues(Isolate* isolate);
337 323
338 // TODO(binji): stupid implementation for now. Is there an easy way to hash an 324 static std::unique_ptr<SerializationData> SerializeValue(
339 // object for use in base::HashMap? By pointer? 325 Isolate* isolate, Local<Value> value, Local<Value> transfer);
340 typedef i::List<Local<Object>> ObjectList; 326 static MaybeLocal<Value> DeserializeValue(
341 static bool SerializeValue(Isolate* isolate, Local<Value> value, 327 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(); 328 static void CleanupWorkers();
349 static int* LookupCounter(const char* name); 329 static int* LookupCounter(const char* name);
350 static void* CreateHistogram(const char* name, 330 static void* CreateHistogram(const char* name,
351 int min, 331 int min,
352 int max, 332 int max,
353 size_t buckets); 333 size_t buckets);
354 static void AddHistogramSample(void* histogram, int sample); 334 static void AddHistogramSample(void* histogram, int sample);
355 static void MapCounters(v8::Isolate* isolate, const char* name); 335 static void MapCounters(v8::Isolate* isolate, const char* name);
356 336
357 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args); 337 static void PerformanceNow(const v8::FunctionCallbackInfo<v8::Value>& args);
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 static Global<Function> stringify_function_; 416 static Global<Function> stringify_function_;
437 static CounterMap* counter_map_; 417 static CounterMap* counter_map_;
438 // We statically allocate a set of local counters to be used if we 418 // 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 419 // don't want to store the stats in a memory-mapped file
440 static CounterCollection local_counters_; 420 static CounterCollection local_counters_;
441 static CounterCollection* counters_; 421 static CounterCollection* counters_;
442 static base::OS::MemoryMappedFile* counters_file_; 422 static base::OS::MemoryMappedFile* counters_file_;
443 static base::LazyMutex context_mutex_; 423 static base::LazyMutex context_mutex_;
444 static const base::TimeTicks kInitialTicks; 424 static const base::TimeTicks kInitialTicks;
445 425
426 struct SharedArrayBufferContentsHash {
427 size_t operator()(const v8::SharedArrayBuffer::Contents& contents) const {
428 return std::hash<void*>()(contents.Data()) ^
429 std::hash<size_t>()(contents.ByteLength());
Camillo Bruni 2017/01/26 20:40:14 nit: use base::hash_combine from base/functional.h
binji 2017/01/27 19:33:21 Done.
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_
OLDNEW
« no previous file with comments | « no previous file | src/d8.cc » ('j') | src/d8.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698