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

#ifndef V8_CANCELABLE_TASK_H_
#define V8_CANCELABLE_TASK_H_

#include "include/v8-platform.h"
9
#include "src/atomic-utils.h"
10
#include "src/base/macros.h"
11 12
#include "src/base/platform/condition-variable.h"
#include "src/hashmap.h"
13 14 15 16

namespace v8 {
namespace internal {

17
class Cancelable;
18 19
class Isolate;

ulan's avatar
ulan committed
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
// Keeps track of cancelable tasks. It is possible to register and remove tasks
// from any fore- and background task/thread.
class CancelableTaskManager {
 public:
  CancelableTaskManager();

  // Registers a new cancelable {task}. Returns the unique {id} of the task that
  // can be used to try to abort a task by calling {Abort}.
  uint32_t Register(Cancelable* task);

  // Try to abort running a task identified by {id}. The possible outcomes are:
  // (1) The task is already finished running and thus has been removed from
  //     the manager.
  // (2) The task is currently running and cannot be canceled anymore.
  // (3) The task is not yet running (or finished) so it is canceled and
  //     removed.
  //
  // Returns {false} for (1) and (2), and {true} for (3).
  bool TryAbort(uint32_t id);

  // Cancels all remaining registered tasks and waits for tasks that are
  // already running.
  void CancelAndWait();

 private:
46 47 48 49
  // Only called by {Cancelable} destructor. The task is done with executing,
  // but needs to be removed.
  void RemoveFinishedTask(uint32_t id);

50 51 52 53 54 55 56 57 58 59 60
  // To mitigate the ABA problem, the api refers to tasks through an id.
  uint32_t task_id_counter_;

  // A set of cancelable tasks that are currently registered.
  HashMap cancelable_tasks_;

  // Mutex and condition variable enabling concurrent register and removing, as
  // well as waiting for background tasks on {CancelAndWait}.
  base::ConditionVariable cancelable_tasks_barrier_;
  base::Mutex mutex_;

61 62
  friend class Cancelable;

63 64 65 66
  DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager);
};


ulan's avatar
ulan committed
67
class Cancelable {
68
 public:
69
  explicit Cancelable(CancelableTaskManager* parent);
ulan's avatar
ulan committed
70 71
  virtual ~Cancelable();

72 73 74 75 76 77
  // Never invoke after handing over the task to the platform! The reason is
  // that {Cancelable} is used in combination with {v8::Task} and handed to
  // a platform. This step transfers ownership to the platform, which destroys
  // the task after running it. Since the exact time is not known, we cannot
  // access the object after handing it to a platform.
  uint32_t id() { return id_; }
ulan's avatar
ulan committed
78 79

 protected:
80 81 82
  bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); }
  bool IsRunning() { return status_.Value() == kRunning; }
  intptr_t CancelAttempts() { return cancel_counter_.Value(); }
ulan's avatar
ulan committed
83 84

 private:
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
  // Identifies the state a cancelable task is in:
  // |kWaiting|: The task is scheduled and waiting to be executed. {TryRun} will
  //   succeed.
  // |kCanceled|: The task has been canceled. {TryRun} will fail.
  // |kRunning|: The task is currently running and cannot be canceled anymore.
  enum Status {
    kWaiting,
    kCanceled,
    kRunning,
  };

  // Use {CancelableTaskManager} to abort a task that has not yet been
  // executed.
  bool Cancel() {
    if (status_.TrySetValue(kWaiting, kCanceled)) {
      return true;
    }
    cancel_counter_.Increment(1);
    return false;
  }

  CancelableTaskManager* parent_;
  AtomicValue<Status> status_;
  uint32_t id_;

  // The counter is incremented for failing tries to cancel a task. This can be
  // used by the task itself as an indication how often external entities tried
  // to abort it.
  AtomicNumber<intptr_t> cancel_counter_;

  friend class CancelableTaskManager;

ulan's avatar
ulan committed
117 118
  DISALLOW_COPY_AND_ASSIGN(Cancelable);
};
119 120


ulan's avatar
ulan committed
121 122 123
// Multiple inheritance can be used because Task is a pure interface.
class CancelableTask : public Cancelable, public Task {
 public:
124
  explicit CancelableTask(Isolate* isolate);
ulan's avatar
ulan committed
125 126

  // Task overrides.
127
  void Run() final {
128
    if (TryRun()) {
129 130 131 132 133 134
      RunInternal();
    }
  }

  virtual void RunInternal() = 0;

135 136
  Isolate* isolate() { return isolate_; }

137
 private:
138
  Isolate* isolate_;
139 140 141
  DISALLOW_COPY_AND_ASSIGN(CancelableTask);
};

ulan's avatar
ulan committed
142 143 144 145

// Multiple inheritance can be used because IdleTask is a pure interface.
class CancelableIdleTask : public Cancelable, public IdleTask {
 public:
146
  explicit CancelableIdleTask(Isolate* isolate);
ulan's avatar
ulan committed
147 148 149

  // IdleTask overrides.
  void Run(double deadline_in_seconds) final {
150
    if (TryRun()) {
ulan's avatar
ulan committed
151 152 153 154 155 156
      RunInternal(deadline_in_seconds);
    }
  }

  virtual void RunInternal(double deadline_in_seconds) = 0;

157 158
  Isolate* isolate() { return isolate_; }

ulan's avatar
ulan committed
159
 private:
160
  Isolate* isolate_;
ulan's avatar
ulan committed
161 162 163 164
  DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask);
};


165 166 167 168
}  // namespace internal
}  // namespace v8

#endif  // V8_CANCELABLE_TASK_H_