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

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: StartAndWait -> StartAndWaitForTesting 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
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.
113 struct BASE_EXPORT InitOptions {
114 using MessagePumpFactory = Callback<scoped_ptr<MessagePump>()>;
danakj 2015/04/24 20:54:39 nit: no () for the type name
kinuko 2015/04/27 16:36:04 It's for Callback typedef, it seems we need it.
115 InitOptions();
116 ~InitOptions();
117
118 // Specify the type of message loop.
danakj 2015/04/24 20:54:39 Comments for "why" not for "what", this doesn't sa
119 // This is ignored if message_pump_factory.is_null() is false.
danakj 2015/04/24 20:54:39 "This is only used if message_pump_factory is null
120 MessageLoop::Type message_loop_type;
121
122 // Specify timer slack for the message loop.
danakj 2015/04/24 20:54:39 Can you say what timer slack is, instead of just r
123 TimerSlack timer_slack;
124
125 // Used to create the MessagePump for the MessageLoop.
126 // If message_pump_factory.is_null() is true, then a MessagePump appropriate
danakj 2015/04/24 20:54:39 If message_pump_factory is null, then
127 // for |message_loop_type| is created. Setting this forces the
128 // MessageLoop::Type to TYPE_CUSTOM.
129 MessagePumpFactory message_pump_factory;
130 };
131
132 // Creates a MessageLoop with |options|. It is valid to create a new
133 // message loop on a different thread from the final 'current' thread,
Nico 2015/04/24 21:31:16 This sounds confusing to me. Maybe "It is valid t
kinuko 2015/04/27 16:36:04 Done.
134 // and then pass it to the final thread where the message loop actually
135 // runs. The caller must call Init method on the final 'current'
136 // thread before calling Run to start running tasks on the message loop.
137 //
112 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it 138 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it
113 // is typical to make use of the current thread's MessageLoop instance. 139 // is typical to make use of the current thread's MessageLoop instance.
140 explicit MessageLoop(const InitOptions& options);
danakj 2015/04/24 20:54:39 I'm a little surprised to see us using a struct he
kinuko 2015/04/27 16:36:04 I wanted to keep them together and wanted to make
141
142 // Creates a MessageLoop of |type| for the current thread. Usually this
143 // constructor is used only for testing. No need to call Init if the
danakj 2015/04/24 20:54:39 Do we need to keep these test-only constructors? O
kinuko 2015/04/27 16:36:05 We need these for now, while I agree that having t
danakj 2015/04/27 16:46:40 Can you leave TODOs (pointing to a bug) for things
144 // message loop is constructed this way.
114 explicit MessageLoop(Type type = TYPE_DEFAULT); 145 explicit MessageLoop(Type type = TYPE_DEFAULT);
115 // Creates a TYPE_CUSTOM MessageLoop with the supplied MessagePump, which must 146 // Creates a TYPE_CUSTOM MessageLoop for the current thread with the
116 // be non-NULL. 147 // supplied MessagePump, which must be non-NULL. Usually used only for
117 explicit MessageLoop(scoped_ptr<base::MessagePump> pump); 148 // testing. No need to call Init if the message loop is constructed this way.
149 explicit MessageLoop(scoped_ptr<MessagePump> pump);
118 ~MessageLoop() override; 150 ~MessageLoop() override;
119 151
120 // Returns the MessageLoop object for the current thread, or null if none. 152 // Returns the MessageLoop object for the current thread, or null if none.
121 static MessageLoop* current(); 153 static MessageLoop* current();
122 154
123 static void EnableHistogrammer(bool enable_histogrammer); 155 static void EnableHistogrammer(bool enable_histogrammer);
124 156
125 typedef scoped_ptr<MessagePump> (MessagePumpFactory)(); 157 typedef scoped_ptr<MessagePump> (MessagePumpFactory)();
126 // Uses the given base::MessagePumpForUIFactory to override the default 158 // Uses the given base::MessagePumpForUIFactory to override the default
127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory 159 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory
(...skipping 12 matching lines...) Expand all
140 // not be run. Instead, they will be deleted. 172 // not be run. Instead, they will be deleted.
141 // 173 //
142 class BASE_EXPORT DestructionObserver { 174 class BASE_EXPORT DestructionObserver {
143 public: 175 public:
144 virtual void WillDestroyCurrentMessageLoop() = 0; 176 virtual void WillDestroyCurrentMessageLoop() = 0;
145 177
146 protected: 178 protected:
147 virtual ~DestructionObserver(); 179 virtual ~DestructionObserver();
148 }; 180 };
149 181
182 // Configure various members and bind this message loop to the current thread.
183 void Init();
danakj 2015/04/24 20:54:40 BindToCurrentThread() ?
kinuko 2015/04/27 16:36:04 Done.
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 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. 193 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces.
159 // TODO(skyostil): Remove these functions (crbug.com/465354). 194 // TODO(skyostil): Remove these functions (crbug.com/465354).
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 // Can only be called from the thread that owns the MessageLoop. 422 // Can only be called from the thread that owns the MessageLoop.
388 bool is_running() const; 423 bool is_running() const;
389 424
390 // Returns true if the message loop has high resolution timers enabled. 425 // Returns true if the message loop has high resolution timers enabled.
391 // Provided for testing. 426 // Provided for testing.
392 bool HasHighResolutionTasks(); 427 bool HasHighResolutionTasks();
393 428
394 // Returns true if the message loop is "idle". Provided for testing. 429 // Returns true if the message loop is "idle". Provided for testing.
395 bool IsIdleForTesting(); 430 bool IsIdleForTesting();
396 431
397 // Wakes up the message pump. Can be called on any thread. The caller is
398 // responsible for synchronizing ScheduleWork() calls.
399 void ScheduleWork();
400
401 // Returns the TaskAnnotator which is used to add debug information to posted 432 // Returns the TaskAnnotator which is used to add debug information to posted
402 // tasks. 433 // tasks.
403 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } 434 debug::TaskAnnotator* task_annotator() { return &task_annotator_; }
404 435
405 // Runs the specified PendingTask. 436 // Runs the specified PendingTask.
406 void RunTask(const PendingTask& pending_task); 437 void RunTask(const PendingTask& pending_task);
407 438
408 //---------------------------------------------------------------------------- 439 //----------------------------------------------------------------------------
409 protected: 440 protected:
410 scoped_ptr<MessagePump> pump_; 441 scoped_ptr<MessagePump> pump_;
411 442
412 private: 443 private:
413 friend class RunLoop; 444 friend class RunLoop;
445 friend class internal::IncomingTaskQueue;
446 friend class ScheduleWorkTest;
414 447
415 // Configures various members for the two constructors. 448 // Wakes up the message pump. Can be called on any thread. The caller is
416 void Init(); 449 // responsible for synchronizing ScheduleWork() calls.
450 void ScheduleWork();
417 451
418 // Invokes the actual run loop using the message pump. 452 // Invokes the actual run loop using the message pump.
419 void RunHandler(); 453 void RunHandler();
420 454
421 // Called to process any delayed non-nestable tasks. 455 // Called to process any delayed non-nestable tasks.
422 bool ProcessNextDelayedNonNestableTask(); 456 bool ProcessNextDelayedNonNestableTask();
423 457
424 // Calls RunTask or queues the pending_task on the deferred task list if it 458 // 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. 459 // cannot be run right now. Returns true if the task was run.
426 bool DeferOrRunPendingTask(const PendingTask& pending_task); 460 bool DeferOrRunPendingTask(const PendingTask& pending_task);
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 // A recursion block that prevents accidentally running additional tasks when 517 // A recursion block that prevents accidentally running additional tasks when
484 // insider a (accidentally induced?) nested message pump. 518 // insider a (accidentally induced?) nested message pump.
485 bool nestable_tasks_allowed_; 519 bool nestable_tasks_allowed_;
486 520
487 #if defined(OS_WIN) 521 #if defined(OS_WIN)
488 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc 522 // Should be set to true before calling Windows APIs like TrackPopupMenu, etc
489 // which enter a modal message loop. 523 // which enter a modal message loop.
490 bool os_modal_loop_; 524 bool os_modal_loop_;
491 #endif 525 #endif
492 526
527 scoped_ptr<InitOptions> init_options_;
danakj 2015/04/24 20:54:39 why scoped_ptr and not just a member? i'd prefer a
kinuko 2015/04/27 16:36:04 Removed this field.
528
493 std::string thread_name_; 529 std::string thread_name_;
494 // A profiling histogram showing the counts of various messages and events. 530 // A profiling histogram showing the counts of various messages and events.
495 HistogramBase* message_histogram_; 531 HistogramBase* message_histogram_;
496 532
497 RunLoop* run_loop_; 533 RunLoop* run_loop_;
498 534
499 ObserverList<TaskObserver> task_observers_; 535 ObserverList<TaskObserver> task_observers_;
500 536
501 debug::TaskAnnotator task_annotator_; 537 debug::TaskAnnotator task_annotator_;
502 538
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
654 690
655 // 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
656 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra 692 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra
657 // 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.
658 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), 694 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO),
659 MessageLoopForIO_should_not_have_extra_member_variables); 695 MessageLoopForIO_should_not_have_extra_member_variables);
660 696
661 } // namespace base 697 } // namespace base
662 698
663 #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