| 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 { |
| 11 namespace internal { | 11 namespace internal { |
| 12 | 12 |
| 13 | 13 |
| 14 Cancelable::Cancelable(CancelableTaskManager* parent) | 14 Cancelable::Cancelable(CancelableTaskManager* parent) |
| 15 : parent_(parent), status_(kWaiting), id_(0), cancel_counter_(0) { | 15 : parent_(parent), status_(kWaiting), id_(0), cancel_counter_(0) { |
| 16 id_ = parent->Register(this); | 16 id_ = parent->Register(this); |
| 17 } | 17 } |
| 18 | 18 |
| 19 | 19 |
| 20 Cancelable::~Cancelable() { | 20 Cancelable::~Cancelable() { |
| 21 // The following check is needed to avoid calling an already terminated | 21 // The following check is needed to avoid calling an already terminated |
| 22 // manager object. This happens when the manager cancels all pending tasks | 22 // manager object. This happens when the manager cancels all pending tasks |
| 23 // in {CancelAndWait} only before destroying the manager object. | 23 // in {CancelAndWait} only before destroying the manager object. |
| 24 if (TryRun() || IsRunning()) { | 24 if (TryRun() || IsRunning()) { |
| 25 parent_->RemoveFinishedTask(id_); | 25 parent_->RemoveFinishedTask(id_); |
| 26 } | 26 } |
| 27 } | 27 } |
| 28 | 28 |
| 29 CancelableTaskManager::CancelableTaskManager() | 29 CancelableTaskManager::CancelableTaskManager() : task_id_counter_(0) {} |
| 30 : task_id_counter_(0), canceled_(false) {} | |
| 31 | 30 |
| 32 uint32_t CancelableTaskManager::Register(Cancelable* task) { | 31 uint32_t CancelableTaskManager::Register(Cancelable* task) { |
| 33 base::LockGuard<base::Mutex> guard(&mutex_); | 32 base::LockGuard<base::Mutex> guard(&mutex_); |
| 34 uint32_t id = ++task_id_counter_; | 33 uint32_t id = ++task_id_counter_; |
| 35 // The loop below is just used when task_id_counter_ overflows. | 34 // The loop below is just used when task_id_counter_ overflows. |
| 36 while (cancelable_tasks_.count(id) > 0) ++id; | 35 while (cancelable_tasks_.count(id) > 0) ++id; |
| 37 if (canceled_) { | 36 cancelable_tasks_[id] = task; |
| 38 bool successfully_canceled_task = task->Cancel(); | |
| 39 CHECK(successfully_canceled_task); | |
| 40 } else { | |
| 41 cancelable_tasks_[id] = task; | |
| 42 } | |
| 43 return id; | 37 return id; |
| 44 } | 38 } |
| 45 | 39 |
| 46 | 40 |
| 47 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { | 41 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { |
| 48 base::LockGuard<base::Mutex> guard(&mutex_); | 42 base::LockGuard<base::Mutex> guard(&mutex_); |
| 49 size_t removed = cancelable_tasks_.erase(id); | 43 size_t removed = cancelable_tasks_.erase(id); |
| 50 USE(removed); | 44 USE(removed); |
| 51 DCHECK_NE(0, removed); | 45 DCHECK_NE(0, removed); |
| 52 cancelable_tasks_barrier_.NotifyOne(); | 46 cancelable_tasks_barrier_.NotifyOne(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 68 return false; | 62 return false; |
| 69 } | 63 } |
| 70 | 64 |
| 71 | 65 |
| 72 void CancelableTaskManager::CancelAndWait() { | 66 void CancelableTaskManager::CancelAndWait() { |
| 73 // Clean up all cancelable fore- and background tasks. Tasks are canceled on | 67 // Clean up all cancelable fore- and background tasks. Tasks are canceled on |
| 74 // the way if possible, i.e., if they have not started yet. After each round | 68 // the way if possible, i.e., if they have not started yet. After each round |
| 75 // of canceling we wait for the background tasks that have already been | 69 // of canceling we wait for the background tasks that have already been |
| 76 // started. | 70 // started. |
| 77 base::LockGuard<base::Mutex> guard(&mutex_); | 71 base::LockGuard<base::Mutex> guard(&mutex_); |
| 78 canceled_ = true; | |
| 79 | 72 |
| 80 // Cancelable tasks could be running or could potentially register new | 73 // Cancelable tasks could be running or could potentially register new |
| 81 // tasks, requiring a loop here. | 74 // tasks, requiring a loop here. |
| 82 while (!cancelable_tasks_.empty()) { | 75 while (!cancelable_tasks_.empty()) { |
| 83 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) { | 76 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) { |
| 84 auto current = it; | 77 auto current = it; |
| 85 // We need to get to the next element before erasing the current. | 78 // We need to get to the next element before erasing the current. |
| 86 ++it; | 79 ++it; |
| 87 if (current->second->Cancel()) { | 80 if (current->second->Cancel()) { |
| 88 cancelable_tasks_.erase(current); | 81 cancelable_tasks_.erase(current); |
| 89 } | 82 } |
| 90 } | 83 } |
| 91 // Wait for already running background tasks. | 84 // Wait for already running background tasks. |
| 92 if (!cancelable_tasks_.empty()) { | 85 if (!cancelable_tasks_.empty()) { |
| 93 cancelable_tasks_barrier_.Wait(&mutex_); | 86 cancelable_tasks_barrier_.Wait(&mutex_); |
| 94 } | 87 } |
| 95 } | 88 } |
| 96 } | 89 } |
| 97 | 90 |
| 98 | 91 |
| 99 CancelableTask::CancelableTask(Isolate* isolate) | 92 CancelableTask::CancelableTask(Isolate* isolate) |
| 100 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} | 93 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} |
| 101 | 94 |
| 102 | 95 |
| 103 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) | 96 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) |
| 104 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} | 97 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} |
| 105 | 98 |
| 106 } // namespace internal | 99 } // namespace internal |
| 107 } // namespace v8 | 100 } // namespace v8 |
| OLD | NEW |