OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #include "base/task_scheduler/delayed_task_manager.h" | 5 #include "base/task_scheduler/delayed_task_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/task_scheduler/scheduler_thread_pool.h" | 10 #include "base/task_scheduler/scheduler_worker_pool.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 struct DelayedTaskManager::DelayedTask { | 15 struct DelayedTaskManager::DelayedTask { |
16 DelayedTask(std::unique_ptr<Task> task, | 16 DelayedTask(std::unique_ptr<Task> task, |
17 scoped_refptr<Sequence> sequence, | 17 scoped_refptr<Sequence> sequence, |
18 SchedulerWorkerThread* worker_thread, | 18 SchedulerWorkerThread* worker_thread, |
19 SchedulerThreadPool* thread_pool, | 19 SchedulerWorkerPool* worker_pool, |
20 uint64_t index) | 20 uint64_t index) |
21 : task(std::move(task)), | 21 : task(std::move(task)), |
22 sequence(std::move(sequence)), | 22 sequence(std::move(sequence)), |
23 worker_thread(worker_thread), | 23 worker_thread(worker_thread), |
24 thread_pool(thread_pool), | 24 worker_pool(worker_pool), |
25 index(index) {} | 25 index(index) {} |
26 | 26 |
27 DelayedTask(DelayedTask&& other) = default; | 27 DelayedTask(DelayedTask&& other) = default; |
28 | 28 |
29 ~DelayedTask() = default; | 29 ~DelayedTask() = default; |
30 | 30 |
31 DelayedTask& operator=(DelayedTask&& other) = default; | 31 DelayedTask& operator=(DelayedTask&& other) = default; |
32 | 32 |
33 // |task| will be posted to |thread_pool| with |sequence| and |worker_thread| | 33 // |task| will be posted to |worker_pool| with |sequence| and |worker_thread| |
34 // when it becomes ripe for execution. | 34 // when it becomes ripe for execution. |
35 std::unique_ptr<Task> task; | 35 std::unique_ptr<Task> task; |
36 scoped_refptr<Sequence> sequence; | 36 scoped_refptr<Sequence> sequence; |
37 SchedulerWorkerThread* worker_thread; | 37 SchedulerWorkerThread* worker_thread; |
38 SchedulerThreadPool* thread_pool; | 38 SchedulerWorkerPool* worker_pool; |
39 | 39 |
40 // Ensures that tasks that have the same |delayed_run_time| are sorted | 40 // Ensures that tasks that have the same |delayed_run_time| are sorted |
41 // according to the order in which they were added to the DelayedTaskManager. | 41 // according to the order in which they were added to the DelayedTaskManager. |
42 uint64_t index; | 42 uint64_t index; |
43 | 43 |
44 private: | 44 private: |
45 DISALLOW_COPY_AND_ASSIGN(DelayedTask); | 45 DISALLOW_COPY_AND_ASSIGN(DelayedTask); |
46 }; | 46 }; |
47 | 47 |
48 DelayedTaskManager::DelayedTaskManager( | 48 DelayedTaskManager::DelayedTaskManager( |
49 const Closure& on_delayed_run_time_updated) | 49 const Closure& on_delayed_run_time_updated) |
50 : on_delayed_run_time_updated_(on_delayed_run_time_updated) { | 50 : on_delayed_run_time_updated_(on_delayed_run_time_updated) { |
51 DCHECK(!on_delayed_run_time_updated_.is_null()); | 51 DCHECK(!on_delayed_run_time_updated_.is_null()); |
52 } | 52 } |
53 | 53 |
54 DelayedTaskManager::~DelayedTaskManager() = default; | 54 DelayedTaskManager::~DelayedTaskManager() = default; |
55 | 55 |
56 void DelayedTaskManager::AddDelayedTask(std::unique_ptr<Task> task, | 56 void DelayedTaskManager::AddDelayedTask(std::unique_ptr<Task> task, |
57 scoped_refptr<Sequence> sequence, | 57 scoped_refptr<Sequence> sequence, |
58 SchedulerWorkerThread* worker_thread, | 58 SchedulerWorkerThread* worker_thread, |
59 SchedulerThreadPool* thread_pool) { | 59 SchedulerWorkerPool* worker_pool) { |
60 DCHECK(task); | 60 DCHECK(task); |
61 DCHECK(sequence); | 61 DCHECK(sequence); |
62 DCHECK(thread_pool); | 62 DCHECK(worker_pool); |
63 | 63 |
64 const TimeTicks new_task_delayed_run_time = task->delayed_run_time; | 64 const TimeTicks new_task_delayed_run_time = task->delayed_run_time; |
65 TimeTicks current_delayed_run_time; | 65 TimeTicks current_delayed_run_time; |
66 | 66 |
67 { | 67 { |
68 AutoSchedulerLock auto_lock(lock_); | 68 AutoSchedulerLock auto_lock(lock_); |
69 | 69 |
70 if (!delayed_tasks_.empty()) | 70 if (!delayed_tasks_.empty()) |
71 current_delayed_run_time = delayed_tasks_.top().task->delayed_run_time; | 71 current_delayed_run_time = delayed_tasks_.top().task->delayed_run_time; |
72 | 72 |
73 delayed_tasks_.emplace(std::move(task), std::move(sequence), worker_thread, | 73 delayed_tasks_.emplace(std::move(task), std::move(sequence), worker_thread, |
74 thread_pool, ++delayed_task_index_); | 74 worker_pool, ++delayed_task_index_); |
75 } | 75 } |
76 | 76 |
77 if (current_delayed_run_time.is_null() || | 77 if (current_delayed_run_time.is_null() || |
78 new_task_delayed_run_time < current_delayed_run_time) { | 78 new_task_delayed_run_time < current_delayed_run_time) { |
79 on_delayed_run_time_updated_.Run(); | 79 on_delayed_run_time_updated_.Run(); |
80 } | 80 } |
81 } | 81 } |
82 | 82 |
83 void DelayedTaskManager::PostReadyTasks() { | 83 void DelayedTaskManager::PostReadyTasks() { |
84 const TimeTicks now = Now(); | 84 const TimeTicks now = Now(); |
(...skipping 11 matching lines...) Expand all Loading... |
96 // the task from |delayed_tasks_|. See DelayedTaskComparator::operator() | 96 // the task from |delayed_tasks_|. See DelayedTaskComparator::operator() |
97 // for minor debug-check implications. | 97 // for minor debug-check implications. |
98 ready_tasks.push_back( | 98 ready_tasks.push_back( |
99 std::move(const_cast<DelayedTask&>(delayed_tasks_.top()))); | 99 std::move(const_cast<DelayedTask&>(delayed_tasks_.top()))); |
100 delayed_tasks_.pop(); | 100 delayed_tasks_.pop(); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 // Post delayed tasks that are ready for execution. | 104 // Post delayed tasks that are ready for execution. |
105 for (auto& delayed_task : ready_tasks) { | 105 for (auto& delayed_task : ready_tasks) { |
106 delayed_task.thread_pool->PostTaskWithSequenceNow( | 106 delayed_task.worker_pool->PostTaskWithSequenceNow( |
107 std::move(delayed_task.task), std::move(delayed_task.sequence), | 107 std::move(delayed_task.task), std::move(delayed_task.sequence), |
108 delayed_task.worker_thread); | 108 delayed_task.worker_thread); |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 TimeTicks DelayedTaskManager::GetDelayedRunTime() const { | 112 TimeTicks DelayedTaskManager::GetDelayedRunTime() const { |
113 AutoSchedulerLock auto_lock(lock_); | 113 AutoSchedulerLock auto_lock(lock_); |
114 | 114 |
115 if (delayed_tasks_.empty()) | 115 if (delayed_tasks_.empty()) |
116 return TimeTicks(); | 116 return TimeTicks(); |
(...skipping 27 matching lines...) Expand all Loading... |
144 return false; | 144 return false; |
145 return left.index > right.index; | 145 return left.index > right.index; |
146 } | 146 } |
147 | 147 |
148 TimeTicks DelayedTaskManager::Now() const { | 148 TimeTicks DelayedTaskManager::Now() const { |
149 return TimeTicks::Now(); | 149 return TimeTicks::Now(); |
150 } | 150 } |
151 | 151 |
152 } // namespace internal | 152 } // namespace internal |
153 } // namespace base | 153 } // namespace base |
OLD | NEW |