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

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

Issue 1460763004: Fix protocol for aborting and waiting for cancelable 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') | 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); 17 CHECK(id_ != 0);
18 } 18 }
19 19
20 20
21 Cancelable::~Cancelable() { 21 Cancelable::~Cancelable() {
22 // The following check is needed to avoid calling an already terminated 22 // The following check is needed to avoid calling an already terminated
23 // manager object. This happens when the manager cancels all pending tasks 23 // manager object. This happens when the manager cancels all pending tasks
24 // in {CancelAndWait} only before destroying the manager object. 24 // in {CancelAndWait} only before destroying the manager object.
25 if (TryRun() || IsRunning()) { 25 if (TryRun() || IsRunning()) {
26 parent_->TryAbort(id_); 26 parent_->RemoveFinishedTask(id_);
27 } 27 }
28 } 28 }
29 29
30 30
31 static bool ComparePointers(void* ptr1, void* ptr2) { return ptr1 == ptr2; } 31 static bool ComparePointers(void* ptr1, void* ptr2) { return ptr1 == ptr2; }
32 32
33 33
34 CancelableTaskManager::CancelableTaskManager() 34 CancelableTaskManager::CancelableTaskManager()
35 : task_id_counter_(0), cancelable_tasks_(ComparePointers) {} 35 : task_id_counter_(0), cancelable_tasks_(ComparePointers) {}
36 36
37 37
38 uint32_t CancelableTaskManager::Register(Cancelable* task) { 38 uint32_t CancelableTaskManager::Register(Cancelable* task) {
39 base::LockGuard<base::Mutex> guard(&mutex_); 39 base::LockGuard<base::Mutex> guard(&mutex_);
40 uint32_t id = ++task_id_counter_; 40 uint32_t id = ++task_id_counter_;
41 // The loop below is just used when task_id_counter_ overflows. 41 // The loop below is just used when task_id_counter_ overflows.
42 while ((id == 0) || (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), 42 while ((id == 0) || (cancelable_tasks_.Lookup(reinterpret_cast<void*>(id),
43 id) != nullptr)) { 43 id) != nullptr)) {
44 ++id; 44 ++id;
45 } 45 }
46 HashMap::Entry* entry = 46 HashMap::Entry* entry =
47 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id); 47 cancelable_tasks_.LookupOrInsert(reinterpret_cast<void*>(id), id);
48 entry->value = task; 48 entry->value = task;
49 return id; 49 return id;
50 } 50 }
51 51
52 52
53 void CancelableTaskManager::RemoveFinishedTask(uint32_t id) {
54 base::LockGuard<base::Mutex> guard(&mutex_);
55 void* removed = cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id);
56 USE(removed);
57 DCHECK(removed != nullptr);
58 cancelable_tasks_barrier_.NotifyOne();
59 }
60
61
53 bool CancelableTaskManager::TryAbort(uint32_t id) { 62 bool CancelableTaskManager::TryAbort(uint32_t id) {
54 base::LockGuard<base::Mutex> guard(&mutex_); 63 base::LockGuard<base::Mutex> guard(&mutex_);
55 Cancelable* value = reinterpret_cast<Cancelable*>( 64 HashMap::Entry* entry =
56 cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id)); 65 cancelable_tasks_.Lookup(reinterpret_cast<void*>(id), id);
57 if (value != nullptr) { 66 if (entry != nullptr) {
58 bool success = value->Cancel(); 67 Cancelable* value = reinterpret_cast<Cancelable*>(entry->value);
59 cancelable_tasks_barrier_.NotifyOne(); 68 if (value->Cancel()) {
60 if (!success) return false; 69 // Cannot call RemoveFinishedTask here because of recursive locking.
61 return true; 70 void* removed = cancelable_tasks_.Remove(reinterpret_cast<void*>(id), id);
71 USE(removed);
72 DCHECK(removed != nullptr);
73 cancelable_tasks_barrier_.NotifyOne();
74 return true;
75 }
62 } 76 }
63 return false; 77 return false;
64 } 78 }
65 79
66 80
67 void CancelableTaskManager::CancelAndWait() { 81 void CancelableTaskManager::CancelAndWait() {
68 // Clean up all cancelable fore- and background tasks. Tasks are canceled on 82 // 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 83 // 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 84 // of canceling we wait for the background tasks that have already been
71 // started. 85 // started.
(...skipping 28 matching lines...) Expand all
100 114
101 CancelableTask::CancelableTask(Isolate* isolate) 115 CancelableTask::CancelableTask(Isolate* isolate)
102 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} 116 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
103 117
104 118
105 CancelableIdleTask::CancelableIdleTask(Isolate* isolate) 119 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
106 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {} 120 : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}
107 121
108 } // namespace internal 122 } // namespace internal
109 } // namespace v8 123 } // namespace v8
OLDNEW
« no previous file with comments | « src/cancelable-task.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698