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

Side by Side Diff: base/message_loop/incoming_task_queue.cc

Issue 1011683002: Lazily initialize MessageLoop for faster thread startup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: non-blocking thread_id() Created 5 years, 9 months 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
OLDNEW
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 29 matching lines...) Expand all
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 } 48 }
49 49
50 // static
51 IncomingTaskQueue* IncomingTaskQueue::CreateForLazyInitMessageLoop(
52 MessageLoop* message_loop) {
53 IncomingTaskQueue* queue = new IncomingTaskQueue(message_loop);
54 queue->message_loop_not_initialized_ = true;
55 return queue;
56 }
57
50 bool IncomingTaskQueue::AddToIncomingQueue( 58 bool IncomingTaskQueue::AddToIncomingQueue(
51 const tracked_objects::Location& from_here, 59 const tracked_objects::Location& from_here,
52 const Closure& task, 60 const Closure& task,
53 TimeDelta delay, 61 TimeDelta delay,
54 bool nestable) { 62 bool nestable) {
55 DLOG_IF(WARNING, 63 DLOG_IF(WARNING,
56 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds) 64 delay.InSeconds() > kTaskDelayWarningThresholdInSeconds)
57 << "Requesting super-long task delay period of " << delay.InSeconds() 65 << "Requesting super-long task delay period of " << delay.InSeconds()
58 << " seconds from here: " << from_here.ToString(); 66 << " seconds from here: " << from_here.ToString();
59 67
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 int high_res_tasks = high_res_task_count_; 110 int high_res_tasks = high_res_task_count_;
103 high_res_task_count_ = 0; 111 high_res_task_count_ = 0;
104 return high_res_tasks; 112 return high_res_tasks;
105 } 113 }
106 114
107 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() { 115 void IncomingTaskQueue::WillDestroyCurrentMessageLoop() {
108 AutoLock lock(incoming_queue_lock_); 116 AutoLock lock(incoming_queue_lock_);
109 message_loop_ = NULL; 117 message_loop_ = NULL;
110 } 118 }
111 119
120 void IncomingTaskQueue::DidInitializeMessageLoop() {
121 AutoLock lock(incoming_queue_lock_);
122 DCHECK(message_loop_not_initialized_);
123 message_loop_not_initialized_ = false;
124 if (!incoming_queue_.empty()) {
125 message_loop_->ScheduleWork();
126 message_loop_scheduled_ = true;
127 }
128 }
129
130 bool IncomingTaskQueue::empty() {
131 AutoLock lock(incoming_queue_lock_);
132 return incoming_queue_.empty();
133 }
134
112 IncomingTaskQueue::~IncomingTaskQueue() { 135 IncomingTaskQueue::~IncomingTaskQueue() {
113 // Verify that WillDestroyCurrentMessageLoop() has been called. 136 // Verify that WillDestroyCurrentMessageLoop() has been called.
114 DCHECK(!message_loop_); 137 DCHECK(!message_loop_);
115 } 138 }
116 139
117 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) { 140 TimeTicks IncomingTaskQueue::CalculateDelayedRuntime(TimeDelta delay) {
118 TimeTicks delayed_run_time; 141 TimeTicks delayed_run_time;
119 if (delay > TimeDelta()) 142 if (delay > TimeDelta())
120 delayed_run_time = TimeTicks::Now() + delay; 143 delayed_run_time = TimeTicks::Now() + delay;
121 else 144 else
(...skipping 19 matching lines...) Expand all
141 // delayed_run_time value) and for identifying the task in about:tracing. 164 // delayed_run_time value) and for identifying the task in about:tracing.
142 pending_task->sequence_num = next_sequence_num_++; 165 pending_task->sequence_num = next_sequence_num_++;
143 166
144 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask", 167 message_loop_->task_annotator()->DidQueueTask("MessageLoop::PostTask",
145 *pending_task); 168 *pending_task);
146 169
147 bool was_empty = incoming_queue_.empty(); 170 bool was_empty = incoming_queue_.empty();
148 incoming_queue_.push(*pending_task); 171 incoming_queue_.push(*pending_task);
149 pending_task->task.Reset(); 172 pending_task->task.Reset();
150 173
151 if (always_schedule_work_ || (!message_loop_scheduled_ && was_empty)) { 174 if (!message_loop_not_initialized_ &&
175 (always_schedule_work_ || (!message_loop_scheduled_ && was_empty))) {
152 // Wake up the message loop. 176 // Wake up the message loop.
153 message_loop_->ScheduleWork(); 177 message_loop_->ScheduleWork();
154 // After we've scheduled the message loop, we do not need to do so again 178 // 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 179 // 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 180 // 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 181 // reload from the incoming queue before waiting again so we clear this flag
158 // in ReloadWorkQueue(). 182 // in ReloadWorkQueue().
159 message_loop_scheduled_ = true; 183 message_loop_scheduled_ = true;
160 } 184 }
161 185
162 return true; 186 return true;
163 } 187 }
164 188
165 } // namespace internal 189 } // namespace internal
166 } // namespace base 190 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698