| 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 <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/callback_forward.h" | 13 #include "base/callback_forward.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/message_loop/message_loop_proxy.h" | 16 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/message_pump.h" | 17 #include "base/message_loop/message_pump.h" |
| 18 #include "base/observer_list.h" | 18 #include "base/observer_list.h" |
| 19 #include "base/pending_task.h" | 19 #include "base/pending_task.h" |
| 20 #include "base/sequenced_task_runner_helpers.h" | 20 #include "base/sequenced_task_runner_helpers.h" |
| 21 #include "base/synchronization/lock.h" | 21 #include "base/synchronization/lock.h" |
| 22 #include "base/tracking_info.h" | 22 #include "base/tracking_info.h" |
| 23 #include "base/time.h" | 23 #include "base/time.h" |
| 24 | 24 |
| 25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 26 // We need this to declare base::MessagePumpWin::Dispatcher, which we should | 26 // We need this to declare base::MessagePumpWin::Dispatcher, which we should |
| 27 // really just eliminate. | 27 // really just eliminate. |
| 28 #include "base/message_pump_win.h" | 28 #include "base/message_loop/message_pump_win.h" |
| 29 #elif defined(OS_IOS) | 29 #elif defined(OS_IOS) |
| 30 #include "base/message_pump_io_ios.h" | 30 #include "base/message_loop/message_pump_io_ios.h" |
| 31 #elif defined(OS_POSIX) | 31 #elif defined(OS_POSIX) |
| 32 #include "base/message_pump_libevent.h" | 32 #include "base/message_loop/message_pump_libevent.h" |
| 33 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 33 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 34 | 34 |
| 35 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL) | 35 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL) |
| 36 #include "base/message_pump_aurax11.h" | 36 #include "base/message_loop/message_pump_aurax11.h" |
| 37 #elif defined(USE_OZONE) && !defined(OS_NACL) | 37 #elif defined(USE_OZONE) && !defined(OS_NACL) |
| 38 #include "base/message_pump_ozone.h" | 38 #include "base/message_loop/message_pump_ozone.h" |
| 39 #else | 39 #else |
| 40 #include "base/message_pump_gtk.h" | 40 #include "base/message_loop/message_pump_gtk.h" |
| 41 #endif | 41 #endif |
| 42 | 42 |
| 43 #endif | 43 #endif |
| 44 #endif | 44 #endif |
| 45 | 45 |
| 46 namespace base { | 46 namespace base { |
| 47 |
| 47 class HistogramBase; | 48 class HistogramBase; |
| 48 class MessageLoopLockTest; | 49 class MessageLoopLockTest; |
| 49 class RunLoop; | 50 class RunLoop; |
| 50 class ThreadTaskRunnerHandle; | 51 class ThreadTaskRunnerHandle; |
| 51 #if defined(OS_ANDROID) | 52 #if defined(OS_ANDROID) |
| 52 class MessagePumpForUI; | 53 class MessagePumpForUI; |
| 53 #endif | 54 #endif |
| 54 | 55 |
| 55 // A MessageLoop is used to process events for a particular thread. There is | 56 // A MessageLoop is used to process events for a particular thread. There is |
| 56 // at most one MessageLoop instance per thread. | 57 // at most one MessageLoop instance per thread. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 76 // HRESULT hr; | 77 // HRESULT hr; |
| 77 // { | 78 // { |
| 78 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | 79 // MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); |
| 79 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. | 80 // hr = DoDragDrop(...); // Implicitly runs a modal message loop. |
| 80 // } | 81 // } |
| 81 // // Process |hr| (the result returned by DoDragDrop()). | 82 // // Process |hr| (the result returned by DoDragDrop()). |
| 82 // | 83 // |
| 83 // Please be SURE your task is reentrant (nestable) and all global variables | 84 // Please be SURE your task is reentrant (nestable) and all global variables |
| 84 // are stable and accessible before calling SetNestableTasksAllowed(true). | 85 // are stable and accessible before calling SetNestableTasksAllowed(true). |
| 85 // | 86 // |
| 86 class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { | 87 class BASE_EXPORT MessageLoop : public MessagePump::Delegate { |
| 87 public: | 88 public: |
| 88 | 89 |
| 89 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 90 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 90 typedef base::MessagePumpDispatcher Dispatcher; | 91 typedef MessagePumpDispatcher Dispatcher; |
| 91 typedef base::MessagePumpObserver Observer; | 92 typedef MessagePumpObserver Observer; |
| 92 #endif | 93 #endif |
| 93 | 94 |
| 94 // A MessageLoop has a particular type, which indicates the set of | 95 // A MessageLoop has a particular type, which indicates the set of |
| 95 // asynchronous events it may process in addition to tasks and timers. | 96 // asynchronous events it may process in addition to tasks and timers. |
| 96 // | 97 // |
| 97 // TYPE_DEFAULT | 98 // TYPE_DEFAULT |
| 98 // This type of ML only supports tasks and timers. | 99 // This type of ML only supports tasks and timers. |
| 99 // | 100 // |
| 100 // TYPE_UI | 101 // TYPE_UI |
| 101 // This type of ML also supports native UI events (e.g., Windows messages). | 102 // This type of ML also supports native UI events (e.g., Windows messages). |
| (...skipping 12 matching lines...) Expand all Loading... |
| 114 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it | 115 // Normally, it is not necessary to instantiate a MessageLoop. Instead, it |
| 115 // is typical to make use of the current thread's MessageLoop instance. | 116 // is typical to make use of the current thread's MessageLoop instance. |
| 116 explicit MessageLoop(Type type = TYPE_DEFAULT); | 117 explicit MessageLoop(Type type = TYPE_DEFAULT); |
| 117 virtual ~MessageLoop(); | 118 virtual ~MessageLoop(); |
| 118 | 119 |
| 119 // Returns the MessageLoop object for the current thread, or null if none. | 120 // Returns the MessageLoop object for the current thread, or null if none. |
| 120 static MessageLoop* current(); | 121 static MessageLoop* current(); |
| 121 | 122 |
| 122 static void EnableHistogrammer(bool enable_histogrammer); | 123 static void EnableHistogrammer(bool enable_histogrammer); |
| 123 | 124 |
| 124 typedef base::MessagePump* (MessagePumpFactory)(); | 125 typedef MessagePump* (MessagePumpFactory)(); |
| 125 // Uses the given base::MessagePumpForUIFactory to override the default | 126 // Uses the given base::MessagePumpForUIFactory to override the default |
| 126 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory | 127 // MessagePump implementation for 'TYPE_UI'. Returns true if the factory |
| 127 // was successfully registered. | 128 // was successfully registered. |
| 128 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); | 129 static bool InitMessagePumpForUIFactory(MessagePumpFactory* factory); |
| 129 | 130 |
| 130 // A DestructionObserver is notified when the current MessageLoop is being | 131 // A DestructionObserver is notified when the current MessageLoop is being |
| 131 // destroyed. These observers are notified prior to MessageLoop::current() | 132 // destroyed. These observers are notified prior to MessageLoop::current() |
| 132 // being changed to return NULL. This gives interested parties the chance to | 133 // being changed to return NULL. This gives interested parties the chance to |
| 133 // do final cleanup that depends on the MessageLoop. | 134 // do final cleanup that depends on the MessageLoop. |
| 134 // | 135 // |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // | 168 // |
| 168 // PostTask(from_here, task) is equivalent to | 169 // PostTask(from_here, task) is equivalent to |
| 169 // PostDelayedTask(from_here, task, 0). | 170 // PostDelayedTask(from_here, task, 0). |
| 170 // | 171 // |
| 171 // The TryPostTask is meant for the cases where the calling thread cannot | 172 // The TryPostTask is meant for the cases where the calling thread cannot |
| 172 // block. If posting the task will block, the call returns false, the task | 173 // block. If posting the task will block, the call returns false, the task |
| 173 // is not posted but the task is consumed anyways. | 174 // is not posted but the task is consumed anyways. |
| 174 // | 175 // |
| 175 // NOTE: These methods may be called on any thread. The Task will be invoked | 176 // NOTE: These methods may be called on any thread. The Task will be invoked |
| 176 // on the thread that executes MessageLoop::Run(). | 177 // on the thread that executes MessageLoop::Run(). |
| 177 void PostTask( | 178 void PostTask(const tracked_objects::Location& from_here, |
| 178 const tracked_objects::Location& from_here, | 179 const Closure& task); |
| 179 const base::Closure& task); | |
| 180 | 180 |
| 181 bool TryPostTask( | 181 bool TryPostTask(const tracked_objects::Location& from_here, |
| 182 const tracked_objects::Location& from_here, | 182 const Closure& task); |
| 183 const base::Closure& task); | |
| 184 | 183 |
| 185 void PostDelayedTask( | 184 void PostDelayedTask(const tracked_objects::Location& from_here, |
| 186 const tracked_objects::Location& from_here, | 185 const Closure& task, |
| 187 const base::Closure& task, | 186 TimeDelta delay); |
| 188 base::TimeDelta delay); | |
| 189 | 187 |
| 190 void PostNonNestableTask( | 188 void PostNonNestableTask(const tracked_objects::Location& from_here, |
| 191 const tracked_objects::Location& from_here, | 189 const Closure& task); |
| 192 const base::Closure& task); | |
| 193 | 190 |
| 194 void PostNonNestableDelayedTask( | 191 void PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
| 195 const tracked_objects::Location& from_here, | 192 const Closure& task, |
| 196 const base::Closure& task, | 193 TimeDelta delay); |
| 197 base::TimeDelta delay); | |
| 198 | 194 |
| 199 // A variant on PostTask that deletes the given object. This is useful | 195 // A variant on PostTask that deletes the given object. This is useful |
| 200 // if the object needs to live until the next run of the MessageLoop (for | 196 // if the object needs to live until the next run of the MessageLoop (for |
| 201 // example, deleting a RenderProcessHost from within an IPC callback is not | 197 // example, deleting a RenderProcessHost from within an IPC callback is not |
| 202 // good). | 198 // good). |
| 203 // | 199 // |
| 204 // NOTE: This method may be called on any thread. The object will be deleted | 200 // NOTE: This method may be called on any thread. The object will be deleted |
| 205 // on the thread that executes MessageLoop::Run(). If this is not the same | 201 // on the thread that executes MessageLoop::Run(). If this is not the same |
| 206 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit | 202 // as the thread that calls PostDelayedTask(FROM_HERE, ), then T MUST inherit |
| 207 // from RefCountedThreadSafe<T>! | 203 // from RefCountedThreadSafe<T>! |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 // which nested run loop you are quitting, so be careful! | 253 // which nested run loop you are quitting, so be careful! |
| 258 void QuitWhenIdle(); | 254 void QuitWhenIdle(); |
| 259 | 255 |
| 260 // Deprecated: use RunLoop instead. | 256 // Deprecated: use RunLoop instead. |
| 261 // | 257 // |
| 262 // This method is a variant of Quit, that does not wait for pending messages | 258 // This method is a variant of Quit, that does not wait for pending messages |
| 263 // to be processed before returning from Run. | 259 // to be processed before returning from Run. |
| 264 void QuitNow(); | 260 void QuitNow(); |
| 265 | 261 |
| 266 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure(). | 262 // TODO(jbates) remove this. crbug.com/131220. See QuitWhenIdleClosure(). |
| 267 static base::Closure QuitClosure() { return QuitWhenIdleClosure(); } | 263 static Closure QuitClosure() { return QuitWhenIdleClosure(); } |
| 268 | 264 |
| 269 // Deprecated: use RunLoop instead. | 265 // Deprecated: use RunLoop instead. |
| 270 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an | 266 // Construct a Closure that will call QuitWhenIdle(). Useful to schedule an |
| 271 // arbitrary MessageLoop to QuitWhenIdle. | 267 // arbitrary MessageLoop to QuitWhenIdle. |
| 272 static base::Closure QuitWhenIdleClosure(); | 268 static Closure QuitWhenIdleClosure(); |
| 273 | 269 |
| 274 // Returns true if this loop is |type|. This allows subclasses (especially | 270 // Returns true if this loop is |type|. This allows subclasses (especially |
| 275 // those in tests) to specialize how they are identified. | 271 // those in tests) to specialize how they are identified. |
| 276 virtual bool IsType(Type type) const; | 272 virtual bool IsType(Type type) const; |
| 277 | 273 |
| 278 // Returns the type passed to the constructor. | 274 // Returns the type passed to the constructor. |
| 279 Type type() const { return type_; } | 275 Type type() const { return type_; } |
| 280 | 276 |
| 281 // Optional call to connect the thread name with this loop. | 277 // Optional call to connect the thread name with this loop. |
| 282 void set_thread_name(const std::string& thread_name) { | 278 void set_thread_name(const std::string& thread_name) { |
| 283 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; | 279 DCHECK(thread_name_.empty()) << "Should not rename this thread!"; |
| 284 thread_name_ = thread_name; | 280 thread_name_ = thread_name; |
| 285 } | 281 } |
| 286 const std::string& thread_name() const { return thread_name_; } | 282 const std::string& thread_name() const { return thread_name_; } |
| 287 | 283 |
| 288 // Gets the message loop proxy associated with this message loop. | 284 // Gets the message loop proxy associated with this message loop. |
| 289 scoped_refptr<base::MessageLoopProxy> message_loop_proxy() { | 285 scoped_refptr<MessageLoopProxy> message_loop_proxy() { |
| 290 return message_loop_proxy_.get(); | 286 return message_loop_proxy_.get(); |
| 291 } | 287 } |
| 292 | 288 |
| 293 // Enables or disables the recursive task processing. This happens in the case | 289 // Enables or disables the recursive task processing. This happens in the case |
| 294 // of recursive message loops. Some unwanted message loop may occurs when | 290 // of recursive message loops. Some unwanted message loop may occurs when |
| 295 // using common controls or printer functions. By default, recursive task | 291 // using common controls or printer functions. By default, recursive task |
| 296 // processing is disabled. | 292 // processing is disabled. |
| 297 // | 293 // |
| 298 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods | 294 // Please utilize |ScopedNestableTaskAllower| instead of calling these methods |
| 299 // directly. In general nestable message loops are to be avoided. They are | 295 // directly. In general nestable message loops are to be avoided. They are |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 | 338 |
| 343 // A TaskObserver is an object that receives task notifications from the | 339 // A TaskObserver is an object that receives task notifications from the |
| 344 // MessageLoop. | 340 // MessageLoop. |
| 345 // | 341 // |
| 346 // NOTE: A TaskObserver implementation should be extremely fast! | 342 // NOTE: A TaskObserver implementation should be extremely fast! |
| 347 class BASE_EXPORT TaskObserver { | 343 class BASE_EXPORT TaskObserver { |
| 348 public: | 344 public: |
| 349 TaskObserver(); | 345 TaskObserver(); |
| 350 | 346 |
| 351 // This method is called before processing a task. | 347 // This method is called before processing a task. |
| 352 virtual void WillProcessTask(const base::PendingTask& pending_task) = 0; | 348 virtual void WillProcessTask(const PendingTask& pending_task) = 0; |
| 353 | 349 |
| 354 // This method is called after processing a task. | 350 // This method is called after processing a task. |
| 355 virtual void DidProcessTask(const base::PendingTask& pending_task) = 0; | 351 virtual void DidProcessTask(const PendingTask& pending_task) = 0; |
| 356 | 352 |
| 357 protected: | 353 protected: |
| 358 virtual ~TaskObserver(); | 354 virtual ~TaskObserver(); |
| 359 }; | 355 }; |
| 360 | 356 |
| 361 // These functions can only be called on the same thread that |this| is | 357 // These functions can only be called on the same thread that |this| is |
| 362 // running on. | 358 // running on. |
| 363 void AddTaskObserver(TaskObserver* task_observer); | 359 void AddTaskObserver(TaskObserver* task_observer); |
| 364 void RemoveTaskObserver(TaskObserver* task_observer); | 360 void RemoveTaskObserver(TaskObserver* task_observer); |
| 365 | 361 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 390 } | 386 } |
| 391 #endif // OS_WIN | 387 #endif // OS_WIN |
| 392 | 388 |
| 393 // Can only be called from the thread that owns the MessageLoop. | 389 // Can only be called from the thread that owns the MessageLoop. |
| 394 bool is_running() const; | 390 bool is_running() const; |
| 395 | 391 |
| 396 //---------------------------------------------------------------------------- | 392 //---------------------------------------------------------------------------- |
| 397 protected: | 393 protected: |
| 398 | 394 |
| 399 #if defined(OS_WIN) | 395 #if defined(OS_WIN) |
| 400 base::MessagePumpWin* pump_win() { | 396 MessagePumpWin* pump_win() { |
| 401 return static_cast<base::MessagePumpWin*>(pump_.get()); | 397 return static_cast<MessagePumpWin*>(pump_.get()); |
| 402 } | 398 } |
| 403 #elif defined(OS_POSIX) && !defined(OS_IOS) | 399 #elif defined(OS_POSIX) && !defined(OS_IOS) |
| 404 base::MessagePumpLibevent* pump_libevent() { | 400 MessagePumpLibevent* pump_libevent() { |
| 405 return static_cast<base::MessagePumpLibevent*>(pump_.get()); | 401 return static_cast<MessagePumpLibevent*>(pump_.get()); |
| 406 } | 402 } |
| 407 #endif | 403 #endif |
| 408 | 404 |
| 409 scoped_refptr<base::MessagePump> pump_; | 405 scoped_refptr<MessagePump> pump_; |
| 410 | 406 |
| 411 private: | 407 private: |
| 412 friend class base::RunLoop; | 408 friend class RunLoop; |
| 413 friend class base::MessageLoopLockTest; | 409 friend class MessageLoopLockTest; |
| 414 | 410 |
| 415 // A function to encapsulate all the exception handling capability in the | 411 // A function to encapsulate all the exception handling capability in the |
| 416 // stacks around the running of a main message loop. It will run the message | 412 // stacks around the running of a main message loop. It will run the message |
| 417 // loop in a SEH try block or not depending on the set_SEH_restoration() | 413 // loop in a SEH try block or not depending on the set_SEH_restoration() |
| 418 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). | 414 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). |
| 419 void RunHandler(); | 415 void RunHandler(); |
| 420 | 416 |
| 421 #if defined(OS_WIN) | 417 #if defined(OS_WIN) |
| 422 __declspec(noinline) void RunInternalInSEHFrame(); | 418 __declspec(noinline) void RunInternalInSEHFrame(); |
| 423 #endif | 419 #endif |
| 424 | 420 |
| 425 // A surrounding stack frame around the running of the message loop that | 421 // A surrounding stack frame around the running of the message loop that |
| 426 // supports all saving and restoring of state, as is needed for any/all (ugly) | 422 // supports all saving and restoring of state, as is needed for any/all (ugly) |
| 427 // recursive calls. | 423 // recursive calls. |
| 428 void RunInternal(); | 424 void RunInternal(); |
| 429 | 425 |
| 430 // Called to process any delayed non-nestable tasks. | 426 // Called to process any delayed non-nestable tasks. |
| 431 bool ProcessNextDelayedNonNestableTask(); | 427 bool ProcessNextDelayedNonNestableTask(); |
| 432 | 428 |
| 433 // Runs the specified PendingTask. | 429 // Runs the specified PendingTask. |
| 434 void RunTask(const base::PendingTask& pending_task); | 430 void RunTask(const PendingTask& pending_task); |
| 435 | 431 |
| 436 // Calls RunTask or queues the pending_task on the deferred task list if it | 432 // Calls RunTask or queues the pending_task on the deferred task list if it |
| 437 // cannot be run right now. Returns true if the task was run. | 433 // cannot be run right now. Returns true if the task was run. |
| 438 bool DeferOrRunPendingTask(const base::PendingTask& pending_task); | 434 bool DeferOrRunPendingTask(const PendingTask& pending_task); |
| 439 | 435 |
| 440 // Adds the pending task to delayed_work_queue_. | 436 // Adds the pending task to delayed_work_queue_. |
| 441 void AddToDelayedWorkQueue(const base::PendingTask& pending_task); | 437 void AddToDelayedWorkQueue(const PendingTask& pending_task); |
| 442 | 438 |
| 443 // This function attempts to add pending task to our incoming_queue_. | 439 // This function attempts to add pending task to our incoming_queue_. |
| 444 // The append can only possibly fail when |use_try_lock| is true. | 440 // The append can only possibly fail when |use_try_lock| is true. |
| 445 // | 441 // |
| 446 // When |use_try_lock| is true, then this call will avoid blocking if | 442 // When |use_try_lock| is true, then this call will avoid blocking if |
| 447 // the related lock is already held, and will in that case (when the | 443 // the related lock is already held, and will in that case (when the |
| 448 // lock is contended) fail to perform the append, and will return false. | 444 // lock is contended) fail to perform the append, and will return false. |
| 449 // | 445 // |
| 450 // If the call succeeds to append to the queue, then this call | 446 // If the call succeeds to append to the queue, then this call |
| 451 // will return true. | 447 // will return true. |
| 452 // | 448 // |
| 453 // In all cases, the caller retains ownership of |pending_task|, but this | 449 // In all cases, the caller retains ownership of |pending_task|, but this |
| 454 // function will reset the value of pending_task->task. This is needed to | 450 // function will reset the value of pending_task->task. This is needed to |
| 455 // ensure that the posting call stack does not retain pending_task->task | 451 // ensure that the posting call stack does not retain pending_task->task |
| 456 // beyond this function call. | 452 // beyond this function call. |
| 457 bool AddToIncomingQueue(base::PendingTask* pending_task, bool use_try_lock); | 453 bool AddToIncomingQueue(PendingTask* pending_task, bool use_try_lock); |
| 458 | 454 |
| 459 // Load tasks from the incoming_queue_ into work_queue_ if the latter is | 455 // Load tasks from the incoming_queue_ into work_queue_ if the latter is |
| 460 // empty. The former requires a lock to access, while the latter is directly | 456 // empty. The former requires a lock to access, while the latter is directly |
| 461 // accessible on this thread. | 457 // accessible on this thread. |
| 462 void ReloadWorkQueue(); | 458 void ReloadWorkQueue(); |
| 463 | 459 |
| 464 // Delete tasks that haven't run yet without running them. Used in the | 460 // Delete tasks that haven't run yet without running them. Used in the |
| 465 // destructor to make sure all the task's destructors get called. Returns | 461 // destructor to make sure all the task's destructors get called. Returns |
| 466 // true if some work was done. | 462 // true if some work was done. |
| 467 bool DeletePendingTasks(); | 463 bool DeletePendingTasks(); |
| 468 | 464 |
| 469 // Calculates the time at which a PendingTask should run. | 465 // Calculates the time at which a PendingTask should run. |
| 470 base::TimeTicks CalculateDelayedRuntime(base::TimeDelta delay); | 466 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
| 471 | 467 |
| 472 // Start recording histogram info about events and action IF it was enabled | 468 // Start recording histogram info about events and action IF it was enabled |
| 473 // and IF the statistics recorder can accept a registration of our histogram. | 469 // and IF the statistics recorder can accept a registration of our histogram. |
| 474 void StartHistogrammer(); | 470 void StartHistogrammer(); |
| 475 | 471 |
| 476 // Add occurrence of event to our histogram, so that we can see what is being | 472 // Add occurrence of event to our histogram, so that we can see what is being |
| 477 // done in a specific MessageLoop instance (i.e., specific thread). | 473 // done in a specific MessageLoop instance (i.e., specific thread). |
| 478 // If message_histogram_ is NULL, this is a no-op. | 474 // If message_histogram_ is NULL, this is a no-op. |
| 479 void HistogramEvent(int event); | 475 void HistogramEvent(int event); |
| 480 | 476 |
| 481 // base::MessagePump::Delegate methods: | 477 // MessagePump::Delegate methods: |
| 482 virtual bool DoWork() OVERRIDE; | 478 virtual bool DoWork() OVERRIDE; |
| 483 virtual bool DoDelayedWork(base::TimeTicks* next_delayed_work_time) OVERRIDE; | 479 virtual bool DoDelayedWork(TimeTicks* next_delayed_work_time) OVERRIDE; |
| 484 virtual bool DoIdleWork() OVERRIDE; | 480 virtual bool DoIdleWork() OVERRIDE; |
| 485 | 481 |
| 486 Type type_; | 482 Type type_; |
| 487 | 483 |
| 488 // A list of tasks that need to be processed by this instance. Note that | 484 // A list of tasks that need to be processed by this instance. Note that |
| 489 // this queue is only accessed (push/pop) by our current thread. | 485 // this queue is only accessed (push/pop) by our current thread. |
| 490 base::TaskQueue work_queue_; | 486 TaskQueue work_queue_; |
| 491 | 487 |
| 492 // Contains delayed tasks, sorted by their 'delayed_run_time' property. | 488 // Contains delayed tasks, sorted by their 'delayed_run_time' property. |
| 493 base::DelayedTaskQueue delayed_work_queue_; | 489 DelayedTaskQueue delayed_work_queue_; |
| 494 | 490 |
| 495 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. | 491 // A recent snapshot of Time::Now(), used to check delayed_work_queue_. |
| 496 base::TimeTicks recent_time_; | 492 TimeTicks recent_time_; |
| 497 | 493 |
| 498 // A queue of non-nestable tasks that we had to defer because when it came | 494 // A queue of non-nestable tasks that we had to defer because when it came |
| 499 // time to execute them we were in a nested message loop. They will execute | 495 // time to execute them we were in a nested message loop. They will execute |
| 500 // once we're out of nested message loops. | 496 // once we're out of nested message loops. |
| 501 base::TaskQueue deferred_non_nestable_work_queue_; | 497 TaskQueue deferred_non_nestable_work_queue_; |
| 502 | 498 |
| 503 ObserverList<DestructionObserver> destruction_observers_; | 499 ObserverList<DestructionObserver> destruction_observers_; |
| 504 | 500 |
| 505 // A recursion block that prevents accidentally running additional tasks when | 501 // A recursion block that prevents accidentally running additional tasks when |
| 506 // insider a (accidentally induced?) nested message pump. | 502 // insider a (accidentally induced?) nested message pump. |
| 507 bool nestable_tasks_allowed_; | 503 bool nestable_tasks_allowed_; |
| 508 | 504 |
| 509 bool exception_restoration_; | 505 bool exception_restoration_; |
| 510 | 506 |
| 511 std::string thread_name_; | 507 std::string thread_name_; |
| 512 // A profiling histogram showing the counts of various messages and events. | 508 // A profiling histogram showing the counts of various messages and events. |
| 513 base::HistogramBase* message_histogram_; | 509 HistogramBase* message_histogram_; |
| 514 | 510 |
| 515 // An incoming queue of tasks that are acquired under a mutex for processing | 511 // An incoming queue of tasks that are acquired under a mutex for processing |
| 516 // on this instance's thread. These tasks have not yet been sorted out into | 512 // on this instance's thread. These tasks have not yet been sorted out into |
| 517 // items for our work_queue_ vs delayed_work_queue_. | 513 // items for our work_queue_ vs delayed_work_queue_. |
| 518 base::TaskQueue incoming_queue_; | 514 TaskQueue incoming_queue_; |
| 519 // Protect access to incoming_queue_. | 515 // Protect access to incoming_queue_. |
| 520 mutable base::Lock incoming_queue_lock_; | 516 mutable Lock incoming_queue_lock_; |
| 521 | 517 |
| 522 base::RunLoop* run_loop_; | 518 RunLoop* run_loop_; |
| 523 | 519 |
| 524 #if defined(OS_WIN) | 520 #if defined(OS_WIN) |
| 525 base::TimeTicks high_resolution_timer_expiration_; | 521 TimeTicks high_resolution_timer_expiration_; |
| 526 // 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 |
| 527 // which enter a modal message loop. | 523 // which enter a modal message loop. |
| 528 bool os_modal_loop_; | 524 bool os_modal_loop_; |
| 529 #endif | 525 #endif |
| 530 | 526 |
| 531 // The next sequence number to use for delayed tasks. Updating this counter is | 527 // The next sequence number to use for delayed tasks. Updating this counter is |
| 532 // protected by incoming_queue_lock_. | 528 // protected by incoming_queue_lock_. |
| 533 int next_sequence_num_; | 529 int next_sequence_num_; |
| 534 | 530 |
| 535 ObserverList<TaskObserver> task_observers_; | 531 ObserverList<TaskObserver> task_observers_; |
| 536 | 532 |
| 537 // The message loop proxy associated with this message loop, if one exists. | 533 // The message loop proxy associated with this message loop, if one exists. |
| 538 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | 534 scoped_refptr<MessageLoopProxy> message_loop_proxy_; |
| 539 scoped_ptr<base::ThreadTaskRunnerHandle> thread_task_runner_handle_; | 535 scoped_ptr<ThreadTaskRunnerHandle> thread_task_runner_handle_; |
| 540 | 536 |
| 541 template <class T, class R> friend class base::subtle::DeleteHelperInternal; | 537 template <class T, class R> friend class base::subtle::DeleteHelperInternal; |
| 542 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; | 538 template <class T, class R> friend class base::subtle::ReleaseHelperInternal; |
| 543 | 539 |
| 544 void DeleteSoonInternal(const tracked_objects::Location& from_here, | 540 void DeleteSoonInternal(const tracked_objects::Location& from_here, |
| 545 void(*deleter)(const void*), | 541 void(*deleter)(const void*), |
| 546 const void* object); | 542 const void* object); |
| 547 void ReleaseSoonInternal(const tracked_objects::Location& from_here, | 543 void ReleaseSoonInternal(const tracked_objects::Location& from_here, |
| 548 void(*releaser)(const void*), | 544 void(*releaser)(const void*), |
| 549 const void* object); | 545 const void* object); |
| 550 | 546 |
| 551 DISALLOW_COPY_AND_ASSIGN(MessageLoop); | 547 DISALLOW_COPY_AND_ASSIGN(MessageLoop); |
| 552 }; | 548 }; |
| 553 | 549 |
| 554 //----------------------------------------------------------------------------- | 550 //----------------------------------------------------------------------------- |
| 555 // MessageLoopForUI extends MessageLoop with methods that are particular to a | 551 // MessageLoopForUI extends MessageLoop with methods that are particular to a |
| 556 // MessageLoop instantiated with TYPE_UI. | 552 // MessageLoop instantiated with TYPE_UI. |
| 557 // | 553 // |
| 558 // This class is typically used like so: | 554 // This class is typically used like so: |
| 559 // MessageLoopForUI::current()->...call some method... | 555 // MessageLoopForUI::current()->...call some method... |
| 560 // | 556 // |
| 561 class BASE_EXPORT MessageLoopForUI : public MessageLoop { | 557 class BASE_EXPORT MessageLoopForUI : public MessageLoop { |
| 562 public: | 558 public: |
| 563 #if defined(OS_WIN) | 559 #if defined(OS_WIN) |
| 564 typedef base::MessagePumpForUI::MessageFilter MessageFilter; | 560 typedef MessagePumpForUI::MessageFilter MessageFilter; |
| 565 #endif | 561 #endif |
| 566 | 562 |
| 567 MessageLoopForUI() : MessageLoop(TYPE_UI) { | 563 MessageLoopForUI() : MessageLoop(TYPE_UI) { |
| 568 } | 564 } |
| 569 | 565 |
| 570 // Returns the MessageLoopForUI of the current thread. | 566 // Returns the MessageLoopForUI of the current thread. |
| 571 static MessageLoopForUI* current() { | 567 static MessageLoopForUI* current() { |
| 572 MessageLoop* loop = MessageLoop::current(); | 568 MessageLoop* loop = MessageLoop::current(); |
| 573 DCHECK(loop); | 569 DCHECK(loop); |
| 574 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); | 570 DCHECK_EQ(MessageLoop::TYPE_UI, loop->type()); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 600 | 596 |
| 601 #if defined(OS_WIN) | 597 #if defined(OS_WIN) |
| 602 // Plese see MessagePumpForUI for definitions of this method. | 598 // Plese see MessagePumpForUI for definitions of this method. |
| 603 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter) { | 599 void SetMessageFilter(scoped_ptr<MessageFilter> message_filter) { |
| 604 pump_ui()->SetMessageFilter(message_filter.Pass()); | 600 pump_ui()->SetMessageFilter(message_filter.Pass()); |
| 605 } | 601 } |
| 606 #endif | 602 #endif |
| 607 | 603 |
| 608 protected: | 604 protected: |
| 609 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL) | 605 #if defined(USE_AURA) && defined(USE_X11) && !defined(OS_NACL) |
| 610 friend class base::MessagePumpAuraX11; | 606 friend class MessagePumpAuraX11; |
| 611 #endif | 607 #endif |
| 612 #if defined(USE_OZONE) && !defined(OS_NACL) | 608 #if defined(USE_OZONE) && !defined(OS_NACL) |
| 613 friend class base::MessagePumpOzone; | 609 friend class MessagePumpOzone; |
| 614 #endif | 610 #endif |
| 615 | 611 |
| 616 // TODO(rvargas): Make this platform independent. | 612 // TODO(rvargas): Make this platform independent. |
| 617 base::MessagePumpForUI* pump_ui() { | 613 MessagePumpForUI* pump_ui() { |
| 618 return static_cast<base::MessagePumpForUI*>(pump_.get()); | 614 return static_cast<MessagePumpForUI*>(pump_.get()); |
| 619 } | 615 } |
| 620 #endif // !defined(OS_MACOSX) | 616 #endif // !defined(OS_MACOSX) |
| 621 }; | 617 }; |
| 622 | 618 |
| 623 // Do not add any member variables to MessageLoopForUI! This is important b/c | 619 // Do not add any member variables to MessageLoopForUI! This is important b/c |
| 624 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra | 620 // MessageLoopForUI is often allocated via MessageLoop(TYPE_UI). Any extra |
| 625 // data that you need should be stored on the MessageLoop's pump_ instance. | 621 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 626 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), | 622 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForUI), |
| 627 MessageLoopForUI_should_not_have_extra_member_variables); | 623 MessageLoopForUI_should_not_have_extra_member_variables); |
| 628 | 624 |
| 629 //----------------------------------------------------------------------------- | 625 //----------------------------------------------------------------------------- |
| 630 // MessageLoopForIO extends MessageLoop with methods that are particular to a | 626 // MessageLoopForIO extends MessageLoop with methods that are particular to a |
| 631 // MessageLoop instantiated with TYPE_IO. | 627 // MessageLoop instantiated with TYPE_IO. |
| 632 // | 628 // |
| 633 // This class is typically used like so: | 629 // This class is typically used like so: |
| 634 // MessageLoopForIO::current()->...call some method... | 630 // MessageLoopForIO::current()->...call some method... |
| 635 // | 631 // |
| 636 class BASE_EXPORT MessageLoopForIO : public MessageLoop { | 632 class BASE_EXPORT MessageLoopForIO : public MessageLoop { |
| 637 public: | 633 public: |
| 638 #if defined(OS_WIN) | 634 #if defined(OS_WIN) |
| 639 typedef base::MessagePumpForIO::IOHandler IOHandler; | 635 typedef MessagePumpForIO::IOHandler IOHandler; |
| 640 typedef base::MessagePumpForIO::IOContext IOContext; | 636 typedef MessagePumpForIO::IOContext IOContext; |
| 641 typedef base::MessagePumpForIO::IOObserver IOObserver; | 637 typedef MessagePumpForIO::IOObserver IOObserver; |
| 642 #elif defined(OS_IOS) | 638 #elif defined(OS_IOS) |
| 643 typedef base::MessagePumpIOSForIO::Watcher Watcher; | 639 typedef MessagePumpIOSForIO::Watcher Watcher; |
| 644 typedef base::MessagePumpIOSForIO::FileDescriptorWatcher | 640 typedef MessagePumpIOSForIO::FileDescriptorWatcher |
| 645 FileDescriptorWatcher; | 641 FileDescriptorWatcher; |
| 646 typedef base::MessagePumpIOSForIO::IOObserver IOObserver; | 642 typedef MessagePumpIOSForIO::IOObserver IOObserver; |
| 647 | 643 |
| 648 enum Mode { | 644 enum Mode { |
| 649 WATCH_READ = base::MessagePumpIOSForIO::WATCH_READ, | 645 WATCH_READ = MessagePumpIOSForIO::WATCH_READ, |
| 650 WATCH_WRITE = base::MessagePumpIOSForIO::WATCH_WRITE, | 646 WATCH_WRITE = MessagePumpIOSForIO::WATCH_WRITE, |
| 651 WATCH_READ_WRITE = base::MessagePumpIOSForIO::WATCH_READ_WRITE | 647 WATCH_READ_WRITE = MessagePumpIOSForIO::WATCH_READ_WRITE |
| 652 }; | 648 }; |
| 653 #elif defined(OS_POSIX) | 649 #elif defined(OS_POSIX) |
| 654 typedef base::MessagePumpLibevent::Watcher Watcher; | 650 typedef MessagePumpLibevent::Watcher Watcher; |
| 655 typedef base::MessagePumpLibevent::FileDescriptorWatcher | 651 typedef MessagePumpLibevent::FileDescriptorWatcher |
| 656 FileDescriptorWatcher; | 652 FileDescriptorWatcher; |
| 657 typedef base::MessagePumpLibevent::IOObserver IOObserver; | 653 typedef MessagePumpLibevent::IOObserver IOObserver; |
| 658 | 654 |
| 659 enum Mode { | 655 enum Mode { |
| 660 WATCH_READ = base::MessagePumpLibevent::WATCH_READ, | 656 WATCH_READ = MessagePumpLibevent::WATCH_READ, |
| 661 WATCH_WRITE = base::MessagePumpLibevent::WATCH_WRITE, | 657 WATCH_WRITE = MessagePumpLibevent::WATCH_WRITE, |
| 662 WATCH_READ_WRITE = base::MessagePumpLibevent::WATCH_READ_WRITE | 658 WATCH_READ_WRITE = MessagePumpLibevent::WATCH_READ_WRITE |
| 663 }; | 659 }; |
| 664 | 660 |
| 665 #endif | 661 #endif |
| 666 | 662 |
| 667 MessageLoopForIO() : MessageLoop(TYPE_IO) { | 663 MessageLoopForIO() : MessageLoop(TYPE_IO) { |
| 668 } | 664 } |
| 669 | 665 |
| 670 // Returns the MessageLoopForIO of the current thread. | 666 // Returns the MessageLoopForIO of the current thread. |
| 671 static MessageLoopForIO* current() { | 667 static MessageLoopForIO* current() { |
| 672 MessageLoop* loop = MessageLoop::current(); | 668 MessageLoop* loop = MessageLoop::current(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 683 } | 679 } |
| 684 | 680 |
| 685 #if defined(OS_WIN) | 681 #if defined(OS_WIN) |
| 686 // Please see MessagePumpWin for definitions of these methods. | 682 // Please see MessagePumpWin for definitions of these methods. |
| 687 void RegisterIOHandler(HANDLE file, IOHandler* handler); | 683 void RegisterIOHandler(HANDLE file, IOHandler* handler); |
| 688 bool RegisterJobObject(HANDLE job, IOHandler* handler); | 684 bool RegisterJobObject(HANDLE job, IOHandler* handler); |
| 689 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); | 685 bool WaitForIOCompletion(DWORD timeout, IOHandler* filter); |
| 690 | 686 |
| 691 protected: | 687 protected: |
| 692 // TODO(rvargas): Make this platform independent. | 688 // TODO(rvargas): Make this platform independent. |
| 693 base::MessagePumpForIO* pump_io() { | 689 MessagePumpForIO* pump_io() { |
| 694 return static_cast<base::MessagePumpForIO*>(pump_.get()); | 690 return static_cast<MessagePumpForIO*>(pump_.get()); |
| 695 } | 691 } |
| 696 | 692 |
| 697 #elif defined(OS_IOS) | 693 #elif defined(OS_IOS) |
| 698 // Please see MessagePumpIOSForIO for definition. | 694 // Please see MessagePumpIOSForIO for definition. |
| 699 bool WatchFileDescriptor(int fd, | 695 bool WatchFileDescriptor(int fd, |
| 700 bool persistent, | 696 bool persistent, |
| 701 Mode mode, | 697 Mode mode, |
| 702 FileDescriptorWatcher *controller, | 698 FileDescriptorWatcher *controller, |
| 703 Watcher *delegate); | 699 Watcher *delegate); |
| 704 | 700 |
| 705 private: | 701 private: |
| 706 base::MessagePumpIOSForIO* pump_io() { | 702 MessagePumpIOSForIO* pump_io() { |
| 707 return static_cast<base::MessagePumpIOSForIO*>(pump_.get()); | 703 return static_cast<MessagePumpIOSForIO*>(pump_.get()); |
| 708 } | 704 } |
| 709 | 705 |
| 710 #elif defined(OS_POSIX) | 706 #elif defined(OS_POSIX) |
| 711 // Please see MessagePumpLibevent for definition. | 707 // Please see MessagePumpLibevent for definition. |
| 712 bool WatchFileDescriptor(int fd, | 708 bool WatchFileDescriptor(int fd, |
| 713 bool persistent, | 709 bool persistent, |
| 714 Mode mode, | 710 Mode mode, |
| 715 FileDescriptorWatcher* controller, | 711 FileDescriptorWatcher* controller, |
| 716 Watcher* delegate); | 712 Watcher* delegate); |
| 717 | 713 |
| 718 private: | 714 private: |
| 719 base::MessagePumpLibevent* pump_io() { | 715 MessagePumpLibevent* pump_io() { |
| 720 return static_cast<base::MessagePumpLibevent*>(pump_.get()); | 716 return static_cast<MessagePumpLibevent*>(pump_.get()); |
| 721 } | 717 } |
| 722 #endif // defined(OS_POSIX) | 718 #endif // defined(OS_POSIX) |
| 723 }; | 719 }; |
| 724 | 720 |
| 725 // Do not add any member variables to MessageLoopForIO! This is important b/c | 721 // Do not add any member variables to MessageLoopForIO! This is important b/c |
| 726 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra | 722 // MessageLoopForIO is often allocated via MessageLoop(TYPE_IO). Any extra |
| 727 // data that you need should be stored on the MessageLoop's pump_ instance. | 723 // data that you need should be stored on the MessageLoop's pump_ instance. |
| 728 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), | 724 COMPILE_ASSERT(sizeof(MessageLoop) == sizeof(MessageLoopForIO), |
| 729 MessageLoopForIO_should_not_have_extra_member_variables); | 725 MessageLoopForIO_should_not_have_extra_member_variables); |
| 730 | 726 |
| 731 } // namespace base | 727 } // namespace base |
| 732 | 728 |
| 733 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ | 729 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_H_ |
| OLD | NEW |