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