V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
cancelable-task.cc
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 
5 #include "src/cancelable-task.h"
6 
7 #include "src/base/platform/platform.h"
8 #include "src/isolate.h"
9 
10 namespace v8 {
11 namespace internal {
12 
13 Cancelable::~Cancelable() {
14  // The following check is needed to avoid calling an already terminated
15  // manager object. This happens when the manager cancels all pending tasks
16  // in {CancelAndWait} only before destroying the manager object.
17  Status previous;
18  if (TryRun(&previous) || previous == kRunning) {
19  parent_->RemoveFinishedTask(id_);
20  }
21 }
22 
23 CancelableTaskManager::CancelableTaskManager()
24  : task_id_counter_(0), canceled_(false) {}
25 
26 CancelableTaskManager::Id CancelableTaskManager::Register(Cancelable* task) {
27  base::MutexGuard guard(&mutex_);
28  CancelableTaskManager::Id id = ++task_id_counter_;
29  // Id overflows are not supported.
30  CHECK_NE(0, id);
31  CHECK(!canceled_);
32  cancelable_tasks_[id] = task;
33  return id;
34 }
35 
36 void CancelableTaskManager::RemoveFinishedTask(CancelableTaskManager::Id id) {
37  base::MutexGuard guard(&mutex_);
38  size_t removed = cancelable_tasks_.erase(id);
39  USE(removed);
40  DCHECK_NE(0u, removed);
41  cancelable_tasks_barrier_.NotifyOne();
42 }
43 
44 TryAbortResult CancelableTaskManager::TryAbort(CancelableTaskManager::Id id) {
45  base::MutexGuard guard(&mutex_);
46  auto entry = cancelable_tasks_.find(id);
47  if (entry != cancelable_tasks_.end()) {
48  Cancelable* value = entry->second;
49  if (value->Cancel()) {
50  // Cannot call RemoveFinishedTask here because of recursive locking.
51  cancelable_tasks_.erase(entry);
52  cancelable_tasks_barrier_.NotifyOne();
53  return TryAbortResult::kTaskAborted;
54  } else {
55  return TryAbortResult::kTaskRunning;
56  }
57  }
58  return TryAbortResult::kTaskRemoved;
59 }
60 
61 void CancelableTaskManager::CancelAndWait() {
62  // Clean up all cancelable fore- and background tasks. Tasks are canceled on
63  // the way if possible, i.e., if they have not started yet. After each round
64  // of canceling we wait for the background tasks that have already been
65  // started.
66  base::MutexGuard guard(&mutex_);
67  canceled_ = true;
68 
69  // Cancelable tasks could be running or could potentially register new
70  // tasks, requiring a loop here.
71  while (!cancelable_tasks_.empty()) {
72  for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
73  auto current = it;
74  // We need to get to the next element before erasing the current.
75  ++it;
76  if (current->second->Cancel()) {
77  cancelable_tasks_.erase(current);
78  }
79  }
80  // Wait for already running background tasks.
81  if (!cancelable_tasks_.empty()) {
82  cancelable_tasks_barrier_.Wait(&mutex_);
83  }
84  }
85 }
86 
87 TryAbortResult CancelableTaskManager::TryAbortAll() {
88  // Clean up all cancelable fore- and background tasks. Tasks are canceled on
89  // the way if possible, i.e., if they have not started yet.
90  base::MutexGuard guard(&mutex_);
91 
92  if (cancelable_tasks_.empty()) return TryAbortResult::kTaskRemoved;
93 
94  for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
95  if (it->second->Cancel()) {
96  it = cancelable_tasks_.erase(it);
97  } else {
98  ++it;
99  }
100  }
101 
102  return cancelable_tasks_.empty() ? TryAbortResult::kTaskAborted
103  : TryAbortResult::kTaskRunning;
104 }
105 
106 CancelableTask::CancelableTask(Isolate* isolate)
107  : CancelableTask(isolate->cancelable_task_manager()) {}
108 
109 CancelableTask::CancelableTask(CancelableTaskManager* manager)
110  : Cancelable(manager) {}
111 
112 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
113  : CancelableIdleTask(isolate->cancelable_task_manager()) {}
114 
115 CancelableIdleTask::CancelableIdleTask(CancelableTaskManager* manager)
116  : Cancelable(manager) {}
117 
118 } // namespace internal
119 } // namespace v8
Definition: libplatform.h:13