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 running a task by calling {Abort}. | |
Hannes Payer (out of office)
2015/11/13 20:03:22
drop the running, it's cleaner
Michael Lippautz
2015/11/13 22:51:03
Done.
| |
29 uint32_t Register(Cancelable* task); | |
30 | |
31 // Try to abort running a task identified by {id}. The possible outcomes are: | |
Hannes Payer (out of office)
2015/11/13 20:03:22
task _remove one whitespace_ identified
Michael Lippautz
2015/11/13 22:51:03
Done.
| |
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) and is canceled and removed. | |
Hannes Payer (out of office)
2015/11/13 20:03:22
"so it is"
Michael Lippautz
2015/11/13 22:51:03
Done.
| |
36 // | |
37 // Returns {false} for (1) and (2), and {true} for (3). | |
38 bool TryAbort(uint32_t id); | |
39 | |
40 // Cancels all remaining registered tasks and waits for tasks that are | |
41 // already running. | |
42 void CancelAndWait(); | |
43 | |
44 private: | |
45 // To mitigate the ABA problem, the api refers to tasks through an id. | |
46 uint32_t task_id_counter_; | |
47 | |
48 // A set of cancelable tasks that are currently registered. | |
49 HashMap cancelable_tasks_; | |
50 | |
51 // Mutex and condition variable enabling concurrent register and removing, as | |
52 // well as waiting for background tasks on {CancelAndWait}. | |
53 base::ConditionVariable cancelable_tasks_barrier_; | |
54 base::Mutex mutex_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); | |
57 }; | |
58 | |
59 | |
17 class Cancelable { | 60 class Cancelable { |
18 public: | 61 public: |
19 explicit Cancelable(Isolate* isolate); | 62 explicit Cancelable(CancelableTaskManager* parent); |
20 virtual ~Cancelable(); | 63 virtual ~Cancelable(); |
21 | 64 |
22 virtual void Cancel() { is_cancelled_ = true; } | 65 // Never invoke after handing over the task to the platform! The reason is |
66 // that {Cancelable} is used in combination with {v8::Task} and handed to | |
67 // a platform. This step transfers ownership to the platform, which destroys | |
68 // the task after running it. Since the exact time is not known, we cannot | |
69 // access the object after handing it to a platform. | |
70 uint32_t id() { return id_; } | |
23 | 71 |
24 protected: | 72 protected: |
25 Isolate* isolate_; | 73 bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); } |
26 bool is_cancelled_; | 74 bool IsRunning() { return status_.Value() == kRunning; } |
75 intptr_t CancelAttempts() { return cancel_counter_.Value(); } | |
27 | 76 |
28 private: | 77 private: |
Hannes Payer (out of office)
2015/11/13 20:03:22
Please describe the states in a comment.
Michael Lippautz
2015/11/13 22:51:03
Done.
| |
78 enum Status { | |
79 kWaiting, | |
80 kCanceled, | |
81 kRunning, | |
82 }; | |
83 | |
84 // Use {CancelableTaskManager} to abort a task that has not yet been | |
85 // executed. | |
86 bool Cancel() { | |
87 if (status_.TrySetValue(kWaiting, kCanceled)) { | |
88 return true; | |
89 } | |
90 cancel_counter_.Increment(1); | |
91 return false; | |
92 } | |
93 | |
94 CancelableTaskManager* parent_; | |
95 AtomicValue<Status> status_; | |
96 uint32_t id_; | |
97 AtomicNumber<intptr_t> cancel_counter_; | |
Hannes Payer (out of office)
2015/11/13 20:03:22
Let's describe the purpose of this counter.
Michael Lippautz
2015/11/13 22:51:03
Done.
| |
98 | |
99 friend class CancelableTaskManager; | |
100 | |
29 DISALLOW_COPY_AND_ASSIGN(Cancelable); | 101 DISALLOW_COPY_AND_ASSIGN(Cancelable); |
30 }; | 102 }; |
31 | 103 |
32 | 104 |
33 // Multiple inheritance can be used because Task is a pure interface. | 105 // Multiple inheritance can be used because Task is a pure interface. |
34 class CancelableTask : public Cancelable, public Task { | 106 class CancelableTask : public Cancelable, public Task { |
35 public: | 107 public: |
36 explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} | 108 explicit CancelableTask(Isolate* isolate); |
37 | 109 |
38 // Task overrides. | 110 // Task overrides. |
39 void Run() final { | 111 void Run() final { |
40 if (!is_cancelled_) { | 112 if (TryRun()) { |
41 RunInternal(); | 113 RunInternal(); |
42 } | 114 } |
43 } | 115 } |
44 | 116 |
45 virtual void RunInternal() = 0; | 117 virtual void RunInternal() = 0; |
46 | 118 |
119 Isolate* isolate() { return isolate_; } | |
120 | |
47 private: | 121 private: |
122 Isolate* isolate_; | |
48 DISALLOW_COPY_AND_ASSIGN(CancelableTask); | 123 DISALLOW_COPY_AND_ASSIGN(CancelableTask); |
49 }; | 124 }; |
50 | 125 |
51 | 126 |
52 // Multiple inheritance can be used because IdleTask is a pure interface. | 127 // Multiple inheritance can be used because IdleTask is a pure interface. |
53 class CancelableIdleTask : public Cancelable, public IdleTask { | 128 class CancelableIdleTask : public Cancelable, public IdleTask { |
54 public: | 129 public: |
55 explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} | 130 explicit CancelableIdleTask(Isolate* isolate); |
56 | 131 |
57 // IdleTask overrides. | 132 // IdleTask overrides. |
58 void Run(double deadline_in_seconds) final { | 133 void Run(double deadline_in_seconds) final { |
59 if (!is_cancelled_) { | 134 if (TryRun()) { |
60 RunInternal(deadline_in_seconds); | 135 RunInternal(deadline_in_seconds); |
61 } | 136 } |
62 } | 137 } |
63 | 138 |
64 virtual void RunInternal(double deadline_in_seconds) = 0; | 139 virtual void RunInternal(double deadline_in_seconds) = 0; |
65 | 140 |
141 Isolate* isolate() { return isolate_; } | |
142 | |
66 private: | 143 private: |
144 Isolate* isolate_; | |
67 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); | 145 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
68 }; | 146 }; |
69 | 147 |
70 | 148 |
71 } // namespace internal | 149 } // namespace internal |
72 } // namespace v8 | 150 } // namespace v8 |
73 | 151 |
74 #endif // V8_CANCELABLE_TASK_H_ | 152 #endif // V8_CANCELABLE_TASK_H_ |
OLD | NEW |