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

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: Fixed concurrent version including unittests 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 } 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 DCHECK(removed);
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 // If the id is still in use, try to find a new one.
42 while (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), id) != nullptr) {
Michael Lippautz 2015/11/10 18:00:05 We could just add a CHECK here for overflow. Also
43 ++id;
44 }
45 HashMap::Entry* entry =
46 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id);
47 entry->value = task;
48 return id;
49 }
50
51
52 bool CancelableTaskManager::Remove(uint32_t id) {
53 base::LockGuard<base::Mutex> guard(&mutex_);
54 Cancelable* value = reinterpret_cast<Cancelable*>(
55 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id));
56 if (value != nullptr) {
57 value->Cancel();
58 cancelable_tasks_barrier_.NotifyOne();
59 }
60 return value != nullptr;
61 }
62
63
64 void CancelableTaskManager::CancelAndWait() {
65 // Clean up all cancelable fore- and background tasks. Tasks are canceled on
66 // the way if possible, i.e., if they have not started yet. After each round
67 // of canceling we wait for the background tasks that have already been
68 // started.
69 base::LockGuard<base::Mutex> guard(&mutex_);
70
71 // HashMap does not support removing while iterating, hence keep a set of
72 // entries that are to be removed.
73 std::set<uint32_t> to_remove;
74
75 // Cancelable tasks could potentially register new tasks, requiring a loop
76 // here.
77 while (cancelable_tasks_.occupancy() > 0) {
78 for (HashMap::Entry* p = cancelable_tasks_.Start(); p != nullptr;
79 p = cancelable_tasks_.Next(p)) {
80 if (reinterpret_cast<Cancelable*>(p->value)->Cancel()) {
81 to_remove.insert(reinterpret_cast<Cancelable*>(p->value)->id());
82 }
83 }
84 // Remove tasks that were successfully canceled.
85 for (auto id : to_remove) {
86 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id);
87 }
88 to_remove.clear();
89
90 // Finally, wait for already running background tasks.
91 if (cancelable_tasks_.occupancy() > 0) {
92 cancelable_tasks_barrier_.Wait(&mutex_);
93 }
94 }
95 }
96
97
98 CancelableTask::CancelableTask(Isolate* isolate)
99 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
100
101 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
102 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
103
27 } // namespace internal 104 } // namespace internal
28 } // namespace v8 105 } // 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