Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(391)

Unified Diff: components/offline_pages/core/task_queue.h

Issue 2359933007: [Offline pages] Introduces TaskQueue to serialize tasks that asynchronously access SQLStore (Closed)
Patch Set: Addressing feedback from Dmitry Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/offline_pages/core/task.cc ('k') | components/offline_pages/core/task_queue.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..941c47632a96271794c7822585f591cf04150585
--- /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 pending (not-running) tasks.
+ bool HasPendingTasks() const;
+ // Whether there is a task currently running.
+ bool HasRunningTask() const;
+
+ 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_
« no previous file with comments | « components/offline_pages/core/task.cc ('k') | components/offline_pages/core/task_queue.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698