V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
default-worker-threads-task-runner.cc
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/base/platform/mutex.h"
8 #include "src/libplatform/worker-thread.h"
9 
10 namespace v8 {
11 namespace platform {
12 
13 DefaultWorkerThreadsTaskRunner::DefaultWorkerThreadsTaskRunner(
14  uint32_t thread_pool_size) {
15  for (uint32_t i = 0; i < thread_pool_size; ++i) {
16  thread_pool_.push_back(base::make_unique<WorkerThread>(&queue_));
17  }
18 }
19 
20 // NOLINTNEXTLINE
21 DefaultWorkerThreadsTaskRunner::~DefaultWorkerThreadsTaskRunner() {
22  // This destructor is needed because we have unique_ptr to the WorkerThreads,
23  // und the {WorkerThread} class is forward declared in the header file.
24 }
25 
26 void DefaultWorkerThreadsTaskRunner::Terminate() {
27  base::MutexGuard guard(&lock_);
28  terminated_ = true;
29  queue_.Terminate();
30  // Clearing the thread pool lets all worker threads join.
31  thread_pool_.clear();
32 }
33 
34 void DefaultWorkerThreadsTaskRunner::PostTask(std::unique_ptr<Task> task) {
35  base::MutexGuard guard(&lock_);
36  if (terminated_) return;
37  queue_.Append(std::move(task));
38 }
39 
40 void DefaultWorkerThreadsTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
41  double delay_in_seconds) {
42  base::MutexGuard guard(&lock_);
43  if (terminated_) return;
44  if (delay_in_seconds == 0) {
45  queue_.Append(std::move(task));
46  return;
47  }
48  // There is no use case for this function with non zero delay_in_second on a
49  // worker thread at the moment, but it is still part of the interface.
50  UNIMPLEMENTED();
51 }
52 
53 void DefaultWorkerThreadsTaskRunner::PostIdleTask(
54  std::unique_ptr<IdleTask> task) {
55  // There are no idle worker tasks.
56  UNREACHABLE();
57 }
58 
59 bool DefaultWorkerThreadsTaskRunner::IdleTasksEnabled() {
60  // There are no idle worker tasks.
61  return false;
62 }
63 
64 } // namespace platform
65 } // namespace v8
Definition: libplatform.h:13