cancelable-task.h 5.35 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.

#ifndef V8_CANCELABLE_TASK_H_
#define V8_CANCELABLE_TASK_H_

8
#include <unordered_map>
9

10
#include "include/v8-platform.h"
lpy's avatar
lpy committed
11
#include "src/base/atomic-utils.h"
12
#include "src/base/macros.h"
13
#include "src/base/platform/condition-variable.h"
14
#include "src/globals.h"
15 16 17 18

namespace v8 {
namespace internal {

19
class Cancelable;
20 21
class Isolate;

ulan's avatar
ulan committed
22

23 24
// Keeps track of cancelable tasks. It is possible to register and remove tasks
// from any fore- and background task/thread.
25
class V8_EXPORT_PRIVATE CancelableTaskManager {
26
 public:
27 28
  using Id = uint64_t;

29 30 31 32
  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}.
33
  // Must not be called after CancelAndWait.
34
  Id Register(Cancelable* task);
35 36

  // Try to abort running a task identified by {id}. The possible outcomes are:
37 38
  // (1) The task is already finished running or was canceled before and
  //     thus has been removed from the manager.
39 40 41 42
  // (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.
  //
43
  enum TryAbortResult { kTaskRemoved, kTaskRunning, kTaskAborted };
44
  TryAbortResult TryAbort(Id id);
45 46

  // Cancels all remaining registered tasks and waits for tasks that are
47
  // already running. This disallows subsequent Register calls.
48 49
  void CancelAndWait();

50 51 52 53 54 55 56 57 58 59 60
  // Tries to cancel all remaining registered tasks. The return value indicates
  // whether
  //
  // 1) No tasks were registered (kTaskRemoved), or
  //
  // 2) There is at least one remaining task that couldn't be cancelled
  // (kTaskRunning), or
  //
  // 3) All registered tasks were cancelled (kTaskAborted).
  TryAbortResult TryAbortAll();

61
 private:
62 63
  // Only called by {Cancelable} destructor. The task is done with executing,
  // but needs to be removed.
64
  void RemoveFinishedTask(Id id);
65

66
  // To mitigate the ABA problem, the api refers to tasks through an id.
67
  Id task_id_counter_;
68 69

  // A set of cancelable tasks that are currently registered.
70
  std::unordered_map<Id, Cancelable*> cancelable_tasks_;
71 72 73 74 75 76

  // 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_;

77 78
  bool canceled_;

79 80
  friend class Cancelable;

81 82 83
  DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager);
};

84
class V8_EXPORT_PRIVATE Cancelable {
85
 public:
86
  explicit Cancelable(CancelableTaskManager* parent);
ulan's avatar
ulan committed
87 88
  virtual ~Cancelable();

89 90 91 92 93
  // 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.
94
  CancelableTaskManager::Id id() { return id_; }
ulan's avatar
ulan committed
95 96

 protected:
97 98
  bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); }
  bool IsRunning() { return status_.Value() == kRunning; }
Hannes Payer's avatar
Hannes Payer committed
99
  intptr_t CancelAttempts() { return cancel_counter_; }
ulan's avatar
ulan committed
100 101

 private:
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
  // 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;
    }
Hannes Payer's avatar
Hannes Payer committed
119
    cancel_counter_++;
120 121 122 123
    return false;
  }

  CancelableTaskManager* parent_;
lpy's avatar
lpy committed
124
  base::AtomicValue<Status> status_;
125
  CancelableTaskManager::Id id_;
126 127 128 129

  // 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.
Hannes Payer's avatar
Hannes Payer committed
130
  std::atomic<intptr_t> cancel_counter_;
131 132 133

  friend class CancelableTaskManager;

ulan's avatar
ulan committed
134 135
  DISALLOW_COPY_AND_ASSIGN(Cancelable);
};
136 137


ulan's avatar
ulan committed
138
// Multiple inheritance can be used because Task is a pure interface.
139 140
class V8_EXPORT_PRIVATE CancelableTask : public Cancelable,
                                         NON_EXPORTED_BASE(public Task) {
ulan's avatar
ulan committed
141
 public:
142
  explicit CancelableTask(Isolate* isolate);
143
  explicit CancelableTask(CancelableTaskManager* manager);
ulan's avatar
ulan committed
144 145

  // Task overrides.
146
  void Run() final {
147
    if (TryRun()) {
148 149 150 151 152 153 154 155 156 157
      RunInternal();
    }
  }

  virtual void RunInternal() = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(CancelableTask);
};

ulan's avatar
ulan committed
158 159 160 161

// Multiple inheritance can be used because IdleTask is a pure interface.
class CancelableIdleTask : public Cancelable, public IdleTask {
 public:
162
  explicit CancelableIdleTask(Isolate* isolate);
163
  explicit CancelableIdleTask(CancelableTaskManager* manager);
ulan's avatar
ulan committed
164 165 166

  // IdleTask overrides.
  void Run(double deadline_in_seconds) final {
167
    if (TryRun()) {
ulan's avatar
ulan committed
168 169 170 171 172 173 174 175 176 177 178
      RunInternal(deadline_in_seconds);
    }
  }

  virtual void RunInternal(double deadline_in_seconds) = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask);
};


179 180 181 182
}  // namespace internal
}  // namespace v8

#endif  // V8_CANCELABLE_TASK_H_