diff --git a/include/v8-platform.h b/include/v8-platform.h index 2bb14df93e..128802be0e 100644 --- a/include/v8-platform.h +++ b/include/v8-platform.h @@ -290,7 +290,7 @@ class Platform { * used to estimate the number of tasks a work package should be split into. * A return value of 0 means that there are no background threads available. * Note that a value of 0 won't prohibit V8 from posting tasks using - * |CallOnBackgroundThread|. + * |CallOnWorkerThread|. */ virtual size_t NumberOfAvailableBackgroundThreads() { return 0; } @@ -324,7 +324,27 @@ class Platform { * thread the task will be run on. */ virtual void CallOnBackgroundThread(Task* task, - ExpectedRuntime expected_runtime) = 0; + ExpectedRuntime expected_runtime) { + // TODO(gab): Remove this when embedders override CallOnWorkerThread() + // instead. + + // An implementation needs to be provided here because this is called by the + // default implementation below. In practice however, all code either: + // - Overrides the new method (thus not making this call) -- i.e. all v8 + // code. + // - Overrides this method (thus not making this call) -- i.e. all + // unadapted embedders. + abort(); + } + + /** + * Schedules a task to be invoked on a worker thread. + * TODO(gab): Make pure virtual when all embedders override this instead of + * CallOnBackgroundThread(). + */ + virtual void CallOnWorkerThread(Task* task) { + CallOnBackgroundThread(task, kShortRunningTask); + } /** * Schedules a task to be invoked on a foreground thread wrt a specific diff --git a/src/compiler-dispatcher/compiler-dispatcher.cc b/src/compiler-dispatcher/compiler-dispatcher.cc index 27af96c85e..95aee02fed 100644 --- a/src/compiler-dispatcher/compiler-dispatcher.cc +++ b/src/compiler-dispatcher/compiler-dispatcher.cc @@ -526,9 +526,8 @@ void CompilerDispatcher::ScheduleMoreBackgroundTasksIfNeeded() { } ++num_background_tasks_; } - platform_->CallOnBackgroundThread( - new BackgroundTask(isolate_, task_manager_.get(), this), - v8::Platform::kShortRunningTask); + platform_->CallOnWorkerThread( + new BackgroundTask(isolate_, task_manager_.get(), this)); } void CompilerDispatcher::DoBackgroundWork() { diff --git a/src/compiler-dispatcher/optimizing-compile-dispatcher.cc b/src/compiler-dispatcher/optimizing-compile-dispatcher.cc index 7dc73b146c..6d44d012e6 100644 --- a/src/compiler-dispatcher/optimizing-compile-dispatcher.cc +++ b/src/compiler-dispatcher/optimizing-compile-dispatcher.cc @@ -224,15 +224,15 @@ void OptimizingCompileDispatcher::QueueForOptimization(CompilationJob* job) { if (FLAG_block_concurrent_recompilation) { blocked_jobs_++; } else { - V8::GetCurrentPlatform()->CallOnBackgroundThread( - new CompileTask(isolate_, this), v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread( + new CompileTask(isolate_, this)); } } void OptimizingCompileDispatcher::Unblock() { while (blocked_jobs_ > 0) { - V8::GetCurrentPlatform()->CallOnBackgroundThread( - new CompileTask(isolate_, this), v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread( + new CompileTask(isolate_, this)); blocked_jobs_--; } } diff --git a/src/d8.cc b/src/d8.cc index 66e54a429e..2fe56a7884 100644 --- a/src/d8.cc +++ b/src/d8.cc @@ -204,8 +204,7 @@ class PredictablePlatform : public Platform { return platform_->GetForegroundTaskRunner(isolate); } - void CallOnBackgroundThread(Task* task, - ExpectedRuntime expected_runtime) override { + void CallOnWorkerThread(Task* task) override { // It's not defined when background tasks are being executed, so we can just // execute them right away. task->Run(); diff --git a/src/heap/array-buffer-collector.cc b/src/heap/array-buffer-collector.cc index cf0297bb2a..bf165425f6 100644 --- a/src/heap/array-buffer-collector.cc +++ b/src/heap/array-buffer-collector.cc @@ -49,8 +49,7 @@ void ArrayBufferCollector::FreeAllocationsOnBackgroundThread() { heap_->account_external_memory_concurrently_freed(); if (heap_->use_tasks() && FLAG_concurrent_array_buffer_freeing) { FreeingTask* task = new FreeingTask(heap_); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } else { // Fallback for when concurrency is disabled/restricted. FreeAllocations(); diff --git a/src/heap/concurrent-marking.cc b/src/heap/concurrent-marking.cc index 3aafd191cc..c61608c376 100644 --- a/src/heap/concurrent-marking.cc +++ b/src/heap/concurrent-marking.cc @@ -523,8 +523,7 @@ void ConcurrentMarking::ScheduleTasks() { ++pending_task_count_; Task* task = new Task(heap_->isolate(), this, &task_state_[i], i); cancelable_id_[i] = task->id(); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } } DCHECK_EQ(task_count_, pending_task_count_); diff --git a/src/heap/item-parallel-job.cc b/src/heap/item-parallel-job.cc index 1c8d4c8ac4..8f50dc9fd3 100644 --- a/src/heap/item-parallel-job.cc +++ b/src/heap/item-parallel-job.cc @@ -105,8 +105,7 @@ void ItemParallelJob::Run(std::shared_ptr async_counters) { : base::Optional()); task_ids[i] = task->id(); if (i > 0) { - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } else { main_task = task; } diff --git a/src/heap/spaces.cc b/src/heap/spaces.cc index d90cac90f2..f5ba201849 100644 --- a/src/heap/spaces.cc +++ b/src/heap/spaces.cc @@ -359,8 +359,7 @@ void MemoryAllocator::Unmapper::FreeQueuedChunks() { DCHECK_GE(active_unmapping_tasks_.Value(), 0); active_unmapping_tasks_.Increment(1); task_ids_[pending_unmapping_tasks_++] = task->id(); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } else { PerformFreeMemoryOnQueuedChunks(); } diff --git a/src/heap/store-buffer.cc b/src/heap/store-buffer.cc index 724edf5721..b6b9663091 100644 --- a/src/heap/store-buffer.cc +++ b/src/heap/store-buffer.cc @@ -95,8 +95,7 @@ void StoreBuffer::FlipStoreBuffers() { if (!task_running_ && FLAG_concurrent_store_buffer) { task_running_ = true; Task* task = new Task(heap_->isolate(), this); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } } diff --git a/src/heap/sweeper.cc b/src/heap/sweeper.cc index 2072e407e9..09f457e2bb 100644 --- a/src/heap/sweeper.cc +++ b/src/heap/sweeper.cc @@ -157,8 +157,7 @@ void Sweeper::StartSweeperTasks() { &num_sweeping_tasks_, space); DCHECK_LT(num_tasks_, kMaxSweeperTasks); task_ids_[num_tasks_++] = task->id(); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); }); ScheduleIncrementalSweepingTask(); } @@ -554,8 +553,7 @@ void Sweeper::StartIterabilityTasks() { &iterability_task_semaphore_); iterability_task_id_ = task->id(); iterability_task_started_ = true; - V8::GetCurrentPlatform()->CallOnBackgroundThread( - task, v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(task); } } diff --git a/src/libplatform/default-platform.cc b/src/libplatform/default-platform.cc index 39d9525eff..b4aba5971e 100644 --- a/src/libplatform/default-platform.cc +++ b/src/libplatform/default-platform.cc @@ -202,8 +202,7 @@ std::shared_ptr DefaultPlatform::GetBackgroundTaskRunner( return background_task_runner_; } -void DefaultPlatform::CallOnBackgroundThread(Task* task, - ExpectedRuntime expected_runtime) { +void DefaultPlatform::CallOnWorkerThread(Task* task) { GetBackgroundTaskRunner(nullptr)->PostTask(std::unique_ptr(task)); } diff --git a/src/libplatform/default-platform.h b/src/libplatform/default-platform.h index b73f38a5fe..feaf776a52 100644 --- a/src/libplatform/default-platform.h +++ b/src/libplatform/default-platform.h @@ -60,8 +60,7 @@ class V8_PLATFORM_EXPORT DefaultPlatform : public NON_EXPORTED_BASE(Platform) { v8::Isolate* isolate) override; std::shared_ptr GetBackgroundTaskRunner( v8::Isolate* isolate) override; - void CallOnBackgroundThread(Task* task, - ExpectedRuntime expected_runtime) override; + void CallOnWorkerThread(Task* task) override; void CallOnForegroundThread(v8::Isolate* isolate, Task* task) override; void CallDelayedOnForegroundThread(Isolate* isolate, Task* task, double delay_in_seconds) override; diff --git a/test/cctest/cctest.h b/test/cctest/cctest.h index 72c7a2f78c..ac827bd425 100644 --- a/test/cctest/cctest.h +++ b/test/cctest/cctest.h @@ -683,9 +683,8 @@ class TestPlatform : public v8::Platform { return old_platform_->GetBackgroundTaskRunner(isolate); } - void CallOnBackgroundThread(v8::Task* task, - ExpectedRuntime expected_runtime) override { - old_platform_->CallOnBackgroundThread(task, expected_runtime); + void CallOnWorkerThread(v8::Task* task) override { + old_platform_->CallOnWorkerThread(task); } void CallOnForegroundThread(v8::Isolate* isolate, v8::Task* task) override { diff --git a/test/cctest/wasm/test-streaming-compilation.cc b/test/cctest/wasm/test-streaming-compilation.cc index ef77708267..0f3e2b29ce 100644 --- a/test/cctest/wasm/test-streaming-compilation.cc +++ b/test/cctest/wasm/test-streaming-compilation.cc @@ -44,8 +44,7 @@ class MockPlatform final : public TestPlatform { task_runner_->PostTask(std::unique_ptr(task)); } - void CallOnBackgroundThread(v8::Task* task, - ExpectedRuntime expected_runtime) override { + void CallOnWorkerThread(v8::Task* task) override { task_runner_->PostTask(std::unique_ptr(task)); } diff --git a/test/unittests/api/isolate-unittest.cc b/test/unittests/api/isolate-unittest.cc index 5a7c8e0cef..be7a56383c 100644 --- a/test/unittests/api/isolate-unittest.cc +++ b/test/unittests/api/isolate-unittest.cc @@ -60,9 +60,8 @@ TEST_F(IsolateTest, MemoryPressureNotificationBackground) { base::Semaphore semaphore(0); - internal::V8::GetCurrentPlatform()->CallOnBackgroundThread( - new MemoryPressureTask(isolate(), &semaphore), - v8::Platform::kShortRunningTask); + internal::V8::GetCurrentPlatform()->CallOnWorkerThread( + new MemoryPressureTask(isolate(), &semaphore)); semaphore.Wait(); diff --git a/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc b/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc index fe97cb30d8..c5c09aa3dd 100644 --- a/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc +++ b/test/unittests/compiler-dispatcher/compiler-dispatcher-unittest.cc @@ -111,8 +111,7 @@ class MockPlatform : public v8::Platform { return std::make_shared(this, is_foreground_task_runner); } - void CallOnBackgroundThread(Task* task, - ExpectedRuntime expected_runtime) override { + void CallOnWorkerThread(Task* task) override { base::LockGuard lock(&mutex_); background_tasks_.push_back(task); } @@ -183,8 +182,7 @@ class MockPlatform : public v8::Platform { base::LockGuard lock(&mutex_); tasks.swap(background_tasks_); } - platform->CallOnBackgroundThread(new TaskWrapper(this, tasks, true), - kShortRunningTask); + platform->CallOnWorkerThread(new TaskWrapper(this, tasks, true)); sem_.Wait(); } @@ -194,8 +192,7 @@ class MockPlatform : public v8::Platform { base::LockGuard lock(&mutex_); tasks.swap(background_tasks_); } - platform->CallOnBackgroundThread(new TaskWrapper(this, tasks, false), - kShortRunningTask); + platform->CallOnWorkerThread(new TaskWrapper(this, tasks, false)); } void RunForegroundTasks() { @@ -868,9 +865,8 @@ TEST_F(CompilerDispatcherTest, MemoryPressureFromBackground) { ASSERT_TRUE(dispatcher.Enqueue(shared)); base::Semaphore sem(0); - V8::GetCurrentPlatform()->CallOnBackgroundThread( - new PressureNotificationTask(i_isolate(), &dispatcher, &sem), - v8::Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread( + new PressureNotificationTask(i_isolate(), &dispatcher, &sem)); sem.Wait(); diff --git a/test/unittests/compiler-dispatcher/unoptimized-compile-job-unittest.cc b/test/unittests/compiler-dispatcher/unoptimized-compile-job-unittest.cc index d6816279ed..fc1818f35e 100644 --- a/test/unittests/compiler-dispatcher/unoptimized-compile-job-unittest.cc +++ b/test/unittests/compiler-dispatcher/unoptimized-compile-job-unittest.cc @@ -228,8 +228,7 @@ TEST_F(UnoptimizedCompileJobTest, CompileOnBackgroundThread) { base::Semaphore semaphore(0); CompileTask* background_task = new CompileTask(job.get(), &semaphore); ASSERT_JOB_STATUS(CompilerDispatcherJob::Status::kPrepared, job); - V8::GetCurrentPlatform()->CallOnBackgroundThread(background_task, - Platform::kShortRunningTask); + V8::GetCurrentPlatform()->CallOnWorkerThread(background_task); semaphore.Wait(); job->FinalizeOnMainThread(isolate()); ASSERT_FALSE(job->IsFailed());