| 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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 DCHECK(work_queue->empty()); | 90 DCHECK(work_queue->empty()); |
| 91 | 91 |
| 92 // Acquire all we can from the inter-thread queue with one lock acquisition. | 92 // Acquire all we can from the inter-thread queue with one lock acquisition. |
| 93 AutoLock lock(incoming_queue_lock_); | 93 AutoLock lock(incoming_queue_lock_); |
| 94 if (incoming_queue_.empty()) { | 94 if (incoming_queue_.empty()) { |
| 95 // If the loop attempts to reload but there are no tasks in the incoming | 95 // If the loop attempts to reload but there are no tasks in the incoming |
| 96 // queue, that means it will go to sleep waiting for more work. If the | 96 // queue, that means it will go to sleep waiting for more work. If the |
| 97 // incoming queue becomes nonempty we need to schedule it again. | 97 // incoming queue becomes nonempty we need to schedule it again. |
| 98 message_loop_scheduled_ = false; | 98 message_loop_scheduled_ = false; |
| 99 } else { | 99 } else { |
| 100 incoming_queue_.Swap(work_queue); | 100 incoming_queue_.swap(*work_queue); |
| 101 } | 101 } |
| 102 // Reset the count of high resolution tasks since our queue is now empty. | 102 // Reset the count of high resolution tasks since our queue is now empty. |
| 103 int high_res_tasks = high_res_task_count_; | 103 int high_res_tasks = high_res_task_count_; |
| 104 high_res_task_count_ = 0; | 104 high_res_task_count_ = 0; |
| 105 return high_res_tasks; | 105 return high_res_tasks; |
| 106 } | 106 } |
| 107 | 107 |
| 108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { | 108 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { |
| 109 AutoLock lock(incoming_queue_lock_); | 109 AutoLock lock(incoming_queue_lock_); |
| 110 message_loop_ = NULL; | 110 message_loop_ = NULL; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 148 |
| 149 // Initialize the sequence number. The sequence number is used for delayed | 149 // Initialize the sequence number. The sequence number is used for delayed |
| 150 // tasks (to facilitate FIFO sorting when two tasks have the same | 150 // tasks (to facilitate FIFO sorting when two tasks have the same |
| 151 // 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. |
| 152 pending_task->sequence_num = next_sequence_num_++; | 152 pending_task->sequence_num = next_sequence_num_++; |
| 153 | 153 |
| 154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", | 154 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", |
| 155 *pending_task); | 155 *pending_task); |
| 156 | 156 |
| 157 bool was_empty = incoming_queue_.empty(); | 157 bool was_empty = incoming_queue_.empty(); |
| 158 incoming_queue_.push(*pending_task); | 158 incoming_queue_.push(std::move(*pending_task)); |
| 159 pending_task->task.Reset(); | |
| 160 | 159 |
| 161 if (is_ready_for_scheduling_ && | 160 if (is_ready_for_scheduling_ && |
| 162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) { | 161 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) { |
| 163 ScheduleWork(); | 162 ScheduleWork(); |
| 164 } | 163 } |
| 165 | 164 |
| 166 return true; | 165 return true; |
| 167 } | 166 } |
| 168 | 167 |
| 169 void IncomingTaskQueue::ScheduleWork() { | 168 void IncomingTaskQueue::ScheduleWork() { |
| 170 DCHECK(is_ready_for_scheduling_); | 169 DCHECK(is_ready_for_scheduling_); |
| 171 // Wake up the message loop. | 170 // Wake up the message loop. |
| 172 message_loop_->ScheduleWork(); | 171 message_loop_->ScheduleWork(); |
| 173 // After we've scheduled the message loop, we do not need to do so again | 172 // 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 | 173 // 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 | 174 // 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 | 175 // reload from the incoming queue before waiting again so we clear this flag |
| 177 // in ReloadWorkQueue(). | 176 // in ReloadWorkQueue(). |
| 178 message_loop_scheduled_ = true; | 177 message_loop_scheduled_ = true; |
| 179 } | 178 } |
| 180 | 179 |
| 181 } // namespace internal | 180 } // namespace internal |
| 182 } // namespace base | 181 } // namespace base |
| OLD | NEW |