OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_CANCELABLE_TASK_H_ | 5 #ifndef V8_CANCELABLE_TASK_H_ |
6 #define V8_CANCELABLE_TASK_H_ | 6 #define V8_CANCELABLE_TASK_H_ |
7 | 7 |
8 #include "include/v8-platform.h" | 8 #include "include/v8-platform.h" |
| 9 #include "src/atomic-utils.h" |
9 #include "src/base/macros.h" | 10 #include "src/base/macros.h" |
| 11 #include "src/base/platform/condition-variable.h" |
| 12 #include "src/hashmap.h" |
10 | 13 |
11 namespace v8 { | 14 namespace v8 { |
12 namespace internal { | 15 namespace internal { |
13 | 16 |
| 17 class Cancelable; |
14 class Isolate; | 18 class Isolate; |
15 | 19 |
16 | 20 |
| 21 // Keeps track of cancelable tasks. It is possible to register and remove tasks |
| 22 // from any fore- and background task/thread. |
| 23 class CancelableTaskManager { |
| 24 public: |
| 25 CancelableTaskManager(); |
| 26 |
| 27 // Registers a new cancelable {task}. Returns the unique {id} of the task that |
| 28 // can be used to try to abort a task by calling {Abort}. |
| 29 uint32_t Register(Cancelable* task); |
| 30 |
| 31 // Try to abort running a task identified by {id}. The possible outcomes are: |
| 32 // (1) The task is already finished running and thus has been removed from |
| 33 // the manager. |
| 34 // (2) The task is currently running and cannot be canceled anymore. |
| 35 // (3) The task is not yet running (or finished) so it is canceled and |
| 36 // removed. |
| 37 // |
| 38 // Returns {false} for (1) and (2), and {true} for (3). |
| 39 bool TryAbort(uint32_t id); |
| 40 |
| 41 // Cancels all remaining registered tasks and waits for tasks that are |
| 42 // already running. |
| 43 void CancelAndWait(); |
| 44 |
| 45 private: |
| 46 // To mitigate the ABA problem, the api refers to tasks through an id. |
| 47 uint32_t task_id_counter_; |
| 48 |
| 49 // A set of cancelable tasks that are currently registered. |
| 50 HashMap cancelable_tasks_; |
| 51 |
| 52 // Mutex and condition variable enabling concurrent register and removing, as |
| 53 // well as waiting for background tasks on {CancelAndWait}. |
| 54 base::ConditionVariable cancelable_tasks_barrier_; |
| 55 base::Mutex mutex_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); |
| 58 }; |
| 59 |
| 60 |
17 class Cancelable { | 61 class Cancelable { |
18 public: | 62 public: |
19 explicit Cancelable(Isolate* isolate); | 63 explicit Cancelable(CancelableTaskManager* parent); |
20 virtual ~Cancelable(); | 64 virtual ~Cancelable(); |
21 | 65 |
22 virtual void Cancel() { is_cancelled_ = true; } | 66 // Never invoke after handing over the task to the platform! The reason is |
| 67 // that {Cancelable} is used in combination with {v8::Task} and handed to |
| 68 // a platform. This step transfers ownership to the platform, which destroys |
| 69 // the task after running it. Since the exact time is not known, we cannot |
| 70 // access the object after handing it to a platform. |
| 71 uint32_t id() { return id_; } |
23 | 72 |
24 protected: | 73 protected: |
25 Isolate* isolate_; | 74 bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); } |
26 bool is_cancelled_; | 75 bool IsRunning() { return status_.Value() == kRunning; } |
| 76 intptr_t CancelAttempts() { return cancel_counter_.Value(); } |
27 | 77 |
28 private: | 78 private: |
| 79 // Identifies the state a cancelable task is in: |
| 80 // |kWaiting|: The task is scheduled and waiting to be executed. {TryRun} will |
| 81 // succeed. |
| 82 // |kCanceled|: The task has been canceled. {TryRun} will fail. |
| 83 // |kRunning|: The task is currently running and cannot be canceled anymore. |
| 84 enum Status { |
| 85 kWaiting, |
| 86 kCanceled, |
| 87 kRunning, |
| 88 }; |
| 89 |
| 90 // Use {CancelableTaskManager} to abort a task that has not yet been |
| 91 // executed. |
| 92 bool Cancel() { |
| 93 if (status_.TrySetValue(kWaiting, kCanceled)) { |
| 94 return true; |
| 95 } |
| 96 cancel_counter_.Increment(1); |
| 97 return false; |
| 98 } |
| 99 |
| 100 CancelableTaskManager* parent_; |
| 101 AtomicValue<Status> status_; |
| 102 uint32_t id_; |
| 103 |
| 104 // The counter is incremented for failing tries to cancel a task. This can be |
| 105 // used by the task itself as an indication how often external entities tried |
| 106 // to abort it. |
| 107 AtomicNumber<intptr_t> cancel_counter_; |
| 108 |
| 109 friend class CancelableTaskManager; |
| 110 |
29 DISALLOW_COPY_AND_ASSIGN(Cancelable); | 111 DISALLOW_COPY_AND_ASSIGN(Cancelable); |
30 }; | 112 }; |
31 | 113 |
32 | 114 |
33 // Multiple inheritance can be used because Task is a pure interface. | 115 // Multiple inheritance can be used because Task is a pure interface. |
34 class CancelableTask : public Cancelable, public Task { | 116 class CancelableTask : public Cancelable, public Task { |
35 public: | 117 public: |
36 explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} | 118 explicit CancelableTask(Isolate* isolate); |
37 | 119 |
38 // Task overrides. | 120 // Task overrides. |
39 void Run() final { | 121 void Run() final { |
40 if (!is_cancelled_) { | 122 if (TryRun()) { |
41 RunInternal(); | 123 RunInternal(); |
42 } | 124 } |
43 } | 125 } |
44 | 126 |
45 virtual void RunInternal() = 0; | 127 virtual void RunInternal() = 0; |
46 | 128 |
| 129 Isolate* isolate() { return isolate_; } |
| 130 |
47 private: | 131 private: |
| 132 Isolate* isolate_; |
48 DISALLOW_COPY_AND_ASSIGN(CancelableTask); | 133 DISALLOW_COPY_AND_ASSIGN(CancelableTask); |
49 }; | 134 }; |
50 | 135 |
51 | 136 |
52 // Multiple inheritance can be used because IdleTask is a pure interface. | 137 // Multiple inheritance can be used because IdleTask is a pure interface. |
53 class CancelableIdleTask : public Cancelable, public IdleTask { | 138 class CancelableIdleTask : public Cancelable, public IdleTask { |
54 public: | 139 public: |
55 explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} | 140 explicit CancelableIdleTask(Isolate* isolate); |
56 | 141 |
57 // IdleTask overrides. | 142 // IdleTask overrides. |
58 void Run(double deadline_in_seconds) final { | 143 void Run(double deadline_in_seconds) final { |
59 if (!is_cancelled_) { | 144 if (TryRun()) { |
60 RunInternal(deadline_in_seconds); | 145 RunInternal(deadline_in_seconds); |
61 } | 146 } |
62 } | 147 } |
63 | 148 |
64 virtual void RunInternal(double deadline_in_seconds) = 0; | 149 virtual void RunInternal(double deadline_in_seconds) = 0; |
65 | 150 |
| 151 Isolate* isolate() { return isolate_; } |
| 152 |
66 private: | 153 private: |
| 154 Isolate* isolate_; |
67 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); | 155 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
68 }; | 156 }; |
69 | 157 |
70 | 158 |
71 } // namespace internal | 159 } // namespace internal |
72 } // namespace v8 | 160 } // namespace v8 |
73 | 161 |
74 #endif // V8_CANCELABLE_TASK_H_ | 162 #endif // V8_CANCELABLE_TASK_H_ |
OLD | NEW |