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 fore- and background tasks. |
| 22 class CancelableTaskManager { |
| 23 public: |
| 24 CancelableTaskManager(); |
| 25 |
| 26 // Registers a new cancelable task. |
| 27 uint32_t Register(Cancelable* task); |
| 28 |
| 29 // Remove a given task. Returns whether the task was present at the time of |
| 30 // calling {Remove}. |
| 31 bool Remove(uint32_t id); |
| 32 |
| 33 // Cancel all registered tasks and wait for tasks that are already running. |
| 34 void CancelAndWait(); |
| 35 |
| 36 private: |
| 37 // To mitigate the ABA problem, the api refers to tasks through an id. |
| 38 uint32_t task_id_counter_; |
| 39 |
| 40 // A set of cancelable tasks that are currently registered. |
| 41 HashMap cancelable_tasks_; |
| 42 |
| 43 // Mutex and condition variable enabling concurrent register and removing, as |
| 44 // well as waiting for background tasks on {CancelAndWait}. |
| 45 base::ConditionVariable cancelable_tasks_barrier_; |
| 46 base::Mutex mutex_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); |
| 49 }; |
| 50 |
| 51 |
17 class Cancelable { | 52 class Cancelable { |
18 public: | 53 public: |
19 explicit Cancelable(Isolate* isolate); | 54 explicit Cancelable(CancelableTaskManager* parent); |
20 virtual ~Cancelable(); | 55 virtual ~Cancelable(); |
21 | 56 |
22 virtual void Cancel() { is_cancelled_ = true; } | 57 // Never invoke after handing over the task to the platform! |
| 58 uint32_t id() { return id_; } |
| 59 |
| 60 // Always invoke indirectly through the connected {CancelableTaskManager}. |
| 61 bool Cancel() { |
| 62 if (status_.TrySetValue(kWaiting, kCanceled)) { |
| 63 return true; |
| 64 } |
| 65 cancel_counter_.Increment(1); |
| 66 return false; |
| 67 } |
23 | 68 |
24 protected: | 69 protected: |
25 Isolate* isolate_; | 70 bool TryRun() { return status_.TrySetValue(kWaiting, kRunning); } |
26 bool is_cancelled_; | 71 bool IsRunning() { return status_.Value() == kRunning; } |
| 72 intptr_t CancelAttempts() { return cancel_counter_.Value(); } |
27 | 73 |
28 private: | 74 private: |
| 75 enum Status { |
| 76 kWaiting, |
| 77 kCanceled, |
| 78 kRunning, |
| 79 }; |
| 80 |
| 81 CancelableTaskManager* parent_; |
| 82 AtomicValue<Status> status_; |
| 83 uint32_t id_; |
| 84 AtomicNumber<intptr_t> cancel_counter_; |
| 85 |
29 DISALLOW_COPY_AND_ASSIGN(Cancelable); | 86 DISALLOW_COPY_AND_ASSIGN(Cancelable); |
30 }; | 87 }; |
31 | 88 |
32 | 89 |
33 // Multiple inheritance can be used because Task is a pure interface. | 90 // Multiple inheritance can be used because Task is a pure interface. |
34 class CancelableTask : public Cancelable, public Task { | 91 class CancelableTask : public Cancelable, public Task { |
35 public: | 92 public: |
36 explicit CancelableTask(Isolate* isolate) : Cancelable(isolate) {} | 93 explicit CancelableTask(Isolate* isolate); |
37 | 94 |
38 // Task overrides. | 95 // Task overrides. |
39 void Run() final { | 96 void Run() final { |
40 if (!is_cancelled_) { | 97 if (TryRun()) { |
41 RunInternal(); | 98 RunInternal(); |
42 } | 99 } |
43 } | 100 } |
44 | 101 |
45 virtual void RunInternal() = 0; | 102 virtual void RunInternal() = 0; |
46 | 103 |
| 104 Isolate* isolate() { return isolate_; } |
| 105 |
47 private: | 106 private: |
| 107 Isolate* isolate_; |
48 DISALLOW_COPY_AND_ASSIGN(CancelableTask); | 108 DISALLOW_COPY_AND_ASSIGN(CancelableTask); |
49 }; | 109 }; |
50 | 110 |
51 | 111 |
52 // Multiple inheritance can be used because IdleTask is a pure interface. | 112 // Multiple inheritance can be used because IdleTask is a pure interface. |
53 class CancelableIdleTask : public Cancelable, public IdleTask { | 113 class CancelableIdleTask : public Cancelable, public IdleTask { |
54 public: | 114 public: |
55 explicit CancelableIdleTask(Isolate* isolate) : Cancelable(isolate) {} | 115 explicit CancelableIdleTask(Isolate* isolate); |
56 | 116 |
57 // IdleTask overrides. | 117 // IdleTask overrides. |
58 void Run(double deadline_in_seconds) final { | 118 void Run(double deadline_in_seconds) final { |
59 if (!is_cancelled_) { | 119 if (TryRun()) { |
60 RunInternal(deadline_in_seconds); | 120 RunInternal(deadline_in_seconds); |
61 } | 121 } |
62 } | 122 } |
63 | 123 |
64 virtual void RunInternal(double deadline_in_seconds) = 0; | 124 virtual void RunInternal(double deadline_in_seconds) = 0; |
65 | 125 |
| 126 Isolate* isolate() { return isolate_; } |
| 127 |
66 private: | 128 private: |
| 129 Isolate* isolate_; |
67 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); | 130 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
68 }; | 131 }; |
69 | 132 |
70 | 133 |
71 } // namespace internal | 134 } // namespace internal |
72 } // namespace v8 | 135 } // namespace v8 |
73 | 136 |
74 #endif // V8_CANCELABLE_TASK_H_ | 137 #endif // V8_CANCELABLE_TASK_H_ |
OLD | NEW |