cancelable-task.h 4.92 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 9
#include <map>

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

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

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

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

53 54 55 56
  // 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.
57
  std::map<uint32_t, Cancelable*> cancelable_tasks_;
58 59 60 61 62 63

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

64 65
  bool canceled_;

66 67
  friend class Cancelable;

68 69 70
  DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager);
};

71
class V8_EXPORT_PRIVATE Cancelable {
72
 public:
73
  explicit Cancelable(CancelableTaskManager* parent);
ulan's avatar
ulan committed
74 75
  virtual ~Cancelable();

76 77 78 79 80 81
  // 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
82 83

 protected:
84 85 86
  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
87 88

 private:
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  // 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_;
lpy's avatar
lpy committed
111
  base::AtomicValue<Status> status_;
112 113 114 115 116
  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.
lpy's avatar
lpy committed
117
  base::AtomicNumber<intptr_t> cancel_counter_;
118 119 120

  friend class CancelableTaskManager;

ulan's avatar
ulan committed
121 122
  DISALLOW_COPY_AND_ASSIGN(Cancelable);
};
123 124


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

  // Task overrides.
131
  void Run() final {
132
    if (TryRun()) {
133 134 135 136 137 138
      RunInternal();
    }
  }

  virtual void RunInternal() = 0;

139 140
  Isolate* isolate() { return isolate_; }

141
 private:
142
  Isolate* isolate_;
143 144 145
  DISALLOW_COPY_AND_ASSIGN(CancelableTask);
};

ulan's avatar
ulan committed
146 147 148 149

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

  // IdleTask overrides.
  void Run(double deadline_in_seconds) final {
154
    if (TryRun()) {
ulan's avatar
ulan committed
155 156 157 158 159 160
      RunInternal(deadline_in_seconds);
    }
  }

  virtual void RunInternal(double deadline_in_seconds) = 0;

161 162
  Isolate* isolate() { return isolate_; }

ulan's avatar
ulan committed
163
 private:
164
  Isolate* isolate_;
ulan's avatar
ulan committed
165 166 167 168
  DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask);
};


169 170 171 172
}  // namespace internal
}  // namespace v8

#endif  // V8_CANCELABLE_TASK_H_