Skip to content

Instantly share code, notes, and snippets.

View deepal's full-sized avatar

Deepal Jayasekara deepal

  • CompareTheMarket.com
  • London, United Kingdom
View GitHub Profile
@deepal
deepal / ref-unref-thread.cc
Created October 15, 2019 21:37
Ref() or Unref() a worker thread
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()));
@deepal
deepal / start-worker-run.cc
Created October 15, 2019 21:36
Trigger worker run
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);
@deepal
deepal / clone_parent_env.cc
Created October 15, 2019 21:33
Clone Parent thread's env variables
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());
}
@deepal
deepal / worker_factory.cc
Created October 15, 2019 21:33
Factory method to create worker instances
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;
}
@deepal
deepal / create_message_port.cc
Created October 15, 2019 21:32
Create message port between parent and child thread
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)
@deepal
deepal / run-worker.cc
Created October 15, 2019 21:31
Worker::Run() Run worker thread
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_);
@deepal
deepal / worker_thread_data.cc
Created October 15, 2019 21:30
WorkerThreadData class
// 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(
@deepal
deepal / worker_constructor.cc
Created October 15, 2019 21:29
Worker::Worker() Worker Constructor
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()),
{
"babel": {
"env": {
"test": {
"plugins": [
"istanbul"
]
}
},
"presets": [
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}`);
});