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

Side by Side 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: 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 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
7 7
8 #include <queue> 8 #include <queue>
9 #include <string> 9 #include <string>
10 10
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 enum Type { 102 enum Type {
103 TYPE_DEFAULT, 103 TYPE_DEFAULT,
104 TYPE_UI, 104 TYPE_UI,
105 TYPE_CUSTOM, 105 TYPE_CUSTOM,
106 TYPE_IO, 106 TYPE_IO,
107 #if defined(OS_ANDROID) 107 #if defined(OS_ANDROID)
108 TYPE_JAVA, 108 TYPE_JAVA,
109 #endif // defined(OS_ANDROID) 109 #endif // defined(OS_ANDROID)
110 }; 110 };
111 111
112 // Init options for CreateForLazyInit().
113 struct BASE_EXPORT LazyInitOptions {
114 using MessagePumpFactory = Callback<scoped_ptr<MessagePump>()>;
115 LazyInitOptions();
116 ~LazyInitOptions();
117
118 // Specify the type of message loop that will be initialized in LazyInit.
119 // This is ignored if message_pump_factory.is_null() is false.
120 MessageLoop::Type message_loop_type;
121
122 // Specify timer slack for the message loop.
123 TimerSlack timer_slack;
124
125 // Used to create the MessagePump for the MessageLoop in LazyInit.
126 // If message_pump_factory.is_null() is true, then a MessagePump appropriate
127 // for |message_loop_type| is created. Setting this forces the
128 // MessageLoop::Type to TYPE_CUSTOM.
129 MessagePumpFactory message_pump_factory;
130 };
131
112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it 132 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
113 // is typical to make use of the current thread's MessageLoop instance. 133 // is typical to make use of the current thread's MessageLoop instance.
114 explicit MessageLoop(Type type = TYPE_DEFAULT); 134 explicit MessageLoop(Type type = TYPE_DEFAULT);
115 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must 135 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must
116 // be non-NULL. 136 // be non-NULL.
117 explicit MessageLoop(scoped_ptr<base::MessagePump> pump); 137 explicit MessageLoop(scoped_ptr<MessagePump> pump);
118 ~MessageLoop() override; 138 ~MessageLoop() override;
119 139
140 // Creates a MessageLoop for lazy initialization. This can be called on a
141 // different thread from the final 'current' thread of this message loop
142 // to pre-construct a message loop that accepts incoming tasks before
143 // the thread becomes ready to run. The caller must call LazyInit method
144 // on the final 'current' thread before calling Run to start running
145 // tasks.
146 //
147 // Note that until LazyInit() is called on the final 'current' thread
148 // only "PostTask" family methods can be called to 'queue' a task.
149 // Usually this should not be a problem in production code, because
150 // almost all other methods (except for ScheduleWork) are allowed to be
151 // called only on the final 'current' thread where the message loop's Run
152 // method runs, and no tasks can run there until the message loop actually
153 // spins up and calls Run, which must happen after LazyInit.
154 //
155 // Typical usage of CreateForLazyInit should look like following:
156 //
157 // // On the calling thread:
158 // {
159 // scoped_ptr<MessageLoop::LazyInitOptions> options(new
160 // MessageLoop::LazyInitOptions);
161 // options->message_loop_type = YOUR_MESSAGE_LOOP_TYPE;
162 // options->timer_slack = YOUR_TIMER_SLACK;
163 // ...
164 //
165 // message_loop_ = MessageLoop::CreateForLazyInit(options.Pass());
166 // // Right after here the caller can start posting tasks to this
167 // // message loop, though it won't run until LazyInit and Run is
168 // // called on the target thread.
169 //
170 // PlatformThread::Create(...);
171 // }
172 //
173 // // On the target thread where the message loop actually runs:
174 // void ThreadMain() {
175 // // Do the remaining initialization. This also sets up current().
176 // message_loop_->LazyInit();
177 //
178 // // Start running the loop.
179 // message_loop_->Run();
180 // }
181 //
182 static MessageLoop* CreateForLazyInit(scoped_ptr<LazyInitOptions> options);
rvargas (doing something else) 2015/03/26 02:19:27 should return a scoped_ptr
rvargas (doing something else) 2015/03/26 02:19:27 nit: Do we really need the "ForLazyInit" part?. Th
kinuko 2015/04/13 02:02:59 Done.
183
120 // Returns the MessageLoop object for the current thread, or null if none. 184 // Returns the MessageLoop object for the current thread, or null if none.
121 static MessageLoop* current(); 185 static MessageLoop* current();
122 186
123 static void EnableHistogrammer(bool enable_histogrammer); 187 static void EnableHistogrammer(bool enable_histogrammer);
124 188
125 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); 189 typedef scoped_ptr<MessagePump> (MessagePumpFactory)();
126 // Uses the given base::MessagePumpForUIFactory to override the default 190 // Uses the given base::MessagePumpForUIFactory to override the default
127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory 191 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
128 // was successfully registered. 192 // was successfully registered.
129 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); 193 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
(...skipping 10 matching lines...) Expand all
140 // not be run. Instead, they will be deleted. 204 // not be run. Instead, they will be deleted.
141 // 205 //
142 class BASE_EXPORT DestructionObserver { 206 class BASE_EXPORT DestructionObserver {
143 public: 207 public:
144 virtual void WillDestroyCurrentMessageLoop() = 0; 208 virtual void WillDestroyCurrentMessageLoop() = 0;
145 209
146 protected: 210 protected:
147 virtual ~DestructionObserver(); 211 virtual ~DestructionObserver();
148 }; 212 };
149 213
214 // Configure various members and bind this message loop to the current thread.
215 // This must be called on the final 'current' thread of this message loop.
216 // It is not valid to call this unless this message loop is constructed by
217 // CreateForLazyInit.
218 // (See the comment at CreateForLazyInit for more details)
219 void LazyInit();
220
150 // Add a DestructionObserver, which will start receiving notifications 221 // Add a DestructionObserver, which will start receiving notifications
151 // immediately. 222 // immediately.
152 void AddDestructionObserver(DestructionObserver* destruction_observer); 223 void AddDestructionObserver(DestructionObserver* destruction_observer);
153 224
154 // Remove a DestructionObserver. It is safe to call this method while a 225 // Remove a DestructionObserver. It is safe to call this method while a
155 // DestructionObserver is receiving a notification callback. 226 // DestructionObserver is receiving a notification callback.
156 void RemoveDestructionObserver(DestructionObserver* destruction_observer); 227 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
157 228
158 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. 229 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces.
159 // TODO(skyostil): Remove these functions (crbug.com/465354). 230 // TODO(skyostil): Remove these functions (crbug.com/465354).
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 // Runs the specified PendingTask. 476 // Runs the specified PendingTask.
406 void RunTask(const PendingTask& pending_task); 477 void RunTask(const PendingTask& pending_task);
407 478
408 //---------------------------------------------------------------------------- 479 //----------------------------------------------------------------------------
409 protected: 480 protected:
410 scoped_ptr<MessagePump> pump_; 481 scoped_ptr<MessagePump> pump_;
411 482
412 private: 483 private:
413 friend class RunLoop; 484 friend class RunLoop;
414 485
415 // Configures various members for the two constructors. 486 // Private constructor for CreateForLazyInit().
487 explicit MessageLoop(scoped_ptr<LazyInitOptions> options);
488
489 // Configures various members for the two public constructors.
416 void Init(); 490 void Init();
417 491
418 // Invokes the actual run loop using the message pump. 492 // Invokes the actual run loop using the message pump.
419 void RunHandler(); 493 void RunHandler();
420 494
421 // Called to process any delayed non-nestable tasks. 495 // Called to process any delayed non-nestable tasks.
422 bool ProcessNextDelayedNonNestableTask(); 496 bool ProcessNextDelayedNonNestableTask();
423 497
424 // Calls RunTask or queues the pending_task on the deferred task list if it 498 // Calls RunTask or queues the pending_task on the deferred task list if it
425 // cannot be run right now. Returns true if the task was run. 499 // cannot be run right now. Returns true if the task was run.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 // A recursion block that prevents accidentally running additional tasks when 557 // A recursion block that prevents accidentally running additional tasks when
484 // insider a (accidentally induced?) nested message pump. 558 // insider a (accidentally induced?) nested message pump.
485 bool nestable_tasks_allowed_; 559 bool nestable_tasks_allowed_;
486 560
487 #if defined(OS_WIN) 561 #if defined(OS_WIN)
488 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc 562 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
489 // which enter a modal message loop. 563 // which enter a modal message loop.
490 bool os_modal_loop_; 564 bool os_modal_loop_;
491 #endif 565 #endif
492 566
567 // Non-null only when this message loop is constructed by CreateForLazyInit.
568 scoped_ptr<LazyInitOptions> lazy_init_options_;
569
493 std::string thread_name_; 570 std::string thread_name_;
494 // A profiling histogram showing the counts of various messages and events. 571 // A profiling histogram showing the counts of various messages and events.
495 HistogramBase* message_histogram_; 572 HistogramBase* message_histogram_;
496 573
497 RunLoop* run_loop_; 574 RunLoop* run_loop_;
498 575
499 ObserverList<TaskObserver> task_observers_; 576 ObserverList<TaskObserver> task_observers_;
500 577
501 debug::TaskAnnotator task_annotator_; 578 debug::TaskAnnotator task_annotator_;
502 579
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 731
655 // Do not add any member variables to MessageLoopForIO! This is important b/c 732 // Do not add any member variables to MessageLoopForIO! This is important b/c
656 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 733 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
657 // data that you need should be stored on the MessageLoop's pump_ instance. 734 // data that you need should be stored on the MessageLoop's pump_ instance.
658 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 735 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
659 MessageLoopForIO_should_not_have_extra_member_variables); 736 MessageLoopForIO_should_not_have_extra_member_variables);
660 737
661 } // namespace base 738 } // namespace base
662 739
663 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ 740 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698