Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(681)

Side by Side Diff: src/cancelable-task.cc

Issue 1963853004: Refactor CancelableTaskManager to use std::map. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/cancelable-task.h ('K') | « src/cancelable-task.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 CHECK(id_ != 0);
18 } 17 }
19 18
20 19
21 Cancelable::~Cancelable() { 20 Cancelable::~Cancelable() {
22 // The following check is needed to avoid calling an already terminated 21 // The following check is needed to avoid calling an already terminated
23 // manager object. This happens when the manager cancels all pending tasks 22 // manager object. This happens when the manager cancels all pending tasks
24 // in {CancelAndWait} only before destroying the manager object. 23 // in {CancelAndWait} only before destroying the manager object.
25 if (TryRun() || IsRunning()) { 24 if (TryRun() || IsRunning()) {
26 parent_->RemoveFinishedTask(id_); 25 parent_->RemoveFinishedTask(id_);
27 } 26 }
28 } 27 }
29 28
30 29 CancelableTaskManager::CancelableTaskManager() : task_id_counter_(0) {}
31 static bool ComparePointers(void* ptr1, void* ptr2) { return ptr1 == ptr2; }
32
33
34 CancelableTaskManager::CancelableTaskManager()
35 : task_id_counter_(0), cancelable_tasks_(ComparePointers) {}
36
37 30
38 uint32_t CancelableTaskManager::Register(Cancelable* task) { 31 uint32_t CancelableTaskManager::Register(Cancelable* task) {
39 base::LockGuard<base::Mutex> guard(&mutex_); 32 base::LockGuard<base::Mutex> guard(&mutex_);
40 uint32_t id = ++task_id_counter_; 33 uint32_t id = ++task_id_counter_;
41 // The loop below is just used when task_id_counter_ overflows. 34 // The loop below is just used when task_id_counter_ overflows.
42 while ((id == 0) || (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), 35 while (cancelable_tasks_.count(id) > 0) ++id;
43 id) != nullptr)) { 36 cancelable_tasks_[id] = task;
44 ++id;
45 }
46 HashMap::Entry* entry =
47 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id);
48 entry->value = task;
49 return id; 37 return id;
50 } 38 }
51 39
52 40
53 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) { 41 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) {
54 base::LockGuard<base::Mutex> guard(&mutex_); 42 base::LockGuard<base::Mutex> guard(&mutex_);
55 void* removed = cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id); 43 size_t removed = cancelable_tasks_.erase(id);
56 USE(removed); 44 USE(removed);
57 DCHECK(removed != nullptr); 45 DCHECK_NE(0, removed);
58 cancelable_tasks_barrier_.NotifyOne(); 46 cancelable_tasks_barrier_.NotifyOne();
59 } 47 }
60 48
61 49
62 bool CancelableTaskManager::TryAbort(uint32_t id) { 50 bool CancelableTaskManager::TryAbort(uint32_t id) {
63 base::LockGuard<base::Mutex> guard(&mutex_); 51 base::LockGuard<base::Mutex> guard(&mutex_);
64 HashMap::Entry* entry = 52 auto entry = cancelable_tasks_.find(id);
65 cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), id); 53 if (entry != cancelable_tasks_.end()) {
66 if (entry != nullptr) { 54 Cancelable* value = entry->second;
67 Cancelable* value = reinterpret_cast<Cancelable*>(entry->value);
68 if (value->Cancel()) { 55 if (value->Cancel()) {
69 // Cannot call RemoveFinishedTask here because of recursive locking. 56 // Cannot call RemoveFinishedTask here because of recursive locking.
70 void* removed = cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id); 57 cancelable_tasks_.erase(entry);
71 USE(removed);
72 DCHECK(removed != nullptr);
73 cancelable_tasks_barrier_.NotifyOne(); 58 cancelable_tasks_barrier_.NotifyOne();
74 return true; 59 return true;
75 } 60 }
76 } 61 }
77 return false; 62 return false;
78 } 63 }
79 64
80 65
81 void CancelableTaskManager::CancelAndWait() { 66 void CancelableTaskManager::CancelAndWait() {
82 // 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
83 // 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
84 // 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
85 // started. 70 // started.
86 base::LockGuard<base::Mutex> guard(&mutex_); 71 base::LockGuard<base::Mutex> guard(&mutex_);
87 72
88 // HashMap does not support removing while iterating, hence keep a set of 73 // Cancelable tasks could be running or could potentially register new
89 // entries that are to be removed. 74 // tasks, requiring a loop here.
90 std::set<uint32_t> to_remove; 75 while (!cancelable_tasks_.empty()) {
91 76 for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
92 // Cancelable tasks could potentially register new tasks, requiring a loop 77 auto current = it;
93 // here. 78 // We need to get to the next element before erasing the current.
94 while (cancelable_tasks_.occupancy() > 0) { 79 ++it;
95 for (HashMap::Entry* p = cancelable_tasks_.Start(); p != nullptr; 80 if (current->second->Cancel()) {
96 p = cancelable_tasks_.Next(p)) { 81 cancelable_tasks_.erase(current);
97 if (reinterpret_cast<Cancelable*>(p->value)->Cancel()) {
98 to_remove.insert(reinterpret_cast<Cancelable*>(p->value)->id());
99 } 82 }
100 } 83 }
101 // Remove tasks that were successfully canceled. 84 // Wait for already running background tasks.
102 for (auto id : to_remove) { 85 if (!cancelable_tasks_.empty()) {
103 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id);
104 }
105 to_remove.clear();
106
107 // Finally, wait for already running background tasks.
108 if (cancelable_tasks_.occupancy() > 0) {
109 cancelable_tasks_barrier_.Wait(&mutex_); 86 cancelable_tasks_barrier_.Wait(&mutex_);
110 } 87 }
111 } 88 }
112 } 89 }
113 90
114 91
115 CancelableTask::CancelableTask(Isolate* isolate) 92 CancelableTask::CancelableTask(Isolate* isolate)
116 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} 93 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
117 94
118 95
119 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) 96 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
120 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} 97 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
121 98
122 } // namespace internal 99 } // namespace internal
123 } // namespace v8 100 } // namespace v8
OLDNEW
« src/cancelable-task.h ('K') | « src/cancelable-task.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698