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(Isolate* isolate) | 14 Cancelable::Cancelable(CancelableTaskManager* parent) |
15 : isolate_(isolate), is_cancelled_(false) { | 15 : parent_(parent), status_(kWaiting), id_(0), cancel_counter_(0) { |
16 isolate->RegisterCancelableTask(this); | 16 id_ = parent->Register(this); |
17 } | 17 } |
18 | 18 |
19 | 19 |
20 Cancelable::~Cancelable() { | 20 Cancelable::~Cancelable() { |
21 if (!is_cancelled_) { | 21 // Idle tasks could still be in kWaiting. Avoid race with shutdown of isolate |
22 isolate_->RemoveCancelableTask(this); | 22 // by moving the state to kRunning. |
| 23 if (TryRun() || IsRunning()) { |
| 24 bool removed = parent_->Remove(id_); |
| 25 USE(removed); |
| 26 // This is counter intuitive but correct: Removing a task also involves |
| 27 // canceling it, which is not possible anymore, since it is already in |
| 28 // state running. |
| 29 DCHECK(removed == false); |
23 } | 30 } |
24 } | 31 } |
25 | 32 |
26 | 33 |
| 34 static bool ComparePointers(void* ptr1, void* ptr2) { return ptr1 == ptr2; } |
| 35 |
| 36 |
| 37 CancelableTaskManager::CancelableTaskManager() |
| 38 : task_id_counter_(0), cancelable_tasks_(ComparePointers) {} |
| 39 |
| 40 |
| 41 uint32_t CancelableTaskManager::Register(Cancelable* task) { |
| 42 base::LockGuard<base::Mutex> guard(&mutex_); |
| 43 uint32_t id = ++task_id_counter_; |
| 44 // If the id is still in use, try to find a new one. |
| 45 while (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), id) != nullptr) { |
| 46 ++id; |
| 47 } |
| 48 HashMap::Entry* entry = |
| 49 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id); |
| 50 entry->value = task; |
| 51 return id; |
| 52 } |
| 53 |
| 54 |
| 55 bool CancelableTaskManager::Remove(uint32_t id) { |
| 56 base::LockGuard<base::Mutex> guard(&mutex_); |
| 57 Cancelable* value = reinterpret_cast<Cancelable*>( |
| 58 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id)); |
| 59 if (value != nullptr) { |
| 60 bool success = value->Cancel(); |
| 61 cancelable_tasks_barrier_.NotifyOne(); |
| 62 if (!success) return false; |
| 63 return true; |
| 64 } |
| 65 return false; |
| 66 } |
| 67 |
| 68 |
| 69 void CancelableTaskManager::CancelAndWait() { |
| 70 // Clean up all cancelable fore- and background tasks. Tasks are canceled on |
| 71 // the way if possible, i.e., if they have not started yet. After each round |
| 72 // of canceling we wait for the background tasks that have already been |
| 73 // started. |
| 74 base::LockGuard<base::Mutex> guard(&mutex_); |
| 75 |
| 76 // HashMap does not support removing while iterating, hence keep a set of |
| 77 // entries that are to be removed. |
| 78 std::set<uint32_t> to_remove; |
| 79 |
| 80 // Cancelable tasks could potentially register new tasks, requiring a loop |
| 81 // here. |
| 82 while (cancelable_tasks_.occupancy() > 0) { |
| 83 for (HashMap::Entry* p = cancelable_tasks_.Start(); p != nullptr; |
| 84 p = cancelable_tasks_.Next(p)) { |
| 85 if (reinterpret_cast<Cancelable*>(p->value)->Cancel()) { |
| 86 to_remove.insert(reinterpret_cast<Cancelable*>(p->value)->id()); |
| 87 } |
| 88 } |
| 89 // Remove tasks that were successfully canceled. |
| 90 for (auto id : to_remove) { |
| 91 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id); |
| 92 } |
| 93 to_remove.clear(); |
| 94 |
| 95 // Finally, wait for already running background tasks. |
| 96 if (cancelable_tasks_.occupancy() > 0) { |
| 97 cancelable_tasks_barrier_.Wait(&mutex_); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 |
| 103 CancelableTask::CancelableTask(Isolate* isolate) |
| 104 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} |
| 105 |
| 106 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) |
| 107 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} |
| 108 |
27 } // namespace internal | 109 } // namespace internal |
28 } // namespace v8 | 110 } // namespace v8 |
OLD | NEW |