| 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/location.h" | |
| 17 #include "base/macros.h" | 16 #include "base/macros.h" |
| 18 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 19 #include "base/message_loop/incoming_task_queue.h" | 18 #include "base/message_loop/incoming_task_queue.h" |
| 20 #include "base/message_loop/message_loop_task_runner.h" | 19 #include "base/message_loop/message_loop_task_runner.h" |
| 21 #include "base/message_loop/message_pump.h" | 20 #include "base/message_loop/message_pump.h" |
| 22 #include "base/message_loop/timer_slack.h" | 21 #include "base/message_loop/timer_slack.h" |
| 23 #include "base/observer_list.h" | 22 #include "base/observer_list.h" |
| 24 #include "base/pending_task.h" | 23 #include "base/pending_task.h" |
| 25 #include "base/sequenced_task_runner_helpers.h" | |
| 26 #include "base/synchronization/lock.h" | 24 #include "base/synchronization/lock.h" |
| 27 #include "base/time/time.h" | 25 #include "base/time/time.h" |
| 28 #include "base/tracking_info.h" | |
| 29 #include "build/build_config.h" | 26 #include "build/build_config.h" |
| 30 | 27 |
| 31 // TODO(sky): these includes should not be necessary. Nuke them. | 28 // TODO(sky): these includes should not be necessary. Nuke them. |
| 32 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
| 33 #include "base/message_loop/message_pump_win.h" | 30 #include "base/message_loop/message_pump_win.h" |
| 34 #elif defined(OS_IOS) | 31 #elif defined(OS_IOS) |
| 35 #include "base/message_loop/message_pump_io_ios.h" | 32 #include "base/message_loop/message_pump_io_ios.h" |
| 36 #elif defined(OS_POSIX) | 33 #elif defined(OS_POSIX) |
| 37 #include "base/message_loop/message_pump_libevent.h" | 34 #include "base/message_loop/message_pump_libevent.h" |
| 38 #endif | 35 #endif |
| (...skipping 11 matching lines...) Expand all Loading... |
| 50 namespace base { | 47 namespace base { |
| 51 | 48 |
| 52 class HistogramBase; | 49 class HistogramBase; |
| 53 class RunLoop; | 50 class RunLoop; |
| 54 class ThreadTaskRunnerHandle; | 51 class ThreadTaskRunnerHandle; |
| 55 class WaitableEvent; | 52 class WaitableEvent; |
| 56 | 53 |
| 57 // A MessageLoop is used to process events for a particular thread. There is | 54 // A MessageLoop is used to process events for a particular thread. There is |
| 58 // at most one MessageLoop instance per thread. | 55 // at most one MessageLoop instance per thread. |
| 59 // | 56 // |
| 60 // Events include at a minimum Task instances submitted to PostTask and its | 57 // Events include at a minimum Task instances submitted to the MessageLoop's |
| 61 // variants. Depending on the type of message pump used by the MessageLoop | 58 // TaskRunner. Depending on the type of message pump used by the MessageLoop |
| 62 // other events such as UI messages may be processed. On Windows APC calls (as | 59 // other events such as UI messages may be processed. On Windows APC calls (as |
| 63 // time permits) and signals sent to a registered set of HANDLEs may also be | 60 // time permits) and signals sent to a registered set of HANDLEs may also be |
| 64 // processed. | 61 // processed. |
| 65 // | 62 // |
| 66 // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called | 63 // NOTE: Unless otherwise specified, a MessageLoop's methods may only be called |
| 67 // on the thread where the MessageLoop's Run method executes. | 64 // on the thread where the MessageLoop's Run method executes. |
| 68 // | 65 // |
| 69 // NOTE: MessageLoop has task reentrancy protection. This means that if a | 66 // NOTE: MessageLoop has task reentrancy protection. This means that if a |
| 70 // task is being processed, a second task cannot start until the first task is | 67 // task is being processed, a second task cannot start until the first task is |
| 71 // finished. Reentrancy can happen when processing a task, and an inner | 68 // finished. Reentrancy can happen when processing a task, and an inner |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 public: | 171 public: |
| 175 virtual void OnBeginNestedMessageLoop() = 0; | 172 virtual void OnBeginNestedMessageLoop() = 0; |
| 176 | 173 |
| 177 protected: | 174 protected: |
| 178 virtual ~NestingObserver(); | 175 virtual ~NestingObserver(); |
| 179 }; | 176 }; |
| 180 | 177 |
| 181 void AddNestingObserver(NestingObserver* observer); | 178 void AddNestingObserver(NestingObserver* observer); |
| 182 void RemoveNestingObserver(NestingObserver* observer); | 179 void RemoveNestingObserver(NestingObserver* observer); |
| 183 | 180 |
| 184 #if !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 185 // NOTE: Deprecated; prefer task_runner() and the TaskRunner interfaces. | |
| 186 // TODO(skyostil): Remove these functions (crbug.com/465354). | |
| 187 // | |
| 188 // The "PostTask" family of methods call the task's Run method asynchronously | |
| 189 // from within a message loop at some point in the future. | |
| 190 // | |
| 191 // With the PostTask variant, tasks are invoked in FIFO order, inter-mixed | |
| 192 // with normal UI or IO event processing. With the PostDelayedTask variant, | |
| 193 // tasks are called after at least approximately 'delay_ms' have elapsed. | |
| 194 // | |
| 195 // The NonNestable variants work similarly except that they promise never to | |
| 196 // dispatch the task from a nested invocation of MessageLoop::Run. Instead, | |
| 197 // such tasks get deferred until the top-most MessageLoop::Run is executing. | |
| 198 // | |
| 199 // The MessageLoop takes ownership of the Task, and deletes it after it has | |
| 200 // been Run(). | |
| 201 // | |
| 202 // PostTask(from_here, task) is equivalent to | |
| 203 // PostDelayedTask(from_here, task, 0). | |
| 204 // | |
| 205 // NOTE: These methods may be called on any thread. The Task will be invoked | |
| 206 // on the thread that executes MessageLoop::Run(). | |
| 207 void PostTask(const tracked_objects::Location& from_here, | |
| 208 const Closure& task); | |
| 209 | |
| 210 void PostDelayedTask(const tracked_objects::Location& from_here, | |
| 211 const Closure& task, | |
| 212 TimeDelta delay); | |
| 213 | |
| 214 // A variant on PostTask that deletes the given object. This is useful | |
| 215 // if the object needs to live until the next run of the MessageLoop (for | |
| 216 // example, deleting a RenderProcessHost from within an IPC callback is not | |
| 217 // good). | |
| 218 // | |
| 219 // NOTE: This method may be called on any thread. The object will be deleted | |
| 220 // on the thread that executes MessageLoop::Run(). | |
| 221 template <class T> | |
| 222 void DeleteSoon(const tracked_objects::Location& from_here, const T* object) { | |
| 223 base::subtle::DeleteHelperInternal<T, void>::DeleteViaSequencedTaskRunner( | |
| 224 this, from_here, object); | |
| 225 } | |
| 226 | |
| 227 // A variant on PostTask that releases the given reference counted object | |
| 228 // (by calling its Release method). This is useful if the object needs to | |
| 229 // live until the next run of the MessageLoop, or if the object needs to be | |
| 230 // released on a particular thread. | |
| 231 // | |
| 232 // A common pattern is to manually increment the object's reference count | |
| 233 // (AddRef), clear the pointer, then issue a ReleaseSoon. The reference count | |
| 234 // is incremented manually to ensure clearing the pointer does not trigger a | |
| 235 // delete and to account for the upcoming decrement (ReleaseSoon). For | |
| 236 // example: | |
| 237 // | |
| 238 // scoped_refptr<Foo> foo = ... | |
| 239 // foo->AddRef(); | |
| 240 // Foo* raw_foo = foo.get(); | |
| 241 // foo = NULL; | |
| 242 // message_loop->ReleaseSoon(raw_foo); | |
| 243 // | |
| 244 // NOTE: This method may be called on any thread. The object will be | |
| 245 // released (and thus possibly deleted) on the thread that executes | |
| 246 // MessageLoop::Run(). If this is not the same as the thread that calls | |
| 247 // ReleaseSoon(FROM_HERE, ), then T MUST inherit from | |
| 248 // RefCountedThreadSafe<T>! | |
| 249 template <class T> | |
| 250 void ReleaseSoon(const tracked_objects::Location& from_here, | |
| 251 const T* object) { | |
| 252 base::subtle::ReleaseHelperInternal<T, void>::ReleaseViaSequencedTaskRunner( | |
| 253 this, from_here, object); | |
| 254 } | |
| 255 #endif // !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 256 | |
| 257 #if defined(OS_MACOSX) && !defined(OS_IOS) | 181 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 258 protected: | 182 protected: |
| 259 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 183 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 260 | 184 |
| 261 // Deprecated: use RunLoop instead. | 185 // Deprecated: use RunLoop instead. |
| 262 // Run the message loop. | 186 // Run the message loop. |
| 263 void Run(); | 187 void Run(); |
| 264 | 188 |
| 265 // Deprecated: use RunLoop instead. | 189 // Deprecated: use RunLoop instead. |
| 266 // Process all pending tasks, windows messages, etc., but don't wait/sleep. | 190 // Process all pending tasks, windows messages, etc., but don't wait/sleep. |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; | 485 scoped_refptr<internal::MessageLoopTaskRunner> unbound_task_runner_; |
| 562 | 486 |
| 563 // The task runner associated with this message loop. | 487 // The task runner associated with this message loop. |
| 564 scoped_refptr<SingleThreadTaskRunner> task_runner_; | 488 scoped_refptr<SingleThreadTaskRunner> task_runner_; |
| 565 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; | 489 std::unique_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 566 | 490 |
| 567 // Id of the thread this message loop is bound to. Initialized once when the | 491 // Id of the thread this message loop is bound to. Initialized once when the |
| 568 // MessageLoop is bound to its thread and constant forever after. | 492 // MessageLoop is bound to its thread and constant forever after. |
| 569 PlatformThreadId thread_id_; | 493 PlatformThreadId thread_id_; |
| 570 | 494 |
| 571 #if !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 572 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | |
| 573 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | |
| 574 | |
| 575 void DeleteSoonInternal(const tracked_objects::Location& from_here, | |
| 576 void(*deleter)(const void*), | |
| 577 const void* object); | |
| 578 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | |
| 579 void(*releaser)(const void*), | |
| 580 const void* object); | |
| 581 #endif // !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 582 | |
| 583 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 495 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 584 }; | 496 }; |
| 585 | 497 |
| 586 #if !defined(OS_NACL) | 498 #if !defined(OS_NACL) |
| 587 | 499 |
| 588 //----------------------------------------------------------------------------- | 500 //----------------------------------------------------------------------------- |
| 589 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 501 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 590 // MessageLoop instantiated with TYPE_UI. | 502 // MessageLoop instantiated with TYPE_UI. |
| 591 // | 503 // |
| 592 // This class is typically used like so: | 504 // This class is typically used like so: |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 726 | 638 |
| 727 // Do not add any member variables to MessageLoopForIO! This is important b/c | 639 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 728 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 640 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 729 // data that you need should be stored on the MessageLoop's pump_ instance. | 641 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 730 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 642 static_assert(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 731 "MessageLoopForIO should not have extra member variables"); | 643 "MessageLoopForIO should not have extra member variables"); |
| 732 | 644 |
| 733 } // namespace base | 645 } // namespace base |
| 734 | 646 |
| 735 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 647 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |