Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(392)

Side by Side Diff: base/task_scheduler/delayed_task_manager.cc

Issue 1876363004: TaskScheduler [11] Support ExecutionMode::SINGLE_THREADED. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@8_delayed
Patch Set: all post tasks go through SchedulerThreadPool Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_interface.h" 10 #include "base/task_scheduler/scheduler_thread_pool_interface.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 SchedulerThreadPoolInterface* thread_pool, 19 SchedulerThreadPoolInterface* thread_pool,
19 uint64_t index) 20 uint64_t index)
20 : task(std::move(task)), 21 : task(std::move(task)),
21 sequence(std::move(sequence)), 22 sequence(std::move(sequence)),
23 worker_thread(worker_thread),
22 thread_pool(thread_pool), 24 thread_pool(thread_pool),
23 index(index) {} 25 index(index) {}
24 26
25 DelayedTask(DelayedTask&& other) = default; 27 DelayedTask(DelayedTask&& other) = default;
26 28
27 ~DelayedTask() = default; 29 ~DelayedTask() = default;
28 30
29 DelayedTask& operator=(DelayedTask&& other) = default; 31 DelayedTask& operator=(DelayedTask&& other) = default;
30 32
31 // |task| will be posted to |thread_pool| as part of |sequence| when it 33 // |task| will be posted to |thread_pool| with |sequence| and |worker_thread|
32 // becomes ripe for execution. 34 // when it becomes ripe for execution.
33 std::unique_ptr<Task> task; 35 std::unique_ptr<Task> task;
34 scoped_refptr<Sequence> sequence; 36 scoped_refptr<Sequence> sequence;
37 SchedulerWorkerThread* worker_thread;
35 SchedulerThreadPoolInterface* thread_pool; 38 SchedulerThreadPoolInterface* thread_pool;
36 39
37 // 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
38 // 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.
39 uint64_t index; 42 uint64_t index;
40 43
41 private: 44 private:
42 DISALLOW_COPY_AND_ASSIGN(DelayedTask); 45 DISALLOW_COPY_AND_ASSIGN(DelayedTask);
43 }; 46 };
44 47
45 DelayedTaskManager::DelayedTaskManager( 48 DelayedTaskManager::DelayedTaskManager(
46 const Closure& on_delayed_run_time_updated) 49 const Closure& on_delayed_run_time_updated)
47 : on_delayed_run_time_updated_(on_delayed_run_time_updated) { 50 : on_delayed_run_time_updated_(on_delayed_run_time_updated) {
48 DCHECK(!on_delayed_run_time_updated_.is_null()); 51 DCHECK(!on_delayed_run_time_updated_.is_null());
49 } 52 }
50 53
51 DelayedTaskManager::~DelayedTaskManager() = default; 54 DelayedTaskManager::~DelayedTaskManager() = default;
52 55
53 void DelayedTaskManager::AddDelayedTask( 56 void DelayedTaskManager::AddDelayedTask(
54 std::unique_ptr<Task> task, 57 std::unique_ptr<Task> task,
55 scoped_refptr<Sequence> sequence, 58 scoped_refptr<Sequence> sequence,
59 SchedulerWorkerThread* worker_thread,
56 SchedulerThreadPoolInterface* thread_pool) { 60 SchedulerThreadPoolInterface* thread_pool) {
57 DCHECK(task); 61 DCHECK(task);
58 DCHECK(sequence); 62 DCHECK(sequence);
59 DCHECK(thread_pool); 63 DCHECK(thread_pool);
60 64
61 const TimeTicks new_task_delayed_run_time = task->delayed_run_time; 65 const TimeTicks new_task_delayed_run_time = task->delayed_run_time;
62 TimeTicks current_delayed_run_time; 66 TimeTicks current_delayed_run_time;
63 67
64 { 68 {
65 AutoSchedulerLock auto_lock(lock_); 69 AutoSchedulerLock auto_lock(lock_);
66 70
67 if (!delayed_tasks_.empty()) 71 if (!delayed_tasks_.empty())
68 current_delayed_run_time = delayed_tasks_.top().task->delayed_run_time; 72 current_delayed_run_time = delayed_tasks_.top().task->delayed_run_time;
69 73
70 delayed_tasks_.emplace(std::move(task), std::move(sequence), thread_pool, 74 delayed_tasks_.emplace(std::move(task), std::move(sequence), worker_thread,
71 ++delayed_task_index_); 75 thread_pool, ++delayed_task_index_);
72 } 76 }
73 77
74 if (current_delayed_run_time.is_null() || 78 if (current_delayed_run_time.is_null() ||
75 new_task_delayed_run_time < current_delayed_run_time) { 79 new_task_delayed_run_time < current_delayed_run_time) {
76 on_delayed_run_time_updated_.Run(); 80 on_delayed_run_time_updated_.Run();
77 } 81 }
78 } 82 }
79 83
80 void DelayedTaskManager::PostReadyTasks() { 84 void DelayedTaskManager::PostReadyTasks() {
81 const TimeTicks now = Now(); 85 const TimeTicks now = Now();
(...skipping 12 matching lines...) Expand all
94 // for minor debug-check implications. 98 // for minor debug-check implications.
95 ready_tasks.push_back( 99 ready_tasks.push_back(
96 std::move(const_cast<DelayedTask&>(delayed_tasks_.top()))); 100 std::move(const_cast<DelayedTask&>(delayed_tasks_.top())));
97 delayed_tasks_.pop(); 101 delayed_tasks_.pop();
98 } 102 }
99 } 103 }
100 104
101 // Post delayed tasks that are ready for execution. 105 // Post delayed tasks that are ready for execution.
102 for (auto& delayed_task : ready_tasks) { 106 for (auto& delayed_task : ready_tasks) {
103 delayed_task.thread_pool->PostTaskWithSequenceNow( 107 delayed_task.thread_pool->PostTaskWithSequenceNow(
104 std::move(delayed_task.task), std::move(delayed_task.sequence)); 108 std::move(delayed_task.task), std::move(delayed_task.sequence),
109 delayed_task.worker_thread);
105 } 110 }
106 } 111 }
107 112
108 TimeTicks DelayedTaskManager::GetDelayedRunTime() const { 113 TimeTicks DelayedTaskManager::GetDelayedRunTime() const {
109 AutoSchedulerLock auto_lock(lock_); 114 AutoSchedulerLock auto_lock(lock_);
110 115
111 if (delayed_tasks_.empty()) 116 if (delayed_tasks_.empty())
112 return TimeTicks(); 117 return TimeTicks();
113 118
114 return delayed_tasks_.top().task->delayed_run_time; 119 return delayed_tasks_.top().task->delayed_run_time;
(...skipping 25 matching lines...) Expand all
140 return false; 145 return false;
141 return left.index > right.index; 146 return left.index > right.index;
142 } 147 }
143 148
144 TimeTicks DelayedTaskManager::Now() const { 149 TimeTicks DelayedTaskManager::Now() const {
145 return TimeTicks::Now(); 150 return TimeTicks::Now();
146 } 151 }
147 152
148 } // namespace internal 153 } // namespace internal
149 } // namespace base 154 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698