5 #include "src/cancelable-task.h" 7 #include "src/base/platform/platform.h" 8 #include "src/isolate.h" 13 Cancelable::~Cancelable() {
18 if (TryRun(&previous) || previous == kRunning) {
19 parent_->RemoveFinishedTask(id_);
23 CancelableTaskManager::CancelableTaskManager()
24 : task_id_counter_(0), canceled_(false) {}
26 CancelableTaskManager::Id CancelableTaskManager::Register(Cancelable* task) {
27 base::MutexGuard guard(&mutex_);
28 CancelableTaskManager::Id
id = ++task_id_counter_;
32 cancelable_tasks_[id] = task;
36 void CancelableTaskManager::RemoveFinishedTask(CancelableTaskManager::Id
id) {
37 base::MutexGuard guard(&mutex_);
38 size_t removed = cancelable_tasks_.erase(
id);
40 DCHECK_NE(0u, removed);
41 cancelable_tasks_barrier_.NotifyOne();
44 TryAbortResult CancelableTaskManager::TryAbort(CancelableTaskManager::Id
id) {
45 base::MutexGuard guard(&mutex_);
46 auto entry = cancelable_tasks_.find(
id);
47 if (entry != cancelable_tasks_.end()) {
48 Cancelable* value = entry->second;
49 if (value->Cancel()) {
51 cancelable_tasks_.erase(entry);
52 cancelable_tasks_barrier_.NotifyOne();
53 return TryAbortResult::kTaskAborted;
55 return TryAbortResult::kTaskRunning;
58 return TryAbortResult::kTaskRemoved;
61 void CancelableTaskManager::CancelAndWait() {
66 base::MutexGuard guard(&mutex_);
71 while (!cancelable_tasks_.empty()) {
72 for (
auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
76 if (current->second->Cancel()) {
77 cancelable_tasks_.erase(current);
81 if (!cancelable_tasks_.empty()) {
82 cancelable_tasks_barrier_.Wait(&mutex_);
87 TryAbortResult CancelableTaskManager::TryAbortAll() {
90 base::MutexGuard guard(&mutex_);
92 if (cancelable_tasks_.empty())
return TryAbortResult::kTaskRemoved;
94 for (
auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
95 if (it->second->Cancel()) {
96 it = cancelable_tasks_.erase(it);
102 return cancelable_tasks_.empty() ? TryAbortResult::kTaskAborted
103 : TryAbortResult::kTaskRunning;
106 CancelableTask::CancelableTask(Isolate* isolate)
107 : CancelableTask(isolate->cancelable_task_manager()) {}
109 CancelableTask::CancelableTask(CancelableTaskManager* manager)
110 : Cancelable(manager) {}
112 CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
113 : CancelableIdleTask(isolate->cancelable_task_manager()) {}
115 CancelableIdleTask::CancelableIdleTask(CancelableTaskManager* manager)
116 : Cancelable(manager) {}