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_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ 6 #define BASE_MESSAGE_LOOP_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 explicit IncomingTaskQueue(MessageLoop* message_loop); 28 29 // Appends a task to the incoming queue. Posting of all tasks is routed though 30 // AddToIncomingQueue() or TryAddToIncomingQueue() to make sure that posting 31 // task is properly synchronized between different threads. 32 // 33 // 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 35 // called method. 36 bool AddToIncomingQueue(const tracked_objects::Location& from_here, 37 const Closure& task, 38 TimeDelta delay, 39 bool nestable); 40 41 // Same as AddToIncomingQueue() except that it will avoid blocking if the lock 42 // is already held, and will in that case (when the lock is contended) fail to 43 // add the task, and will return false. 44 bool TryAddToIncomingQueue(const tracked_objects::Location& from_here, 45 const Closure& task); 46 47 // Returns true if the message loop has high resolution timers enabled. 48 // Provided for testing. 49 bool IsHighResolutionTimerEnabledForTesting(); 50 51 // Returns true if the message loop is "idle". Provided for testing. 52 bool IsIdleForTesting(); 53 54 // Takes the incoming queue lock, signals |caller_wait| and waits until 55 // |caller_signal| is signalled. 56 void LockWaitUnLockForTesting(WaitableEvent* caller_wait, 57 WaitableEvent* caller_signal); 58 59 // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called 60 // from the thread that is running the loop. 61 void ReloadWorkQueue(TaskQueue* work_queue); 62 63 // Disconnects |this| from the parent message loop. 64 void WillDestroyCurrentMessageLoop(); 65 66 private: 67 friend class RefCountedThreadSafe<IncomingTaskQueue>; 68 virtual ~IncomingTaskQueue(); 69 70 // Calculates the time at which a PendingTask should run. 71 TimeTicks CalculateDelayedRuntime(TimeDelta delay); 72 73 // Adds a task to |incoming_queue_|. The caller retains ownership of 74 // |pending_task|, but this function will reset the value of 75 // |pending_task->task|. This is needed to ensure that the posting call stack 76 // does not retain |pending_task->task| beyond this function call. 77 bool PostPendingTask(PendingTask* pending_task); 78 79 #if defined(OS_WIN) 80 TimeTicks high_resolution_timer_expiration_; 81 #endif 82 83 // The lock that protects access to |incoming_queue_|, |message_loop_| and 84 // |next_sequence_num_|. 85 base::Lock incoming_queue_lock_; 86 87 // An incoming queue of tasks that are acquired under a mutex for processing 88 // on this instance's thread. These tasks have not yet been been pushed to 89 // |message_loop_|. 90 TaskQueue incoming_queue_; 91 92 // Points to the message loop that owns |this|. 93 MessageLoop* message_loop_; 94 95 // The next sequence number to use for delayed tasks. 96 int next_sequence_num_; 97 98 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); 99 }; 100 101 } // namespace internal 102 } // namespace base 103 104 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ 105