| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 TaskRunner, | 5 // CancelableTaskTracker posts tasks (in the form of a Closure) to a |
| 6 // and is able to cancel the task later if it's not needed anymore. On | 6 // TaskRunner, and is able to cancel the task later if it's not needed |
| 7 // destruction, CancelableTaskTracker will cancel all tracked tasks. | 7 // anymore. On destruction, CancelableTaskTracker will cancel all |
| 8 // tracked tasks. |
| 8 // | 9 // |
| 9 // 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 |
| 10 // the task is run on the TaskRunner, |reply| will be posted back to originating | 11 // the task is run on the TaskRunner, |reply| will be posted back to |
| 11 // TaskRunner. | 12 // originating TaskRunner. |
| 12 // | 13 // |
| 13 // NOTE: | 14 // NOTE: |
| 14 // | 15 // |
| 15 // CancelableCallback (base/cancelable_callback.h) and WeakPtr binding are | 16 // CancelableCallback (base/cancelable_callback.h) and WeakPtr binding are |
| 16 // preferred solutions for canceling a task. However, they don't support | 17 // preferred solutions for canceling a task. However, they don't support |
| 17 // cancelation from another thread. This is sometimes a performance critical | 18 // cancelation from another thread. This is sometimes a performance critical |
| 18 // 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 |
| 19 // 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 |
| 20 // cancelation of a task, then CancelableTaskTracker is appropriate, otherwise | 21 // cancelation of a task, then CancelableTaskTracker is appropriate, |
| 21 // use one of the other mechanisms. | 22 // otherwise use one of the other mechanisms. |
| 22 // | 23 // |
| 23 // THREAD-SAFETY: | 24 // THREAD-SAFETY: |
| 24 // | 25 // |
| 25 // 1. CancelableTaskTracker objects are not thread safe. They must be created, | 26 // 1. CancelableTaskTracker objects are not thread safe. They must |
| 26 // used, and destroyed on the originating thread that posts the task. It's safe | 27 // be created, used, and destroyed on the originating thread that posts the |
| 27 // to destroy a CancelableTaskTracker while there are outstanding tasks. This is | 28 // task. It's safe to destroy a CancelableTaskTracker while there |
| 28 // commonly used to cancel all outstanding tasks. | 29 // are outstanding tasks. This is commonly used to cancel all outstanding |
| 30 // tasks. |
| 29 // | 31 // |
| 30 // 2. Both task and reply are deleted on the originating thread. | 32 // 2. Both task and reply are deleted on the originating thread. |
| 31 // | 33 // |
| 32 // 3. IsCanceledCallback is thread safe and can be run or deleted on any thread. | 34 // 3. IsCanceledCallback is thread safe and can be run or deleted on any |
| 35 // thread. |
| 36 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| 37 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| 33 | 38 |
| 34 #ifndef CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ | 39 #include "base/base_export.h" |
| 35 #define CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ | |
| 36 | |
| 37 #include "base/basictypes.h" | 40 #include "base/basictypes.h" |
| 38 #include "base/callback.h" | 41 #include "base/callback.h" |
| 39 #include "base/containers/hash_tables.h" | 42 #include "base/containers/hash_tables.h" |
| 40 #include "base/memory/weak_ptr.h" | 43 #include "base/memory/weak_ptr.h" |
| 41 #include "base/threading/thread_checker.h" | 44 #include "base/threading/thread_checker.h" |
| 42 | 45 |
| 43 namespace base { | |
| 44 class CancellationFlag; | |
| 45 class TaskRunner; | |
| 46 } // namespace base | |
| 47 | |
| 48 namespace tracked_objects { | 46 namespace tracked_objects { |
| 49 class Location; | 47 class Location; |
| 50 } // namespace tracked_objects | 48 } // namespace tracked_objects |
| 51 | 49 |
| 52 class CancelableTaskTracker { | 50 namespace base { |
| 51 |
| 52 class CancellationFlag; |
| 53 class TaskRunner; |
| 54 |
| 55 class BASE_EXPORT CancelableTaskTracker { |
| 53 public: | 56 public: |
| 54 // All values except kBadTaskId are valid. | 57 // All values except kBadTaskId are valid. |
| 55 typedef int64 TaskId; | 58 typedef int64 TaskId; |
| 56 static const TaskId kBadTaskId; | 59 static const TaskId kBadTaskId; |
| 57 | 60 |
| 58 typedef base::Callback<bool()> IsCanceledCallback; | 61 typedef base::Callback<bool()> IsCanceledCallback; |
| 59 | 62 |
| 60 CancelableTaskTracker(); | 63 CancelableTaskTracker(); |
| 61 | 64 |
| 62 // Cancels all tracked tasks. | 65 // Cancels all tracked tasks. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 | 108 |
| 106 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; | 109 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; |
| 107 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; | 110 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; |
| 108 | 111 |
| 109 TaskId next_id_; | 112 TaskId next_id_; |
| 110 base::ThreadChecker thread_checker_; | 113 base::ThreadChecker thread_checker_; |
| 111 | 114 |
| 112 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); | 115 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); |
| 113 }; | 116 }; |
| 114 | 117 |
| 115 #endif // CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ | 118 } // namespace base |
| 119 |
| 120 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ |
| OLD | NEW |