зеркало из https://github.com/electron/electron.git
refactor: Allow PostDelayedTasks() to be called from another thread
refactor: Add UvTaskRunner::Shutdown()
This commit is contained in:
Родитель
3dd7e46291
Коммит
6a9e258b26
|
@ -289,6 +289,8 @@ int NodeMain(int argc, char* argv[]) {
|
|||
// is stopping, or the user hooks process.emit('exit').
|
||||
exit_code = node::SpinEventLoop(env).FromMaybe(1);
|
||||
|
||||
uv_task_runner->Shutdown();
|
||||
|
||||
node::ResetStdio();
|
||||
|
||||
node::Stop(env, node::StopFlags::kDoNotTerminateIsolate);
|
||||
|
|
|
@ -11,25 +11,54 @@
|
|||
|
||||
namespace electron {
|
||||
|
||||
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
|
||||
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {
|
||||
uv_async_init(loop_, post_tasks_.get(), [](uv_async_t* handle) {
|
||||
reinterpret_cast<UvTaskRunner*>(handle->data)->PostTasks();
|
||||
});
|
||||
post_tasks_->data = this;
|
||||
uv_unref(reinterpret_cast<uv_handle_t*>(post_tasks_.get()));
|
||||
}
|
||||
|
||||
UvTaskRunner::~UvTaskRunner() = default;
|
||||
|
||||
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
|
||||
base::OnceClosure task,
|
||||
void UvTaskRunner::Shutdown() {
|
||||
post_tasks_.reset();
|
||||
}
|
||||
|
||||
bool UvTaskRunner::PostDelayedTask(const base::Location& /*from_here*/,
|
||||
base::OnceClosure closure,
|
||||
base::TimeDelta delay) {
|
||||
if (!post_tasks_.get())
|
||||
return false;
|
||||
|
||||
unposted_tasks_lock_.Acquire();
|
||||
unposted_tasks_.emplace_front(std::move(closure), delay);
|
||||
unposted_tasks_lock_.Release();
|
||||
|
||||
uv_async_send(post_tasks_.get());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void UvTaskRunner::PostTasks() {
|
||||
auto on_timeout = [](uv_timer_t* timer) {
|
||||
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
|
||||
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->posted_tasks_;
|
||||
if (auto iter = tasks.find(timer); iter != tasks.end())
|
||||
std::move(tasks.extract(iter).mapped()).Run();
|
||||
};
|
||||
|
||||
auto timer = UvHandle<uv_timer_t>{};
|
||||
timer->data = this;
|
||||
uv_timer_init(loop_, timer.get());
|
||||
uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
|
||||
tasks_.insert_or_assign(std::move(timer), std::move(task));
|
||||
return true;
|
||||
unposted_tasks_lock_.Acquire();
|
||||
decltype(unposted_tasks_) tasks;
|
||||
std::swap(tasks, unposted_tasks_);
|
||||
unposted_tasks_lock_.Release();
|
||||
|
||||
for (auto& [closure, delay] : tasks) {
|
||||
UvHandle<uv_timer_t> timer;
|
||||
uv_timer_init(loop_, timer.get());
|
||||
timer->data = this;
|
||||
uv_timer_start(timer.get(), on_timeout, delay.InMilliseconds(), 0);
|
||||
posted_tasks_.try_emplace(std::move(timer), std::move(closure));
|
||||
}
|
||||
}
|
||||
|
||||
bool UvTaskRunner::RunsTasksInCurrentSequence() const {
|
||||
|
@ -37,9 +66,9 @@ bool UvTaskRunner::RunsTasksInCurrentSequence() const {
|
|||
}
|
||||
|
||||
bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
|
||||
base::OnceClosure task,
|
||||
base::OnceClosure closure,
|
||||
base::TimeDelta delay) {
|
||||
return PostDelayedTask(from_here, std::move(task), delay);
|
||||
return PostDelayedTask(from_here, std::move(closure), delay);
|
||||
}
|
||||
|
||||
} // namespace electron
|
||||
|
|
|
@ -5,9 +5,11 @@
|
|||
#ifndef ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
|
||||
#define ELECTRON_SHELL_APP_UV_TASK_RUNNER_H_
|
||||
|
||||
#include <forward_list>
|
||||
#include <map>
|
||||
|
||||
#include "base/memory/raw_ptr.h"
|
||||
#include "base/synchronization/lock.h"
|
||||
#include "base/task/single_thread_task_runner.h"
|
||||
#include "shell/common/node_bindings.h"
|
||||
|
||||
|
@ -23,6 +25,8 @@ class UvTaskRunner : public base::SingleThreadTaskRunner {
|
|||
public:
|
||||
explicit UvTaskRunner(uv_loop_t* loop);
|
||||
|
||||
void Shutdown();
|
||||
|
||||
// disable copy
|
||||
UvTaskRunner(const UvTaskRunner&) = delete;
|
||||
UvTaskRunner& operator=(const UvTaskRunner&) = delete;
|
||||
|
@ -39,9 +43,18 @@ class UvTaskRunner : public base::SingleThreadTaskRunner {
|
|||
private:
|
||||
~UvTaskRunner() override;
|
||||
|
||||
raw_ptr<uv_loop_t> loop_;
|
||||
void PostTasks();
|
||||
|
||||
std::map<UvHandle<uv_timer_t>, base::OnceClosure, UvHandleCompare> tasks_;
|
||||
const raw_ptr<uv_loop_t> loop_;
|
||||
|
||||
UvHandle<uv_async_t> post_tasks_;
|
||||
|
||||
base::Lock unposted_tasks_lock_;
|
||||
using UnpostedTask = std::pair<base::OnceClosure, base::TimeDelta>;
|
||||
std::forward_list<UnpostedTask> unposted_tasks_;
|
||||
|
||||
std::map<UvHandle<uv_timer_t>, base::OnceClosure, UvHandleCompare>
|
||||
posted_tasks_;
|
||||
};
|
||||
|
||||
} // namespace electron
|
||||
|
|
Загрузка…
Ссылка в новой задаче