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

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

Issue 1409993012: Add {CancelableTaskManager} to handle {Cancelable} concurrent tasks. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Addressed comments Created 5 years, 1 month 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
« no previous file with comments | « src/cancelable-task.h ('k') | src/global-handles.cc » ('j') | 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(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 CHECK(id_ != 0);
17 } 18 }
18 19
19 20
20 Cancelable::~Cancelable() { 21 Cancelable::~Cancelable() {
21 if (!is_cancelled_) { 22 // The following check is needed to avoid calling an already terminated
22 isolate_->RemoveCancelableTask(this); 23 // manager object. This happens when the manager cancels all pending tasks
24 // in {CancelAndWait} only before destroying the manager object.
25 if (TryRun() || IsRunning()) {
26 parent_->TryAbort(id_);
23 } 27 }
24 } 28 }
25 29
26 30
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
38 uint32_t CancelableTaskManager::Register(Cancelable* task) {
39 base::LockGuard<base::Mutex> guard(&mutex_);
40 uint32_t id = ++task_id_counter_;
41 // The loop below is just used when task_id_counter_ overflows.
42 while ((id == 0) || (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id),
43 id) != nullptr)) {
44 ++id;
45 }
46 HashMap::Entry* entry =
47 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id);
48 entry->value = task;
49 return id;
50 }
51
52
53 bool CancelableTaskManager::TryAbort(uint32_t id) {
54 base::LockGuard<base::Mutex> guard(&mutex_);
55 Cancelable* value = reinterpret_cast<Cancelable*>(
56 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id));
57 if (value != nullptr) {
58 bool success = value->Cancel();
59 cancelable_tasks_barrier_.NotifyOne();
60 if (!success) return false;
61 return true;
62 }
63 return false;
64 }
65
66
67 void CancelableTaskManager::CancelAndWait() {
68 // Clean up all cancelable fore- and background tasks. Tasks are canceled on
69 // the way if possible, i.e., if they have not started yet. After each round
70 // of canceling we wait for the background tasks that have already been
71 // started.
72 base::LockGuard<base::Mutex> guard(&mutex_);
73
74 // HashMap does not support removing while iterating, hence keep a set of
75 // entries that are to be removed.
76 std::set<uint32_t> to_remove;
77
78 // Cancelable tasks could potentially register new tasks, requiring a loop
79 // here.
80 while (cancelable_tasks_.occupancy() > 0) {
81 for (HashMap::Entry* p = cancelable_tasks_.Start(); p != nullptr;
82 p = cancelable_tasks_.Next(p)) {
83 if (reinterpret_cast<Cancelable*>(p->value)->Cancel()) {
84 to_remove.insert(reinterpret_cast<Cancelable*>(p->value)->id());
85 }
86 }
87 // Remove tasks that were successfully canceled.
88 for (auto id : to_remove) {
89 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id);
90 }
91 to_remove.clear();
92
93 // Finally, wait for already running background tasks.
94 if (cancelable_tasks_.occupancy() > 0) {
95 cancelable_tasks_barrier_.Wait(&mutex_);
96 }
97 }
98 }
99
100
101 CancelableTask::CancelableTask(Isolate* isolate)
102 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
103
104
105 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
106 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
107
27 } // namespace internal 108 } // namespace internal
28 } // namespace v8 109 } // namespace v8
OLDNEW
« no previous file with comments | « src/cancelable-task.h ('k') | src/global-handles.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698