2015-09-03 02:28:50 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2017-06-16 20:39:08 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2021-07-02 00:51:37 +00:00
|
|
|
#include "base/location.h"
|
2022-02-25 18:17:35 +00:00
|
|
|
#include "base/time/time.h"
|
2021-07-02 00:51:37 +00:00
|
|
|
#include "shell/app/uv_task_runner.h"
|
2015-09-03 02:28:50 +00:00
|
|
|
|
|
|
|
namespace electron {
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_(loop) {}
|
2015-09-03 02:28:50 +00:00
|
|
|
|
|
|
|
UvTaskRunner::~UvTaskRunner() {
|
|
|
|
for (auto& iter : tasks_) {
|
|
|
|
uv_unref(reinterpret_cast<uv_handle_t*>(iter.first));
|
|
|
|
delete iter.first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:56:18 +00:00
|
|
|
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
|
2017-06-16 20:39:08 +00:00
|
|
|
base::OnceClosure task,
|
2015-09-03 02:28:50 +00:00
|
|
|
base::TimeDelta delay) {
|
2016-07-10 11:09:55 +00:00
|
|
|
auto* timer = new uv_timer_t;
|
2015-09-03 02:28:50 +00:00
|
|
|
timer->data = this;
|
|
|
|
uv_timer_init(loop_, timer);
|
|
|
|
uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0);
|
2017-06-16 20:39:08 +00:00
|
|
|
tasks_[timer] = std::move(task);
|
2015-09-03 02:28:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-07 21:14:35 +00:00
|
|
|
bool UvTaskRunner::RunsTasksInCurrentSequence() const {
|
2015-09-03 02:28:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
|
|
|
|
base::OnceClosure task,
|
|
|
|
base::TimeDelta delay) {
|
2017-06-16 20:39:08 +00:00
|
|
|
return PostDelayedTask(from_here, std::move(task), delay);
|
2015-09-03 02:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void UvTaskRunner::OnTimeout(uv_timer_t* timer) {
|
2019-08-28 14:39:21 +00:00
|
|
|
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->tasks_;
|
|
|
|
const auto iter = tasks.find(timer);
|
|
|
|
if (iter == std::end(tasks))
|
2015-09-03 02:28:50 +00:00
|
|
|
return;
|
|
|
|
|
2019-08-28 14:39:21 +00:00
|
|
|
std::move(iter->second).Run();
|
|
|
|
tasks.erase(iter);
|
2015-10-21 12:46:16 +00:00
|
|
|
uv_timer_stop(timer);
|
|
|
|
uv_close(reinterpret_cast<uv_handle_t*>(timer), UvTaskRunner::OnClose);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void UvTaskRunner::OnClose(uv_handle_t* handle) {
|
|
|
|
delete reinterpret_cast<uv_timer_t*>(handle);
|
2015-09-03 02:28:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace electron
|