v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
cancelable-task.cc
Go to the documentation of this file.
1// Copyright 2015 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
8
9namespace v8 {
10namespace internal {
11
13 // The following check is needed to avoid calling an already terminated
14 // manager object. This happens when the manager cancels all pending tasks
15 // in {CancelAndWait} only before destroying the manager object.
17 if (TryRun(&previous) || previous == kRunning) {
19 }
20}
21
23 : task_id_counter_(kInvalidTaskId), canceled_(false) {}
24
26 // It is required that {CancelAndWait} is called before the manager object is
27 // destroyed. This guarantees that all tasks managed by this
28 // {CancelableTaskManager} are either canceled or finished their execution
29 // when the {CancelableTaskManager} dies.
31}
32
35 if (canceled_) {
36 // The CancelableTaskManager has already been canceled. Therefore we mark
37 // the new task immediately as canceled so that it does not get executed.
38 task->Cancel();
39 return kInvalidTaskId;
40 }
42 // Id overflows are not supported.
45 cancelable_tasks_[id] = task;
46 return id;
47}
48
57
61 auto entry = cancelable_tasks_.find(id);
62 if (entry != cancelable_tasks_.end()) {
63 Cancelable* value = entry->second;
64 if (value->Cancel()) {
65 // Cannot call RemoveFinishedTask here because of recursive locking.
66 cancelable_tasks_.erase(entry);
69 } else {
71 }
72 }
74}
75
77 // Clean up all cancelable fore- and background tasks. Tasks are canceled on
78 // the way if possible, i.e., if they have not started yet. After each round
79 // of canceling we wait for the background tasks that have already been
80 // started.
82 canceled_ = true;
83
84 // Cancelable tasks could be running or could potentially register new
85 // tasks, requiring a loop here.
86 while (!cancelable_tasks_.empty()) {
87 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
88 auto current = it;
89 // We need to get to the next element before erasing the current.
90 ++it;
91 if (current->second->Cancel()) {
92 cancelable_tasks_.erase(current);
93 }
94 }
95 // Wait for already running background tasks.
96 if (!cancelable_tasks_.empty()) {
98 }
99 }
100}
101
103 // Clean up all cancelable fore- and background tasks. Tasks are canceled on
104 // the way if possible, i.e., if they have not started yet.
105 base::MutexGuard guard(&mutex_);
106
108
109 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
110 if (it->second->Cancel()) {
111 it = cancelable_tasks_.erase(it);
112 } else {
113 ++it;
114 }
115 }
116
119}
120
122 : CancelableTask(isolate->cancelable_task_manager()) {}
123
126
128 : CancelableIdleTask(isolate->cancelable_task_manager()) {}
129
132
133} // namespace internal
134} // namespace v8
std::unordered_map< Id, Cancelable * > cancelable_tasks_
base::ConditionVariable cancelable_tasks_barrier_
const CancelableTaskManager::Id id_
CancelableTaskManager *const parent_
bool TryRun(Status *previous=nullptr)
LineAndColumn previous
#define CHECK(condition)
Definition logging.h:124
#define DCHECK_NE(v1, v2)
Definition logging.h:486
#define CHECK_NE(lhs, rhs)
#define USE(...)
Definition macros.h:293