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 <map> | 8 #include <map> |
9 | 9 |
10 #include "include/v8-platform.h" | 10 #include "include/v8-platform.h" |
11 #include "src/base/atomic-utils.h" | 11 #include "src/base/atomic-utils.h" |
12 #include "src/base/macros.h" | 12 #include "src/base/macros.h" |
13 #include "src/base/platform/condition-variable.h" | 13 #include "src/base/platform/condition-variable.h" |
14 | 14 |
15 namespace v8 { | 15 namespace v8 { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 class Cancelable; | 18 class Cancelable; |
19 class Isolate; | 19 class Isolate; |
20 | 20 |
21 | 21 |
22 // Keeps track of cancelable tasks. It is possible to register and remove tasks | 22 // Keeps track of cancelable tasks. It is possible to register and remove tasks |
23 // from any fore- and background task/thread. | 23 // from any fore- and background task/thread. |
24 class CancelableTaskManager { | 24 class CancelableTaskManager { |
25 public: | 25 public: |
26 CancelableTaskManager(); | 26 CancelableTaskManager(); |
27 | 27 |
28 // Registers a new cancelable {task}. Returns the unique {id} of the task that | 28 // Registers a new cancelable {task}. Returns the unique {id} of the task that |
29 // can be used to try to abort a task by calling {Abort}. | 29 // can be used to try to abort a task by calling {Abort}. |
| 30 // Must not be called after CancelAndWait. |
30 uint32_t Register(Cancelable* task); | 31 uint32_t Register(Cancelable* task); |
31 | 32 |
32 // Try to abort running a task identified by {id}. The possible outcomes are: | 33 // Try to abort running a task identified by {id}. The possible outcomes are: |
33 // (1) The task is already finished running and thus has been removed from | 34 // (1) The task is already finished running and thus has been removed from |
34 // the manager. | 35 // the manager. |
35 // (2) The task is currently running and cannot be canceled anymore. | 36 // (2) The task is currently running and cannot be canceled anymore. |
36 // (3) The task is not yet running (or finished) so it is canceled and | 37 // (3) The task is not yet running (or finished) so it is canceled and |
37 // removed. | 38 // removed. |
38 // | 39 // |
39 // Returns {false} for (1) and (2), and {true} for (3). | 40 // Returns {false} for (1) and (2), and {true} for (3). |
40 bool TryAbort(uint32_t id); | 41 bool TryAbort(uint32_t id); |
41 | 42 |
42 // Cancels all remaining registered tasks and waits for tasks that are | 43 // Cancels all remaining registered tasks and waits for tasks that are |
43 // already running. | 44 // already running. This disallows subsequent Register calls. |
44 void CancelAndWait(); | 45 void CancelAndWait(); |
45 | 46 |
46 private: | 47 private: |
47 // Only called by {Cancelable} destructor. The task is done with executing, | 48 // Only called by {Cancelable} destructor. The task is done with executing, |
48 // but needs to be removed. | 49 // but needs to be removed. |
49 void RemoveFinishedTask(uint32_t id); | 50 void RemoveFinishedTask(uint32_t id); |
50 | 51 |
51 // To mitigate the ABA problem, the api refers to tasks through an id. | 52 // To mitigate the ABA problem, the api refers to tasks through an id. |
52 uint32_t task_id_counter_; | 53 uint32_t task_id_counter_; |
53 | 54 |
54 // A set of cancelable tasks that are currently registered. | 55 // A set of cancelable tasks that are currently registered. |
55 std::map<uint32_t, Cancelable*> cancelable_tasks_; | 56 std::map<uint32_t, Cancelable*> cancelable_tasks_; |
56 | 57 |
57 // Mutex and condition variable enabling concurrent register and removing, as | 58 // Mutex and condition variable enabling concurrent register and removing, as |
58 // well as waiting for background tasks on {CancelAndWait}. | 59 // well as waiting for background tasks on {CancelAndWait}. |
59 base::ConditionVariable cancelable_tasks_barrier_; | 60 base::ConditionVariable cancelable_tasks_barrier_; |
60 base::Mutex mutex_; | 61 base::Mutex mutex_; |
61 | 62 |
| 63 bool canceled_; |
| 64 |
62 friend class Cancelable; | 65 friend class Cancelable; |
63 | 66 |
64 DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); | 67 DISALLOW_COPY_AND_ASSIGN(CancelableTaskManager); |
65 }; | 68 }; |
66 | 69 |
67 | 70 |
68 class Cancelable { | 71 class Cancelable { |
69 public: | 72 public: |
70 explicit Cancelable(CancelableTaskManager* parent); | 73 explicit Cancelable(CancelableTaskManager* parent); |
71 virtual ~Cancelable(); | 74 virtual ~Cancelable(); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 private: | 163 private: |
161 Isolate* isolate_; | 164 Isolate* isolate_; |
162 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); | 165 DISALLOW_COPY_AND_ASSIGN(CancelableIdleTask); |
163 }; | 166 }; |
164 | 167 |
165 | 168 |
166 } // namespace internal | 169 } // namespace internal |
167 } // namespace v8 | 170 } // namespace v8 |
168 | 171 |
169 #endif // V8_CANCELABLE_TASK_H_ | 172 #endif // V8_CANCELABLE_TASK_H_ |
OLD | NEW |