Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
|
gab
2016/10/17 19:09:18
Weird that this file is marked as 'D'...
fdoray
2016/10/18 20:10:49
In the first patch set I deleted it and the code r
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 5 #ifndef BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 6 #define BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <memory> | 8 #include <memory> |
| 11 #include <queue> | |
| 12 #include <vector> | |
| 13 | 9 |
| 14 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 15 #include "base/callback.h" | |
| 16 #include "base/macros.h" | 11 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 12 #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" | 13 #include "base/time/time.h" |
| 22 | 14 |
| 23 namespace base { | 15 namespace base { |
| 16 | |
| 17 class TaskRunner; | |
| 18 | |
| 24 namespace internal { | 19 namespace internal { |
| 25 | 20 |
| 26 class SchedulerWorker; | 21 class SchedulerWorker; |
| 27 class SchedulerWorkerPool; | 22 class SchedulerWorkerPool; |
| 23 class Sequence; | |
| 24 struct Task; | |
| 28 | 25 |
| 29 // A DelayedTaskManager holds delayed Tasks until they become ripe for | 26 // A DelayedTaskManager forwards Tasks to a SchedulerWorkerPool when they become |
| 30 // execution. This class is thread-safe. | 27 // ripe for execution. This class is thread-safe. |
| 31 class BASE_EXPORT DelayedTaskManager { | 28 class BASE_EXPORT DelayedTaskManager { |
| 32 public: | 29 public: |
| 33 // |on_delayed_run_time_updated| is invoked when the delayed run time is | 30 // |service_thread_task_runner| posts tasks to the TaskScheduler service |
| 34 // updated as a result of adding a delayed task to the manager. | 31 // thread. |
| 35 explicit DelayedTaskManager(const Closure& on_delayed_run_time_updated); | 32 explicit DelayedTaskManager( |
| 33 scoped_refptr<TaskRunner> service_thread_task_runner); | |
| 36 ~DelayedTaskManager(); | 34 ~DelayedTaskManager(); |
| 37 | 35 |
| 38 // Adds |task| to a queue of delayed tasks. The task will be posted to | 36 // Posts |task|. The task will be forwarded to |worker_pool| with |sequence| |
| 39 // |worker_pool| with |sequence| and |worker| the first time that | 37 // and |worker| when it becomes ripe for execution. |worker| is a |
| 40 // PostReadyTasks() is called while Now() is passed |task->delayed_run_time|. | 38 // SchedulerWorker owned by |worker_pool| or nullptr. |
| 41 // |worker| is a SchedulerWorker owned by |worker_pool| or nullptr. | |
| 42 // | 39 // |
| 43 // TODO(robliao): Find a concrete way to manage the memory of |worker| and | 40 // TODO(robliao): Find a concrete way to manage the memory of |worker| and |
| 44 // |worker_pool|. These objects are never deleted in production, but it is | 41 // |worker_pool|. These objects are never deleted in production, but it is |
| 45 // better not to spread this assumption throughout the scheduler. | 42 // better not to spread this assumption throughout the scheduler. |
| 46 void AddDelayedTask(std::unique_ptr<Task> task, | 43 void AddDelayedTask(std::unique_ptr<Task> task, |
| 47 scoped_refptr<Sequence> sequence, | 44 scoped_refptr<Sequence> sequence, |
| 48 SchedulerWorker* worker, | 45 SchedulerWorker* worker, |
| 49 SchedulerWorkerPool* worker_pool); | 46 SchedulerWorkerPool* worker_pool); |
| 50 | 47 |
| 51 // Posts delayed tasks that are ripe for execution. | |
| 52 void PostReadyTasks(); | |
| 53 | |
| 54 // Returns the next time at which a delayed task will become ripe for | |
| 55 // execution, or a null TimeTicks if there are no pending delayed tasks. | |
| 56 TimeTicks GetDelayedRunTime() const; | |
| 57 | |
| 58 // Returns the current time. Can be overridden for tests. | |
| 59 virtual TimeTicks Now() const; | |
| 60 | |
| 61 private: | 48 private: |
| 62 struct DelayedTask; | 49 const scoped_refptr<TaskRunner> service_thread_task_runner_; |
| 63 struct DelayedTaskComparator { | |
| 64 bool operator()(const DelayedTask& left, const DelayedTask& right) const; | |
| 65 }; | |
| 66 | |
| 67 const Closure on_delayed_run_time_updated_; | |
| 68 | |
| 69 // Synchronizes access to all members below. | |
| 70 mutable SchedulerLock lock_; | |
| 71 | |
| 72 // Priority queue of delayed tasks. The delayed task with the smallest | |
| 73 // |task->delayed_run_time| is in front of the priority queue. | |
| 74 using DelayedTaskQueue = std::priority_queue<DelayedTask, | |
| 75 std::vector<DelayedTask>, | |
| 76 DelayedTaskComparator>; | |
| 77 DelayedTaskQueue delayed_tasks_; | |
| 78 | |
| 79 // The index to assign to the next delayed task added to the manager. | |
| 80 uint64_t delayed_task_index_ = 0; | |
| 81 | 50 |
| 82 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); | 51 DISALLOW_COPY_AND_ASSIGN(DelayedTaskManager); |
| 83 }; | 52 }; |
| 84 | 53 |
| 85 } // namespace internal | 54 } // namespace internal |
| 86 } // namespace base | 55 } // namespace base |
| 87 | 56 |
| 88 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ | 57 #endif // BASE_TASK_SCHEDULER_DELAYED_TASK_MANAGER_H_ |
| OLD | NEW |