OLD | NEW |
---|---|
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 <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/base_export.h" | 12 #include "base/base_export.h" |
13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
14 #include "base/debug/task_annotator.h" | 14 #include "base/debug/task_annotator.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/message_loop/incoming_task_queue.h" | 18 #include "base/message_loop/incoming_task_queue.h" |
19 #include "base/message_loop/message_loop_task_runner.h" | 19 #include "base/message_loop/message_loop_task_runner.h" |
20 #include "base/message_loop/message_pump.h" | 20 #include "base/message_loop/message_pump.h" |
21 #include "base/message_loop/timer_slack.h" | 21 #include "base/message_loop/timer_slack.h" |
22 #include "base/observer_list.h" | 22 #include "base/observer_list.h" |
23 #include "base/pending_task.h" | 23 #include "base/pending_task.h" |
24 #include "base/run_loop.h" | |
24 #include "base/synchronization/lock.h" | 25 #include "base/synchronization/lock.h" |
25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
26 #include "build/build_config.h" | 27 #include "build/build_config.h" |
27 | 28 |
28 // TODO(sky): these includes should not be necessary. Nuke them. | 29 // TODO(sky): these includes should not be necessary. Nuke them. |
29 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
30 #include "base/message_loop/message_pump_win.h" | 31 #include "base/message_loop/message_pump_win.h" |
31 #elif defined(OS_IOS) | 32 #elif defined(OS_IOS) |
32 #include "base/message_loop/message_pump_io_ios.h" | 33 #include "base/message_loop/message_pump_io_ios.h" |
33 #elif defined(OS_POSIX) | 34 #elif defined(OS_POSIX) |
34 #include "base/message_loop/message_pump_libevent.h" | 35 #include "base/message_loop/message_pump_libevent.h" |
35 #endif | 36 #endif |
36 | 37 |
37 #if defined(OS_ANDROID) | 38 #if defined(OS_ANDROID) |
38 namespace base { | 39 namespace base { |
39 namespace android { | 40 namespace android { |
40 | 41 |
41 class JavaMessageHandlerFactory; | 42 class JavaMessageHandlerFactory; |
42 | 43 |
43 } // namespace android | 44 } // namespace android |
44 } // namespace base | 45 } // namespace base |
45 #endif // defined(OS_ANDROID) | 46 #endif // defined(OS_ANDROID) |
46 | 47 |
47 namespace base { | 48 namespace base { |
48 | 49 |
49 class RunLoop; | |
50 class ThreadTaskRunnerHandle; | 50 class ThreadTaskRunnerHandle; |
51 class WaitableEvent; | 51 class WaitableEvent; |
52 | 52 |
53 // A MessageLoop is used to process events for a particular thread. There is | 53 // A MessageLoop is used to process events for a particular thread. There is |
54 // at most one MessageLoop instance per thread. | 54 // at most one MessageLoop instance per thread. |
55 // | 55 // |
56 // Events include at a minimum Task instances submitted to the MessageLoop's | 56 // Events include at a minimum Task instances submitted to the MessageLoop's |
57 // TaskRunner. Depending on the type of message pump used by the MessageLoop | 57 // TaskRunner. Depending on the type of message pump used by the MessageLoop |
58 // other events such as UI messages may be processed. On Windows APC calls (as | 58 // other events such as UI messages may be processed. On Windows APC calls (as |
59 // time permits) and signals sent to a registered set of HANDLEs may also be | 59 // time permits) and signals sent to a registered set of HANDLEs may also be |
(...skipping 14 matching lines...) Expand all Loading... | |
74 // HRESULT hr; | 74 // HRESULT hr; |
75 // { | 75 // { |
76 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 76 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
77 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. | 77 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
78 // } | 78 // } |
79 // // Process |hr| (the result returned by DoDragDrop()). | 79 // // Process |hr| (the result returned by DoDragDrop()). |
80 // | 80 // |
81 // Please be SURE your task is reentrant (nestable) and all global variables | 81 // Please be SURE your task is reentrant (nestable) and all global variables |
82 // are stable and accessible before calling SetNestableTasksAllowed(true). | 82 // are stable and accessible before calling SetNestableTasksAllowed(true). |
83 // | 83 // |
84 class BASE_EXPORT MessageLoop : public MessagePump::Delegate { | 84 class BASE_EXPORT MessageLoop : public MessagePump::Delegate, |
85 public RunLoop::Delegate { | |
85 public: | 86 public: |
86 // A MessageLoop has a particular type, which indicates the set of | 87 // A MessageLoop has a particular type, which indicates the set of |
87 // asynchronous events it may process in addition to tasks and timers. | 88 // asynchronous events it may process in addition to tasks and timers. |
88 // | 89 // |
89 // TYPE_DEFAULT | 90 // TYPE_DEFAULT |
90 // This type of ML only supports tasks and timers. | 91 // This type of ML only supports tasks and timers. |
91 // | 92 // |
92 // TYPE_UI | 93 // TYPE_UI |
93 // This type of ML also supports native UI events (e.g., Windows messages). | 94 // This type of ML also supports native UI events (e.g., Windows messages). |
94 // See also MessageLoopForUI. | 95 // See also MessageLoopForUI. |
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
313 OnceCallback<std::unique_ptr<MessagePump>()>; | 314 OnceCallback<std::unique_ptr<MessagePump>()>; |
314 | 315 |
315 // Common protected constructor. Other constructors delegate the | 316 // Common protected constructor. Other constructors delegate the |
316 // initialization to this constructor. | 317 // initialization to this constructor. |
317 // A subclass can invoke this constructor to create a message_loop of a | 318 // A subclass can invoke this constructor to create a message_loop of a |
318 // specific type with a custom loop. The implementation does not call | 319 // specific type with a custom loop. The implementation does not call |
319 // BindToCurrentThread. If this constructor is invoked directly by a subclass, | 320 // BindToCurrentThread. If this constructor is invoked directly by a subclass, |
320 // then the subclass must subsequently bind the message loop. | 321 // then the subclass must subsequently bind the message loop. |
321 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); | 322 MessageLoop(Type type, MessagePumpFactoryCallback pump_factory); |
322 | 323 |
323 // Configure various members and bind this message loop to the current thread. | |
324 void BindToCurrentThread(); | |
325 | |
326 private: | 324 private: |
327 friend class internal::IncomingTaskQueue; | 325 friend class internal::IncomingTaskQueue; |
328 friend class RunLoop; | |
329 friend class ScheduleWorkTest; | 326 friend class ScheduleWorkTest; |
330 friend class Thread; | 327 friend class Thread; |
331 friend struct PendingTask; | 328 friend struct PendingTask; |
332 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); | 329 FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop); |
333 friend class PendingTaskTest; | 330 friend class PendingTaskTest; |
334 | 331 |
335 // Creates a MessageLoop without binding to a thread. | 332 // Creates a MessageLoop without binding to a thread. |
336 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given | 333 // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given |
337 // to create a message pump for this message loop. Otherwise a default | 334 // to create a message pump for this message loop. Otherwise a default |
338 // message pump for the |type| is created. | 335 // message pump for the |type| is created. |
339 // | 336 // |
340 // It is valid to call this to create a new message loop on one thread, | 337 // It is valid to call this to create a new message loop on one thread, |
341 // and then pass it to the thread where the message loop actually runs. | 338 // and then pass it to the thread where the message loop actually runs. |
342 // The message loop's BindToCurrentThread() method must be called on the | 339 // The message loop's BindToCurrentThread() method must be called on the |
343 // thread the message loop runs on, before calling Run(). | 340 // thread the message loop runs on, before calling Run(). |
344 // Before BindToCurrentThread() is called, only Post*Task() functions can | 341 // Before BindToCurrentThread() is called, only Post*Task() functions can |
345 // be called on the message loop. | 342 // be called on the message loop. |
346 static std::unique_ptr<MessageLoop> CreateUnbound( | 343 static std::unique_ptr<MessageLoop> CreateUnbound( |
347 Type type, | 344 Type type, |
348 MessagePumpFactoryCallback pump_factory); | 345 MessagePumpFactoryCallback pump_factory); |
349 | 346 |
350 // Sets the ThreadTaskRunnerHandle for the current thread to point to the | 347 // Sets the ThreadTaskRunnerHandle for the current thread to point to the |
351 // task runner for this message loop. | 348 // task runner for this message loop. |
352 void SetThreadTaskRunnerHandle(); | 349 void SetThreadTaskRunnerHandle(); |
353 | 350 |
354 // Invokes the actual run loop using the message pump. | 351 // RunLoop::Delegate: |
danakj
2017/05/15 16:23:13
nit: "RunLoop::Delegate implementation." ?
gab
2017/05/15 17:28:35
I've been told to do the opposite (this CL's versi
| |
355 void RunHandler(); | 352 void BindToCurrentThread() override; |
353 void Run() override; | |
354 void Quit() override; | |
356 | 355 |
357 // Called to process any delayed non-nestable tasks. | 356 // Called to process any delayed non-nestable tasks. |
358 bool ProcessNextDelayedNonNestableTask(); | 357 bool ProcessNextDelayedNonNestableTask(); |
359 | 358 |
360 // Calls RunTask or queues the pending_task on the deferred task list if it | 359 // Calls RunTask or queues the pending_task on the deferred task list if it |
361 // cannot be run right now. Returns true if the task was run. | 360 // cannot be run right now. Returns true if the task was run. |
362 bool DeferOrRunPendingTask(PendingTask pending_task); | 361 bool DeferOrRunPendingTask(PendingTask pending_task); |
363 | 362 |
364 // Adds the pending task to delayed_work_queue_. | 363 // Adds the pending task to delayed_work_queue_. |
365 void AddToDelayedWorkQueue(PendingTask pending_task); | 364 void AddToDelayedWorkQueue(PendingTask pending_task); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
412 ObserverList<DestructionObserver> destruction_observers_; | 411 ObserverList<DestructionObserver> destruction_observers_; |
413 | 412 |
414 // A recursion block that prevents accidentally running additional tasks when | 413 // A recursion block that prevents accidentally running additional tasks when |
415 // insider a (accidentally induced?) nested message pump. | 414 // insider a (accidentally induced?) nested message pump. |
416 bool nestable_tasks_allowed_; | 415 bool nestable_tasks_allowed_; |
417 | 416 |
418 // pump_factory_.Run() is called to create a message pump for this loop | 417 // pump_factory_.Run() is called to create a message pump for this loop |
419 // if type_ is TYPE_CUSTOM and pump_ is null. | 418 // if type_ is TYPE_CUSTOM and pump_ is null. |
420 MessagePumpFactoryCallback pump_factory_; | 419 MessagePumpFactoryCallback pump_factory_; |
421 | 420 |
422 RunLoop* run_loop_; | |
423 | |
424 ObserverList<TaskObserver> task_observers_; | 421 ObserverList<TaskObserver> task_observers_; |
425 | 422 |
426 debug::TaskAnnotator task_annotator_; | 423 debug::TaskAnnotator task_annotator_; |
427 | 424 |
428 // Used to allow creating a breadcrumb of program counters in PostTask. | 425 // Used to allow creating a breadcrumb of program counters in PostTask. |
429 // This variable is only initialized while a task is being executed and is | 426 // This variable is only initialized while a task is being executed and is |
430 // meant only to store context for creating a backtrace breadcrumb. Do not | 427 // meant only to store context for creating a backtrace breadcrumb. Do not |
431 // attach other semantics to it without thinking through the use caes | 428 // attach other semantics to it without thinking through the use caes |
432 // thoroughly. | 429 // thoroughly. |
433 const PendingTask* current_pending_task_; | 430 const PendingTask* current_pending_task_; |
434 | 431 |
435 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; | 432 scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_; |
436 | 433 |
437 // A task runner which we haven't bound to a thread yet. | 434 // A task runner which we haven't bound to a thread yet. |
438 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 435 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
439 | 436 |
440 // The task runner associated with this message loop. | 437 // The task runner associated with this message loop. |
441 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 438 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
442 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 439 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
443 | 440 |
444 // Id of the thread this message loop is bound to. Initialized once when the | 441 // Id of the thread this message loop is bound to. Initialized once when the |
445 // MessageLoop is bound to its thread and constant forever after. | 442 // MessageLoop is bound to its thread and constant forever after. |
446 PlatformThreadId thread_id_; | 443 PlatformThreadId thread_id_; |
447 | 444 |
448 // Whether this MessageLoop is currently running in nested RunLoops. | |
449 bool is_nested_ = false; | |
450 | |
451 // Whether task observers are allowed. | 445 // Whether task observers are allowed. |
452 bool allow_task_observers_ = true; | 446 bool allow_task_observers_ = true; |
453 | 447 |
454 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 448 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
455 }; | 449 }; |
456 | 450 |
457 #if !defined(OS_NACL) | 451 #if !defined(OS_NACL) |
458 | 452 |
459 //----------------------------------------------------------------------------- | 453 //----------------------------------------------------------------------------- |
460 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 454 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
591 | 585 |
592 // Do not add any member variables to MessageLoopForIO! This is important b/c | 586 // Do not add any member variables to MessageLoopForIO! This is important b/c |
593 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 587 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
594 // data that you need should be stored on the MessageLoop's pump_ instance. | 588 // data that you need should be stored on the MessageLoop's pump_ instance. |
595 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 589 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
596 "MessageLoopForIO should not have extra member variables"); | 590 "MessageLoopForIO should not have extra member variables"); |
597 | 591 |
598 } // namespace base | 592 } // namespace base |
599 | 593 |
600 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 594 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
OLD | NEW |