1// Copyright 2018 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/task-utils.h"
6
7#include "src/cancelable-task.h"
8
9namespace v8 {
10namespace internal {
11
12namespace {
13
14class CancelableFuncTask final : public CancelableTask {
15 public:
16 CancelableFuncTask(Isolate* isolate, std::function<void()> func)
17 : CancelableTask(isolate), func_(std::move(func)) {}
18 CancelableFuncTask(CancelableTaskManager* manager, std::function<void()> func)
19 : CancelableTask(manager), func_(std::move(func)) {}
20 void RunInternal() final { func_(); }
21
22 private:
23 const std::function<void()> func_;
24};
25
26class CancelableIdleFuncTask final : public CancelableIdleTask {
27 public:
28 CancelableIdleFuncTask(Isolate* isolate, std::function<void(double)> func)
29 : CancelableIdleTask(isolate), func_(std::move(func)) {}
30 CancelableIdleFuncTask(CancelableTaskManager* manager,
31 std::function<void(double)> func)
32 : CancelableIdleTask(manager), func_(std::move(func)) {}
33 void RunInternal(double deadline_in_seconds) final {
34 func_(deadline_in_seconds);
35 }
36
37 private:
38 const std::function<void(double)> func_;
39};
40
41} // namespace
42
43std::unique_ptr<CancelableTask> MakeCancelableTask(Isolate* isolate,
44 std::function<void()> func) {
45 return base::make_unique<CancelableFuncTask>(isolate, std::move(func));
46}
47
48std::unique_ptr<CancelableTask> MakeCancelableTask(
49 CancelableTaskManager* manager, std::function<void()> func) {
50 return base::make_unique<CancelableFuncTask>(manager, std::move(func));
51}
52
53std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
54 Isolate* isolate, std::function<void(double)> func) {
55 return base::make_unique<CancelableIdleFuncTask>(isolate, std::move(func));
56}
57
58std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
59 CancelableTaskManager* manager, std::function<void(double)> func) {
60 return base::make_unique<CancelableIdleFuncTask>(manager, std::move(func));
61}
62
63} // namespace internal
64} // namespace v8
65