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_INCOMING_TASK_QUEUE_H_ | 5 #ifndef BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ |
6 #define BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ | 6 #define BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/debug/task_annotator.h" |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
10 #include "base/pending_task.h" | 11 #include "base/pending_task.h" |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 | 14 |
14 namespace base { | 15 namespace base { |
15 | 16 |
16 class MessageLoop; | 17 class MessageLoop; |
17 class WaitableEvent; | 18 class WaitableEvent; |
18 | 19 |
19 namespace internal { | 20 namespace internal { |
20 | 21 |
21 // Implements a queue of tasks posted to the message loop running on the current | 22 // Implements a queue of tasks posted to the message loop running on the current |
22 // thread. This class takes care of synchronizing posting tasks from different | 23 // thread. This class takes care of synchronizing posting tasks from different |
23 // threads and together with MessageLoop ensures clean shutdown. | 24 // threads and together with MessageLoop ensures clean shutdown. |
24 class BASE_EXPORT IncomingTaskQueue | 25 class BASE_EXPORT IncomingTaskQueue |
25 : public RefCountedThreadSafe<IncomingTaskQueue> { | 26 : public RefCountedThreadSafe<IncomingTaskQueue> { |
26 public: | 27 public: |
27 explicit IncomingTaskQueue(MessageLoop* message_loop); | 28 IncomingTaskQueue(); |
| 29 |
| 30 // Associates the |message_loop| to this task queue and starts scheduling |
| 31 // tasks. |
| 32 void StartScheduling(MessageLoop* message_loop); |
28 | 33 |
29 // Appends a task to the incoming queue. Posting of all tasks is routed though | 34 // Appends a task to the incoming queue. Posting of all tasks is routed though |
30 // AddToIncomingQueue() or TryAddToIncomingQueue() to make sure that posting | 35 // AddToIncomingQueue() or TryAddToIncomingQueue() to make sure that posting |
31 // task is properly synchronized between different threads. | 36 // task is properly synchronized between different threads. |
32 // | 37 // |
33 // Returns true if the task was successfully added to the queue, otherwise | 38 // Returns true if the task was successfully added to the queue, otherwise |
34 // returns false. In all cases, the ownership of |task| is transferred to the | 39 // returns false. In all cases, the ownership of |task| is transferred to the |
35 // called method. | 40 // called method. |
36 bool AddToIncomingQueue(const tracked_objects::Location& from_here, | 41 bool AddToIncomingQueue(const tracked_objects::Location& from_here, |
37 const Closure& task, | 42 const Closure& task, |
38 TimeDelta delay, | 43 TimeDelta delay, |
39 bool nestable); | 44 bool nestable); |
40 | 45 |
41 // Returns true if the queue contains tasks that require higher than default | 46 // Returns true if the queue contains tasks that require higher than default |
42 // timer resolution. Currently only needed for Windows. | 47 // timer resolution. Currently only needed for Windows. |
43 bool HasHighResolutionTasks(); | 48 bool HasHighResolutionTasks(); |
44 | 49 |
45 // Returns true if the message loop is "idle". Provided for testing. | 50 // Returns true if the message loop is "idle". Provided for testing. |
46 bool IsIdleForTesting(); | 51 bool IsIdleForTesting(); |
47 | 52 |
48 // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called | 53 // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called |
49 // from the thread that is running the loop. Returns the number of tasks that | 54 // from the thread that is running the loop. Returns the number of tasks that |
50 // require high resolution timers. | 55 // require high resolution timers. |
51 int ReloadWorkQueue(TaskQueue* work_queue); | 56 int ReloadWorkQueue(TaskQueue* work_queue); |
52 | 57 |
53 // Disconnects |this| from the parent message loop. | 58 // Disconnects |this| from the parent message loop. |
54 void WillDestroyCurrentMessageLoop(); | 59 void WillDestroyCurrentMessageLoop(); |
55 | 60 |
| 61 // Returns the TaskAnnotator which is used to add debug information to posted |
| 62 // tasks. |
| 63 debug::TaskAnnotator* task_annotator() { return &task_annotator_; } |
| 64 |
56 private: | 65 private: |
57 friend class RefCountedThreadSafe<IncomingTaskQueue>; | 66 friend class RefCountedThreadSafe<IncomingTaskQueue>; |
58 virtual ~IncomingTaskQueue(); | 67 virtual ~IncomingTaskQueue(); |
59 | 68 |
60 // Calculates the time at which a PendingTask should run. | 69 // Calculates the time at which a PendingTask should run. |
61 TimeTicks CalculateDelayedRuntime(TimeDelta delay); | 70 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
62 | 71 |
63 // Adds a task to |incoming_queue_|. The caller retains ownership of | 72 // Adds a task to |incoming_queue_|. The caller retains ownership of |
64 // |pending_task|, but this function will reset the value of | 73 // |pending_task|, but this function will reset the value of |
65 // |pending_task->task|. This is needed to ensure that the posting call stack | 74 // |pending_task->task|. This is needed to ensure that the posting call stack |
(...skipping 13 matching lines...) Expand all Loading... |
79 TaskQueue incoming_queue_; | 88 TaskQueue incoming_queue_; |
80 | 89 |
81 // Points to the message loop that owns |this|. | 90 // Points to the message loop that owns |this|. |
82 MessageLoop* message_loop_; | 91 MessageLoop* message_loop_; |
83 | 92 |
84 // The next sequence number to use for delayed tasks. | 93 // The next sequence number to use for delayed tasks. |
85 int next_sequence_num_; | 94 int next_sequence_num_; |
86 | 95 |
87 // True if our message loop has already been scheduled and does not need to be | 96 // True if our message loop has already been scheduled and does not need to be |
88 // scheduled again until an empty reload occurs. | 97 // scheduled again until an empty reload occurs. |
89 bool message_loop_scheduled_; | 98 bool message_loop_scheduled_ = false; |
| 99 |
| 100 // True after WillDestroyCurrentMessageLoop is called. |
| 101 bool message_loop_terminated_ = false; |
90 | 102 |
91 // True if we always need to call ScheduleWork when receiving a new task, even | 103 // True if we always need to call ScheduleWork when receiving a new task, even |
92 // if the incoming queue was not empty. | 104 // if the incoming queue was not empty. |
93 const bool always_schedule_work_; | 105 bool always_schedule_work_ = false; |
| 106 |
| 107 debug::TaskAnnotator task_annotator_; |
94 | 108 |
95 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); | 109 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); |
96 }; | 110 }; |
97 | 111 |
98 } // namespace internal | 112 } // namespace internal |
99 } // namespace base | 113 } // namespace base |
100 | 114 |
101 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ | 115 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ |
OLD | NEW |