Commit 578fe721 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

Extract lambda tasks to task-utils.h

This extracts the lambda tasks to an own compilation unit and header
file. Additionally, it addresses the TODO to avoid templates and just
store the function to execute in an std::function.
Third, it provides the same functionality for pure (non-cancellable
non-idle) tasks.
Last, it removes the "Lambda" part from the methods, because we can
actually instantiate it with anything that is invocable (function
pointer, lambda, functor, ...).

R=ahaas@chromium.org

Bug: v8:8238
Change-Id: I2f613f5b15ee208f215bbf74bd6d1d41889fd637
Reviewed-on: https://chromium-review.googlesource.com/c/1328923
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57397}
parent 1f147c50
......@@ -2532,6 +2532,8 @@ v8_source_set("v8_base") {
"src/string-stream.h",
"src/strtod.cc",
"src/strtod.h",
"src/task-utils.cc",
"src/task-utils.h",
"src/third_party/utf8-decoder/utf8-decoder.h",
"src/thread-id.cc",
"src/thread-id.h",
......
......@@ -165,33 +165,6 @@ class V8_EXPORT_PRIVATE CancelableTask : public Cancelable,
DISALLOW_COPY_AND_ASSIGN(CancelableTask);
};
// TODO(clemensh): Use std::function and move implementation to cc file.
template <typename Func>
class CancelableLambdaTask final : public CancelableTask {
public:
CancelableLambdaTask(Isolate* isolate, Func func)
: CancelableTask(isolate), func_(std::move(func)) {}
CancelableLambdaTask(CancelableTaskManager* manager, Func func)
: CancelableTask(manager), func_(std::move(func)) {}
void RunInternal() final { func_(); }
private:
Func func_;
};
template <typename Func>
std::unique_ptr<CancelableTask> MakeCancelableLambdaTask(Isolate* isolate,
Func func) {
return std::unique_ptr<CancelableTask>(
new CancelableLambdaTask<Func>(isolate, std::move(func)));
}
template <typename Func>
std::unique_ptr<CancelableTask> MakeCancelableLambdaTask(
CancelableTaskManager* manager, Func func) {
return std::unique_ptr<CancelableTask>(
new CancelableLambdaTask<Func>(manager, std::move(func)));
}
// Multiple inheritance can be used because IdleTask is a pure interface.
class CancelableIdleTask : public Cancelable, public IdleTask {
public:
......@@ -211,34 +184,6 @@ class CancelableIdleTask : public Cancelable, public IdleTask {
DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask);
};
template <typename Func>
class CancelableIdleLambdaTask final : public CancelableIdleTask {
public:
CancelableIdleLambdaTask(Isolate* isolate, Func func)
: CancelableIdleTask(isolate), func_(std::move(func)) {}
CancelableIdleLambdaTask(CancelableTaskManager* manager, Func func)
: CancelableIdleTask(manager), func_(std::move(func)) {}
void RunInternal(double deadline_in_seconds) final {
func_(deadline_in_seconds);
}
private:
Func func_;
};
template <typename Func>
std::unique_ptr<CancelableIdleTask> MakeCancelableIdleLambdaTask(
Isolate* isolate, Func func) {
return std::unique_ptr<CancelableIdleTask>(
new CancelableIdleLambdaTask<Func>(isolate, std::move(func)));
}
template <typename Func>
std::unique_ptr<CancelableIdleTask> MakeCancelableIdleLambdaTask(
CancelableTaskManager* manager, Func func) {
return std::unique_ptr<CancelableIdleTask>(
new CancelableIdleLambdaTask<Func>(manager, std::move(func)));
}
} // namespace internal
} // namespace v8
......
......@@ -14,6 +14,7 @@
#include "src/objects-inl.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parser.h"
#include "src/task-utils.h"
namespace v8 {
namespace internal {
......@@ -231,7 +232,7 @@ void CompilerDispatcher::ScheduleIdleTaskFromAnyThread(
if (idle_task_scheduled_) return;
idle_task_scheduled_ = true;
taskrunner_->PostIdleTask(MakeCancelableIdleLambdaTask(
taskrunner_->PostIdleTask(MakeCancelableIdleTask(
task_manager_.get(),
[this](double deadline_in_seconds) { DoIdleWork(deadline_in_seconds); }));
}
......@@ -247,8 +248,8 @@ void CompilerDispatcher::ScheduleMoreWorkerTasksIfNeeded() {
}
++num_worker_tasks_;
}
platform_->CallOnWorkerThread(MakeCancelableLambdaTask(
task_manager_.get(), [this] { DoBackgroundWork(); }));
platform_->CallOnWorkerThread(
MakeCancelableTask(task_manager_.get(), [this] { DoBackgroundWork(); }));
}
void CompilerDispatcher::DoBackgroundWork() {
......
......@@ -8,6 +8,7 @@
#include "src/cancelable-task.h"
#include "src/objects-inl.h"
#include "src/objects/slots.h"
#include "src/task-utils.h"
#include "src/v8.h"
#include "src/visitors.h"
#include "src/vm-state-inl.h"
......@@ -861,7 +862,7 @@ int GlobalHandles::DispatchPendingPhantomCallbacks(
second_pass_callbacks_task_posted_ = true;
auto taskrunner = V8::GetCurrentPlatform()->GetForegroundTaskRunner(
reinterpret_cast<v8::Isolate*>(isolate()));
taskrunner->PostTask(MakeCancelableLambdaTask(
taskrunner->PostTask(MakeCancelableTask(
isolate(), [this] { InvokeSecondPassPhantomCallbacksFromTask(); }));
}
}
......
......@@ -8,6 +8,7 @@
#include "src/heap/array-buffer-tracker.h"
#include "src/heap/gc-tracer.h"
#include "src/heap/heap-inl.h"
#include "src/task-utils.h"
namespace v8 {
namespace internal {
......@@ -48,7 +49,7 @@ void ArrayBufferCollector::FreeAllocations() {
if (!heap_->IsTearingDown() && !heap_->ShouldReduceMemory() &&
FLAG_concurrent_array_buffer_freeing) {
V8::GetCurrentPlatform()->CallOnWorkerThread(
MakeCancelableLambdaTask(heap_->isolate(), [this] {
MakeCancelableTask(heap_->isolate(), [this] {
TRACE_BACKGROUND_GC(
heap_->tracer(),
GCTracer::BackgroundScope::BACKGROUND_ARRAY_BUFFER_FREE);
......
// 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
// 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.
#ifndef V8_TASK_UTILS_H_
#define V8_TASK_UTILS_H_
#include <functional>
#include <memory>
namespace v8 {
namespace internal {
class CancelableIdleTask;
class CancelableTask;
class CancelableTaskManager;
class Isolate;
std::unique_ptr<CancelableTask> MakeCancelableTask(Isolate*,
std::function<void()>);
std::unique_ptr<CancelableTask> MakeCancelableTask(CancelableTaskManager*,
std::function<void()>);
std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
Isolate*, std::function<void(double)>);
std::unique_ptr<CancelableIdleTask> MakeCancelableIdleTask(
CancelableTaskManager* manager, std::function<void(double)>);
} // namespace internal
} // namespace v8
#endif // V8_TASK_UTILS_H_
......@@ -13,6 +13,7 @@
#include "src/counters.h"
#include "src/identity-map.h"
#include "src/property-descriptor.h"
#include "src/task-utils.h"
#include "src/tracing/trace-event.h"
#include "src/trap-handler/trap-handler.h"
#include "src/wasm/module-decoder.h"
......@@ -3185,7 +3186,7 @@ void CompilationStateImpl::SetError(uint32_t func_index,
// Schedule a foreground task to call the callback and notify users about the
// compile error.
foreground_task_runner_->PostTask(
MakeCancelableLambdaTask(&foreground_task_manager_, [this] {
MakeCancelableTask(&foreground_task_manager_, [this] {
VoidResult error_result = GetCompileError();
NotifyOnEvent(CompilationEvent::kFailedCompilation, &error_result);
}));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment