Commit 3d98b956 authored by binji's avatar binji Committed by Commit bot

Add d8 API for spawning function on a new thread.

This API closely matches the Worker API. The differences:

1) The argument to the Worker constructor is a function to run, not a script.
2) Receiving a message from a worker is a synchronous API (as there is no event
loop).

The serialization done here is not robust as the real DOM implementation. For
example, recursive data structures or otherwise duplicated objects are not
allowed.

BUG=none
R=jochen@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/1185643004

Cr-Commit-Position: refs/heads/master@{#29126}
parent bf92b53f
This diff is collapsed.
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#ifndef V8_SHARED #ifndef V8_SHARED
#include "src/allocation.h" #include "src/allocation.h"
#include "src/hashmap.h" #include "src/hashmap.h"
#include "src/list.h"
#include "src/smart-pointers.h" #include "src/smart-pointers.h"
#include "src/unbound-queue.h"
#include "src/v8.h" #include "src/v8.h"
#else #else
#include "include/v8.h" #include "include/v8.h"
...@@ -167,6 +169,99 @@ class SourceGroup { ...@@ -167,6 +169,99 @@ class SourceGroup {
int end_offset_; int end_offset_;
}; };
#ifndef V8_SHARED
enum SerializationTag {
kSerializationTagUndefined,
kSerializationTagNull,
kSerializationTagTrue,
kSerializationTagFalse,
kSerializationTagNumber,
kSerializationTagString,
kSerializationTagArray,
kSerializationTagObject,
kSerializationTagArrayBuffer,
kSerializationTagTransferredArrayBuffer,
kSerializationTagTransferredSharedArrayBuffer,
};
class SerializationData {
public:
SerializationData() {}
~SerializationData();
void WriteTag(SerializationTag tag);
void WriteMemory(const void* p, int length);
void WriteArrayBufferContents(const ArrayBuffer::Contents& contents);
void WriteSharedArrayBufferContents(
const SharedArrayBuffer::Contents& contents);
template <typename T>
void Write(const T& data) {
WriteMemory(&data, sizeof(data));
}
SerializationTag ReadTag(int* offset) const;
void ReadMemory(void* p, int length, int* offset) const;
void ReadArrayBufferContents(ArrayBuffer::Contents* contents,
int* offset) const;
void ReadSharedArrayBufferContents(SharedArrayBuffer::Contents* contents,
int* offset) const;
template <typename T>
T Read(int* offset) const {
T value;
ReadMemory(&value, sizeof(value), offset);
return value;
}
private:
i::List<uint8_t> data;
i::List<ArrayBuffer::Contents> array_buffer_contents;
i::List<SharedArrayBuffer::Contents> shared_array_buffer_contents;
};
class Worker {
public:
Worker();
~Worker();
void StartExecuteInThread(Isolate* isolate, const char* function_string);
void PostMessage(SerializationData* data);
SerializationData* GetMessage();
void Terminate();
private:
class WorkerThread : public base::Thread {
public:
explicit WorkerThread(Worker* worker)
: base::Thread(base::Thread::Options("WorkerThread")),
worker_(worker) {}
virtual void Run() { worker_->ExecuteInThread(); }
private:
Worker* worker_;
};
typedef i::UnboundQueue<SerializationData*> SerializationDataQueue;
void ExecuteInThread();
static void ClearQueue(SerializationDataQueue* queue);
void Cleanup();
static void PostMessageOut(const v8::FunctionCallbackInfo<v8::Value>& args);
base::Mutex mutex_;
base::Semaphore in_semaphore_;
base::Semaphore out_semaphore_;
SerializationDataQueue in_queue_;
SerializationDataQueue out_queue_;
base::Thread* thread_;
char* script_;
};
#endif // !V8_SHARED
class ShellOptions { class ShellOptions {
public: public:
...@@ -246,6 +341,17 @@ class Shell : public i::AllStatic { ...@@ -246,6 +341,17 @@ class Shell : public i::AllStatic {
static void CollectGarbage(Isolate* isolate); static void CollectGarbage(Isolate* isolate);
#ifndef V8_SHARED #ifndef V8_SHARED
// TODO(binji): stupid implementation for now. Is there an easy way to hash an
// object for use in i::HashMap? By pointer?
typedef i::List<Handle<Object>> ObjectList;
static bool SerializeValue(Isolate* isolate, Handle<Value> value,
const ObjectList& to_transfer,
ObjectList* seen_objects,
SerializationData* out_data);
static MaybeLocal<Value> DeserializeValue(Isolate* isolate,
const SerializationData& data,
int* offset);
static void CleanupWorkers();
static Handle<Array> GetCompletions(Isolate* isolate, static Handle<Array> GetCompletions(Isolate* isolate,
Handle<String> text, Handle<String> text,
Handle<String> full); Handle<String> full);
...@@ -289,6 +395,11 @@ class Shell : public i::AllStatic { ...@@ -289,6 +395,11 @@ class Shell : public i::AllStatic {
args.GetReturnValue().Set(ReadFromStdin(args.GetIsolate())); args.GetReturnValue().Set(ReadFromStdin(args.GetIsolate()));
} }
static void Load(const v8::FunctionCallbackInfo<v8::Value>& args); static void Load(const v8::FunctionCallbackInfo<v8::Value>& args);
static void WorkerNew(const v8::FunctionCallbackInfo<v8::Value>& args);
static void WorkerPostMessage(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void WorkerGetMessage(const v8::FunctionCallbackInfo<v8::Value>& args);
static void WorkerTerminate(const v8::FunctionCallbackInfo<v8::Value>& args);
// The OS object on the global object contains methods for performing // The OS object on the global object contains methods for performing
// operating system calls: // operating system calls:
// //
...@@ -328,6 +439,7 @@ class Shell : public i::AllStatic { ...@@ -328,6 +439,7 @@ class Shell : public i::AllStatic {
static const char* kPrompt; static const char* kPrompt;
static ShellOptions options; static ShellOptions options;
static ArrayBuffer::Allocator* array_buffer_allocator;
private: private:
static Persistent<Context> evaluation_context_; static Persistent<Context> evaluation_context_;
...@@ -341,6 +453,8 @@ class Shell : public i::AllStatic { ...@@ -341,6 +453,8 @@ class Shell : public i::AllStatic {
static base::OS::MemoryMappedFile* counters_file_; static base::OS::MemoryMappedFile* counters_file_;
static base::Mutex context_mutex_; static base::Mutex context_mutex_;
static const base::TimeTicks kInitialTicks; static const base::TimeTicks kInitialTicks;
static Worker worker_;
static i::List<SharedArrayBuffer::Contents> externalized_shared_contents_;
static Counter* GetCounter(const char* name, bool is_histogram); static Counter* GetCounter(const char* name, bool is_histogram);
static void InstallUtilityScript(Isolate* isolate); static void InstallUtilityScript(Isolate* isolate);
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Test the Worker API of d8. This test only makes sense with d8. A Worker
// spawns a new OS thread and isolate, and runs it concurrently with the
// current running thread.
function f() {
postMessage("Starting worker");
// Set a global variable; should not be visible outside of the worker's
// context.
foo = 100;
var c = 0;
onmessage = function(m) {
switch (c++) {
case 0:
if (m !== undefined) throw new Error("undefined");
break;
case 1:
if (m !== null) throw new Error("null");
break;
case 2:
if (m !== true) throw new Error("true");
break;
case 3:
if (m !== false) throw new Error("false");
break;
case 4:
if (m !== 100) throw new Error("Number");
break;
case 5:
if (m !== "hi") throw new Error("String");
break;
case 6:
if (JSON.stringify(m) !== '[4,true,"bye"]') throw new Error("Array");
break;
case 7:
if (JSON.stringify(m) !== '{"a":1,"b":2.5,"c":"three"}')
throw new Error("Object");
break;
case 8:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 16)
throw new Error("ArrayBuffer clone byteLength");
for (var i = 0; i < 4; ++i)
if (t[i] !== i)
throw new Error("ArrayBuffer clone value " + i);
break;
case 9:
var ab = m;
var t = new Uint32Array(ab);
if (ab.byteLength !== 32)
throw new Error("ArrayBuffer transfer byteLength");
for (var i = 0; i < 8; ++i)
if (t[i] !== i)
throw new Error("ArrayBuffer transfer value " + i);
break;
}
if (c == 10) {
postMessage("DONE");
}
}
}
function createArrayBuffer(byteLength) {
var ab = new ArrayBuffer(byteLength);
var t = new Uint32Array(ab);
for (var i = 0; i < byteLength / 4; ++i)
t[i] = i;
return ab;
}
var w = new Worker(f);
assertEquals("Starting worker", w.getMessage());
// Currently can only create one worker at a time.
assertThrows(function() { new Worker(f); });
w.postMessage(undefined);
w.postMessage(null);
w.postMessage(true);
w.postMessage(false);
w.postMessage(100);
w.postMessage("hi");
w.postMessage([4, true, "bye"]);
w.postMessage({a: 1, b: 2.5, c: "three"});
// Clone ArrayBuffer
var ab1 = createArrayBuffer(16);
w.postMessage(ab1);
assertEquals(16, ab1.byteLength); // ArrayBuffer should not be neutered.
// Transfer ArrayBuffer
var ab2 = createArrayBuffer(32);
w.postMessage(ab2, [ab2]);
assertEquals(0, ab2.byteLength); // ArrayBuffer should be neutered.
assertEquals("undefined", typeof foo);
// Read a message from the worker.
assertEquals("DONE", w.getMessage());
w.terminate();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment