11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved. 21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 31cb0ef41Sopenharmony_ci// found in the LICENSE file. 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci#include "src/libplatform/default-worker-threads-task-runner.h" 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci#include "src/libplatform/delayed-task-queue.h" 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cinamespace v8 { 101cb0ef41Sopenharmony_cinamespace platform { 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciDefaultWorkerThreadsTaskRunner::DefaultWorkerThreadsTaskRunner( 131cb0ef41Sopenharmony_ci uint32_t thread_pool_size, TimeFunction time_function) 141cb0ef41Sopenharmony_ci : queue_(time_function), time_function_(time_function) { 151cb0ef41Sopenharmony_ci for (uint32_t i = 0; i < thread_pool_size; ++i) { 161cb0ef41Sopenharmony_ci thread_pool_.push_back(std::make_unique<WorkerThread>(this)); 171cb0ef41Sopenharmony_ci } 181cb0ef41Sopenharmony_ci} 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ciDefaultWorkerThreadsTaskRunner::~DefaultWorkerThreadsTaskRunner() = default; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_cidouble DefaultWorkerThreadsTaskRunner::MonotonicallyIncreasingTime() { 231cb0ef41Sopenharmony_ci return time_function_(); 241cb0ef41Sopenharmony_ci} 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_civoid DefaultWorkerThreadsTaskRunner::Terminate() { 271cb0ef41Sopenharmony_ci base::MutexGuard guard(&lock_); 281cb0ef41Sopenharmony_ci terminated_ = true; 291cb0ef41Sopenharmony_ci queue_.Terminate(); 301cb0ef41Sopenharmony_ci // Clearing the thread pool lets all worker threads join. 311cb0ef41Sopenharmony_ci thread_pool_.clear(); 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_civoid DefaultWorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) { 351cb0ef41Sopenharmony_ci base::MutexGuard guard(&lock_); 361cb0ef41Sopenharmony_ci if (terminated_) return; 371cb0ef41Sopenharmony_ci queue_.Append(std::move(task)); 381cb0ef41Sopenharmony_ci} 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_civoid DefaultWorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task, 411cb0ef41Sopenharmony_ci double delay_in_seconds) { 421cb0ef41Sopenharmony_ci base::MutexGuard guard(&lock_); 431cb0ef41Sopenharmony_ci if (terminated_) return; 441cb0ef41Sopenharmony_ci queue_.AppendDelayed(std::move(task), delay_in_seconds); 451cb0ef41Sopenharmony_ci} 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_civoid DefaultWorkerThreadsTaskRunner::PostIdleTask( 481cb0ef41Sopenharmony_ci std::unique_ptr<IdleTask> task) { 491cb0ef41Sopenharmony_ci // There are no idle worker tasks. 501cb0ef41Sopenharmony_ci UNREACHABLE(); 511cb0ef41Sopenharmony_ci} 521cb0ef41Sopenharmony_ci 531cb0ef41Sopenharmony_cibool DefaultWorkerThreadsTaskRunner::IdleTasksEnabled() { 541cb0ef41Sopenharmony_ci // There are no idle worker tasks. 551cb0ef41Sopenharmony_ci return false; 561cb0ef41Sopenharmony_ci} 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_cistd::unique_ptr<Task> DefaultWorkerThreadsTaskRunner::GetNext() { 591cb0ef41Sopenharmony_ci return queue_.GetNext(); 601cb0ef41Sopenharmony_ci} 611cb0ef41Sopenharmony_ci 621cb0ef41Sopenharmony_ciDefaultWorkerThreadsTaskRunner::WorkerThread::WorkerThread( 631cb0ef41Sopenharmony_ci DefaultWorkerThreadsTaskRunner* runner) 641cb0ef41Sopenharmony_ci : Thread(Options("V8 DefaultWorkerThreadsTaskRunner WorkerThread")), 651cb0ef41Sopenharmony_ci runner_(runner) { 661cb0ef41Sopenharmony_ci CHECK(Start()); 671cb0ef41Sopenharmony_ci} 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ciDefaultWorkerThreadsTaskRunner::WorkerThread::~WorkerThread() { Join(); } 701cb0ef41Sopenharmony_ci 711cb0ef41Sopenharmony_civoid DefaultWorkerThreadsTaskRunner::WorkerThread::Run() { 721cb0ef41Sopenharmony_ci while (std::unique_ptr<Task> task = runner_->GetNext()) { 731cb0ef41Sopenharmony_ci task->Run(); 741cb0ef41Sopenharmony_ci } 751cb0ef41Sopenharmony_ci} 761cb0ef41Sopenharmony_ci 771cb0ef41Sopenharmony_ci} // namespace platform 781cb0ef41Sopenharmony_ci} // namespace v8 79