| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium 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 // CancelableTaskTracker posts tasks (in the form of a Closure) to a | 5 // CancelableTaskTracker posts tasks (in the form of a Closure) to a |
| 6 // TaskRunner, and is able to cancel the task later if it's not needed | 6 // TaskRunner, and is able to cancel the task later if it's not needed |
| 7 // anymore. On destruction, CancelableTaskTracker will cancel all | 7 // anymore. On destruction, CancelableTaskTracker will cancel all |
| 8 // tracked tasks. | 8 // tracked tasks. |
| 9 // | 9 // |
| 10 // Each cancelable task can be associated with a reply (also a Closure). After | 10 // Each cancelable task can be associated with a reply (also a Closure). After |
| 11 // the task is run on the TaskRunner, |reply| will be posted back to | 11 // the task is run on the TaskRunner, |reply| will be posted back to |
| 12 // originating TaskRunner. | 12 // originating TaskRunner. |
| 13 // | 13 // |
| 14 // NOTE: | 14 // NOTE: |
| 15 // | 15 // |
| 16 // CancelableCallback (base/cancelable_callback.h) and WeakPtr binding are | 16 // CancelableCallback (base/cancelable_callback.h) and WeakPtr binding are |
| 17 // preferred solutions for canceling a task. However, they don't support | 17 // preferred solutions for canceling a task. However, they don't support |
| 18 // cancelation from another thread. This is sometimes a performance critical | 18 // cancelation from another sequence. This is sometimes a performance critical |
| 19 // requirement. E.g. We need to cancel database lookup task on DB thread when | 19 // requirement. E.g. We need to cancel database lookup task on DB thread when |
| 20 // user changes inputed text. If it is performance critical to do a best effort | 20 // user changes inputed text. If it is performance critical to do a best effort |
| 21 // cancelation of a task, then CancelableTaskTracker is appropriate, | 21 // cancelation of a task, then CancelableTaskTracker is appropriate, otherwise |
| 22 // otherwise use one of the other mechanisms. | 22 // use one of the other mechanisms. |
| 23 // | 23 // |
| 24 // THREAD-SAFETY: | 24 // THREAD-SAFETY: |
| 25 // | 25 // |
| 26 // 1. CancelableTaskTracker objects are not thread safe. They must | 26 // 1. A CancelableTaskTracker object must be created, used, and destroyed on a |
| 27 // be created, used, and destroyed on the originating thread that posts the | 27 // single sequence. |
| 28 // task. It's safe to destroy a CancelableTaskTracker while there | |
| 29 // are outstanding tasks. This is commonly used to cancel all outstanding | |
| 30 // tasks. | |
| 31 // | 28 // |
| 32 // 2. Both task and reply are deleted on the originating thread. | 29 // 2. It's safe to destroy a CancelableTaskTracker while there are outstanding |
| 30 // tasks. This is commonly used to cancel all outstanding tasks. |
| 33 // | 31 // |
| 34 // 3. IsCanceledCallback is thread safe and can be run or deleted on any | 32 // 3. Both task and reply are deleted on the originating sequence. |
| 35 // thread. | 33 // |
| 34 // 4. IsCanceledCallback can be run or deleted on any sequence. |
| 36 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 35 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| 37 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 36 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| 38 | 37 |
| 39 #include <stdint.h> | 38 #include <stdint.h> |
| 40 | 39 |
| 41 #include "base/base_export.h" | 40 #include "base/base_export.h" |
| 42 #include "base/bind.h" | 41 #include "base/bind.h" |
| 43 #include "base/callback.h" | 42 #include "base/callback.h" |
| 44 #include "base/containers/hash_tables.h" | 43 #include "base/containers/hash_tables.h" |
| 45 #include "base/macros.h" | 44 #include "base/macros.h" |
| 46 #include "base/memory/weak_ptr.h" | 45 #include "base/memory/weak_ptr.h" |
| 47 #include "base/post_task_and_reply_with_result_internal.h" | 46 #include "base/post_task_and_reply_with_result_internal.h" |
| 48 #include "base/threading/thread_checker.h" | 47 #include "base/sequence_checker.h" |
| 49 | 48 |
| 50 namespace tracked_objects { | 49 namespace tracked_objects { |
| 51 class Location; | 50 class Location; |
| 52 } // namespace tracked_objects | 51 } // namespace tracked_objects |
| 53 | 52 |
| 54 namespace base { | 53 namespace base { |
| 55 | 54 |
| 56 class CancellationFlag; | 55 class CancellationFlag; |
| 57 class TaskRunner; | 56 class TaskRunner; |
| 58 | 57 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 // tracked. | 123 // tracked. |
| 125 bool HasTrackedTasks() const; | 124 bool HasTrackedTasks() const; |
| 126 | 125 |
| 127 private: | 126 private: |
| 128 void Track(TaskId id, base::CancellationFlag* flag); | 127 void Track(TaskId id, base::CancellationFlag* flag); |
| 129 void Untrack(TaskId id); | 128 void Untrack(TaskId id); |
| 130 | 129 |
| 131 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; | 130 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; |
| 132 | 131 |
| 133 TaskId next_id_; | 132 TaskId next_id_; |
| 134 base::ThreadChecker thread_checker_; | 133 SequenceChecker sequence_checker_; |
| 135 | 134 |
| 136 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; | 135 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; |
| 137 | 136 |
| 138 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); | 137 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); |
| 139 }; | 138 }; |
| 140 | 139 |
| 141 } // namespace base | 140 } // namespace base |
| 142 | 141 |
| 143 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ | 142 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| OLD | NEW |