OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_H_ | 5 #ifndef BASE_MESSAGE_LOOP_H_ |
6 #define BASE_MESSAGE_LOOP_H_ | 6 #define BASE_MESSAGE_LOOP_H_ |
7 | 7 |
8 #include <queue> | 8 #include <queue> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 #elif defined(USE_MESSAGEPUMP_LINUX) && !defined(OS_NACL) | 37 #elif defined(USE_MESSAGEPUMP_LINUX) && !defined(OS_NACL) |
38 #include "base/message_pump_linux.h" | 38 #include "base/message_pump_linux.h" |
39 #else | 39 #else |
40 #include "base/message_pump_gtk.h" | 40 #include "base/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 | |
48 class HistogramBase; | 47 class HistogramBase; |
| 48 class MessageLoopLockTest; |
49 class RunLoop; | 49 class RunLoop; |
50 class ThreadTaskRunnerHandle; | 50 class ThreadTaskRunnerHandle; |
51 #if defined(OS_ANDROID) | 51 #if defined(OS_ANDROID) |
52 class MessagePumpForUI; | 52 class MessagePumpForUI; |
53 #endif | 53 #endif |
54 | 54 |
55 // A MessageLoop is used to process events for a particular thread. There is | 55 // A MessageLoop is used to process events for a particular thread. There is |
56 // at most one MessageLoop instance per thread. | 56 // at most one MessageLoop instance per thread. |
57 // | 57 // |
58 // Events include at a minimum Task instances submitted to PostTask and its | 58 // Events include at a minimum Task instances submitted to PostTask and its |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 // The NonNestable variants work similarly except that they promise never to | 161 // The NonNestable variants work similarly except that they promise never to |
162 // dispatch the task from a nested invocation of MessageLoop::Run. Instead, | 162 // dispatch the task from a nested invocation of MessageLoop::Run. Instead, |
163 // such tasks get deferred until the top-most MessageLoop::Run is executing. | 163 // such tasks get deferred until the top-most MessageLoop::Run is executing. |
164 // | 164 // |
165 // The MessageLoop takes ownership of the Task, and deletes it after it has | 165 // The MessageLoop takes ownership of the Task, and deletes it after it has |
166 // been Run(). | 166 // been Run(). |
167 // | 167 // |
168 // PostTask(from_here, task) is equivalent to | 168 // PostTask(from_here, task) is equivalent to |
169 // PostDelayedTask(from_here, task, 0). | 169 // PostDelayedTask(from_here, task, 0). |
170 // | 170 // |
| 171 // 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 // is not posted but the task is consumed anyways. |
| 174 // |
171 // NOTE: These methods may be called on any thread. The Task will be invoked | 175 // NOTE: These methods may be called on any thread. The Task will be invoked |
172 // on the thread that executes MessageLoop::Run(). | 176 // on the thread that executes MessageLoop::Run(). |
173 void PostTask( | 177 void PostTask( |
174 const tracked_objects::Location& from_here, | 178 const tracked_objects::Location& from_here, |
175 const base::Closure& task); | 179 const base::Closure& task); |
176 | 180 |
| 181 bool TryPostTask( |
| 182 const tracked_objects::Location& from_here, |
| 183 const base::Closure& task); |
| 184 |
177 void PostDelayedTask( | 185 void PostDelayedTask( |
178 const tracked_objects::Location& from_here, | 186 const tracked_objects::Location& from_here, |
179 const base::Closure& task, | 187 const base::Closure& task, |
180 base::TimeDelta delay); | 188 base::TimeDelta delay); |
181 | 189 |
182 void PostNonNestableTask( | 190 void PostNonNestableTask( |
183 const tracked_objects::Location& from_here, | 191 const tracked_objects::Location& from_here, |
184 const base::Closure& task); | 192 const base::Closure& task); |
185 | 193 |
186 void PostNonNestableDelayedTask( | 194 void PostNonNestableDelayedTask( |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 #elif defined(OS_POSIX) && !defined(OS_IOS) | 403 #elif defined(OS_POSIX) && !defined(OS_IOS) |
396 base::MessagePumpLibevent* pump_libevent() { | 404 base::MessagePumpLibevent* pump_libevent() { |
397 return static_cast<base::MessagePumpLibevent*>(pump_.get()); | 405 return static_cast<base::MessagePumpLibevent*>(pump_.get()); |
398 } | 406 } |
399 #endif | 407 #endif |
400 | 408 |
401 scoped_refptr<base::MessagePump> pump_; | 409 scoped_refptr<base::MessagePump> pump_; |
402 | 410 |
403 private: | 411 private: |
404 friend class base::RunLoop; | 412 friend class base::RunLoop; |
| 413 friend class base::MessageLoopLockTest; |
405 | 414 |
406 // A function to encapsulate all the exception handling capability in the | 415 // A function to encapsulate all the exception handling capability in the |
407 // stacks around the running of a main message loop. It will run the message | 416 // stacks around the running of a main message loop. It will run the message |
408 // loop in a SEH try block or not depending on the set_SEH_restoration() | 417 // loop in a SEH try block or not depending on the set_SEH_restoration() |
409 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). | 418 // flag invoking respectively RunInternalInSEHFrame() or RunInternal(). |
410 void RunHandler(); | 419 void RunHandler(); |
411 | 420 |
412 #if defined(OS_WIN) | 421 #if defined(OS_WIN) |
413 __declspec(noinline) void RunInternalInSEHFrame(); | 422 __declspec(noinline) void RunInternalInSEHFrame(); |
414 #endif | 423 #endif |
415 | 424 |
416 // A surrounding stack frame around the running of the message loop that | 425 // A surrounding stack frame around the running of the message loop that |
417 // supports all saving and restoring of state, as is needed for any/all (ugly) | 426 // supports all saving and restoring of state, as is needed for any/all (ugly) |
418 // recursive calls. | 427 // recursive calls. |
419 void RunInternal(); | 428 void RunInternal(); |
420 | 429 |
421 // Called to process any delayed non-nestable tasks. | 430 // Called to process any delayed non-nestable tasks. |
422 bool ProcessNextDelayedNonNestableTask(); | 431 bool ProcessNextDelayedNonNestableTask(); |
423 | 432 |
424 // Runs the specified PendingTask. | 433 // Runs the specified PendingTask. |
425 void RunTask(const base::PendingTask& pending_task); | 434 void RunTask(const base::PendingTask& pending_task); |
426 | 435 |
427 // Calls RunTask or queues the pending_task on the deferred task list if it | 436 // Calls RunTask or queues the pending_task on the deferred task list if it |
428 // cannot be run right now. Returns true if the task was run. | 437 // cannot be run right now. Returns true if the task was run. |
429 bool DeferOrRunPendingTask(const base::PendingTask& pending_task); | 438 bool DeferOrRunPendingTask(const base::PendingTask& pending_task); |
430 | 439 |
431 // Adds the pending task to delayed_work_queue_. | 440 // Adds the pending task to delayed_work_queue_. |
432 void AddToDelayedWorkQueue(const base::PendingTask& pending_task); | 441 void AddToDelayedWorkQueue(const base::PendingTask& pending_task); |
433 | 442 |
434 // Adds the pending task to our incoming_queue_. | 443 // This function attempts to add pending task to our incoming_queue_. |
| 444 // The append can only possibly fail when |use_try_lock| is true. |
435 // | 445 // |
436 // Caller retains ownership of |pending_task|, but this function will | 446 // When |use_try_lock| is true, then this call will avoid blocking if |
437 // reset the value of pending_task->task. This is needed to ensure | 447 // the related lock is already held, and will in that case (when the |
438 // that the posting call stack does not retain pending_task->task | 448 // lock is contended) fail to perform the append, and will return false. |
| 449 // |
| 450 // If the call succeeds to append to the queue, then this call |
| 451 // will return true. |
| 452 // |
| 453 // 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 |
| 455 // ensure that the posting call stack does not retain pending_task->task |
439 // beyond this function call. | 456 // beyond this function call. |
440 void AddToIncomingQueue(base::PendingTask* pending_task); | 457 bool AddToIncomingQueue(base::PendingTask* pending_task, bool use_try_lock); |
441 | 458 |
442 // Load tasks from the incoming_queue_ into work_queue_ if the latter is | 459 // Load tasks from the incoming_queue_ into work_queue_ if the latter is |
443 // empty. The former requires a lock to access, while the latter is directly | 460 // empty. The former requires a lock to access, while the latter is directly |
444 // accessible on this thread. | 461 // accessible on this thread. |
445 void ReloadWorkQueue(); | 462 void ReloadWorkQueue(); |
446 | 463 |
447 // Delete tasks that haven't run yet without running them. Used in the | 464 // Delete tasks that haven't run yet without running them. Used in the |
448 // destructor to make sure all the task's destructors get called. Returns | 465 // destructor to make sure all the task's destructors get called. Returns |
449 // true if some work was done. | 466 // true if some work was done. |
450 bool DeletePendingTasks(); | 467 bool DeletePendingTasks(); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 | 730 |
714 } // namespace base | 731 } // namespace base |
715 | 732 |
716 // TODO(brettw) remove this when all users are updated to explicitly use the | 733 // TODO(brettw) remove this when all users are updated to explicitly use the |
717 // namespace | 734 // namespace |
718 using base::MessageLoop; | 735 using base::MessageLoop; |
719 using base::MessageLoopForIO; | 736 using base::MessageLoopForIO; |
720 using base::MessageLoopForUI; | 737 using base::MessageLoopForUI; |
721 | 738 |
722 #endif // BASE_MESSAGE_LOOP_H_ | 739 #endif // BASE_MESSAGE_LOOP_H_ |
OLD | NEW |