Commit 11572d4a authored by tzik's avatar tzik Committed by Commit Bot

Support OnceCallback on file_system_provider::Queue

After this CL, file_system_provider::Queue can take base::OnceCallback
in addition to legacy base::Callback.

Change-Id: I29e306205a27e4e514c040ff1304be56bcce8373
Reviewed-on: https://chromium-review.googlesource.com/625643
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: default avatarTomasz Mikolajewski <mtomasz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#496576}
parent e0dfc1ce
......@@ -15,11 +15,11 @@ namespace file_system_provider {
Queue::Task::Task() : token(0) {
}
Queue::Task::Task(size_t token, const AbortableCallback& callback)
: token(token), callback(callback) {
}
Queue::Task::Task(size_t token, AbortableCallback callback)
: token(token), callback(std::move(callback)) {}
Queue::Task::Task(const Task& other) = default;
Queue::Task::Task(Task&& other) = default;
Queue::Task& Queue::Task::operator=(Task&& other) = default;
Queue::Task::~Task() {
}
......@@ -38,14 +38,14 @@ size_t Queue::NewToken() {
return next_token_++;
}
void Queue::Enqueue(size_t token, const AbortableCallback& callback) {
void Queue::Enqueue(size_t token, AbortableCallback callback) {
#if !NDEBUG
CHECK(executed_.find(token) == executed_.end());
for (auto& task : pending_) {
CHECK(token != task.token);
}
#endif
pending_.push_back(Task(token, callback));
pending_.push_back(Task(token, std::move(callback)));
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&Queue::MaybeRun, weak_ptr_factory_.GetWeakPtr()));
......@@ -65,11 +65,12 @@ void Queue::MaybeRun() {
return;
CHECK_GT(max_in_parallel_, executed_.size());
Task task = pending_.front();
Task task = std::move(pending_.front());
pending_.pop_front();
executed_[task.token] = task;
AbortCallback abort_callback = task.callback.Run();
auto callback = std::move(task.callback);
executed_[task.token] = std::move(task);
AbortCallback abort_callback = std::move(callback).Run();
// It may happen that the task is completed and removed synchronously. Hence,
// we need to check if the task is still in the executed collection.
......
......@@ -37,7 +37,7 @@ namespace file_system_provider {
// saying, just call Complete() from the completion callback of the task.
class Queue {
public:
typedef base::Callback<AbortCallback(void)> AbortableCallback;
using AbortableCallback = base::OnceCallback<AbortCallback(void)>;
// Creates a queue with a maximum number of tasks running in parallel.
explicit Queue(size_t max_in_parallel);
......@@ -52,7 +52,7 @@ class Queue {
// until another task is finished. Once the task is finished, Complete() must
// be called. The callback's abort callback may be NULL. In such case, Abort()
// must not be called.
void Enqueue(size_t token, const AbortableCallback& callback);
void Enqueue(size_t token, AbortableCallback callback);
// Forcibly aborts a previously enqueued task. May be called at any time as
// long as the task is still in the queue and is not marked as completed.
......@@ -67,10 +67,12 @@ class Queue {
// Information about an enqueued task which hasn't been removed, nor aborted.
struct Task {
Task();
Task(size_t token, const AbortableCallback& callback);
Task(const Task& other);
Task(size_t token, AbortableCallback callback);
Task(Task&& other);
~Task();
Task& operator=(Task&& other);
size_t token;
AbortableCallback callback;
AbortCallback abort_callback;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment