task_queue.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_OFFLINE_PAGES_TASK_TASK_QUEUE_H_
  5. #define COMPONENTS_OFFLINE_PAGES_TASK_TASK_QUEUE_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/callback.h"
  9. #include "base/containers/circular_deque.h"
  10. #include "base/location.h"
  11. #include "base/memory/raw_ptr.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/sequence_checker.h"
  15. #include "components/offline_pages/task/task.h"
  16. namespace base {
  17. class SingleThreadTaskRunner;
  18. } // namespace base
  19. namespace offline_pages {
  20. // Class for coordinating |Task|s in relation to access to a specific resource.
  21. // As a task, we understand a set of asynchronous operations (possibly switching
  22. // threads) that access a set of sensitive resource(s). Because the resource
  23. // state is modified and individual steps of a task are asynchronous, allowing
  24. // certain tasks to run in parallel may lead to incorrect results. This class
  25. // allows for ordering of tasks in a FIFO manner, to ensure two tasks modifying
  26. // a resources are not run at the same time.
  27. //
  28. // Consumers of this class should create an instance of TaskQueue and implement
  29. // tasks that need to be run sequentially. New task will only be started when
  30. // the previous one calls |Task::TaskComplete|.
  31. //
  32. // Methods on TaskQueue should be called from the same thread from which it
  33. // is created.
  34. class TaskQueue {
  35. public:
  36. class Delegate {
  37. public:
  38. virtual ~Delegate() {}
  39. // Invoked once when TaskQueue reached 0 tasks.
  40. virtual void OnTaskQueueIsIdle() = 0;
  41. };
  42. explicit TaskQueue(Delegate* delegate);
  43. TaskQueue(const TaskQueue&) = delete;
  44. TaskQueue& operator=(const TaskQueue&) = delete;
  45. ~TaskQueue();
  46. // Adds a task to the queue. Queue takes ownership of the task. Optionally,
  47. // use FROM_HERE as the first parameter for debugging.
  48. void AddTask(std::unique_ptr<Task> task);
  49. void AddTask(const base::Location& from_here, std::unique_ptr<Task> task);
  50. // Whether the task queue has any pending (not-running) tasks.
  51. bool HasPendingTasks() const;
  52. // Whether there is a task currently running.
  53. bool HasRunningTask() const;
  54. // Returns a human-readable string describing the contents of the task queue.
  55. std::string GetStateForTesting() const;
  56. private:
  57. friend Task;
  58. struct Entry;
  59. // Checks whether there are any tasks to run, as well as whether no task is
  60. // currently running. When both are met, it will start the next task in the
  61. // queue.
  62. void StartTaskIfAvailable();
  63. void RunCurrentTask();
  64. void ResumeCurrentTask(base::OnceClosure on_resume);
  65. // Callback for informing the queue that a task was completed. Can be called
  66. // from any thread.
  67. static void TaskCompletedCallback(
  68. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  69. base::WeakPtr<TaskQueue> task_queue,
  70. Task* task);
  71. void SuspendTask(Task* task);
  72. void ResumeTask(Task* task, base::OnceClosure on_resume);
  73. void TaskCompleted(Task* task);
  74. void InformTaskQueueIsIdle();
  75. // This TaskQueue's task runner, set on construction using the instance
  76. // assigned to the current thread.
  77. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  78. // Owns and outlives this TaskQueue.
  79. raw_ptr<Delegate> delegate_;
  80. // Currently running tasks.
  81. std::unique_ptr<Task> current_task_;
  82. base::Location current_task_location_;
  83. // A FIFO queue of tasks that will be run using this task queue.
  84. base::circular_deque<Entry> tasks_;
  85. // A set of tasks which are suspended.
  86. std::vector<Entry> suspended_tasks_;
  87. SEQUENCE_CHECKER(sequence_checker_);
  88. base::WeakPtrFactory<TaskQueue> weak_ptr_factory_{this};
  89. };
  90. } // namespace offline_pages
  91. #endif // COMPONENTS_OFFLINE_PAGES_TASK_TASK_QUEUE_H_