OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BASE_INCOMING_TASK_QUEUE_H_ | |
darin (slow to review)
2013/07/21 04:09:42
nit: BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_
alexeypa (please no reviews)
2013/07/21 06:52:26
Done.
| |
6 #define BASE_INCOMING_TASK_QUEUE_H_ | |
7 | |
8 #include "base/base_export.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/pending_task.h" | |
11 #include "base/synchronization/lock.h" | |
12 #include "base/time/time.h" | |
13 | |
14 namespace base { | |
15 | |
16 class MessageLoop; | |
17 class WaitableEvent; | |
18 | |
19 namespace internal { | |
20 | |
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 | |
23 // threads and together with MessageLoop ensures clean shutdown. | |
24 class BASE_EXPORT IncomingTaskQueue | |
25 : public RefCountedThreadSafe<IncomingTaskQueue> { | |
26 public: | |
27 IncomingTaskQueue(); | |
28 | |
29 // Acquires |incoming_queue_lock_| and appends a task to |incoming_queue_|. | |
darin (slow to review)
2013/07/21 04:09:42
nit: I don't think it is useful to spell out the i
alexeypa (please no reviews)
2013/07/21 06:52:26
Done.
| |
30 // Posting of all tasks is routed though AddToIncomingQueue() or | |
31 // TryAddToIncomingQueue() to make sure that posting task is properly | |
32 // synchronized between different threads. | |
33 // | |
34 // Returns true if the task was successfully added to the queue, otherwise | |
35 // returns false. In all cases, the ownership of |task| is transferred to the | |
36 // called method. | |
37 bool AddToIncomingQueue(const tracked_objects::Location& from_here, | |
38 const Closure& task, | |
39 TimeDelta delay, | |
40 bool nestable); | |
41 | |
42 // Returns true if the message loop has high resolution timers enabled. | |
43 // Provided for testing. | |
44 bool IsHighResolutionTimerEnabledForTest(); | |
darin (slow to review)
2013/07/21 04:09:42
micro-nit: I thought our convention was to use a F
alexeypa (please no reviews)
2013/07/21 06:52:26
Done.
| |
45 | |
46 // Returns true if the message loop is "idle". Provided for testing. | |
47 bool IsIdleForTest(); | |
48 | |
49 // Takes the incoming queue lock, signals |caller_wait| and waits until | |
50 // |caller_signal| is signalled. | |
51 void LockWaitUnLockForTest(WaitableEvent* caller_wait, | |
52 WaitableEvent* caller_signal); | |
53 | |
54 // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called | |
55 // from the thread that is running the loop. | |
56 void ReloadWorkQueue(TaskQueue* work_queue); | |
57 | |
58 // Same as AddToIncomingQueue() except that it will avoid blocking if the lock | |
59 // is already held, and will in that case (when the lock is contended) fail to | |
60 // add the task, and will return false. | |
61 bool TryAddToIncomingQueue(const tracked_objects::Location& from_here, | |
darin (slow to review)
2013/07/21 04:09:42
nit: It feels like this should be listed after Add
alexeypa (please no reviews)
2013/07/21 06:52:26
Done.
| |
62 const Closure& task); | |
63 | |
64 // Disconnects |this| from the parent message loop. | |
65 void WillDestroyCurrentMessageLoop(); | |
66 | |
67 private: | |
68 friend class RefCountedThreadSafe<IncomingTaskQueue>; | |
69 virtual ~IncomingTaskQueue(); | |
70 | |
71 // Calculates the time at which a PendingTask should run. | |
72 TimeTicks CalculateDelayedRuntime(TimeDelta delay); | |
73 | |
74 // Adds a task to |incoming_queue_|. The caller retains ownership of | |
75 // |pending_task|, but this function will reset the value of | |
76 // |pending_task->task|. This is needed to ensure that the posting call stack | |
77 // does not retain |pending_task->task| beyond this function call. | |
78 bool PostPendingTask(PendingTask* pending_task); | |
79 | |
80 #if defined(OS_WIN) | |
81 TimeTicks high_resolution_timer_expiration_; | |
82 #endif | |
83 | |
84 // The lock that protects access to |incoming_queue_|, |message_loop_| and | |
85 // |next_sequence_num_|. | |
86 base::Lock incoming_queue_lock_; | |
87 | |
88 // An incoming queue of tasks that are acquired under a mutex for processing | |
89 // on this instance's thread. These tasks have not yet been been pushed to | |
90 // |message_loop_|. | |
91 TaskQueue incoming_queue_; | |
92 | |
93 // Points to the message loop that owns |this|. | |
94 MessageLoop* message_loop_; | |
95 | |
96 // The next sequence number to use for delayed tasks. | |
97 int next_sequence_num_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); | |
100 }; | |
101 | |
102 } // namespace internal | |
103 } // namespace base | |
104 | |
105 #endif // BASE_INCOMING_TASK_QUEUE_H_ | |
OLD | NEW |