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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/pending_task.h" | 10 #include "base/pending_task.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 class MessageLoop; | 16 class MessageLoop; |
17 class WaitableEvent; | 17 class WaitableEvent; |
18 | 18 |
19 namespace internal { | 19 namespace internal { |
20 | 20 |
21 // Implements a queue of tasks posted to the message loop running on the current | 21 // 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 | 22 // thread. This class takes care of synchronizing posting tasks from different |
23 // threads and together with MessageLoop ensures clean shutdown. | 23 // threads and together with MessageLoop ensures clean shutdown. |
24 class BASE_EXPORT IncomingTaskQueue | 24 class BASE_EXPORT IncomingTaskQueue |
25 : public RefCountedThreadSafe<IncomingTaskQueue> { | 25 : public RefCountedThreadSafe<IncomingTaskQueue> { |
26 public: | 26 public: |
27 explicit IncomingTaskQueue(MessageLoop* message_loop); | 27 explicit IncomingTaskQueue(MessageLoop* message_loop); |
28 | 28 |
29 // Creates a task queue for the message loop that is to be initialized lazily | |
30 // (i.e. created by CreateForLazyInit()). | |
rvargas (doing something else)
2015/03/26 02:19:26
nit: try to keep cross-refs to a minimum (as in do
kinuko
2015/04/13 02:02:59
Done.
| |
31 static IncomingTaskQueue* CreateForLazyInitMessageLoop( | |
rvargas (doing something else)
2015/03/26 02:19:26
return a scoped ptr (and same comment about this n
kinuko
2015/04/13 02:02:59
Done. (This is ref-counted so returning scoped_ref
| |
32 MessageLoop* message_loop); | |
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 // Called when |message_loop_|'s LazyInit() is called, so that now it's ready | |
62 // for scheduling work. It is not valid to call this unless this task queue | |
63 // is created by CreateForLazyInitMessageLoop(). | |
64 void DidInitializeMessageLoop(); | |
rvargas (doing something else)
2015/03/26 02:19:26
Maybe StartScheduling() ?
kinuko
2015/04/13 02:02:59
Done.
| |
65 | |
66 bool empty(); | |
rvargas (doing something else)
2015/03/26 02:19:26
nit: doc
kinuko
2015/04/13 02:02:59
Done.
| |
67 | |
56 private: | 68 private: |
57 friend class RefCountedThreadSafe<IncomingTaskQueue>; | 69 friend class RefCountedThreadSafe<IncomingTaskQueue>; |
58 virtual ~IncomingTaskQueue(); | 70 virtual ~IncomingTaskQueue(); |
59 | 71 |
60 // Calculates the time at which a PendingTask should run. | 72 // Calculates the time at which a PendingTask should run. |
61 TimeTicks CalculateDelayedRuntime(TimeDelta delay); | 73 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
62 | 74 |
63 // Adds a task to |incoming_queue_|. The caller retains ownership of | 75 // Adds a task to |incoming_queue_|. The caller retains ownership of |
64 // |pending_task|, but this function will reset the value of | 76 // |pending_task|, but this function will reset the value of |
65 // |pending_task->task|. This is needed to ensure that the posting call stack | 77 // |pending_task->task|. This is needed to ensure that the posting call stack |
(...skipping 19 matching lines...) Expand all Loading... | |
85 int next_sequence_num_; | 97 int next_sequence_num_; |
86 | 98 |
87 // True if our message loop has already been scheduled and does not need to be | 99 // True if our message loop has already been scheduled and does not need to be |
88 // scheduled again until an empty reload occurs. | 100 // scheduled again until an empty reload occurs. |
89 bool message_loop_scheduled_; | 101 bool message_loop_scheduled_; |
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 const bool always_schedule_work_; |
94 | 106 |
107 // True if our message loop is created for lazy initialization and hasn't been | |
108 // initialized yet. Note that while this is true this task queue won't call | |
109 // ScheduleWork even if always_schedule_work_ is true. | |
110 bool message_loop_not_initialized_ = false; | |
111 | |
95 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); | 112 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); |
96 }; | 113 }; |
97 | 114 |
98 } // namespace internal | 115 } // namespace internal |
99 } // namespace base | 116 } // namespace base |
100 | 117 |
101 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ | 118 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ |
OLD | NEW |