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.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 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 until
143 // LazyInit() is called.
144 // 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
145 // won't run before LazyInit() is called.
146 static MessageLoop* CreateForLazyInit(scoped_ptr<LazyInitOptions> options);
147
120 // Returns the MessageLoop object for the current thread, or null if none. 148 // Returns the MessageLoop object for the current thread, or null if none.
121 static MessageLoop* current(); 149 static MessageLoop* current();
122 150
123 static void EnableHistogrammer(bool enable_histogrammer); 151 static void EnableHistogrammer(bool enable_histogrammer);
124 152
125 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); 153 typedef scoped_ptr<MessagePump> (MessagePumpFactory)();
126 // Uses the given base::MessagePumpForUIFactory to override the default 154 // Uses the given base::MessagePumpForUIFactory to override the default
127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory 155 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
128 // was successfully registered. 156 // was successfully registered.
129 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); 157 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory);
(...skipping 10 matching lines...) Expand all
140 // not be run. Instead, they will be deleted. 168 // not be run. Instead, they will be deleted.
141 // 169 //
142 class BASE_EXPORT DestructionObserver { 170 class BASE_EXPORT DestructionObserver {
143 public: 171 public:
144 virtual void WillDestroyCurrentMessageLoop() = 0; 172 virtual void WillDestroyCurrentMessageLoop() = 0;
145 173
146 protected: 174 protected:
147 virtual ~DestructionObserver(); 175 virtual ~DestructionObserver();
148 }; 176 };
149 177
178 // Configure various members for the private constructor for
179 // CreateForLazyInit().
180 // This must be called on the final 'current' thread of this message loop.
181 // It is not valid to call this unless this message loop is constructed by
182 // CreateForLazyInit().
183 void LazyInit();
184
150 // Add a DestructionObserver, which will start receiving notifications 185 // Add a DestructionObserver, which will start receiving notifications
151 // immediately. 186 // immediately.
152 void AddDestructionObserver(DestructionObserver* destruction_observer); 187 void AddDestructionObserver(DestructionObserver* destruction_observer);
153 188
154 // Remove a DestructionObserver. It is safe to call this method while a 189 // Remove a DestructionObserver. It is safe to call this method while a
155 // DestructionObserver is receiving a notification callback. 190 // DestructionObserver is receiving a notification callback.
156 void RemoveDestructionObserver(DestructionObserver* destruction_observer); 191 void RemoveDestructionObserver(DestructionObserver* destruction_observer);
157 192
158 // The "PostTask" family of methods call the task's Run method asynchronously 193 // The "PostTask" family of methods call the task's Run method asynchronously
159 // from within a message loop at some point in the future. 194 // from within a message loop at some point in the future.
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 // Runs the specified PendingTask. 435 // Runs the specified PendingTask.
401 void RunTask(const PendingTask& pending_task); 436 void RunTask(const PendingTask& pending_task);
402 437
403 //---------------------------------------------------------------------------- 438 //----------------------------------------------------------------------------
404 protected: 439 protected:
405 scoped_ptr<MessagePump> pump_; 440 scoped_ptr<MessagePump> pump_;
406 441
407 private: 442 private:
408 friend class RunLoop; 443 friend class RunLoop;
409 444
410 // Configures various members for the two constructors. 445 // Private constructor for CreateForLazyInit().
446 explicit MessageLoop(scoped_ptr<LazyInitOptions> options);
447
448 // Configures various members for the two public constructors.
411 void Init(); 449 void Init();
412 450
413 // Invokes the actual run loop using the message pump. 451 // Invokes the actual run loop using the message pump.
414 void RunHandler(); 452 void RunHandler();
415 453
416 // Called to process any delayed non-nestable tasks. 454 // Called to process any delayed non-nestable tasks.
417 bool ProcessNextDelayedNonNestableTask(); 455 bool ProcessNextDelayedNonNestableTask();
418 456
419 // Calls RunTask or queues the pending_task on the deferred task list if it 457 // Calls RunTask or queues the pending_task on the deferred task list if it
420 // cannot be run right now. Returns true if the task was run. 458 // cannot be run right now. Returns true if the task was run.
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
478 // A recursion block that prevents accidentally running additional tasks when 516 // A recursion block that prevents accidentally running additional tasks when
479 // insider a (accidentally induced?) nested message pump. 517 // insider a (accidentally induced?) nested message pump.
480 bool nestable_tasks_allowed_; 518 bool nestable_tasks_allowed_;
481 519
482 #if defined(OS_WIN) 520 #if defined(OS_WIN)
483 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc 521 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
484 // which enter a modal message loop. 522 // which enter a modal message loop.
485 bool os_modal_loop_; 523 bool os_modal_loop_;
486 #endif 524 #endif
487 525
526 // Non-null only when this message loop is constructed by CreateForLazyInit.
527 scoped_ptr<LazyInitOptions> lazy_init_options_;
528
488 std::string thread_name_; 529 std::string thread_name_;
489 // A profiling histogram showing the counts of various messages and events. 530 // A profiling histogram showing the counts of various messages and events.
490 HistogramBase* message_histogram_; 531 HistogramBase* message_histogram_;
491 532
492 RunLoop* run_loop_; 533 RunLoop* run_loop_;
493 534
494 ObserverList<TaskObserver> task_observers_; 535 ObserverList<TaskObserver> task_observers_;
495 536
496 debug::TaskAnnotator task_annotator_; 537 debug::TaskAnnotator task_annotator_;
497 538
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 690
650 // Do not add any member variables to MessageLoopForIO! This is important b/c 691 // Do not add any member variables to MessageLoopForIO! This is important b/c
651 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 692 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
652 // data that you need should be stored on the MessageLoop's pump_ instance. 693 // data that you need should be stored on the MessageLoop's pump_ instance.
653 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 694 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
654 MessageLoopForIO_should_not_have_extra_member_variables); 695 MessageLoopForIO_should_not_have_extra_member_variables);
655 696
656 } // namespace base 697 } // namespace base
657 698
658 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ 699 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698