Chromium Code Reviews| Index: components/offline_pages/core/task_queue.h |
| diff --git a/components/offline_pages/core/task_queue.h b/components/offline_pages/core/task_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7093ea83854cc3d06eb39e677d46e9bbd4a8e468 |
| --- /dev/null |
| +++ b/components/offline_pages/core/task_queue.h |
| @@ -0,0 +1,64 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |
| +#define COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |
| + |
| +#include <memory> |
| +#include <queue> |
| + |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "components/offline_pages/core/task.h" |
| + |
| +namespace offline_pages { |
| + |
| +// Class for coordinating |Task|s in relation to access to a specific resource. |
| +// As a task, we understand a set of asynchronous operations (possibly switching |
| +// threads) that access a set of sensitive resource(s). Because the resource |
| +// state is modified and individual steps of a task are asynchronous, allowing |
| +// certain tasks to run in parallel may lead to incorrect results. This class |
| +// allows for ordering of tasks in a FIFO manner, to ensure two tasks modifying |
| +// a resources are not run at the same time. |
| +// |
| +// Consumers of this class should create an instance of TaskQueue and implement |
| +// tasks that need to be run sequentially. New task will only be started when |
| +// the previous one calls |Task::TaskComplete|. |
| +class TaskQueue { |
| + public: |
| + TaskQueue(); |
| + ~TaskQueue(); |
| + |
| + // Adds a task to the queue. Queue takes ownership of the task. |
| + void AddTask(std::unique_ptr<Task> task); |
| + // Whether the task queue has any tasks (running or waiting). |
| + bool HasTasks() const; |
|
dewittj
2016/09/29 18:40:56
This is misleading for the public API, it should n
fgorski
2016/09/30 17:33:12
Done.
|
| + // Whether there is a task currently running. Used for testing. |
| + bool CurrentlyRunning() const; |
|
dewittj
2016/09/29 18:40:56
CurrentlyRunningForTest
fgorski
2016/09/30 17:33:12
Renamed to IsCurrentlyRunning().
It is used inter
|
| + |
| + private: |
| + // Checks whether there are any tasks to run, as well as whether no task is |
| + // currently running. When both are met, it will start the next task in the |
| + // queue. |
| + void MaybeStartTask(); |
| + |
| + // Callback for informing the queue that a task was completed. |
| + void TaskCompleted(Task* task); |
| + |
| + // Currently running tasks. |
| + std::unique_ptr<Task> current_task_; |
| + |
| + // A FIFO queue of tasks that will be run using this task queue. |
| + std::queue<std::unique_ptr<Task>> tasks_; |
| + |
| + base::WeakPtrFactory<TaskQueue> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| +}; |
| + |
| +} // namespace offline_pages |
| + |
| +#endif // COMPONENTS_OFFLINE_PAGES_CORE_TASK_QUEUE_H_ |