| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/message_loop/incoming_task_queue.h" | 5 #include "base/message_loop/incoming_task_queue.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #endif | 37 #endif |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) | 42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) |
| 43 : high_res_task_count_(0), | 43 : high_res_task_count_(0), |
| 44 message_loop_(message_loop), | 44 message_loop_(message_loop), |
| 45 next_sequence_num_(0), | 45 next_sequence_num_(0), |
| 46 message_loop_scheduled_(false), | 46 message_loop_scheduled_(false), |
| 47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())) { | 47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())), |
| 48 is_ready_for_scheduling_(false) { |
| 48 } | 49 } |
| 49 | 50 |
| 50 bool IncomingTaskQueue::AddToIncomingQueue( | 51 bool IncomingTaskQueue::AddToIncomingQueue( |
| 51 const tracked_objects::Location& from_here, | 52 const tracked_objects::Location& from_here, |
| 52 const Closure& task, | 53 const Closure& task, |
| 53 TimeDelta delay, | 54 TimeDelta delay, |
| 54 bool nestable) { | 55 bool nestable) { |
| 55 DLOG_IF(WARNING, | 56 DLOG_IF(WARNING, |
| 56 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) | 57 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) |
| 57 << "Requesting super-long task delay period of " << delay.InSeconds() | 58 << "Requesting super-long task delay period of " << delay.InSeconds() |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 int high_res_tasks = high_res_task_count_; | 103 int high_res_tasks = high_res_task_count_; |
| 103 high_res_task_count_ = 0; | 104 high_res_task_count_ = 0; |
| 104 return high_res_tasks; | 105 return high_res_tasks; |
| 105 } | 106 } |
| 106 | 107 |
| 107 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { | 108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { |
| 108 AutoLock lock(incoming_queue_lock_); | 109 AutoLock lock(incoming_queue_lock_); |
| 109 message_loop_ = NULL; | 110 message_loop_ = NULL; |
| 110 } | 111 } |
| 111 | 112 |
| 113 void IncomingTaskQueue::StartScheduling() { |
| 114 AutoLock lock(incoming_queue_lock_); |
| 115 DCHECK(!is_ready_for_scheduling_); |
| 116 DCHECK(!message_loop_scheduled_); |
| 117 is_ready_for_scheduling_ = true; |
| 118 if (!incoming_queue_.empty()) |
| 119 ScheduleWork(); |
| 120 } |
| 121 |
| 112 IncomingTaskQueue::~IncomingTaskQueue() { | 122 IncomingTaskQueue::~IncomingTaskQueue() { |
| 113 // Verify that WillDestroyCurrentMessageLoop() has been called. | 123 // Verify that WillDestroyCurrentMessageLoop() has been called. |
| 114 DCHECK(!message_loop_); | 124 DCHECK(!message_loop_); |
| 115 } | 125 } |
| 116 | 126 |
| 117 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) { | 127 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) { |
| 118 TimeTicks delayed_run_time; | 128 TimeTicks delayed_run_time; |
| 119 if (delay > TimeDelta()) | 129 if (delay > TimeDelta()) |
| 120 delayed_run_time = TimeTicks::Now() + delay; | 130 delayed_run_time = TimeTicks::Now() + delay; |
| 121 else | 131 else |
| (...skipping 19 matching lines...) Expand all Loading... |
| 141 // delayed_run_time value) and for identifying the task in about:tracing. | 151 // delayed_run_time value) and for identifying the task in about:tracing. |
| 142 pending_task->sequence_num = next_sequence_num_++; | 152 pending_task->sequence_num = next_sequence_num_++; |
| 143 | 153 |
| 144 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", | 154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", |
| 145 *pending_task); | 155 *pending_task); |
| 146 | 156 |
| 147 bool was_empty = incoming_queue_.empty(); | 157 bool was_empty = incoming_queue_.empty(); |
| 148 incoming_queue_.push(*pending_task); | 158 incoming_queue_.push(*pending_task); |
| 149 pending_task->task.Reset(); | 159 pending_task->task.Reset(); |
| 150 | 160 |
| 151 if (always_schedule_work_ || (!message_loop_scheduled_ && was_empty)) { | 161 if (is_ready_for_scheduling_ && |
| 152 // Wake up the message loop. | 162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) { |
| 153 message_loop_->ScheduleWork(); | 163 ScheduleWork(); |
| 154 // After we've scheduled the message loop, we do not need to do so again | |
| 155 // until we know it has processed all of the work in our queue and is | |
| 156 // waiting for more work again. The message loop will always attempt to | |
| 157 // reload from the incoming queue before waiting again so we clear this flag | |
| 158 // in ReloadWorkQueue(). | |
| 159 message_loop_scheduled_ = true; | |
| 160 } | 164 } |
| 161 | 165 |
| 162 return true; | 166 return true; |
| 163 } | 167 } |
| 164 | 168 |
| 169 void IncomingTaskQueue::ScheduleWork() { |
| 170 DCHECK(is_ready_for_scheduling_); |
| 171 // Wake up the message loop. |
| 172 message_loop_->ScheduleWork(); |
| 173 // After we've scheduled the message loop, we do not need to do so again |
| 174 // until we know it has processed all of the work in our queue and is |
| 175 // waiting for more work again. The message loop will always attempt to |
| 176 // reload from the incoming queue before waiting again so we clear this flag |
| 177 // in ReloadWorkQueue(). |
| 178 message_loop_scheduled_ = true; |
| 179 } |
| 180 |
| 165 } // namespace internal | 181 } // namespace internal |
| 166 } // namespace base | 182 } // namespace base |
| OLD | NEW |