src: refactor thread stopping mechanism · nodejs/node@ad5d8e3 (original) (raw)

`@@ -350,13 +350,13 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {

`

350

350

`uv_unref(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_));

`

351

351

`uv_unref(reinterpret_cast<uv_handle_t*>(&idle_check_handle_));

`

352

352

``

353

``

`-

GetAsyncRequest()->Install(

`

``

353

`+

thread_stopper()->Install(

`

354

354

`this, static_cast<void*>(this), [](uv_async_t* handle) {

`

355

355

` Environment* env = static_cast<Environment*>(handle->data);

`

356

356

`uv_stop(env->event_loop());

`

357

357

` });

`

358

``

`-

GetAsyncRequest()->SetStopped(false);

`

359

``

`-

uv_unref(reinterpret_cast<uv_handle_t*>(GetAsyncRequest()->GetHandle()));

`

``

358

`+

thread_stopper()->set_stopped(false);

`

``

359

`+

uv_unref(reinterpret_cast<uv_handle_t*>(thread_stopper()->GetHandle()));

`

360

360

``

361

361

`// Register clean-up cb to be called to clean up the handles

`

362

362

`// when the environment is freed, note that they are not cleaned in

`

`@@ -375,7 +375,7 @@ void Environment::InitializeLibuv(bool start_profiler_idle_notifier) {

`

375

375

``

376

376

`void Environment::ExitEnv() {

`

377

377

`set_can_call_into_js(false);

`

378

``

`-

GetAsyncRequest()->Stop();

`

``

378

`+

thread_stopper()->Stop();

`

379

379

` isolate_->TerminateExecution();

`

380

380

`}

`

381

381

``

`@@ -543,7 +543,7 @@ void Environment::RunCleanup() {

`

543

543

` started_cleanup_ = true;

`

544

544

` TraceEventScope trace_scope(TRACING_CATEGORY_NODE1(environment),

`

545

545

`"RunCleanup", this);

`

546

``

`-

GetAsyncRequest()->Uninstall();

`

``

546

`+

thread_stopper()->Uninstall();

`

547

547

`CleanupHandles();

`

548

548

``

549

549

`while (!cleanup_hooks_.empty()) {

`

`@@ -977,49 +977,34 @@ char* Environment::Reallocate(char* data, size_t old_size, size_t size) {

`

977

977

`}

`

978

978

``

979

979

`void AsyncRequest::Install(Environment* env, void* data, uv_async_cb target) {

`

980

``

`-

Mutex::ScopedLock lock(mutex_);

`

``

980

`+

CHECK_NULL(async_);

`

981

981

` env_ = env;

`

982

982

` async_ = new uv_async_t;

`

983

983

` async_->data = data;

`

984

984

`CHECK_EQ(uv_async_init(env_->event_loop(), async_, target), 0);

`

985

985

`}

`

986

986

``

987

987

`void AsyncRequest::Uninstall() {

`

988

``

`-

Mutex::ScopedLock lock(mutex_);

`

989

988

`if (async_ != nullptr) {

`

990

989

` env_->CloseHandle(async_, [](uv_async_t* async) { delete async; });

`

991

990

` async_ = nullptr;

`

992

991

` }

`

993

992

`}

`

994

993

``

995

994

`void AsyncRequest::Stop() {

`

996

``

`-

Mutex::ScopedLock lock(mutex_);

`

997

``

`-

stop_ = true;

`

``

995

`+

set_stopped(true);

`

998

996

`if (async_ != nullptr) uv_async_send(async_);

`

999

997

`}

`

1000

998

``

1001

``

`-

void AsyncRequest::SetStopped(bool flag) {

`

1002

``

`-

Mutex::ScopedLock lock(mutex_);

`

1003

``

`-

stop_ = flag;

`

1004

``

`-

}

`

1005

``

-

1006

``

`-

bool AsyncRequest::IsStopped() const {

`

1007

``

`-

Mutex::ScopedLock lock(mutex_);

`

1008

``

`-

return stop_;

`

1009

``

`-

}

`

1010

``

-

1011

999

`uv_async_t* AsyncRequest::GetHandle() {

`

1012

``

`-

Mutex::ScopedLock lock(mutex_);

`

1013

1000

`return async_;

`

1014

1001

`}

`

1015

1002

``

1016

1003

`void AsyncRequest::MemoryInfo(MemoryTracker* tracker) const {

`

1017

``

`-

Mutex::ScopedLock lock(mutex_);

`

1018

1004

`if (async_ != nullptr) tracker->TrackField("async_request", *async_);

`

1019

1005

`}

`

1020

1006

``

1021

1007

`AsyncRequest::~AsyncRequest() {

`

1022

``

`-

Mutex::ScopedLock lock(mutex_);

`

1023

1008

`CHECK_NULL(async_);

`

1024

1009

`}

`

1025

1010

``