| 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 21 matching lines...) Expand all Loading... |
| 32 // added | 32 // added |
| 33 // to the incoming queue. | 33 // to the incoming queue. |
| 34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; | 34 return type == MessageLoop::TYPE_UI || type == MessageLoop::TYPE_JAVA; |
| 35 #else | 35 #else |
| 36 return false; | 36 return false; |
| 37 #endif | 37 #endif |
| 38 } | 38 } |
| 39 | 39 |
| 40 } // namespace | 40 } // namespace |
| 41 | 41 |
| 42 IncomingTaskQueue::IncomingTaskQueue(MessageLoop* message_loop) | 42 IncomingTaskQueue::IncomingTaskQueue() |
| 43 : high_res_task_count_(0), | 43 : high_res_task_count_(0), |
| 44 message_loop_(message_loop), | 44 message_loop_(nullptr), |
| 45 next_sequence_num_(0), | 45 next_sequence_num_(0) { |
| 46 message_loop_scheduled_(false), | 46 } |
| 47 always_schedule_work_(AlwaysNotifyPump(message_loop_->type())) { | 47 |
| 48 void IncomingTaskQueue::StartScheduling(MessageLoop* message_loop) { |
| 49 AutoLock lock(incoming_queue_lock_); |
| 50 DCHECK(!message_loop_); |
| 51 DCHECK(!message_loop_terminated_); |
| 52 DCHECK(message_loop); |
| 53 message_loop_ = message_loop; |
| 54 always_schedule_work_ = AlwaysNotifyPump(message_loop_->type()); |
| 55 if (!incoming_queue_.empty()) |
| 56 message_loop_->ScheduleWork(); |
| 48 } | 57 } |
| 49 | 58 |
| 50 bool IncomingTaskQueue::AddToIncomingQueue( | 59 bool IncomingTaskQueue::AddToIncomingQueue( |
| 51 const tracked_objects::Location& from_here, | 60 const tracked_objects::Location& from_here, |
| 52 const Closure& task, | 61 const Closure& task, |
| 53 TimeDelta delay, | 62 TimeDelta delay, |
| 54 bool nestable) { | 63 bool nestable) { |
| 55 DLOG_IF(WARNING, | 64 DLOG_IF(WARNING, |
| 56 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) | 65 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) |
| 57 << "Requesting super-long task delay period of " << delay.InSeconds() | 66 << "Requesting super-long task delay period of " << delay.InSeconds() |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 } | 109 } |
| 101 // Reset the count of high resolution tasks since our queue is now empty. | 110 // Reset the count of high resolution tasks since our queue is now empty. |
| 102 int high_res_tasks = high_res_task_count_; | 111 int high_res_tasks = high_res_task_count_; |
| 103 high_res_task_count_ = 0; | 112 high_res_task_count_ = 0; |
| 104 return high_res_tasks; | 113 return high_res_tasks; |
| 105 } | 114 } |
| 106 | 115 |
| 107 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { | 116 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { |
| 108 AutoLock lock(incoming_queue_lock_); | 117 AutoLock lock(incoming_queue_lock_); |
| 109 message_loop_ = NULL; | 118 message_loop_ = NULL; |
| 119 message_loop_terminated_ = true; |
| 110 } | 120 } |
| 111 | 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_); |
| 125 DCHECK(message_loop_terminated_); |
| 115 } | 126 } |
| 116 | 127 |
| 117 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) { | 128 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) { |
| 118 TimeTicks delayed_run_time; | 129 TimeTicks delayed_run_time; |
| 119 if (delay > TimeDelta()) | 130 if (delay > TimeDelta()) |
| 120 delayed_run_time = TimeTicks::Now() + delay; | 131 delayed_run_time = TimeTicks::Now() + delay; |
| 121 else | 132 else |
| 122 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; | 133 DCHECK_EQ(delay.InMilliseconds(), 0) << "delay should not be negative"; |
| 123 return delayed_run_time; | 134 return delayed_run_time; |
| 124 } | 135 } |
| 125 | 136 |
| 126 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) { | 137 bool IncomingTaskQueue::PostPendingTask(PendingTask* pending_task) { |
| 127 // Warning: Don't try to short-circuit, and handle this thread's tasks more | 138 // Warning: Don't try to short-circuit, and handle this thread's tasks more |
| 128 // directly, as it could starve handling of foreign threads. Put every task | 139 // directly, as it could starve handling of foreign threads. Put every task |
| 129 // into this queue. | 140 // into this queue. |
| 130 | 141 |
| 131 // This should only be called while the lock is taken. | 142 // This should only be called while the lock is taken. |
| 132 incoming_queue_lock_.AssertAcquired(); | 143 incoming_queue_lock_.AssertAcquired(); |
| 133 | 144 |
| 134 if (!message_loop_) { | 145 if (message_loop_terminated_) { |
| 135 pending_task->task.Reset(); | 146 pending_task->task.Reset(); |
| 136 return false; | 147 return false; |
| 137 } | 148 } |
| 138 | 149 |
| 139 // Initialize the sequence number. The sequence number is used for delayed | 150 // Initialize the sequence number. The sequence number is used for delayed |
| 140 // tasks (to faciliate FIFO sorting when two tasks have the same | 151 // tasks (to faciliate FIFO sorting when two tasks have the same |
| 141 // delayed_run_time value) and for identifying the task in about:tracing. | 152 // delayed_run_time value) and for identifying the task in about:tracing. |
| 142 pending_task->sequence_num = next_sequence_num_++; | 153 pending_task->sequence_num = next_sequence_num_++; |
| 143 | 154 |
| 144 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", | 155 task_annotator_.DidQueueTask("MessageLoop::PostTask", *pending_task); |
| 145 *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 (message_loop_ && |
| 162 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) { |
| 152 // Wake up the message loop. | 163 // Wake up the message loop. |
| 153 message_loop_->ScheduleWork(); | 164 message_loop_->ScheduleWork(); |
| 154 // After we've scheduled the message loop, we do not need to do so again | 165 // 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 | 166 // 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 | 167 // 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 | 168 // reload from the incoming queue before waiting again so we clear this flag |
| 158 // in ReloadWorkQueue(). | 169 // in ReloadWorkQueue(). |
| 159 message_loop_scheduled_ = true; | 170 message_loop_scheduled_ = true; |
| 160 } | 171 } |
| 161 | 172 |
| 162 return true; | 173 return true; |
| 163 } | 174 } |
| 164 | 175 |
| 165 } // namespace internal | 176 } // namespace internal |
| 166 } // namespace base | 177 } // namespace base |
| OLD | NEW |