OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/cancelable-task.h" | 5 #include "src/cancelable-task.h" |
6 | 6 |
7 #include "src/base/platform/platform.h" | 7 #include "src/base/platform/platform.h" |
8 #include "src/isolate.h" | 8 #include "src/isolate.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
(...skipping 16 matching lines...) Expand all Loading... | |
27 } | 27 } |
28 | 28 |
29 CancelableTaskManager::CancelableTaskManager() | 29 CancelableTaskManager::CancelableTaskManager() |
30 : task_id_counter_(0), canceled_(false) {} | 30 : task_id_counter_(0), canceled_(false) {} |
31 | 31 |
32 uint32_t CancelableTaskManager::Register(Cancelable* task) { | 32 uint32_t CancelableTaskManager::Register(Cancelable* task) { |
33 base::LockGuard<base::Mutex> guard(&mutex_); | 33 base::LockGuard<base::Mutex> guard(&mutex_); |
34 uint32_t id = ++task_id_counter_; | 34 uint32_t id = ++task_id_counter_; |
35 // The loop below is just used when task_id_counter_ overflows. | 35 // The loop below is just used when task_id_counter_ overflows. |
36 while (cancelable_tasks_.count(id) > 0) ++id; | 36 while (cancelable_tasks_.count(id) > 0) ++id; |
37 CHECK(!canceled_); | 37 if (canceled_) { |
marja
2017/01/04 12:54:43
Why is this needed now?
Is it because we might no
| |
38 cancelable_tasks_[id] = task; | 38 CHECK(task->Cancel()); |
vogelheim
2017/01/04 12:58:08
Why are we sure that Cancel() will always return t
| |
39 } else { | |
40 cancelable_tasks_[id] = task; | |
41 } | |
39 return id; | 42 return id; |
40 } | 43 } |
41 | 44 |
42 | 45 |
43 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { | 46 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { |
44 base::LockGuard<base::Mutex> guard(&mutex_); | 47 base::LockGuard<base::Mutex> guard(&mutex_); |
45 size_t removed = cancelable_tasks_.erase(id); | 48 size_t removed = cancelable_tasks_.erase(id); |
46 USE(removed); | 49 USE(removed); |
47 DCHECK_NE(0u, removed); | 50 DCHECK_NE(0u, removed); |
48 cancelable_tasks_barrier_.NotifyOne(); | 51 cancelable_tasks_barrier_.NotifyOne(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 cancelable_tasks_.erase(current); | 89 cancelable_tasks_.erase(current); |
87 } | 90 } |
88 } | 91 } |
89 // Wait for already running background tasks. | 92 // Wait for already running background tasks. |
90 if (!cancelable_tasks_.empty()) { | 93 if (!cancelable_tasks_.empty()) { |
91 cancelable_tasks_barrier_.Wait(&mutex_); | 94 cancelable_tasks_barrier_.Wait(&mutex_); |
92 } | 95 } |
93 } | 96 } |
94 } | 97 } |
95 | 98 |
99 CancelableTaskManager::TryAbortResult CancelableTaskManager::Cancel() { | |
100 // Clean up all cancelable fore- and background tasks. Tasks are canceled on | |
101 // the way if possible, i.e., if they have not started yet. | |
102 base::LockGuard<base::Mutex> guard(&mutex_); | |
103 | |
104 if (cancelable_tasks_.empty()) return kTaskRemoved; | |
105 | |
106 canceled_ = true; | |
107 | |
108 bool tasks_running = false; | |
109 | |
110 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) { | |
111 auto current = it; | |
112 // We need to get to the next element before erasing the current. | |
113 ++it; | |
114 if (current->second->Cancel()) { | |
115 cancelable_tasks_.erase(current); | |
116 } else { | |
117 tasks_running = true; | |
118 } | |
119 } | |
120 | |
121 canceled_ = false; | |
vogelheim
2017/01/04 12:58:08
I'm not sure I understang this code...
Most Canc
jochen (gone - plz use gerrit)
2017/01/04 13:18:31
ah, fair enough. I forgot about the lock..
remove
| |
122 | |
123 return tasks_running ? kTaskRunning : kTaskAborted; | |
vogelheim
2017/01/04 12:58:08
tasks_running == !cancelable_tasks_.empty() ?
If s
| |
124 } | |
125 | |
96 CancelableTask::CancelableTask(Isolate* isolate) | 126 CancelableTask::CancelableTask(Isolate* isolate) |
97 : CancelableTask(isolate, isolate->cancelable_task_manager()) {} | 127 : CancelableTask(isolate, isolate->cancelable_task_manager()) {} |
98 | 128 |
99 CancelableTask::CancelableTask(Isolate* isolate, CancelableTaskManager* manager) | 129 CancelableTask::CancelableTask(Isolate* isolate, CancelableTaskManager* manager) |
100 : Cancelable(manager), isolate_(isolate) {} | 130 : Cancelable(manager), isolate_(isolate) {} |
101 | 131 |
102 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) | 132 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) |
103 : CancelableIdleTask(isolate, isolate->cancelable_task_manager()) {} | 133 : CancelableIdleTask(isolate, isolate->cancelable_task_manager()) {} |
104 | 134 |
105 CancelableIdleTask::CancelableIdleTask(Isolate* isolate, | 135 CancelableIdleTask::CancelableIdleTask(Isolate* isolate, |
106 CancelableTaskManager* manager) | 136 CancelableTaskManager* manager) |
107 : Cancelable(manager), isolate_(isolate) {} | 137 : Cancelable(manager), isolate_(isolate) {} |
108 | 138 |
109 } // namespace internal | 139 } // namespace internal |
110 } // namespace v8 | 140 } // namespace v8 |
OLD | NEW |