// Copyright 2014 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // CancelableTaskTracker posts tasks (in the form of a OnceClosure) to a // TaskRunner, and is able to cancel the task later if it's not needed // anymore. On destruction, CancelableTaskTracker will cancel all // tracked tasks. // // Each cancelable task can be associated with a reply (also a OnceClosure). // After the task is run on the TaskRunner, |reply| will be posted back to // originating TaskRunner. // // NOTE: // // CancelableOnceCallback (base/cancelable_callback.h) and WeakPtr binding are // preferred solutions for canceling a task. However, they don't support // cancelation from another sequence. This is sometimes a performance critical // requirement. E.g. We need to cancel database lookup task on DB thread when // user changes inputted text. If it is performance critical to do a best effort // cancelation of a task, then CancelableTaskTracker is appropriate, otherwise // use one of the other mechanisms. // // THREAD-SAFETY: // // 1. A CancelableTaskTracker object must be created, used, and destroyed on a // single sequence. // // 2. It's safe to destroy a CancelableTaskTracker while there are outstanding // tasks. This is commonly used to cancel all outstanding tasks. // // 3. The task is deleted on the target sequence, and the reply are deleted on // the originating sequence. // // 4. IsCanceledCallback can be run or deleted on any sequence. #ifndef BASE_TASK_CANCELABLE_TASK_TRACKER_H_ #define BASE_TASK_CANCELABLE_TASK_TRACKER_H_ #include <stdint.h> #include <memory> #include <utility> #include "base/base_export.h" #include "base/containers/small_map.h" #include "base/functional/bind.h" #include "base/functional/callback.h" #include "base/functional/callback_helpers.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "base/sequence_checker.h" #include "base/synchronization/atomic_flag.h" #include "base/task/post_task_and_reply_with_result_internal.h" namespace base { class Location; class ScopedClosureRunner; class TaskRunner; class BASE_EXPORT CancelableTaskTracker { … }; } // namespace base #endif // BASE_TASK_CANCELABLE_TASK_TRACKER_H_