task-utils.cc 2.1 KB
Newer Older
1 2 3 4
// Copyright 2018 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.

5
#include "src/tasks/task-utils.h"
6

7
#include "src/tasks/cancelable-task.h"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

namespace v8 {
namespace internal {

namespace {

class CancelableFuncTask final : public CancelableTask {
 public:
  CancelableFuncTask(Isolate* isolate, std::function<void()> func)
      : CancelableTask(isolate), func_(std::move(func)) {}
  CancelableFuncTask(CancelableTaskManager* manager, std::function<void()> func)
      : CancelableTask(manager), func_(std::move(func)) {}
  void RunInternal() final { func_(); }

 private:
  const std::function<void()> func_;
};

class CancelableIdleFuncTask final : public CancelableIdleTask {
 public:
  CancelableIdleFuncTask(Isolate* isolate, std::function<void(double)> func)
      : CancelableIdleTask(isolate), func_(std::move(func)) {}
  CancelableIdleFuncTask(CancelableTaskManager* manager,
                         std::function<void(double)> func)
      : CancelableIdleTask(manager), func_(std::move(func)) {}
  void RunInternal(double deadline_in_seconds) final {
    func_(deadline_in_seconds);
  }

 private:
  const std::function<void(double)> func_;
};

}  // namespace

std::unique_ptr<CancelableTask> MakeCancelableTask(Isolate* isolate,
                                                   std::function<void()> func) {
45
  return std::make_unique<CancelableFuncTask>(isolate, std::move(func));
46 47 48 49
}

std::unique_ptr<CancelableTask> MakeCancelableTask(
    CancelableTaskManager* manager, std::function<void()> func) {
50
  return std::make_unique<CancelableFuncTask>(manager, std::move(func));
51 52 53 54
}

std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
    Isolate* isolate, std::function<void(double)> func) {
55
  return std::make_unique<CancelableIdleFuncTask>(isolate, std::move(func));
56 57 58 59
}

std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
    CancelableTaskManager* manager, std::function<void(double)> func) {
60
  return std::make_unique<CancelableIdleFuncTask>(manager, std::move(func));
61 62 63 64
}

}  // namespace internal
}  // namespace v8