default-foreground-task-runner.h 2.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_
#define V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_

#include <queue>

#include "include/libplatform/libplatform.h"
#include "include/v8-platform.h"
12
#include "src/base/platform/condition-variable.h"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include "src/base/platform/mutex.h"

namespace v8 {
namespace platform {

class V8_PLATFORM_EXPORT DefaultForegroundTaskRunner
    : public NON_EXPORTED_BASE(TaskRunner) {
 public:
  using TimeFunction = double (*)();

  DefaultForegroundTaskRunner(IdleTaskSupport idle_task_support,
                              TimeFunction time_function);

  void Terminate();

28
  std::unique_ptr<Task> PopTaskFromQueue(MessageLoopBehavior wait_for_work);
29 30 31

  std::unique_ptr<IdleTask> PopTaskFromIdleQueue();

32
  void WaitForTaskLocked(const base::LockGuard<base::Mutex>&);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  double MonotonicallyIncreasingTime();

  // v8::TaskRunner implementation.
  void PostTask(std::unique_ptr<Task> task) override;

  void PostDelayedTask(std::unique_ptr<Task> task,
                       double delay_in_seconds) override;

  void PostIdleTask(std::unique_ptr<IdleTask> task) override;

  bool IdleTasksEnabled() override;

 private:
  // The same as PostTask, but the lock is already held by the caller. The
  // {guard} parameter should make sure that the caller is holding the lock.
  void PostTaskLocked(std::unique_ptr<Task> task,
50
                      const base::LockGuard<base::Mutex>&);
51 52 53 54

  // A caller of this function has to hold {lock_}. The {guard} parameter should
  // make sure that the caller is holding the lock.
  std::unique_ptr<Task> PopTaskFromDelayedQueueLocked(
55
      const base::LockGuard<base::Mutex>&);
56 57 58

  bool terminated_ = false;
  base::Mutex lock_;
59
  base::ConditionVariable event_loop_control_;
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  std::queue<std::unique_ptr<Task>> task_queue_;
  IdleTaskSupport idle_task_support_;
  std::queue<std::unique_ptr<IdleTask>> idle_task_queue_;

  // Some helper constructs for the {delayed_task_queue_}.
  using DelayedEntry = std::pair<double, std::unique_ptr<Task>>;
  // Define a comparison operator for the delayed_task_queue_ to make sure
  // that the unique_ptr in the DelayedEntry is not accessed in the priority
  // queue. This is necessary because we have to reset the unique_ptr when we
  // remove a DelayedEntry from the priority queue.
  struct DelayedEntryCompare {
    bool operator()(DelayedEntry& left, DelayedEntry& right) {
      return left.first > right.first;
    }
  };
  std::priority_queue<DelayedEntry, std::vector<DelayedEntry>,
                      DelayedEntryCompare>
      delayed_task_queue_;

  TimeFunction time_function_;
};

}  // namespace platform
}  // namespace v8
#endif  // V8_LIBPLATFORM_DEFAULT_FOREGROUND_TASK_RUNNER_H_