chore: remove redundant code in node integration (#33500)
This commit is contained in:
parent
df3cfb663c
commit
c119b1ebef
11 changed files with 32 additions and 108 deletions
|
@ -402,8 +402,8 @@ void ElectronBrowserMainParts::ToolkitInitialized() {
|
||||||
int ElectronBrowserMainParts::PreMainMessageLoopRun() {
|
int ElectronBrowserMainParts::PreMainMessageLoopRun() {
|
||||||
// Run user's main script before most things get initialized, so we can have
|
// Run user's main script before most things get initialized, so we can have
|
||||||
// a chance to setup everything.
|
// a chance to setup everything.
|
||||||
node_bindings_->PrepareMessageLoop();
|
node_bindings_->PrepareEmbedThread();
|
||||||
node_bindings_->RunMessageLoop();
|
node_bindings_->StartPolling();
|
||||||
|
|
||||||
// url::Add*Scheme are not threadsafe, this helps prevent data races.
|
// url::Add*Scheme are not threadsafe, this helps prevent data races.
|
||||||
url::LockSchemeRegistries();
|
url::LockSchemeRegistries();
|
||||||
|
|
|
@ -584,7 +584,16 @@ void NodeBindings::LoadEnvironment(node::Environment* env) {
|
||||||
gin_helper::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
gin_helper::EmitEvent(env->isolate(), env->process_object(), "loaded");
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeBindings::PrepareMessageLoop() {
|
void NodeBindings::PrepareEmbedThread() {
|
||||||
|
// IOCP does not change for the process until the loop is recreated,
|
||||||
|
// we ensure that there is only a single polling thread satisfying
|
||||||
|
// the concurrency limit set from CreateIoCompletionPort call by
|
||||||
|
// uv_loop_init for the lifetime of this process.
|
||||||
|
// More background can be found at:
|
||||||
|
// https://github.com/microsoft/vscode/issues/142786#issuecomment-1061673400
|
||||||
|
if (initialized_)
|
||||||
|
return;
|
||||||
|
|
||||||
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
// Add dummy handle for libuv, otherwise libuv would quit when there is
|
||||||
// nothing to do.
|
// nothing to do.
|
||||||
uv_async_init(uv_loop_, dummy_uv_handle_.get(), nullptr);
|
uv_async_init(uv_loop_, dummy_uv_handle_.get(), nullptr);
|
||||||
|
@ -594,7 +603,15 @@ void NodeBindings::PrepareMessageLoop() {
|
||||||
uv_thread_create(&embed_thread_, EmbedThreadRunner, this);
|
uv_thread_create(&embed_thread_, EmbedThreadRunner, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeBindings::RunMessageLoop() {
|
void NodeBindings::StartPolling() {
|
||||||
|
// Avoid calling UvRunOnce if the loop is already active,
|
||||||
|
// otherwise it can lead to situations were the number of active
|
||||||
|
// threads processing on IOCP is greater than the concurrency limit.
|
||||||
|
if (initialized_)
|
||||||
|
return;
|
||||||
|
|
||||||
|
initialized_ = true;
|
||||||
|
|
||||||
// The MessageLoop should have been created, remember the one in main thread.
|
// The MessageLoop should have been created, remember the one in main thread.
|
||||||
task_runner_ = base::ThreadTaskRunnerHandle::Get();
|
task_runner_ = base::ThreadTaskRunnerHandle::Get();
|
||||||
|
|
||||||
|
|
|
@ -92,11 +92,11 @@ class NodeBindings {
|
||||||
// Load node.js in the environment.
|
// Load node.js in the environment.
|
||||||
void LoadEnvironment(node::Environment* env);
|
void LoadEnvironment(node::Environment* env);
|
||||||
|
|
||||||
// Prepare for message loop integration.
|
// Prepare embed thread for message loop integration.
|
||||||
virtual void PrepareMessageLoop();
|
void PrepareEmbedThread();
|
||||||
|
|
||||||
// Do message loop integration.
|
// Notify embed thread to start polling after environment is loaded.
|
||||||
virtual void RunMessageLoop();
|
void StartPolling();
|
||||||
|
|
||||||
// Gets/sets the per isolate data.
|
// Gets/sets the per isolate data.
|
||||||
void set_isolate_data(node::IsolateData* isolate_data) {
|
void set_isolate_data(node::IsolateData* isolate_data) {
|
||||||
|
@ -144,6 +144,9 @@ class NodeBindings {
|
||||||
// Thread to poll uv events.
|
// Thread to poll uv events.
|
||||||
static void EmbedThreadRunner(void* arg);
|
static void EmbedThreadRunner(void* arg);
|
||||||
|
|
||||||
|
// Indicates whether polling thread has been created.
|
||||||
|
bool initialized_ = false;
|
||||||
|
|
||||||
// Whether the libuv loop has ended.
|
// Whether the libuv loop has ended.
|
||||||
bool embed_closed_ = false;
|
bool embed_closed_ = false;
|
||||||
|
|
||||||
|
|
|
@ -17,30 +17,6 @@ NodeBindingsLinux::NodeBindingsLinux(BrowserEnvironment browser_env)
|
||||||
epoll_ctl(epoll_, EPOLL_CTL_ADD, backend_fd, &ev);
|
epoll_ctl(epoll_, EPOLL_CTL_ADD, backend_fd, &ev);
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeBindingsLinux::~NodeBindingsLinux() = default;
|
|
||||||
|
|
||||||
void NodeBindingsLinux::PrepareMessageLoop() {
|
|
||||||
int handle = uv_backend_fd(uv_loop_);
|
|
||||||
|
|
||||||
// If the backend fd hasn't changed, don't proceed.
|
|
||||||
if (handle == handle_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
NodeBindings::PrepareMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsLinux::RunMessageLoop() {
|
|
||||||
int handle = uv_backend_fd(uv_loop_);
|
|
||||||
|
|
||||||
// If the backend fd hasn't changed, don't proceed.
|
|
||||||
if (handle == handle_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
handle_ = handle;
|
|
||||||
|
|
||||||
NodeBindings::RunMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsLinux::PollEvents() {
|
void NodeBindingsLinux::PollEvents() {
|
||||||
int timeout = uv_backend_timeout(uv_loop_);
|
int timeout = uv_backend_timeout(uv_loop_);
|
||||||
|
|
||||||
|
|
|
@ -13,19 +13,12 @@ namespace electron {
|
||||||
class NodeBindingsLinux : public NodeBindings {
|
class NodeBindingsLinux : public NodeBindings {
|
||||||
public:
|
public:
|
||||||
explicit NodeBindingsLinux(BrowserEnvironment browser_env);
|
explicit NodeBindingsLinux(BrowserEnvironment browser_env);
|
||||||
~NodeBindingsLinux() override;
|
|
||||||
|
|
||||||
void PrepareMessageLoop() override;
|
|
||||||
void RunMessageLoop() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void PollEvents() override;
|
void PollEvents() override;
|
||||||
|
|
||||||
// Epoll to poll for uv's backend fd.
|
// Epoll to poll for uv's backend fd.
|
||||||
int epoll_;
|
int epoll_;
|
||||||
|
|
||||||
// uv's backend fd.
|
|
||||||
int handle_ = -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -17,30 +17,6 @@ namespace electron {
|
||||||
NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)
|
NodeBindingsMac::NodeBindingsMac(BrowserEnvironment browser_env)
|
||||||
: NodeBindings(browser_env) {}
|
: NodeBindings(browser_env) {}
|
||||||
|
|
||||||
NodeBindingsMac::~NodeBindingsMac() = default;
|
|
||||||
|
|
||||||
void NodeBindingsMac::PrepareMessageLoop() {
|
|
||||||
int handle = uv_backend_fd(uv_loop_);
|
|
||||||
|
|
||||||
// If the backend fd hasn't changed, don't proceed.
|
|
||||||
if (handle == handle_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
NodeBindings::PrepareMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::RunMessageLoop() {
|
|
||||||
int handle = uv_backend_fd(uv_loop_);
|
|
||||||
|
|
||||||
// If the backend fd hasn't changed, don't proceed.
|
|
||||||
if (handle == handle_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
handle_ = handle;
|
|
||||||
|
|
||||||
NodeBindings::RunMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsMac::PollEvents() {
|
void NodeBindingsMac::PollEvents() {
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int timeout = uv_backend_timeout(uv_loop_);
|
int timeout = uv_backend_timeout(uv_loop_);
|
||||||
|
|
|
@ -13,16 +13,9 @@ namespace electron {
|
||||||
class NodeBindingsMac : public NodeBindings {
|
class NodeBindingsMac : public NodeBindings {
|
||||||
public:
|
public:
|
||||||
explicit NodeBindingsMac(BrowserEnvironment browser_env);
|
explicit NodeBindingsMac(BrowserEnvironment browser_env);
|
||||||
~NodeBindingsMac() override;
|
|
||||||
|
|
||||||
void PrepareMessageLoop() override;
|
|
||||||
void RunMessageLoop() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void PollEvents() override;
|
void PollEvents() override;
|
||||||
|
|
||||||
// uv's backend fd.
|
|
||||||
int handle_ = -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -27,31 +27,6 @@ NodeBindingsWin::NodeBindingsWin(BrowserEnvironment browser_env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeBindingsWin::~NodeBindingsWin() = default;
|
|
||||||
|
|
||||||
void NodeBindingsWin::PrepareMessageLoop() {
|
|
||||||
// IOCP does not change for the process until the loop is recreated,
|
|
||||||
// we ensure that there is only a single polling thread satisfying
|
|
||||||
// the concurrency limit set from CreateIoCompletionPort call by
|
|
||||||
// uv_loop_init for the lifetime of this process.
|
|
||||||
if (initialized_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
NodeBindings::PrepareMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsWin::RunMessageLoop() {
|
|
||||||
// Avoid calling UvRunOnce if the loop is already active,
|
|
||||||
// otherwise it can lead to situations were the number of active
|
|
||||||
// threads processing on IOCP is greater than the concurrency limit.
|
|
||||||
if (initialized_)
|
|
||||||
return;
|
|
||||||
|
|
||||||
initialized_ = true;
|
|
||||||
|
|
||||||
NodeBindings::RunMessageLoop();
|
|
||||||
}
|
|
||||||
|
|
||||||
void NodeBindingsWin::PollEvents() {
|
void NodeBindingsWin::PollEvents() {
|
||||||
// If there are other kinds of events pending, uv_backend_timeout will
|
// If there are other kinds of events pending, uv_backend_timeout will
|
||||||
// instruct us not to wait.
|
// instruct us not to wait.
|
||||||
|
|
|
@ -13,16 +13,9 @@ namespace electron {
|
||||||
class NodeBindingsWin : public NodeBindings {
|
class NodeBindingsWin : public NodeBindings {
|
||||||
public:
|
public:
|
||||||
explicit NodeBindingsWin(BrowserEnvironment browser_env);
|
explicit NodeBindingsWin(BrowserEnvironment browser_env);
|
||||||
~NodeBindingsWin() override;
|
|
||||||
|
|
||||||
void PrepareMessageLoop() override;
|
|
||||||
void RunMessageLoop() override;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void PollEvents() override;
|
void PollEvents() override;
|
||||||
|
|
||||||
// Indicates whether polling thread has been created.
|
|
||||||
bool initialized_ = false;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace electron
|
} // namespace electron
|
||||||
|
|
|
@ -93,9 +93,7 @@ void ElectronRendererClient::DidCreateScriptContext(
|
||||||
if (!node_integration_initialized_) {
|
if (!node_integration_initialized_) {
|
||||||
node_integration_initialized_ = true;
|
node_integration_initialized_ = true;
|
||||||
node_bindings_->Initialize();
|
node_bindings_->Initialize();
|
||||||
node_bindings_->PrepareMessageLoop();
|
node_bindings_->PrepareEmbedThread();
|
||||||
} else {
|
|
||||||
node_bindings_->PrepareMessageLoop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup node tracing controller.
|
// Setup node tracing controller.
|
||||||
|
@ -131,7 +129,7 @@ void ElectronRendererClient::DidCreateScriptContext(
|
||||||
node_bindings_->set_uv_env(env);
|
node_bindings_->set_uv_env(env);
|
||||||
|
|
||||||
// Give the node loop a run to make sure everything is ready.
|
// Give the node loop a run to make sure everything is ready.
|
||||||
node_bindings_->RunMessageLoop();
|
node_bindings_->StartPolling();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
|
||||||
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
|
isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
|
||||||
|
|
||||||
// Start the embed thread.
|
// Start the embed thread.
|
||||||
node_bindings_->PrepareMessageLoop();
|
node_bindings_->PrepareEmbedThread();
|
||||||
|
|
||||||
// Setup node tracing controller.
|
// Setup node tracing controller.
|
||||||
if (!node::tracing::TraceEventHelper::GetAgent())
|
if (!node::tracing::TraceEventHelper::GetAgent())
|
||||||
|
@ -78,7 +78,7 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
|
||||||
node_bindings_->set_uv_env(env);
|
node_bindings_->set_uv_env(env);
|
||||||
|
|
||||||
// Give the node loop a run to make sure everything is ready.
|
// Give the node loop a run to make sure everything is ready.
|
||||||
node_bindings_->RunMessageLoop();
|
node_bindings_->StartPolling();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebWorkerObserver::ContextWillDestroy(v8::Local<v8::Context> context) {
|
void WebWorkerObserver::ContextWillDestroy(v8::Local<v8::Context> context) {
|
||||||
|
|
Loading…
Reference in a new issue