Skip to content

Instantly share code, notes, and snippets.

@deepal
Created October 15, 2019 21:29
Show Gist options
  • Save deepal/e0758c7b40a9c6f96dd912dd37a5ac7b to your computer and use it in GitHub Desktop.
Save deepal/e0758c7b40a9c6f96dd912dd37a5ac7b to your computer and use it in GitHub Desktop.
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()),
start_profiler_idle_notifier_(env->profiler_idle_notifier_started()),
thread_id_(Environment::AllocateThreadId()), // Create a new thread ID everytime a new worker is constructed
env_vars_(env->env_vars()) {
Debug(this, "Creating new worker instance with thread id %llu", thread_id_);
// Set up everything that needs to be set up in the parent environment.
parent_port_ = MessagePort::New(env, env->context());
if (parent_port_ == nullptr) {
// This can happen e.g. because execution is terminating.
return;
}
child_port_data_ = std::make_unique<MessagePortData>(nullptr);
MessagePort::Entangle(parent_port_, child_port_data_.get());
object()->Set(env->context(),
env->message_port_string(),
parent_port_->object()).Check();
object()->Set(env->context(),
env->thread_id_string(),
Number::New(env->isolate(), static_cast<double>(thread_id_)))
.Check();
#if NODE_USE_V8_PLATFORM && HAVE_INSPECTOR
inspector_parent_handle_ =
env->inspector_agent()->GetParentHandle(thread_id_, url);
#endif
argv_ = std::vector<std::string>{env->argv()[0]};
// Mark this Worker object as weak until we actually start the thread.
MakeWeak();
std::cout << "node_workers.cc[Worker::Worker] Preparation for worker " << thread_id_ << " finished\n";
Debug(this, "Preparation for worker %llu finished", thread_id_);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment