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

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

Issue 1058603004: [Approach 3] Pre-allocate IncomigTaskQueue before MessageLoop for faster thread startup Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 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
« no previous file with comments | « base/message_loop/message_loop.h ('k') | base/threading/platform_thread.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/message_loop.h" 5 #include "base/message_loop/message_loop.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/compiler_specific.h" 10 #include "base/compiler_specific.h"
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 #elif defined(OS_POSIX) 93 #elif defined(OS_POSIX)
94 typedef MessagePumpLibevent MessagePumpForIO; 94 typedef MessagePumpLibevent MessagePumpForIO;
95 #endif 95 #endif
96 96
97 #if !defined(OS_NACL_SFI) 97 #if !defined(OS_NACL_SFI)
98 MessagePumpForIO* ToPumpIO(MessagePump* pump) { 98 MessagePumpForIO* ToPumpIO(MessagePump* pump) {
99 return static_cast<MessagePumpForIO*>(pump); 99 return static_cast<MessagePumpForIO*>(pump);
100 } 100 }
101 #endif // !defined(OS_NACL_SFI) 101 #endif // !defined(OS_NACL_SFI)
102 102
103 MessageLoop::Type GetMessageLoopType(const MessageLoop::Options& options) {
104 if (options.message_pump_factory.is_null())
105 return options.message_loop_type;
106 return MessageLoop::TYPE_CUSTOM;
107 }
108
103 } // namespace 109 } // namespace
104 110
105 //------------------------------------------------------------------------------ 111 //------------------------------------------------------------------------------
106 112
113 MessageLoop::Options::Options()
114 : message_loop_type(MessageLoop::TYPE_DEFAULT) {
115 }
116
117 MessageLoop::Options::~Options() {
118 }
119
120 //------------------------------------------------------------------------------
121
107 MessageLoop::TaskObserver::TaskObserver() { 122 MessageLoop::TaskObserver::TaskObserver() {
108 } 123 }
109 124
110 MessageLoop::TaskObserver::~TaskObserver() { 125 MessageLoop::TaskObserver::~TaskObserver() {
111 } 126 }
112 127
113 MessageLoop::DestructionObserver::~DestructionObserver() { 128 MessageLoop::DestructionObserver::~DestructionObserver() {
114 } 129 }
115 130
116 //------------------------------------------------------------------------------ 131 //------------------------------------------------------------------------------
117 132
133 MessageLoop::MessageLoop(const Options& options)
134 : type_(GetMessageLoopType(options)),
135 #if defined(OS_WIN)
136 pending_high_res_tasks_(0),
137 in_high_res_mode_(false),
138 #endif
139 nestable_tasks_allowed_(true),
140 #if defined(OS_WIN)
141 os_modal_loop_(false),
142 #endif // OS_WIN
143 message_histogram_(NULL),
144 run_loop_(NULL),
145 incoming_task_queue_(options.incoming_task_queue) {
146 if (!options.message_pump_factory.is_null())
147 pump_ = options.message_pump_factory.Run();
148 else
149 pump_ = CreateMessagePumpForType(type_);
150 Init();
151 }
152
118 MessageLoop::MessageLoop(Type type) 153 MessageLoop::MessageLoop(Type type)
119 : type_(type), 154 : type_(type),
120 #if defined(OS_WIN) 155 #if defined(OS_WIN)
121 pending_high_res_tasks_(0), 156 pending_high_res_tasks_(0),
122 in_high_res_mode_(false), 157 in_high_res_mode_(false),
123 #endif 158 #endif
124 nestable_tasks_allowed_(true), 159 nestable_tasks_allowed_(true),
125 #if defined(OS_WIN) 160 #if defined(OS_WIN)
126 os_modal_loop_(false), 161 os_modal_loop_(false),
127 #endif // OS_WIN 162 #endif // OS_WIN
128 message_histogram_(NULL), 163 message_histogram_(NULL),
129 run_loop_(NULL) { 164 run_loop_(NULL) {
130 Init(); 165 Init();
131
132 pump_ = CreateMessagePumpForType(type).Pass(); 166 pump_ = CreateMessagePumpForType(type).Pass();
133 } 167 }
134 168
135 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump) 169 MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump)
136 : pump_(pump.Pass()), 170 : pump_(pump.Pass()),
137 type_(TYPE_CUSTOM), 171 type_(TYPE_CUSTOM),
138 #if defined(OS_WIN) 172 #if defined(OS_WIN)
139 pending_high_res_tasks_(0), 173 pending_high_res_tasks_(0),
140 in_high_res_mode_(false), 174 in_high_res_mode_(false),
141 #endif 175 #endif
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 // queue. 414 // queue.
381 return incoming_task_queue_->IsIdleForTesting(); 415 return incoming_task_queue_->IsIdleForTesting();
382 } 416 }
383 417
384 //------------------------------------------------------------------------------ 418 //------------------------------------------------------------------------------
385 419
386 void MessageLoop::Init() { 420 void MessageLoop::Init() {
387 DCHECK(!current()) << "should only have one message loop per thread"; 421 DCHECK(!current()) << "should only have one message loop per thread";
388 lazy_tls_ptr.Pointer()->Set(this); 422 lazy_tls_ptr.Pointer()->Set(this);
389 423
390 incoming_task_queue_ = new internal::IncomingTaskQueue(this); 424 if (!incoming_task_queue_)
425 incoming_task_queue_ = new internal::IncomingTaskQueue();
426 incoming_task_queue_->StartScheduling(this);
391 message_loop_proxy_ = 427 message_loop_proxy_ =
392 new internal::MessageLoopProxyImpl(incoming_task_queue_); 428 new internal::MessageLoopProxyImpl(incoming_task_queue_);
393 thread_task_runner_handle_.reset( 429 thread_task_runner_handle_.reset(
394 new ThreadTaskRunnerHandle(message_loop_proxy_)); 430 new ThreadTaskRunnerHandle(message_loop_proxy_));
395 } 431 }
396 432
397 void MessageLoop::RunHandler() { 433 void MessageLoop::RunHandler() {
398 DCHECK_EQ(this, current()); 434 DCHECK_EQ(this, current());
399 435
400 StartHistogrammer(); 436 StartHistogrammer();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 } 470 }
435 #endif 471 #endif
436 472
437 // Execute the task and assume the worst: It is probably not reentrant. 473 // Execute the task and assume the worst: It is probably not reentrant.
438 nestable_tasks_allowed_ = false; 474 nestable_tasks_allowed_ = false;
439 475
440 HistogramEvent(kTaskRunEvent); 476 HistogramEvent(kTaskRunEvent);
441 477
442 FOR_EACH_OBSERVER(TaskObserver, task_observers_, 478 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
443 WillProcessTask(pending_task)); 479 WillProcessTask(pending_task));
444 task_annotator_.RunTask( 480 task_annotator()->RunTask(
445 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task); 481 "MessageLoop::PostTask", "MessageLoop::RunTask", pending_task);
446 FOR_EACH_OBSERVER(TaskObserver, task_observers_, 482 FOR_EACH_OBSERVER(TaskObserver, task_observers_,
447 DidProcessTask(pending_task)); 483 DidProcessTask(pending_task));
448 484
449 nestable_tasks_allowed_ = true; 485 nestable_tasks_allowed_ = true;
450 } 486 }
451 487
452 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) { 488 bool MessageLoop::DeferOrRunPendingTask(const PendingTask& pending_task) {
453 if (pending_task.nestable || run_loop_->run_depth_ == 1) { 489 if (pending_task.nestable || run_loop_->run_depth_ == 1) {
454 RunTask(pending_task); 490 RunTask(pending_task);
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 persistent, 747 persistent,
712 mode, 748 mode,
713 controller, 749 controller,
714 delegate); 750 delegate);
715 } 751 }
716 #endif 752 #endif
717 753
718 #endif // !defined(OS_NACL_SFI) 754 #endif // !defined(OS_NACL_SFI)
719 755
720 } // namespace base 756 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop/message_loop.h ('k') | base/threading/platform_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698