Created
October 15, 2019 21:30
-
-
Save deepal/16d247c9144249f8727724c7a95f42f3 to your computer and use it in GitHub Desktop.
WorkerThreadData class
This file contains hidden or 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( | |
w->array_buffer_allocator_.get(), | |
&loop_, | |
w->platform_); | |
CHECK_NOT_NULL(isolate); | |
{ | |
Locker locker(isolate); | |
Isolate::Scope isolate_scope(isolate); | |
isolate->SetStackLimit(w_->stack_base_); | |
HandleScope handle_scope(isolate); | |
isolate_data_.reset(CreateIsolateData(isolate, | |
&loop_, | |
w_->platform_, | |
w->array_buffer_allocator_.get())); | |
CHECK(isolate_data_); | |
if (w_->per_isolate_opts_) | |
isolate_data_->set_options(std::move(w_->per_isolate_opts_)); | |
} | |
Mutex::ScopedLock lock(w_->mutex_); | |
std::cout << "node_worker.cc[WorkerThreadData::WorkerThreadData(Worker* w)] Assigned new isolate to the worker thread " << w_->thread_id_ << "\n"; | |
w_->isolate_ = isolate; | |
} | |
~WorkerThreadData() { | |
Debug(w_, "Worker %llu dispose isolate", w_->thread_id_); | |
std::cout<<"node_worker.cc[WorkerThreadData::~WorkerThreadData()] Destruct WorkerThreadDataInstance\n"; | |
Isolate* isolate; | |
{ | |
Mutex::ScopedLock lock(w_->mutex_); | |
isolate = w_->isolate_; | |
w_->isolate_ = nullptr; | |
} | |
w_->platform_->CancelPendingDelayedTasks(isolate); | |
bool platform_finished = false; | |
isolate_data_.reset(); | |
w_->platform_->AddIsolateFinishedCallback(isolate, [](void* data) { | |
*static_cast<bool*>(data) = true; | |
}, &platform_finished); | |
w_->platform_->UnregisterIsolate(isolate); | |
std::cout<<"node_worker.cc[WorkerThreadData::~WorkerThreadData()] Disposing isolate for worker " << w_->thread_id_ << "\n"; | |
isolate->Dispose(); | |
// Wait until the platform has cleaned up all relevant resources. | |
// What's happening here????????????? | |
while (!platform_finished) | |
uv_run(&loop_, UV_RUN_ONCE); | |
CheckedUvLoopClose(&loop_); | |
} | |
private: | |
Worker* const w_; | |
uv_loop_t loop_; | |
DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_; | |
friend class Worker; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment