OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "platform/scheduler/base/task_queue_manager.h" | 5 #include "platform/scheduler/base/task_queue_manager.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 return; | 211 return; |
212 } | 212 } |
213 | 213 |
214 any_thread().immediate_do_work_posted_count++; | 214 any_thread().immediate_do_work_posted_count++; |
215 } | 215 } |
216 delegate_->PostTask(from_here, immediate_do_work_closure_); | 216 delegate_->PostTask(from_here, immediate_do_work_closure_); |
217 } | 217 } |
218 | 218 |
219 void TaskQueueManager::MaybeScheduleDelayedWork( | 219 void TaskQueueManager::MaybeScheduleDelayedWork( |
220 const tracked_objects::Location& from_here, | 220 const tracked_objects::Location& from_here, |
221 LazyNow* lazy_now, | 221 base::TimeTicks now, |
222 base::TimeTicks run_time) { | 222 base::TimeDelta delay) { |
223 DCHECK(main_thread_checker_.CalledOnValidThread()); | 223 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 224 DCHECK_GE(delay, base::TimeDelta()); |
224 { | 225 { |
225 base::AutoLock lock(any_thread_lock_); | 226 base::AutoLock lock(any_thread_lock_); |
226 | 227 |
227 // Unless we're nested, don't post a delayed DoWork if there's an immediate | 228 // Unless we're nested, don't post a delayed DoWork if there's an immediate |
228 // DoWork in flight or we're inside a DoWork. We can rely on DoWork posting | 229 // DoWork in flight or we're inside a DoWork. We can rely on DoWork posting |
229 // a delayed continuation as needed. | 230 // a delayed continuation as needed. |
230 if (!any_thread().is_nested && | 231 if (!any_thread().is_nested && |
231 (any_thread().immediate_do_work_posted_count > 0 || | 232 (any_thread().immediate_do_work_posted_count > 0 || |
232 any_thread().do_work_running_count == 1)) { | 233 any_thread().do_work_running_count == 1)) { |
233 return; | 234 return; |
234 } | 235 } |
235 } | 236 } |
236 | 237 |
237 // De-duplicate DoWork posts. | 238 // De-duplicate DoWork posts. |
| 239 base::TimeTicks run_time = now + delay; |
238 if (next_delayed_do_work_ <= run_time && !next_delayed_do_work_.is_null()) | 240 if (next_delayed_do_work_ <= run_time && !next_delayed_do_work_.is_null()) |
239 return; | 241 return; |
240 | 242 |
241 cancelable_delayed_do_work_closure_.Reset(delayed_do_work_closure_); | |
242 | |
243 base::TimeDelta delay = | |
244 std::max(base::TimeDelta(), run_time - lazy_now->Now()); | |
245 next_delayed_do_work_ = lazy_now->Now() + delay; | |
246 | |
247 TRACE_EVENT1(tracing_category_, "MaybeScheduleDelayedWorkInternal", | 243 TRACE_EVENT1(tracing_category_, "MaybeScheduleDelayedWorkInternal", |
248 "delay_ms", delay.InMillisecondsF()); | 244 "delay_ms", delay.InMillisecondsF()); |
249 | 245 |
| 246 cancelable_delayed_do_work_closure_.Reset(delayed_do_work_closure_); |
| 247 next_delayed_do_work_ = run_time; |
250 delegate_->PostDelayedTask( | 248 delegate_->PostDelayedTask( |
251 from_here, cancelable_delayed_do_work_closure_.callback(), delay); | 249 from_here, cancelable_delayed_do_work_closure_.callback(), delay); |
252 } | 250 } |
253 | 251 |
254 void TaskQueueManager::CancelDelayedWork(base::TimeTicks run_time) { | |
255 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
256 if (next_delayed_do_work_ != run_time) | |
257 return; | |
258 | |
259 cancelable_delayed_do_work_closure_.Cancel(); | |
260 next_delayed_do_work_ = base::TimeTicks(); | |
261 } | |
262 | |
263 void TaskQueueManager::DoWork(bool delayed) { | 252 void TaskQueueManager::DoWork(bool delayed) { |
264 DCHECK(main_thread_checker_.CalledOnValidThread()); | 253 DCHECK(main_thread_checker_.CalledOnValidThread()); |
265 TRACE_EVENT1(tracing_category_, "TaskQueueManager::DoWork", "delayed", | 254 TRACE_EVENT1(tracing_category_, "TaskQueueManager::DoWork", "delayed", |
266 delayed); | 255 delayed); |
267 LazyNow lazy_now(real_time_domain()->CreateLazyNow()); | 256 LazyNow lazy_now(real_time_domain()->CreateLazyNow()); |
268 | 257 |
269 bool is_nested = delegate_->IsNested(); | 258 bool is_nested = delegate_->IsNested(); |
270 if (!is_nested) | 259 if (!is_nested) |
271 queues_to_delete_.clear(); | 260 queues_to_delete_.clear(); |
272 | 261 |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 } | 643 } |
655 | 644 |
656 void TaskQueueManager::SetRecordTaskDelayHistograms( | 645 void TaskQueueManager::SetRecordTaskDelayHistograms( |
657 bool record_task_delay_histograms) { | 646 bool record_task_delay_histograms) { |
658 DCHECK(main_thread_checker_.CalledOnValidThread()); | 647 DCHECK(main_thread_checker_.CalledOnValidThread()); |
659 record_task_delay_histograms_ = record_task_delay_histograms; | 648 record_task_delay_histograms_ = record_task_delay_histograms; |
660 } | 649 } |
661 | 650 |
662 } // namespace scheduler | 651 } // namespace scheduler |
663 } // namespace blink | 652 } // namespace blink |
OLD | NEW |