OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| 7 |
| 8 #include <queue> |
| 9 |
| 10 #include "base/base_export.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/task_scheduler/scheduler_lock.h" |
| 15 #include "base/task_scheduler/sequence.h" |
| 16 #include "base/task_scheduler/task.h" |
| 17 #include "base/time/time.h" |
| 18 |
| 19 namespace base { |
| 20 namespace internal { |
| 21 |
| 22 class PriorityQueue; |
| 23 class ShutdownManager; |
| 24 |
| 25 // Holds delayed tasks until they become ripe for execution. This class is |
| 26 // thread-safe. |
| 27 class BASE_EXPORT DelayedTaskManager { |
| 28 public: |
| 29 // |delayed_run_time_changed| is invoked when the next delayed run time |
| 30 // changes as a result of adding a new delayed task to the manager. |
| 31 // |shutdown_manager| is used to handle shutdown behavior of tasks. |
| 32 DelayedTaskManager(const Closure& delayed_run_time_changed, |
| 33 ShutdownManager* shutdown_manager); |
| 34 ~DelayedTaskManager(); |
| 35 |
| 36 // Adds |task| to a queue of delayed tasks. The task will be inserted into |
| 37 // |sequence| and |priority_queue| the first time that PostReadyTasks() is |
| 38 // called with Now() >= |task.delayed_run_time|. |
| 39 void AddDelayedTask(const Task& task, |
| 40 scoped_refptr<Sequence> sequence, |
| 41 PriorityQueue* priority_queue); |
| 42 |
| 43 // Posts delayed tasks that are ripe for execution. |
| 44 void PostReadyTasks(); |
| 45 |
| 46 // Returns the next time at which a delayed task will become ripe for |
| 47 // execution, or a null TimeTicks if there is no pending delayed tasks. |
| 48 TimeTicks GetNextDelayedRunTime() const; |
| 49 |
| 50 private: |
| 51 // Returns the current time. Can be overridden for tests. |
| 52 virtual TimeTicks Now(); |
| 53 |
| 54 struct DelayedTask { |
| 55 DelayedTask(); |
| 56 ~DelayedTask(); |
| 57 |
| 58 Task task; |
| 59 |
| 60 // The sequence and priority queue in which the task is inserted once it |
| 61 // becomes ripe for execution. |
| 62 scoped_refptr<Sequence> sequence; |
| 63 PriorityQueue* priority_queue; |
| 64 |
| 65 // Ensures that tasks that have the same |delayed_run_time| are popped from |
| 66 // |delayed_tasks_| in the order in which they were pushed into it. |
| 67 size_t index; |
| 68 |
| 69 bool operator<(const DelayedTask& other) const; |
| 70 }; |
| 71 |
| 72 // Lock protecting |delayed_tasks_| and |next_delayed_task_index_|. |
| 73 mutable SchedulerLock lock_; |
| 74 |
| 75 // Priority queue of delayed tasks. The delayed task with the smallest |
| 76 // |task.delayed_run_time| is in front of the priority queue. |
| 77 std::priority_queue<DelayedTask> delayed_tasks_; |
| 78 |
| 79 // The index to assign to the next delayed task added to |delayed_tasks_|. |
| 80 size_t next_delayed_task_index_; |
| 81 |
| 82 const Closure delayed_run_time_changed_; |
| 83 |
| 84 ShutdownManager* const shutdown_manager_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
| 87 }; |
| 88 |
| 89 } // namespace internal |
| 90 } // namespace base |
| 91 |
| 92 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
OLD | NEW |