LCOV - code coverage report
Current view: top level - src/libplatform - default-foreground-task-runner.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 43 47 91.5 %
Date: 2019-03-21 Functions: 9 12 75.0 %

          Line data    Source code
       1             : // Copyright 2017 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "src/libplatform/default-foreground-task-runner.h"
       6             : 
       7             : #include "src/base/platform/mutex.h"
       8             : #include "src/libplatform/default-platform.h"
       9             : 
      10             : namespace v8 {
      11             : namespace platform {
      12             : 
      13       61544 : DefaultForegroundTaskRunner::DefaultForegroundTaskRunner(
      14             :     IdleTaskSupport idle_task_support, TimeFunction time_function)
      15      246176 :     : idle_task_support_(idle_task_support), time_function_(time_function) {}
      16             : 
      17       60270 : void DefaultForegroundTaskRunner::Terminate() {
      18       60270 :   base::MutexGuard guard(&lock_);
      19       60270 :   terminated_ = true;
      20             : 
      21             :   // Drain the task queues.
      22       62103 :   while (!task_queue_.empty()) task_queue_.pop();
      23       65469 :   while (!delayed_task_queue_.empty()) delayed_task_queue_.pop();
      24       60271 :   while (!idle_task_queue_.empty()) idle_task_queue_.pop();
      25       60270 : }
      26             : 
      27           0 : void DefaultForegroundTaskRunner::PostTaskLocked(std::unique_ptr<Task> task,
      28             :                                                  const base::MutexGuard&) {
      29       61459 :   if (terminated_) return;
      30             :   task_queue_.push(std::move(task));
      31       61458 :   event_loop_control_.NotifyOne();
      32             : }
      33             : 
      34       60868 : void DefaultForegroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
      35       60868 :   base::MutexGuard guard(&lock_);
      36       60868 :   PostTaskLocked(std::move(task), guard);
      37       60868 : }
      38             : 
      39           0 : double DefaultForegroundTaskRunner::MonotonicallyIncreasingTime() {
      40       25669 :   return time_function_();
      41             : }
      42             : 
      43        5793 : void DefaultForegroundTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
      44             :                                                   double delay_in_seconds) {
      45             :   DCHECK_GE(delay_in_seconds, 0.0);
      46        5793 :   base::MutexGuard guard(&lock_);
      47        5793 :   if (terminated_) return;
      48        5792 :   double deadline = MonotonicallyIncreasingTime() + delay_in_seconds;
      49       11584 :   delayed_task_queue_.push(std::make_pair(deadline, std::move(task)));
      50             : }
      51             : 
      52        2877 : void DefaultForegroundTaskRunner::PostIdleTask(std::unique_ptr<IdleTask> task) {
      53        2877 :   CHECK_EQ(IdleTaskSupport::kEnabled, idle_task_support_);
      54        2877 :   base::MutexGuard guard(&lock_);
      55        2877 :   if (terminated_) return;
      56             :   idle_task_queue_.push(std::move(task));
      57             : }
      58             : 
      59          50 : bool DefaultForegroundTaskRunner::IdleTasksEnabled() {
      60          50 :   return idle_task_support_ == IdleTaskSupport::kEnabled;
      61             : }
      62             : 
      63      247108 : std::unique_ptr<Task> DefaultForegroundTaskRunner::PopTaskFromQueue(
      64             :     MessageLoopBehavior wait_for_work) {
      65      247108 :   base::MutexGuard guard(&lock_);
      66             :   // Move delayed tasks that hit their deadline to the main queue.
      67      247108 :   std::unique_ptr<Task> task = PopTaskFromDelayedQueueLocked(guard);
      68      248290 :   while (task) {
      69         591 :     PostTaskLocked(std::move(task), guard);
      70        1182 :     task = PopTaskFromDelayedQueueLocked(guard);
      71             :   }
      72             : 
      73      248597 :   while (task_queue_.empty()) {
      74      188972 :     if (wait_for_work == MessageLoopBehavior::kDoNotWait) return {};
      75             :     WaitForTaskLocked(guard);
      76             :   }
      77             : 
      78             :   task = std::move(task_queue_.front());
      79             :   task_queue_.pop();
      80             : 
      81             :   return task;
      82             : }
      83             : 
      84             : std::unique_ptr<Task>
      85      247699 : DefaultForegroundTaskRunner::PopTaskFromDelayedQueueLocked(
      86             :     const base::MutexGuard&) {
      87      247699 :   if (delayed_task_queue_.empty()) return {};
      88             : 
      89             :   double now = MonotonicallyIncreasingTime();
      90             :   const DelayedEntry& deadline_and_task = delayed_task_queue_.top();
      91       19877 :   if (deadline_and_task.first > now) return {};
      92             :   // The const_cast here is necessary because there does not exist a clean way
      93             :   // to get a unique_ptr out of the priority queue. We provide the priority
      94             :   // queue with a custom comparison operator to make sure that the priority
      95             :   // queue does not access the unique_ptr. Therefore it should be safe to reset
      96             :   // the unique_ptr in the priority queue here. Note that the DelayedEntry is
      97             :   // removed from the priority_queue immediately afterwards.
      98             :   std::unique_ptr<Task> result =
      99             :       std::move(const_cast<DelayedEntry&>(deadline_and_task).second);
     100         591 :   delayed_task_queue_.pop();
     101             :   return result;
     102             : }
     103             : 
     104      163427 : std::unique_ptr<IdleTask> DefaultForegroundTaskRunner::PopTaskFromIdleQueue() {
     105      163427 :   base::MutexGuard guard(&lock_);
     106      163427 :   if (idle_task_queue_.empty()) return {};
     107             : 
     108             :   std::unique_ptr<IdleTask> task = std::move(idle_task_queue_.front());
     109             :   idle_task_queue_.pop();
     110             : 
     111             :   return task;
     112             : }
     113             : 
     114           0 : void DefaultForegroundTaskRunner::WaitForTaskLocked(const base::MutexGuard&) {
     115        1489 :   event_loop_control_.Wait(&lock_);
     116           0 : }
     117             : 
     118             : }  // namespace platform
     119             : }  // namespace v8

Generated by: LCOV version 1.10