[reland] Use TaskScheduler instead of WorkerPool in v8_platform.cc.
A first version of this CL landed as
https://codereview.chromium.org/2610473002/. It was reverted because
of hangs in extensions_unittests. These hangs should have been fixed
by the addition of a call to TaskScheduler::FlushForTesting() in the
destructors of ScopedTaskEnvironment and ScopedAsyncTaskScheduler.
The following traits are used:
Priority: USER_VISIBLE
This task affects UI or responsiveness of future user interactions. It is
not an immediate response to a user interaction.
Shutdown behavior: CONTINUE_ON_SHUTDOWN
Tasks posted with this mode which have not started executing before
shutdown is initiated will never run. Tasks with this mode running at
shutdown will be ignored (the worker will not be joined).
Note: Tasks that were previously posted to base::WorkerPool should
use this shutdown behavior because this is how base::WorkerPool
handles all its tasks.
MayBlock():
The task may block.
BUG=659191
Review-Url: https://codereview.chromium.org/2876523002
Cr-Commit-Position: refs/heads/master@{#474346}
diff --git a/gin/v8_platform.cc b/gin/v8_platform.cc
index dc887c4..1ee4c3a 100644
--- a/gin/v8_platform.cc
+++ b/gin/v8_platform.cc
@@ -4,11 +4,14 @@
#include "gin/public/v8_platform.h"
+#include <algorithm>
+
#include "base/bind.h"
#include "base/debug/stack_trace.h"
#include "base/location.h"
#include "base/sys_info.h"
-#include "base/threading/worker_pool.h"
+#include "base/task_scheduler/post_task.h"
+#include "base/task_scheduler/task_scheduler.h"
#include "base/trace_event/trace_event.h"
#include "gin/per_isolate_data.h"
@@ -16,6 +19,10 @@
namespace {
+constexpr base::TaskTraits kBackgroundThreadTaskTraits = {
+ base::MayBlock(), base::TaskPriority::USER_VISIBLE,
+ base::TaskShutdownBehavior::BLOCK_SHUTDOWN};
+
base::LazyInstance<V8Platform>::Leaky g_v8_platform = LAZY_INSTANCE_INITIALIZER;
void RunWithLocker(v8::Isolate* isolate, v8::Task* task) {
@@ -58,25 +65,16 @@
V8Platform::~V8Platform() {}
size_t V8Platform::NumberOfAvailableBackgroundThreads() {
- // WorkerPool will currently always create additional threads for posted
- // background tasks, unless there are threads sitting idle (on posix).
- // Indicate that V8 should create no more than the number of cores available,
- // reserving one core for the main thread.
- const size_t available_cores =
- static_cast<size_t>(base::SysInfo::NumberOfProcessors());
- if (available_cores > 1) {
- return available_cores - 1;
- }
- return 1;
+ return std::max(1, base::TaskScheduler::GetInstance()
+ ->GetMaxConcurrentTasksWithTraitsDeprecated(
+ kBackgroundThreadTaskTraits));
}
void V8Platform::CallOnBackgroundThread(
v8::Task* task,
v8::Platform::ExpectedRuntime expected_runtime) {
- base::WorkerPool::PostTask(
- FROM_HERE,
- base::Bind(&v8::Task::Run, base::Owned(task)),
- expected_runtime == v8::Platform::kLongRunningTask);
+ base::PostTaskWithTraits(FROM_HERE, kBackgroundThreadTaskTraits,
+ base::Bind(&v8::Task::Run, base::Owned(task)));
}
void V8Platform::CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) {