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/macros.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/pending_task.h" 12 #include "base/synchronization/lock.h" 13 #include "base/synchronization/read_write_lock.h" 14 #include "base/time/time.h" 15 16 namespace base { 17 18 class MessageLoop; 19 class WaitableEvent; 20 21 namespace internal { 22 23 // Implements a queue of tasks posted to the message loop running on the current 24 // thread. This class takes care of synchronizing posting tasks from different 25 // threads and together with MessageLoop ensures clean shutdown. 26 class BASE_EXPORT IncomingTaskQueue 27 : public RefCountedThreadSafe<IncomingTaskQueue> { 28 public: 29 explicit IncomingTaskQueue(MessageLoop* message_loop); 30 31 // Appends a task to the incoming queue. Posting of all tasks is routed though 32 // AddToIncomingQueue() or TryAddToIncomingQueue() to make sure that posting 33 // task is properly synchronized between different threads. 34 // 35 // Returns true if the task was successfully added to the queue, otherwise 36 // returns false. In all cases, the ownership of |task| is transferred to the 37 // called method. 38 bool AddToIncomingQueue(const tracked_objects::Location& from_here, 39 const Closure& task, 40 TimeDelta delay, 41 bool nestable); 42 43 // Returns true if the queue contains tasks that require higher than default 44 // timer resolution. Currently only needed for Windows. 45 bool HasHighResolutionTasks(); 46 47 // Returns true if the message loop is "idle". Provided for testing. 48 bool IsIdleForTesting(); 49 50 // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called 51 // from the thread that is running the loop. Returns the number of tasks that 52 // require high resolution timers. 53 int ReloadWorkQueue(TaskQueue* work_queue); 54 55 // Disconnects |this| from the parent message loop. 56 void WillDestroyCurrentMessageLoop(); 57 58 // This should be called when the message loop becomes ready for 59 // scheduling work. 60 void StartScheduling(); 61 62 private: 63 friend class RefCountedThreadSafe<IncomingTaskQueue>; 64 virtual ~IncomingTaskQueue(); 65 66 // Adds a task to |incoming_queue_|. The caller retains ownership of 67 // |pending_task|, but this function will reset the value of 68 // |pending_task->task|. This is needed to ensure that the posting call stack 69 // does not retain |pending_task->task| beyond this function call. 70 bool PostPendingTask(PendingTask* pending_task); 71 72 // Wakes up the message loop and schedules work. 73 void ScheduleWork(); 74 75 // Number of tasks that require high resolution timing. This value is kept 76 // so that ReloadWorkQueue() completes in constant time. 77 int high_res_task_count_; 78 79 // The lock that protects access to the members of this class, except 80 // |message_loop_|. 81 base::Lock incoming_queue_lock_; 82 83 // Lock that protects |message_loop_| to prevent it from being deleted while a 84 // task is being posted. 85 base::subtle::ReadWriteLock message_loop_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 // True if our message loop has already been scheduled and does not need to be 99 // scheduled again until an empty reload occurs. 100 bool message_loop_scheduled_; 101 102 // True if we always need to call ScheduleWork when receiving a new task, even 103 // if the incoming queue was not empty. 104 const bool always_schedule_work_; 105 106 // False until StartScheduling() is called. 107 bool is_ready_for_scheduling_; 108 109 DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); 110 }; 111 112 } // namespace internal 113 } // namespace base 114 115 #endif // BASE_MESSAGE_LOOP_INCOMING_TASK_QUEUE_H_ 116