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 3 // found in the LICENSE file. 4 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 7 // anymore. On destruction, CancelableTaskTracker will cancel all 8 // tracked tasks. 9 // 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 12 // originating TaskRunner. 13 // 14 // NOTE: 15 // 16 // CancelableCallback (base/cancelable_callback.h) and WeakPtr binding are 17 // preferred solutions for canceling a task. However, they don't support 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 20 // user changes inputed text. If it is performance critical to do a best effort 21 // cancelation of a task, then CancelableTaskTracker is appropriate, otherwise 22 // use one of the other mechanisms. 23 // 24 // THREAD-SAFETY: 25 // 26 // 1. A CancelableTaskTracker object must be created, used, and destroyed on a 27 // single sequence. 28 // 29 // 2. It's safe to destroy a CancelableTaskTracker while there are outstanding 30 // tasks. This is commonly used to cancel all outstanding tasks. 31 // 32 // 3. Both task and reply are deleted on the originating sequence. 33 // 34 // 4. IsCanceledCallback can be run or deleted on any sequence. 35 #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ 36 #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ 37 38 #include <stdint.h> 39 40 #include <utility> 41 42 #include "base/base_export.h" 43 #include "base/bind.h" 44 #include "base/callback.h" 45 #include "base/containers/hash_tables.h" 46 #include "base/macros.h" 47 #include "base/memory/weak_ptr.h" 48 #include "base/post_task_and_reply_with_result_internal.h" 49 #include "base/sequence_checker.h" 50 51 namespace tracked_objects { 52 class Location; 53 } // namespace tracked_objects 54 55 namespace base { 56 57 class CancellationFlag; 58 class TaskRunner; 59 60 class BASE_EXPORT CancelableTaskTracker { 61 public: 62 // All values except kBadTaskId are valid. 63 typedef int64_t TaskId; 64 static const TaskId kBadTaskId; 65 66 typedef base::Callback<bool()> IsCanceledCallback; 67 68 CancelableTaskTracker(); 69 70 // Cancels all tracked tasks. 71 ~CancelableTaskTracker(); 72 73 TaskId PostTask(base::TaskRunner* task_runner, 74 const tracked_objects::Location& from_here, 75 const base::Closure& task); 76 77 TaskId PostTaskAndReply(base::TaskRunner* task_runner, 78 const tracked_objects::Location& from_here, 79 base::Closure task, 80 base::Closure reply); 81 82 template <typename TaskReturnType, typename ReplyArgType> 83 TaskId PostTaskAndReplyWithResult(base::TaskRunner* task_runner, 84 const tracked_objects::Location& from_here, 85 base::Callback<TaskReturnType()> task, 86 base::Callback<void(ReplyArgType)> reply) { 87 TaskReturnType* result = new TaskReturnType(); 88 return PostTaskAndReply( 89 task_runner, from_here, 90 base::Bind(&base::internal::ReturnAsParamAdapter<TaskReturnType>, 91 std::move(task), base::Unretained(result)), 92 base::Bind(&base::internal::ReplyAdapter<TaskReturnType, ReplyArgType>, 93 std::move(reply), base::Owned(result))); 94 } 95 96 // Creates a tracked TaskId and an associated IsCanceledCallback. Client can 97 // later call TryCancel() with the returned TaskId, and run |is_canceled_cb| 98 // from any thread to check whether the TaskId is canceled. 99 // 100 // The returned task ID is tracked until the last copy of 101 // |is_canceled_cb| is destroyed. 102 // 103 // Note. This function is used to address some special cancelation requirement 104 // in existing code. You SHOULD NOT need this function in new code. 105 TaskId NewTrackedTaskId(IsCanceledCallback* is_canceled_cb); 106 107 // After calling this function, |task| and |reply| will not run. If the 108 // cancelation happens when |task| is running or has finished running, |reply| 109 // will not run. If |reply| is running or has finished running, cancellation 110 // is a noop. 111 // 112 // Note. It's OK to cancel a |task| for more than once. The later calls are 113 // noops. 114 void TryCancel(TaskId id); 115 116 // It's OK to call this function for more than once. The later calls are 117 // noops. 118 void TryCancelAll(); 119 120 // Returns true iff there are in-flight tasks that are still being 121 // tracked. 122 bool HasTrackedTasks() const; 123 124 private: 125 void Track(TaskId id, base::CancellationFlag* flag); 126 void Untrack(TaskId id); 127 128 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; 129 130 TaskId next_id_; 131 SequenceChecker sequence_checker_; 132 133 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; 134 135 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); 136 }; 137 138 } // namespace base 139 140 #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_ 141