Line data Source code
1 : // Copyright 2017 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/libplatform/default-worker-threads-task-runner.h"
6 :
7 : #include "src/libplatform/delayed-task-queue.h"
8 :
9 : namespace v8 {
10 : namespace platform {
11 :
12 60135 : DefaultWorkerThreadsTaskRunner::DefaultWorkerThreadsTaskRunner(
13 : uint32_t thread_pool_size, TimeFunction time_function)
14 120270 : : queue_(time_function), time_function_(time_function) {
15 901947 : for (uint32_t i = 0; i < thread_pool_size; ++i) {
16 841812 : thread_pool_.push_back(base::make_unique<WorkerThread>(this));
17 : }
18 60135 : }
19 :
20 : DefaultWorkerThreadsTaskRunner::~DefaultWorkerThreadsTaskRunner() = default;
21 :
22 0 : double DefaultWorkerThreadsTaskRunner::MonotonicallyIncreasingTime() {
23 0 : return time_function_();
24 : }
25 :
26 57056 : void DefaultWorkerThreadsTaskRunner::Terminate() {
27 57056 : base::MutexGuard guard(&lock_);
28 57056 : terminated_ = true;
29 57056 : queue_.Terminate();
30 : // Clearing the thread pool lets all worker threads join.
31 57056 : thread_pool_.clear();
32 57056 : }
33 :
34 1357443 : void DefaultWorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
35 1357443 : base::MutexGuard guard(&lock_);
36 1357443 : if (terminated_) return;
37 2714882 : queue_.Append(std::move(task));
38 : }
39 :
40 35 : void DefaultWorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
41 : double delay_in_seconds) {
42 35 : base::MutexGuard guard(&lock_);
43 35 : if (terminated_) return;
44 70 : queue_.AppendDelayed(std::move(task), delay_in_seconds);
45 : }
46 :
47 0 : void DefaultWorkerThreadsTaskRunner::PostIdleTask(
48 : std::unique_ptr<IdleTask> task) {
49 : // There are no idle worker tasks.
50 0 : UNREACHABLE();
51 : }
52 :
53 1 : bool DefaultWorkerThreadsTaskRunner::IdleTasksEnabled() {
54 : // There are no idle worker tasks.
55 1 : return false;
56 : }
57 :
58 0 : std::unique_ptr<Task> DefaultWorkerThreadsTaskRunner::GetNext() {
59 1776449 : return queue_.GetNext();
60 : }
61 :
62 420906 : DefaultWorkerThreadsTaskRunner::WorkerThread::WorkerThread(
63 : DefaultWorkerThreadsTaskRunner* runner)
64 : : Thread(Options("V8 DefaultWorkerThreadsTaskRunner WorkerThread")),
65 420906 : runner_(runner) {
66 420906 : Start();
67 420906 : }
68 :
69 399353 : DefaultWorkerThreadsTaskRunner::WorkerThread::~WorkerThread() { Join(); }
70 :
71 419146 : void DefaultWorkerThreadsTaskRunner::WorkerThread::Run() {
72 3532162 : while (std::unique_ptr<Task> task = runner_->GetNext()) {
73 1356395 : task->Run();
74 : }
75 399318 : }
76 :
77 : } // namespace platform
78 : } // namespace v8
|