summaryrefslogtreecommitdiff
path: root/deps/v8/src/libplatform/default-background-task-runner.cc
blob: b556b6c3feca1f037505cd73a72007e6d524b1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/libplatform/default-background-task-runner.h"

#include "src/base/platform/mutex.h"
#include "src/libplatform/worker-thread.h"

namespace v8 {
namespace platform {

DefaultBackgroundTaskRunner::DefaultBackgroundTaskRunner(
    uint32_t thread_pool_size) {
  for (uint32_t i = 0; i < thread_pool_size; ++i) {
    thread_pool_.push_back(base::make_unique<WorkerThread>(&queue_));
  }
}

DefaultBackgroundTaskRunner::~DefaultBackgroundTaskRunner() {
  // This destructor is needed because we have unique_ptr to the WorkerThreads,
  // und the {WorkerThread} class is forward declared in the header file.
}

void DefaultBackgroundTaskRunner::Terminate() {
  base::LockGuard<base::Mutex> guard(&lock_);
  terminated_ = true;
  queue_.Terminate();
  // Clearing the thread pool lets all worker threads join.
  thread_pool_.clear();
}

void DefaultBackgroundTaskRunner::PostTask(std::unique_ptr<Task> task) {
  base::LockGuard<base::Mutex> guard(&lock_);
  if (terminated_) return;
  queue_.Append(std::move(task));
}

void DefaultBackgroundTaskRunner::PostDelayedTask(std::unique_ptr<Task> task,
                                                  double delay_in_seconds) {
  base::LockGuard<base::Mutex> guard(&lock_);
  if (terminated_) return;
  // There is no use case for this function on a background thread at the
  // moment, but it is still part of the interface.
  UNIMPLEMENTED();
}

void DefaultBackgroundTaskRunner::PostIdleTask(std::unique_ptr<IdleTask> task) {
  // There are no idle background tasks.
  UNREACHABLE();
}

bool DefaultBackgroundTaskRunner::IdleTasksEnabled() {
  // There are no idle background tasks.
  return false;
}

}  // namespace platform
}  // namespace v8