| 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 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ |
| 6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <memory> |
| 11 #include <set> | 12 #include <set> |
| 12 #include <utility> | 13 #include <utility> |
| 13 | 14 |
| 14 #include "base/bind.h" | 15 #include "base/bind.h" |
| 15 #include "base/callback.h" | 16 #include "base/callback.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/message_loop/message_loop.h" | 19 #include "base/message_loop/message_loop.h" |
| 20 #include "base/threading/non_thread_safe.h" | 20 #include "base/threading/non_thread_safe.h" |
| 21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 22 #include "base/timer/timer.h" | 22 #include "base/timer/timer.h" |
| 23 #include "net/base/backoff_entry.h" | 23 #include "net/base/backoff_entry.h" |
| 24 | 24 |
| 25 namespace syncer { | 25 namespace syncer { |
| 26 | 26 |
| 27 // A queue that dispatches tasks, ignores duplicates, and provides backoff | 27 // A queue that dispatches tasks, ignores duplicates, and provides backoff |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 // Reset any backoff delay and resume dispatching of tasks. | 128 // Reset any backoff delay and resume dispatching of tasks. |
| 129 // | 129 // |
| 130 // Useful for when you know the cause of previous failures has been resolved | 130 // Useful for when you know the cause of previous failures has been resolved |
| 131 // and you want don't want to wait for the accumulated backoff delay to | 131 // and you want don't want to wait for the accumulated backoff delay to |
| 132 // elapse. | 132 // elapse. |
| 133 void ResetBackoff(); | 133 void ResetBackoff(); |
| 134 | 134 |
| 135 // Use |timer| for scheduled events. | 135 // Use |timer| for scheduled events. |
| 136 // | 136 // |
| 137 // Used in tests. See also MockTimer. | 137 // Used in tests. See also MockTimer. |
| 138 void SetTimerForTest(scoped_ptr<base::Timer> timer); | 138 void SetTimerForTest(std::unique_ptr<base::Timer> timer); |
| 139 | 139 |
| 140 private: | 140 private: |
| 141 void FinishTask(const T& task); | 141 void FinishTask(const T& task); |
| 142 void ScheduleDispatch(); | 142 void ScheduleDispatch(); |
| 143 void Dispatch(); | 143 void Dispatch(); |
| 144 // Return true if we should dispatch tasks. | 144 // Return true if we should dispatch tasks. |
| 145 bool ShouldDispatch(); | 145 bool ShouldDispatch(); |
| 146 | 146 |
| 147 const HandleTaskCallback process_callback_; | 147 const HandleTaskCallback process_callback_; |
| 148 net::BackoffEntry::Policy backoff_policy_; | 148 net::BackoffEntry::Policy backoff_policy_; |
| 149 scoped_ptr<net::BackoffEntry> backoff_entry_; | 149 std::unique_ptr<net::BackoffEntry> backoff_entry_; |
| 150 // The number of tasks currently being handled. | 150 // The number of tasks currently being handled. |
| 151 int num_in_progress_; | 151 int num_in_progress_; |
| 152 std::deque<T> queue_; | 152 std::deque<T> queue_; |
| 153 // The set of tasks in queue_ or currently being handled. | 153 // The set of tasks in queue_ or currently being handled. |
| 154 std::set<T> tasks_; | 154 std::set<T> tasks_; |
| 155 base::Closure dispatch_closure_; | 155 base::Closure dispatch_closure_; |
| 156 scoped_ptr<base::Timer> backoff_timer_; | 156 std::unique_ptr<base::Timer> backoff_timer_; |
| 157 base::TimeDelta delay_; | 157 base::TimeDelta delay_; |
| 158 | 158 |
| 159 // Must be last data member. | 159 // Must be last data member. |
| 160 base::WeakPtrFactory<TaskQueue> weak_ptr_factory_; | 160 base::WeakPtrFactory<TaskQueue> weak_ptr_factory_; |
| 161 | 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(TaskQueue); | 162 DISALLOW_COPY_AND_ASSIGN(TaskQueue); |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 // The maximum number of tasks that may be concurrently executed. Think | 165 // The maximum number of tasks that may be concurrently executed. Think |
| 166 // carefully before changing this value. The desired behavior of backoff may | 166 // carefully before changing this value. The desired behavior of backoff may |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 } | 227 } |
| 228 | 228 |
| 229 template <typename T> | 229 template <typename T> |
| 230 void TaskQueue<T>::ResetBackoff() { | 230 void TaskQueue<T>::ResetBackoff() { |
| 231 backoff_timer_->Stop(); | 231 backoff_timer_->Stop(); |
| 232 backoff_entry_->Reset(); | 232 backoff_entry_->Reset(); |
| 233 ScheduleDispatch(); | 233 ScheduleDispatch(); |
| 234 } | 234 } |
| 235 | 235 |
| 236 template <typename T> | 236 template <typename T> |
| 237 void TaskQueue<T>::SetTimerForTest(scoped_ptr<base::Timer> timer) { | 237 void TaskQueue<T>::SetTimerForTest(std::unique_ptr<base::Timer> timer) { |
| 238 DCHECK(CalledOnValidThread()); | 238 DCHECK(CalledOnValidThread()); |
| 239 DCHECK(timer.get()); | 239 DCHECK(timer.get()); |
| 240 backoff_timer_ = std::move(timer); | 240 backoff_timer_ = std::move(timer); |
| 241 } | 241 } |
| 242 | 242 |
| 243 template <typename T> | 243 template <typename T> |
| 244 void TaskQueue<T>::FinishTask(const T& task) { | 244 void TaskQueue<T>::FinishTask(const T& task) { |
| 245 DCHECK(CalledOnValidThread()); | 245 DCHECK(CalledOnValidThread()); |
| 246 DCHECK_GE(num_in_progress_, 1); | 246 DCHECK_GE(num_in_progress_, 1); |
| 247 --num_in_progress_; | 247 --num_in_progress_; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 277 } | 277 } |
| 278 | 278 |
| 279 template <typename T> | 279 template <typename T> |
| 280 bool TaskQueue<T>::ShouldDispatch() { | 280 bool TaskQueue<T>::ShouldDispatch() { |
| 281 return num_in_progress_ < kMaxConcurrentTasks && !queue_.empty(); | 281 return num_in_progress_ < kMaxConcurrentTasks && !queue_.empty(); |
| 282 } | 282 } |
| 283 | 283 |
| 284 } // namespace syncer | 284 } // namespace syncer |
| 285 | 285 |
| 286 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ | 286 #endif // SYNC_INTERNAL_API_PUBLIC_ATTACHMENTS_TASK_QUEUE_H_ |
| OLD | NEW |