cancelable-task.cc 4.46 KB
Newer Older
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/tasks/cancelable-task.h"
6 7

#include "src/base/platform/platform.h"
8
#include "src/execution/isolate.h"
9 10 11 12

namespace v8 {
namespace internal {

ulan's avatar
ulan committed
13
Cancelable::~Cancelable() {
14 15 16
  // The following check is needed to avoid calling an already terminated
  // manager object. This happens when the manager cancels all pending tasks
  // in {CancelAndWait} only before destroying the manager object.
17 18
  Status previous;
  if (TryRun(&previous) || previous == kRunning) {
19
    parent_->RemoveFinishedTask(id_);
20 21 22
  }
}

23
CancelableTaskManager::CancelableTaskManager()
24 25 26 27 28 29 30 31 32
    : task_id_counter_(kInvalidTaskId), canceled_(false) {}

CancelableTaskManager::~CancelableTaskManager() {
  // It is required that {CancelAndWait} is called before the manager object is
  // destroyed. This guarantees that all tasks managed by this
  // {CancelableTaskManager} are either canceled or finished their execution
  // when the {CancelableTaskManager} dies.
  CHECK(canceled_);
}
33

34
CancelableTaskManager::Id CancelableTaskManager::Register(Cancelable* task) {
35
  base::MutexGuard guard(&mutex_);
36 37 38 39 40 41
  if (canceled_) {
    // The CancelableTaskManager has already been canceled. Therefore we mark
    // the new task immediately as canceled so that it does not get executed.
    task->Cancel();
    return kInvalidTaskId;
  }
42 43
  CancelableTaskManager::Id id = ++task_id_counter_;
  // Id overflows are not supported.
44
  CHECK_NE(kInvalidTaskId, id);
45
  CHECK(!canceled_);
46
  cancelable_tasks_[id] = task;
47 48 49
  return id;
}

50
void CancelableTaskManager::RemoveFinishedTask(CancelableTaskManager::Id id) {
51
  CHECK_NE(kInvalidTaskId, id);
52
  base::MutexGuard guard(&mutex_);
53
  size_t removed = cancelable_tasks_.erase(id);
54
  USE(removed);
55
  DCHECK_NE(0u, removed);
56 57 58
  cancelable_tasks_barrier_.NotifyOne();
}

59
TryAbortResult CancelableTaskManager::TryAbort(CancelableTaskManager::Id id) {
60
  CHECK_NE(kInvalidTaskId, id);
61
  base::MutexGuard guard(&mutex_);
62 63 64
  auto entry = cancelable_tasks_.find(id);
  if (entry != cancelable_tasks_.end()) {
    Cancelable* value = entry->second;
65 66
    if (value->Cancel()) {
      // Cannot call RemoveFinishedTask here because of recursive locking.
67
      cancelable_tasks_.erase(entry);
68
      cancelable_tasks_barrier_.NotifyOne();
69
      return TryAbortResult::kTaskAborted;
70
    } else {
71
      return TryAbortResult::kTaskRunning;
72
    }
73
  }
74
  return TryAbortResult::kTaskRemoved;
75 76 77 78 79 80 81
}

void CancelableTaskManager::CancelAndWait() {
  // Clean up all cancelable fore- and background tasks. Tasks are canceled on
  // the way if possible, i.e., if they have not started yet.  After each round
  // of canceling we wait for the background tasks that have already been
  // started.
82
  base::MutexGuard guard(&mutex_);
83
  canceled_ = true;
84

85 86 87 88 89 90 91 92 93
  // Cancelable tasks could be running or could potentially register new
  // tasks, requiring a loop here.
  while (!cancelable_tasks_.empty()) {
    for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
      auto current = it;
      // We need to get to the next element before erasing the current.
      ++it;
      if (current->second->Cancel()) {
        cancelable_tasks_.erase(current);
94 95
      }
    }
96 97
    // Wait for already running background tasks.
    if (!cancelable_tasks_.empty()) {
98 99 100 101 102
      cancelable_tasks_barrier_.Wait(&mutex_);
    }
  }
}

103
TryAbortResult CancelableTaskManager::TryAbortAll() {
104 105
  // Clean up all cancelable fore- and background tasks. Tasks are canceled on
  // the way if possible, i.e., if they have not started yet.
106
  base::MutexGuard guard(&mutex_);
107

108
  if (cancelable_tasks_.empty()) return TryAbortResult::kTaskRemoved;
109 110 111 112 113 114 115 116 117

  for (auto it = cancelable_tasks_.begin(); it != cancelable_tasks_.end();) {
    if (it->second->Cancel()) {
      it = cancelable_tasks_.erase(it);
    } else {
      ++it;
    }
  }

118 119
  return cancelable_tasks_.empty() ? TryAbortResult::kTaskAborted
                                   : TryAbortResult::kTaskRunning;
120 121
}

122
CancelableTask::CancelableTask(Isolate* isolate)
123
    : CancelableTask(isolate->cancelable_task_manager()) {}
124

125 126
CancelableTask::CancelableTask(CancelableTaskManager* manager)
    : Cancelable(manager) {}
127 128

CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
129
    : CancelableIdleTask(isolate->cancelable_task_manager()) {}
130

131 132
CancelableIdleTask::CancelableIdleTask(CancelableTaskManager* manager)
    : Cancelable(manager) {}
133

134 135
}  // namespace internal
}  // namespace v8