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

Unified Diff: base/message_loop/message_loop.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 side-by-side diff with in-line comments
Download patch
Index: base/message_loop/message_loop.cc
diff --git a/base/message_loop/message_loop.cc b/base/message_loop/message_loop.cc
index eb0f968849485ea73ef8a1aab9094bb9527af403..1a36e2f43bad5695725194af6136d26fa121ffe5 100644
--- a/base/message_loop/message_loop.cc
+++ b/base/message_loop/message_loop.cc
@@ -100,10 +100,26 @@ MessagePumpForIO* ToPumpIO(MessagePump* pump) {
}
#endif // !defined(OS_NACL_SFI)
+MessageLoop::Type GetMessageLoopType(MessageLoop::LazyInitOptions* options) {
+ if (options->message_pump_factory.is_null())
+ return options->message_loop_type;
+ return MessageLoop::TYPE_CUSTOM;
+}
+
} // namespace
//------------------------------------------------------------------------------
+MessageLoop::LazyInitOptions::LazyInitOptions()
+ : message_loop_type(MessageLoop::TYPE_DEFAULT),
+ timer_slack(TIMER_SLACK_NONE) {
+}
+
+MessageLoop::LazyInitOptions::~LazyInitOptions() {
+}
+
+//------------------------------------------------------------------------------
+
MessageLoop::TaskObserver::TaskObserver() {
}
@@ -150,6 +166,16 @@ MessageLoop::MessageLoop(scoped_ptr<MessagePump> pump)
}
MessageLoop::~MessageLoop() {
+ if (lazy_init_options_) {
+ // This message loop is destructed before we call LazyInit.
+ DCHECK(current() == NULL);
+ DCHECK(incoming_task_queue_->empty());
+ DCHECK(!pump_);
+ DCHECK(!destruction_observers_.might_have_observers());
+ incoming_task_queue_->WillDestroyCurrentMessageLoop();
+ return;
+ }
+
DCHECK_EQ(this, current());
// iOS just attaches to the loop, it doesn't Run it.
@@ -195,6 +221,12 @@ MessageLoop::~MessageLoop() {
}
// static
+MessageLoop* MessageLoop::CreateForLazyInit(
+ scoped_ptr<LazyInitOptions> options) {
+ return new MessageLoop(options.Pass());
+}
+
+// static
MessageLoop* MessageLoop::current() {
// TODO(darin): sadly, we cannot enable this yet since people call us even
// when they have no intention of using us.
@@ -260,6 +292,27 @@ scoped_ptr<MessagePump> MessageLoop::CreateMessagePumpForType(Type type) {
return MESSAGE_PUMP_DEFAULT;
}
+void MessageLoop::LazyInit() {
+ DCHECK(lazy_init_options_)
+ << "should be called only when created for lazy init";
+ DCHECK(!current()) << "should only have one message loop per thread";
+ lazy_tls_ptr.Pointer()->Set(this);
+
+ if (!lazy_init_options_->message_pump_factory.is_null()) {
+ pump_ = lazy_init_options_->message_pump_factory.Run().Pass();
rvargas (doing something else) 2015/03/26 02:19:26 no need to Pass()
kinuko 2015/04/13 02:02:59 Done.
+ } else {
+ pump_ =
+ CreateMessagePumpForType(lazy_init_options_->message_loop_type).Pass();
rvargas (doing something else) 2015/03/26 02:19:27 no need to Pass()
kinuko 2015/04/13 02:02:59 Done.
+ }
+ SetTimerSlack(lazy_init_options_->timer_slack);
+ lazy_init_options_.reset();
+
+ incoming_task_queue_->DidInitializeMessageLoop();
+ message_loop_proxy_->LazyInit();
+ thread_task_runner_handle_.reset(
+ new ThreadTaskRunnerHandle(message_loop_proxy_));
+}
+
void MessageLoop::AddDestructionObserver(
DestructionObserver* destruction_observer) {
DCHECK_EQ(this, current());
@@ -299,11 +352,13 @@ void MessageLoop::PostNonNestableDelayedTask(
}
void MessageLoop::Run() {
+ DCHECK(!lazy_init_options_);
RunLoop run_loop;
run_loop.Run();
}
void MessageLoop::RunUntilIdle() {
+ DCHECK(!lazy_init_options_);
RunLoop run_loop;
run_loop.RunUntilIdle();
}
@@ -383,13 +438,34 @@ bool MessageLoop::IsIdleForTesting() {
//------------------------------------------------------------------------------
+MessageLoop::MessageLoop(scoped_ptr<LazyInitOptions> options)
+ : type_(GetMessageLoopType(options.get())),
+#if defined(OS_WIN)
+ pending_high_res_tasks_(0),
+ in_high_res_mode_(false),
+#endif
+ nestable_tasks_allowed_(true),
+#if defined(OS_WIN)
+ os_modal_loop_(false),
+#endif // OS_WIN
+ lazy_init_options_(options.Pass()),
+ message_histogram_(NULL),
+ run_loop_(NULL) {
+ incoming_task_queue_ =
+ internal::IncomingTaskQueue::CreateForLazyInitMessageLoop(this);
+ message_loop_proxy_ =
+ internal::MessageLoopProxyImpl::CreateForLazyInit(incoming_task_queue_);
+}
+
void MessageLoop::Init() {
+ DCHECK(!lazy_init_options_) << "should not be called when created for "
+ "lazy initialization";
DCHECK(!current()) << "should only have one message loop per thread";
lazy_tls_ptr.Pointer()->Set(this);
incoming_task_queue_ = new internal::IncomingTaskQueue(this);
message_loop_proxy_ =
- new internal::MessageLoopProxyImpl(incoming_task_queue_);
+ internal::MessageLoopProxyImpl::Create(incoming_task_queue_);
thread_task_runner_handle_.reset(
new ThreadTaskRunnerHandle(message_loop_proxy_));
}

Powered by Google App Engine
This is Rietveld 408576698