task-utils.cc 2.09 KB
Newer Older
1 2 3 4 5 6 7 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
// 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.

#include "src/task-utils.h"

#include "src/cancelable-task.h"

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) {
  return base::make_unique<CancelableFuncTask>(isolate, std::move(func));
}

std::unique_ptr<CancelableTask> MakeCancelableTask(
    CancelableTaskManager* manager, std::function<void()> func) {
  return base::make_unique<CancelableFuncTask>(manager, std::move(func));
}

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

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

}  // namespace internal
}  // namespace v8