Chromium Code Reviews| 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 <stddef.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <queue> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/base_export.h" | |
| 15 #include "base/callback.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/task_scheduler/scheduler_lock.h" | |
| 19 #include "base/task_scheduler/sequence.h" | |
| 20 #include "base/task_scheduler/task.h" | |
| 21 #include "base/time/time.h" | |
| 22 | |
| 23 namespace base { | |
| 24 namespace internal { | |
| 25 | |
| 26 class PriorityQueue; | |
| 27 | |
| 28 // A DelayedTaskManager holds delayed Tasks until they become ripe for | |
| 29 // execution. When they become ripe for execution, it posts them to their | |
| 30 // associated Sequence and PriorityQueue. This class is thread-safe. | |
| 31 class BASE_EXPORT DelayedTaskManager { | |
| 32 public: | |
| 33 // |on_next_delayed_run_time_updated| is invoked when the next delayed run | |
| 34 // time is updated as a result of adding a delayed task to the manager. | |
| 35 explicit DelayedTaskManager(const Closure& on_next_delayed_run_time_updated); | |
| 36 ~DelayedTaskManager(); | |
| 37 | |
| 38 // Adds |task| to a queue of delayed tasks. The task will be inserted into | |
| 39 // |sequence| and |priority_queue| the first time that PostReadyTasks() is | |
| 40 // called while Now() is passed |task->delayed_run_time|. | |
| 41 void AddDelayedTask(std::unique_ptr<Task> task, | |
| 42 scoped_refptr<Sequence> sequence, | |
| 43 PriorityQueue* priority_queue); | |
| 44 | |
| 45 // Posts delayed tasks that are ripe for execution. | |
| 46 void PostReadyTasks(); | |
| 47 | |
| 48 // Returns the next time at which a delayed task will become ripe for | |
| 49 // execution, or a null TimeTicks if there are no pending delayed tasks. | |
| 50 TimeTicks GetNextDelayedRunTime() const; | |
| 51 | |
| 52 private: | |
| 53 struct DelayedTask; | |
| 54 struct DelayedTaskComparator { | |
| 55 bool operator()(const DelayedTask& left, const DelayedTask& right) const; | |
| 56 }; | |
| 57 | |
| 58 // Returns the current time. Can be overridden for tests. | |
| 59 virtual TimeTicks Now() const; | |
| 60 | |
| 61 const Closure on_next_delayed_run_time_updated_; | |
| 62 | |
| 63 // Synchronizes access to all members below. | |
| 64 mutable SchedulerLock lock_; | |
| 65 | |
| 66 // Priority queue of delayed tasks. The delayed task with the smallest | |
| 67 // |task->delayed_run_time| is in front of the priority queue. | |
| 68 using DelayedTaskQueue = std::priority_queue<DelayedTask, | |
| 69 std::vector<DelayedTask>, | |
| 70 DelayedTaskComparator>; | |
| 71 DelayedTaskQueue delayed_tasks_; | |
| 72 | |
| 73 // The index to assign to the next delayed task added to the manager. | |
| 74 size_t next_delayed_task_index_ = 0; | |
|
gab
2016/04/11 18:28:30
According to http://stackoverflow.com/questions/22
fdoray
2016/04/11 19:57:06
Done.
| |
| 75 | |
| 76 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | |
| 77 }; | |
| 78 | |
| 79 } // namespace internal | |
| 80 } // namespace base | |
| 81 | |
| 82 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | |
| OLD | NEW |