cancelable-task.cc 3.17 KB
Newer Older
1 2 3 4 5 6 7
// 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.

#include "src/cancelable-task.h"

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

namespace v8 {
namespace internal {


14 15 16
Cancelable::Cancelable(CancelableTaskManager* parent)
    : parent_(parent), status_(kWaiting), id_(0), cancel_counter_(0) {
  id_ = parent->Register(this);
17 18 19
}


ulan's avatar
ulan committed
20
Cancelable::~Cancelable() {
21 22 23 24
  // 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.
  if (TryRun() || IsRunning()) {
25
    parent_->RemoveFinishedTask(id_);
26 27 28
  }
}

29 30
CancelableTaskManager::CancelableTaskManager()
    : task_id_counter_(0), canceled_(false) {}
31 32 33 34 35

uint32_t CancelableTaskManager::Register(Cancelable* task) {
  base::LockGuard<base::Mutex> guard(&mutex_);
  uint32_t id = ++task_id_counter_;
  // The loop below is just used when task_id_counter_ overflows.
36
  while (cancelable_tasks_.count(id) > 0) ++id;
37
  CHECK(!canceled_);
38
  cancelable_tasks_[id] = task;
39 40 41 42
  return id;
}


43 44
void CancelableTaskManager::RemoveFinishedTask(uint32_t id) {
  base::LockGuard<base::Mutex> guard(&mutex_);
45
  size_t removed = cancelable_tasks_.erase(id);
46
  USE(removed);
47
  DCHECK_NE(0u, removed);
48 49 50
  cancelable_tasks_barrier_.NotifyOne();
}

51 52
CancelableTaskManager::TryAbortResult CancelableTaskManager::TryAbort(
    uint32_t id) {
53
  base::LockGuard<base::Mutex> guard(&mutex_);
54 55 56
  auto entry = cancelable_tasks_.find(id);
  if (entry != cancelable_tasks_.end()) {
    Cancelable* value = entry->second;
57 58
    if (value->Cancel()) {
      // Cannot call RemoveFinishedTask here because of recursive locking.
59
      cancelable_tasks_.erase(entry);
60
      cancelable_tasks_barrier_.NotifyOne();
61 62 63
      return kTaskAborted;
    } else {
      return kTaskRunning;
64
    }
65
  }
66
  return kTaskRemoved;
67 68 69 70 71 72 73 74 75
}


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.
  base::LockGuard<base::Mutex> guard(&mutex_);
76
  canceled_ = true;
77

78 79 80 81 82 83 84 85 86
  // 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);
87 88
      }
    }
89 90
    // Wait for already running background tasks.
    if (!cancelable_tasks_.empty()) {
91 92 93 94 95 96 97 98 99 100 101 102 103
      cancelable_tasks_barrier_.Wait(&mutex_);
    }
  }
}


CancelableTask::CancelableTask(Isolate* isolate)
    : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}


CancelableIdleTask::CancelableIdleTask(Isolate* isolate)
    : Cancelable(isolate->cancelable_task_manager()), isolate_(isolate) {}

104 105
}  // namespace internal
}  // namespace v8