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

Unified Diff: base/message_loop/message_loop.h

Issue 1011683002: Lazily initialize MessageLoop for faster thread startup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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.h
diff --git a/base/message_loop/message_loop.h b/base/message_loop/message_loop.h
index 7c76616f20f9123056c9d78dfcf1c964e752d017..2c822a4e7abccb6ee3493a157e165f3ff9ecfb9e 100644
--- a/base/message_loop/message_loop.h
+++ b/base/message_loop/message_loop.h
@@ -109,14 +109,42 @@ class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
#endif // defined(OS_ANDROID)
};
+ // Init options for CreateForLazyInit().
+ struct BASE_EXPORT LazyInitOptions {
+ using MessagePumpFactory = Callback<scoped_ptr<MessagePump>()>;
+ LazyInitOptions();
+ ~LazyInitOptions();
+
+ // Specify the type of message loop that will be initialized in LazyInit.
+ // This is ignored if message_pump_factory.is_null() is false.
+ MessageLoop::Type message_loop_type;
+
+ // Specify timer slack for the message loop.
+ TimerSlack timer_slack;
+
+ // Used to create the MessagePump for the MessageLoop in LazyInit.
+ // If message_pump_factory.is_null() is true, then a MessagePump appropriate
+ // for |message_loop_type| is created. Setting this forces the
+ // MessageLoop::Type to TYPE_CUSTOM.
+ MessagePumpFactory message_pump_factory;
+ };
+
// Normally, it is not necessary to instantiate a MessageLoop. Instead, it
// is typical to make use of the current thread's MessageLoop instance.
explicit MessageLoop(Type type = TYPE_DEFAULT);
// Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
// be non-NULL.
- explicit MessageLoop(scoped_ptr<base::MessagePump> pump);
+ explicit MessageLoop(scoped_ptr<MessagePump> pump);
~MessageLoop() override;
+ // Creates a MessageLoop for lazy initialization. This can be called on a
+ // different thread from the final 'current' thread of this message loop
+ // to pre-construct a message loop that accepts incoming tasks until
+ // LazyInit() is called.
+ // Note that only "PostTask" family methods can be called and posted tasks
DaleCurtis 2015/03/18 22:04:28 Seems error prone; what methods exactly shouldn't
kinuko 2015/03/19 03:08:16 To be sure, almost all other methods (except for S
+ // won't run before LazyInit() is called.
+ static MessageLoop* CreateForLazyInit(scoped_ptr<LazyInitOptions> options);
+
// Returns the MessageLoop object for the current thread, or null if none.
static MessageLoop* current();
@@ -147,6 +175,13 @@ class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
virtual ~DestructionObserver();
};
+ // Configure various members for the private constructor for
+ // CreateForLazyInit().
+ // This must be called on the final 'current' thread of this message loop.
+ // It is not valid to call this unless this message loop is constructed by
+ // CreateForLazyInit().
+ void LazyInit();
+
// Add a DestructionObserver, which will start receiving notifications
// immediately.
void AddDestructionObserver(DestructionObserver* destruction_observer);
@@ -407,7 +442,10 @@ class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
private:
friend class RunLoop;
- // Configures various members for the two constructors.
+ // Private constructor for CreateForLazyInit().
+ explicit MessageLoop(scoped_ptr<LazyInitOptions> options);
+
+ // Configures various members for the two public constructors.
void Init();
// Invokes the actual run loop using the message pump.
@@ -485,6 +523,9 @@ class BASE_EXPORT MessageLoop : public MessagePump::Delegate {
bool os_modal_loop_;
#endif
+ // Non-null only when this message loop is constructed by CreateForLazyInit.
+ scoped_ptr<LazyInitOptions> lazy_init_options_;
+
std::string thread_name_;
// A profiling histogram showing the counts of various messages and events.
HistogramBase* message_histogram_;

Powered by Google App Engine
This is Rietveld 408576698