v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
default-worker-threads-task-runner.cc
Go to the documentation of this file.
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
6
9
10namespace v8 {
11namespace platform {
12
14 uint32_t thread_pool_size, TimeFunction time_function,
16 : queue_(time_function), time_function_(time_function) {
17 for (uint32_t i = 0; i < thread_pool_size; ++i) {
18 thread_pool_.push_back(std::make_unique<WorkerThread>(this, priority));
19 }
20}
21
23
27
29 {
30 base::MutexGuard guard(&lock_);
31 terminated_ = true;
33 idle_threads_.clear();
34 }
35 // Clearing the thread pool lets all worker threads join.
36 thread_pool_.clear();
37}
38
40 std::unique_ptr<Task> task, const SourceLocation& location) {
41 base::MutexGuard guard(&lock_);
42 if (terminated_) return;
43 queue_.Append(std::move(task));
44
45 if (!idle_threads_.empty()) {
46 idle_threads_.back()->Notify();
47 idle_threads_.pop_back();
48 }
49}
50
52 std::unique_ptr<Task> task, double delay_in_seconds,
53 const SourceLocation& location) {
54 base::MutexGuard guard(&lock_);
55 if (terminated_) return;
56 queue_.AppendDelayed(std::move(task), delay_in_seconds);
57
58 if (!idle_threads_.empty()) {
59 idle_threads_.back()->Notify();
60 idle_threads_.pop_back();
61 }
62}
63
65 std::unique_ptr<IdleTask> task, const SourceLocation& location) {
66 // There are no idle worker tasks.
68}
69
71 // There are no idle worker tasks.
72 return false;
73}
74
77 : Thread(
78 Options("V8 DefaultWorkerThreadsTaskRunner WorkerThread", priority)),
79 runner_(runner) {
80 CHECK(Start());
81}
82
84 condition_var_.NotifyAll();
85 Join();
86}
87
89 base::MutexGuard guard(&runner_->lock_);
90 while (true) {
91 DelayedTaskQueue::MaybeNextTask next_task = runner_->queue_.TryGetNext();
92 switch (next_task.state) {
94 runner_->lock_.Unlock();
95 next_task.task->Run();
96 runner_->lock_.Lock();
97 continue;
99 return;
101 runner_->idle_threads_.push_back(this);
102 condition_var_.Wait(&runner_->lock_);
103 continue;
105 // WaitFor unfortunately doesn't care about our fake time and will wait
106 // the 'real' amount of time, based on whatever clock the system call
107 // uses.
108 runner_->idle_threads_.push_back(this);
109 bool notified =
110 condition_var_.WaitFor(&runner_->lock_, next_task.wait_time);
111 USE(notified);
112 continue;
113 }
114 }
115}
116
118 condition_var_.NotifyAll();
119}
120
121} // namespace platform
122} // namespace v8
V8_WARN_UNUSED_RESULT bool Start()
WorkerThread(DefaultWorkerThreadsTaskRunner *runner, base::Thread::Priority priority)
void PostDelayedTaskImpl(std::unique_ptr< Task > task, double delay_in_seconds, const SourceLocation &location) override
DefaultWorkerThreadsTaskRunner(uint32_t thread_pool_size, TimeFunction time_function, base::Thread::Priority priority=base::Thread::Priority::kDefault)
void PostTaskImpl(std::unique_ptr< Task > task, const SourceLocation &location) override
std::vector< std::unique_ptr< WorkerThread > > thread_pool_
void PostIdleTaskImpl(std::unique_ptr< IdleTask > task, const SourceLocation &location) override
void Append(std::unique_ptr< Task > task)
void AppendDelayed(std::unique_ptr< Task > task, double delay_in_seconds)
size_t priority
#define UNREACHABLE()
Definition logging.h:67
#define CHECK(condition)
Definition logging.h:124
#define USE(...)
Definition macros.h:293
enum v8::platform::DelayedTaskQueue::MaybeNextTask::@82 state