This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::Ref(const FunctionCallbackInfo<Value>& args) { | |
Worker* w; | |
ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); | |
uv_ref(reinterpret_cast<uv_handle_t*>(w->on_thread_finished_.GetHandle())); | |
} | |
void Worker::Unref(const FunctionCallbackInfo<Value>& args) { | |
Worker* w; | |
ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); | |
uv_unref(reinterpret_cast<uv_handle_t*>(w->on_thread_finished_.GetHandle())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::StartThread(const FunctionCallbackInfo<Value>& args) { | |
Worker* w; | |
ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); | |
Mutex::ScopedLock lock(w->mutex_); | |
// The object now owns the created thread and should not be garbage collected | |
// until that finishes. | |
w->ClearWeak(); | |
w->env()->add_sub_worker_context(w); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::CloneParentEnvVars(const FunctionCallbackInfo<Value>& args) { | |
Worker* w; | |
ASSIGN_OR_RETURN_UNWRAP(&w, args.This()); | |
CHECK(w->thread_joined_); // The Worker has not started yet. | |
w->env_vars_ = w->env()->env_vars()->Clone(args.GetIsolate()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::New(const FunctionCallbackInfo<Value>& args) { | |
Environment* env = Environment::GetCurrent(args); | |
CHECK(args.IsConstructCall()); | |
if (env->isolate_data()->platform() == nullptr) { | |
THROW_ERR_MISSING_PLATFORM_FOR_WORKER(env); | |
return; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::CreateEnvMessagePort(Environment* env) { | |
HandleScope handle_scope(isolate_); | |
Mutex::ScopedLock lock(mutex_); | |
// Set up the message channel for receiving messages in the child. | |
child_port_ = MessagePort::New(env, | |
env->context(), | |
std::move(child_port_data_)); | |
// MessagePort::New() may return nullptr if execution is terminated | |
// within it. | |
if (child_port_ != nullptr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Worker::Run() { | |
std::string name = "WorkerThread "; | |
name += std::to_string(thread_id_); | |
TRACE_EVENT_METADATA1( | |
"__metadata", "thread_name", "name", | |
TRACE_STR_COPY(name.c_str())); | |
CHECK_NOT_NULL(platform_); | |
std::cout << "node_worker.cc[Worker::Run()] Creating WorkerThreadData for worker with id " << thread_id_ << "\n"; | |
Debug(this, "Creating isolate for worker with id %llu", thread_id_); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This class contains data that is only relevant to the child thread itself, | |
// and only while it is running. | |
// (Eventually, the Environment instance should probably also be moved here.) | |
class WorkerThreadData { | |
public: | |
explicit WorkerThreadData(Worker* w) | |
: w_(w) { | |
CHECK_EQ(uv_loop_init(&loop_), 0); | |
std::cout<< "node_worker.cc[WorkerThreadData::WorkerThreadData(Worker* w)] Construct WorkerThreadDataInstance for thread " << w->thread_id_ << "\n"; | |
Isolate* isolate = NewIsolate( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Worker::Worker(Environment* env, | |
Local<Object> wrap, | |
const std::string& url, | |
std::shared_ptr<PerIsolateOptions> per_isolate_opts, | |
std::vector<std::string>&& exec_argv) | |
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_WORKER), | |
per_isolate_opts_(per_isolate_opts), | |
exec_argv_(exec_argv), | |
platform_(env->isolate_data()->platform()), | |
array_buffer_allocator_(ArrayBufferAllocator::Create()), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"babel": { | |
"env": { | |
"test": { | |
"plugins": [ | |
"istanbul" | |
] | |
} | |
}, | |
"presets": [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {EventEmitter} = require('events'); | |
const emitter = new EventEmitter(); | |
emitter.on('message', (message) => { | |
console.log(`Handler 1 message: ${message}`); | |
}); | |
emitter.on('message', (message) => { | |
console.log(`Handler 2 message: ${message}`); | |
}); |