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 "content/renderer/scheduler/task_queue_manager.h" | 5 #include "content/renderer/scheduler/task_queue_manager.h" |
6 | 6 |
7 #include <queue> | 7 #include <queue> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/observer_list.h" | |
11 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
12 #include "base/trace_event/trace_event_argument.h" | 11 #include "base/trace_event/trace_event_argument.h" |
13 #include "cc/test/test_now_source.h" | 12 #include "cc/test/test_now_source.h" |
14 #include "content/renderer/scheduler/task_queue_selector.h" | 13 #include "content/renderer/scheduler/task_queue_selector.h" |
15 | 14 |
16 namespace { | 15 namespace { |
17 const int64_t kMaxTimeTicks = std::numeric_limits<int64>::max(); | 16 const int64_t kMaxTimeTicks = std::numeric_limits<int64>::max(); |
18 } | 17 } |
19 | 18 |
20 namespace content { | 19 namespace content { |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 void TaskQueueManager::ProcessTaskFromWorkQueue(size_t queue_index) { | 406 void TaskQueueManager::ProcessTaskFromWorkQueue(size_t queue_index) { |
408 DCHECK(main_thread_checker_.CalledOnValidThread()); | 407 DCHECK(main_thread_checker_.CalledOnValidThread()); |
409 internal::TaskQueue* queue = Queue(queue_index); | 408 internal::TaskQueue* queue = Queue(queue_index); |
410 base::PendingTask pending_task = queue->TakeTaskFromWorkQueue(); | 409 base::PendingTask pending_task = queue->TakeTaskFromWorkQueue(); |
411 if (!pending_task.nestable) { | 410 if (!pending_task.nestable) { |
412 // Defer non-nestable work to the main task runner. NOTE these tasks can be | 411 // Defer non-nestable work to the main task runner. NOTE these tasks can be |
413 // arbitrarily delayed so the additional delay should not be a problem. | 412 // arbitrarily delayed so the additional delay should not be a problem. |
414 main_task_runner_->PostNonNestableTask(pending_task.posted_from, | 413 main_task_runner_->PostNonNestableTask(pending_task.posted_from, |
415 pending_task.task); | 414 pending_task.task); |
416 } else { | 415 } else { |
417 FOR_EACH_OBSERVER(base::MessageLoop::TaskObserver, task_observers_, | |
418 WillProcessTask(pending_task)); | |
419 task_annotator_.RunTask("TaskQueueManager::PostTask", | 416 task_annotator_.RunTask("TaskQueueManager::PostTask", |
420 "TaskQueueManager::RunTask", pending_task); | 417 "TaskQueueManager::RunTask", pending_task); |
421 FOR_EACH_OBSERVER(base::MessageLoop::TaskObserver, task_observers_, | |
422 DidProcessTask(pending_task)); | |
423 } | 418 } |
424 } | 419 } |
425 | 420 |
426 bool TaskQueueManager::RunsTasksOnCurrentThread() const { | 421 bool TaskQueueManager::RunsTasksOnCurrentThread() const { |
427 return main_task_runner_->RunsTasksOnCurrentThread(); | 422 return main_task_runner_->RunsTasksOnCurrentThread(); |
428 } | 423 } |
429 | 424 |
430 bool TaskQueueManager::PostDelayedTask( | 425 bool TaskQueueManager::PostDelayedTask( |
431 const tracked_objects::Location& from_here, | 426 const tracked_objects::Location& from_here, |
432 const base::Closure& task, | 427 const base::Closure& task, |
433 base::TimeDelta delay) { | 428 base::TimeDelta delay) { |
434 DCHECK(delay > base::TimeDelta()); | 429 DCHECK(delay > base::TimeDelta()); |
435 return main_task_runner_->PostDelayedTask(from_here, task, delay); | 430 return main_task_runner_->PostDelayedTask(from_here, task, delay); |
436 } | 431 } |
437 | 432 |
438 void TaskQueueManager::SetQueueName(size_t queue_index, const char* name) { | 433 void TaskQueueManager::SetQueueName(size_t queue_index, const char* name) { |
439 DCHECK(main_thread_checker_.CalledOnValidThread()); | 434 DCHECK(main_thread_checker_.CalledOnValidThread()); |
440 internal::TaskQueue* queue = Queue(queue_index); | 435 internal::TaskQueue* queue = Queue(queue_index); |
441 queue->set_name(name); | 436 queue->set_name(name); |
442 } | 437 } |
443 | 438 |
444 void TaskQueueManager::SetWorkBatchSize(int work_batch_size) { | 439 void TaskQueueManager::SetWorkBatchSize(int work_batch_size) { |
445 DCHECK(main_thread_checker_.CalledOnValidThread()); | 440 DCHECK(main_thread_checker_.CalledOnValidThread()); |
446 DCHECK_GE(work_batch_size, 1); | 441 DCHECK_GE(work_batch_size, 1); |
447 work_batch_size_ = work_batch_size; | 442 work_batch_size_ = work_batch_size; |
448 } | 443 } |
449 | 444 |
450 void TaskQueueManager::AddTaskObserver( | |
451 base::MessageLoop::TaskObserver* task_observer) { | |
452 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
453 task_observers_.AddObserver(task_observer); | |
454 } | |
455 | |
456 void TaskQueueManager::RemoveTaskObserver( | |
457 base::MessageLoop::TaskObserver* task_observer) { | |
458 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
459 task_observers_.RemoveObserver(task_observer); | |
460 } | |
461 | |
462 void TaskQueueManager::SetTimeSourceForTesting( | 445 void TaskQueueManager::SetTimeSourceForTesting( |
463 scoped_refptr<cc::TestNowSource> time_source) { | 446 scoped_refptr<cc::TestNowSource> time_source) { |
464 DCHECK(main_thread_checker_.CalledOnValidThread()); | 447 DCHECK(main_thread_checker_.CalledOnValidThread()); |
465 time_source_ = time_source; | 448 time_source_ = time_source; |
466 } | 449 } |
467 | 450 |
468 base::TimeTicks TaskQueueManager::Now() const { | 451 base::TimeTicks TaskQueueManager::Now() const { |
469 return UNLIKELY(time_source_) ? time_source_->Now() : base::TimeTicks::Now(); | 452 return UNLIKELY(time_source_) ? time_source_->Now() : base::TimeTicks::Now(); |
470 } | 453 } |
471 | 454 |
472 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | 455 scoped_refptr<base::trace_event::ConvertableToTraceFormat> |
473 TaskQueueManager::AsValueWithSelectorResult(bool should_run, | 456 TaskQueueManager::AsValueWithSelectorResult(bool should_run, |
474 size_t selected_queue) const { | 457 size_t selected_queue) const { |
475 DCHECK(main_thread_checker_.CalledOnValidThread()); | 458 DCHECK(main_thread_checker_.CalledOnValidThread()); |
476 scoped_refptr<base::trace_event::TracedValue> state = | 459 scoped_refptr<base::trace_event::TracedValue> state = |
477 new base::trace_event::TracedValue(); | 460 new base::trace_event::TracedValue(); |
478 state->BeginArray("queues"); | 461 state->BeginArray("queues"); |
479 for (auto& queue : queues_) | 462 for (auto& queue : queues_) |
480 queue->AsValueInto(state.get()); | 463 queue->AsValueInto(state.get()); |
481 state->EndArray(); | 464 state->EndArray(); |
482 state->BeginDictionary("selector"); | 465 state->BeginDictionary("selector"); |
483 selector_->AsValueInto(state.get()); | 466 selector_->AsValueInto(state.get()); |
484 state->EndDictionary(); | 467 state->EndDictionary(); |
485 if (should_run) | 468 if (should_run) |
486 state->SetInteger("selected_queue", selected_queue); | 469 state->SetInteger("selected_queue", selected_queue); |
487 return state; | 470 return state; |
488 } | 471 } |
489 | 472 |
490 } // namespace content | 473 } // namespace content |
OLD | NEW |