Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(365)

Side by Side Diff: chrome/common/cancelable_task_tracker.h

Issue 11410073: Add function to CancelableTaskTracker and convert BootTimeLoader (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase again and add tests Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 TaskRunner,
6 // and is able to cancel the task later if it's not needed anymore. On 6 // and is able to cancel the task later if it's not needed anymore. On
7 // destruction, CancelableTaskTracker will cancel all tracked tasks. 7 // destruction, CancelableTaskTracker will cancel all tracked tasks.
8 // 8 //
9 // Each cancelable task can be associated with a reply (also a Closure). After 9 // 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 10 // the task is run on the TaskRunner, |reply| will be posted back to originating
(...skipping 10 matching lines...) Expand all
21 // use one of the other mechanisms. 21 // use one of the other mechanisms.
22 // 22 //
23 // THREAD-SAFETY: 23 // THREAD-SAFETY:
24 // 24 //
25 // 1. CancelableTaskTracker objects are not thread safe. They must be created, 25 // 1. CancelableTaskTracker objects are not thread safe. They must be created,
26 // used, and destroyed on the originating thread that posts the task. It's safe 26 // used, and destroyed on the originating thread that posts the task. It's safe
27 // to destroy a CancelableTaskTracker while there are outstanding tasks. This is 27 // to destroy a CancelableTaskTracker while there are outstanding tasks. This is
28 // commonly used to cancel all outstanding tasks. 28 // commonly used to cancel all outstanding tasks.
29 // 29 //
30 // 2. Both task and reply are deleted on the originating thread. 30 // 2. Both task and reply are deleted on the originating thread.
31 //
32 // 3. IsCanceledCallback is thread safe and can be run or deleted on any thread.
31 33
32 #ifndef CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ 34 #ifndef CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
33 #define CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ 35 #define CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
34 36
35 #include "base/basictypes.h" 37 #include "base/basictypes.h"
36 #include "base/callback.h" 38 #include "base/callback.h"
37 #include "base/hash_tables.h" 39 #include "base/hash_tables.h"
38 #include "base/memory/weak_ptr.h" 40 #include "base/memory/weak_ptr.h"
39 #include "base/threading/thread_checker.h" 41 #include "base/threading/thread_checker.h"
40 42
41 namespace base { 43 namespace base {
42 class CancellationFlag; 44 class CancellationFlag;
43 class TaskRunner; 45 class TaskRunner;
44 } // namespace base 46 } // namespace base
45 47
46 namespace tracked_objects { 48 namespace tracked_objects {
47 class Location; 49 class Location;
48 } // namespace tracked_objects 50 } // namespace tracked_objects
49 51
50 class CancelableTaskTracker { 52 class CancelableTaskTracker {
51 public: 53 public:
52 // All values except kBadTaskId are valid. 54 // All values except kBadTaskId are valid.
53 typedef int64 TaskId; 55 typedef int64 TaskId;
54 static const TaskId kBadTaskId; 56 static const TaskId kBadTaskId;
55 57
58 typedef base::Callback<bool()> IsCanceledCallback;
59
56 CancelableTaskTracker(); 60 CancelableTaskTracker();
57 61
58 // Cancels all tracked tasks. 62 // Cancels all tracked tasks.
59 ~CancelableTaskTracker(); 63 ~CancelableTaskTracker();
60 64
61 TaskId PostTask(base::TaskRunner* task_runner, 65 TaskId PostTask(base::TaskRunner* task_runner,
62 const tracked_objects::Location& from_here, 66 const tracked_objects::Location& from_here,
63 const base::Closure& task); 67 const base::Closure& task);
64 68
65 TaskId PostTaskAndReply(base::TaskRunner* task_runner, 69 TaskId PostTaskAndReply(base::TaskRunner* task_runner,
66 const tracked_objects::Location& from_here, 70 const tracked_objects::Location& from_here,
67 const base::Closure& task, 71 const base::Closure& task,
68 const base::Closure& reply); 72 const base::Closure& reply);
69 73
74 // Creates a tracked TaskId and an associated IsCanceledCallback. Client can
75 // later call TryCancel() with the returned TaskId, and run |is_canceled_cb|
76 // to check whether the TaskId is canceled.
77 //
78 // Note. This function is used to address some special cancelation requirement
79 // in existing code. You SHOULD NOT need this function in new code.
80 TaskId NewTrackedTaskId(IsCanceledCallback* is_canceled_cb);
81
70 // After calling this function, |task| and |reply| will not run. If the 82 // After calling this function, |task| and |reply| will not run. If the
71 // cancelation happens when |task| is running or has finished running, |reply| 83 // cancelation happens when |task| is running or has finished running, |reply|
72 // will not run. If |reply| is running or has finished running, cancellation 84 // will not run. If |reply| is running or has finished running, cancellation
73 // is a noop. 85 // is a noop.
74 // 86 //
75 // Note. It's OK to cancel a |task| for more than once. The later calls are 87 // Note. It's OK to cancel a |task| for more than once. The later calls are
76 // noops. 88 // noops.
77 void TryCancel(TaskId id); 89 void TryCancel(TaskId id);
78 90
79 // It's OK to call this function for more than once. The later calls are 91 // It's OK to call this function for more than once. The later calls are
80 // noops. 92 // noops.
81 void TryCancelAll(); 93 void TryCancelAll();
82 94
83 private: 95 private:
84 void Track(TaskId id, base::CancellationFlag* flag); 96 void Track(TaskId id, base::CancellationFlag* flag);
85 void Untrack(TaskId id); 97 void Untrack(TaskId id);
86 98
87 base::hash_map<TaskId, base::CancellationFlag*> task_flags_; 99 base::hash_map<TaskId, base::CancellationFlag*> task_flags_;
88 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_; 100 base::WeakPtrFactory<CancelableTaskTracker> weak_factory_;
89 101
90 TaskId next_id_; 102 TaskId next_id_;
91 base::ThreadChecker thread_checker_; 103 base::ThreadChecker thread_checker_;
92 104
93 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker); 105 DISALLOW_COPY_AND_ASSIGN(CancelableTaskTracker);
94 }; 106 };
95 107
96 #endif // CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_ 108 #endif // CHROME_COMMON_CANCELABLE_TASK_TRACKER_H_
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/version_info_updater.cc ('k') | chrome/common/cancelable_task_tracker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698