Migrate base::TaskRunner from Closure to OnceClosure

Migrate base::TaskRunner from Closure to OnceClosure
https://codereview.chromium.org/2637843002

Pass Callback to TaskRunner by value and consume it on invocation (1)
https://codereview.chromium.org/2726523002

Replace base::get with std::get
https://codereview.chromium.org/2797133002
This commit is contained in:
Aleksei Kuzmin 2017-06-16 23:39:08 +03:00
parent ffbb16e854
commit d09cab2e21
4 changed files with 20 additions and 17 deletions

View file

@ -2,6 +2,8 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <utility>
#include "atom/app/uv_task_runner.h"
#include "base/stl_util.h"
@ -19,13 +21,13 @@ UvTaskRunner::~UvTaskRunner() {
}
bool UvTaskRunner::PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) {
auto* timer = new uv_timer_t;
timer->data = this;
uv_timer_init(loop_, timer);
uv_timer_start(timer, UvTaskRunner::OnTimeout, delay.InMilliseconds(), 0);
tasks_[timer] = task;
tasks_[timer] = std::move(task);
return true;
}
@ -35,9 +37,9 @@ bool UvTaskRunner::RunsTasksOnCurrentThread() const {
bool UvTaskRunner::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) {
return PostDelayedTask(from_here, task, delay);
return PostDelayedTask(from_here, std::move(task), delay);
}
// static
@ -46,7 +48,7 @@ void UvTaskRunner::OnTimeout(uv_timer_t* timer) {
if (!ContainsKey(self->tasks_, timer))
return;
self->tasks_[timer].Run();
std::move(self->tasks_[timer]).Run();
self->tasks_.erase(timer);
uv_timer_stop(timer);
uv_close(reinterpret_cast<uv_handle_t*>(timer), UvTaskRunner::OnClose);

View file

@ -21,12 +21,12 @@ class UvTaskRunner : public base::SingleThreadTaskRunner {
// base::SingleThreadTaskRunner:
bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) override;
bool RunsTasksOnCurrentThread() const override;
bool PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) override;
private:
@ -35,7 +35,7 @@ class UvTaskRunner : public base::SingleThreadTaskRunner {
uv_loop_t* loop_;
std::map<uv_timer_t*, base::Closure> tasks_;
std::map<uv_timer_t*, base::OnceClosure> tasks_;
DISALLOW_COPY_AND_ASSIGN(UvTaskRunner);
};

View file

@ -13,17 +13,17 @@ void BridgeTaskRunner::MessageLoopIsReady() {
CHECK(message_loop);
for (const TaskPair& task : tasks_) {
message_loop->task_runner()->PostDelayedTask(
base::get<0>(task), base::get<1>(task), base::get<2>(task));
std::get<0>(task), std::get<1>(task), std::get<2>(task));
}
for (const TaskPair& task : non_nestable_tasks_) {
message_loop->task_runner()->PostNonNestableDelayedTask(
base::get<0>(task), base::get<1>(task), base::get<2>(task));
std::get<0>(task), std::get<1>(task), std::get<2>(task));
}
}
bool BridgeTaskRunner::PostDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) {
auto message_loop = base::MessageLoop::current();
if (!message_loop) {
@ -31,7 +31,8 @@ bool BridgeTaskRunner::PostDelayedTask(
return true;
}
return message_loop->task_runner()->PostDelayedTask(from_here, task, delay);
return message_loop->task_runner()->PostDelayedTask(
from_here, std::move(task), delay);
}
bool BridgeTaskRunner::RunsTasksOnCurrentThread() const {
@ -44,7 +45,7 @@ bool BridgeTaskRunner::RunsTasksOnCurrentThread() const {
bool BridgeTaskRunner::PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) {
auto message_loop = base::MessageLoop::current();
if (!message_loop) {
@ -53,7 +54,7 @@ bool BridgeTaskRunner::PostNonNestableDelayedTask(
}
return message_loop->task_runner()->PostNonNestableDelayedTask(
from_here, task, delay);
from_here, std::move(task), delay);
}
} // namespace atom

View file

@ -25,17 +25,17 @@ class BridgeTaskRunner : public base::SingleThreadTaskRunner {
// base::SingleThreadTaskRunner:
bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) override;
bool RunsTasksOnCurrentThread() const override;
bool PostNonNestableDelayedTask(
const tracked_objects::Location& from_here,
const base::Closure& task,
base::OnceClosure task,
base::TimeDelta delay) override;
private:
using TaskPair = std::tuple<
tracked_objects::Location, base::Closure, base::TimeDelta>;
tracked_objects::Location, base::OnceClosure, base::TimeDelta>;
std::vector<TaskPair> tasks_;
std::vector<TaskPair> non_nestable_tasks_;