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