electron/shell/app/uv_task_runner.cc
trop[bot] 9da190f786
fix: delete UvTaskRunner's timers only after they're closed (#43598)
* fix: free UvTaskRunner timers only after they are closed

Co-authored-by: Charles Kerr <charles@charleskerr.com>

* refactor: UvTaskRunner now holds UvHandles

Co-authored-by: Charles Kerr <charles@charleskerr.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2024-09-06 11:18:52 -05:00

46 lines
1.4 KiB
C++

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/app/uv_task_runner.h"
#include <utility>
#include "base/location.h"
#include "base/stl_util.h"
#include "base/time/time.h"
namespace electron {
UvTaskRunner::UvTaskRunner(uv_loop_t* loop) : loop_{loop} {}
UvTaskRunner::~UvTaskRunner() = default;
bool UvTaskRunner::PostDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) {
auto on_timeout = [](uv_timer_t* timer) {
auto& tasks = static_cast<UvTaskRunner*>(timer->data)->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;
}
bool UvTaskRunner::RunsTasksInCurrentSequence() const {
return true;
}
bool UvTaskRunner::PostNonNestableDelayedTask(const base::Location& from_here,
base::OnceClosure task,
base::TimeDelta delay) {
return PostDelayedTask(from_here, std::move(task), delay);
}
} // namespace electron