Chromium Code Reviews| Index: base/message_loop/incoming_task_queue.h |
| diff --git a/base/message_loop/incoming_task_queue.h b/base/message_loop/incoming_task_queue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e3b211235ec6767f35d094a7baf5ec987f407fe9 |
| --- /dev/null |
| +++ b/base/message_loop/incoming_task_queue.h |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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.
|
| +#define BASE_INCOMING_TASK_QUEUE_H_ |
| + |
| +#include "base/base_export.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/pending_task.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/time/time.h" |
| + |
| +namespace base { |
| + |
| +class MessageLoop; |
| +class WaitableEvent; |
| + |
| +namespace internal { |
| + |
| +// Implements a queue of tasks posted to the message loop running on the current |
| +// thread. This class takes care of synchronizing posting tasks from different |
| +// threads and together with MessageLoop ensures clean shutdown. |
| +class BASE_EXPORT IncomingTaskQueue |
| + : public RefCountedThreadSafe<IncomingTaskQueue> { |
| + public: |
| + IncomingTaskQueue(); |
| + |
| + // 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.
|
| + // Posting of all tasks is routed though AddToIncomingQueue() or |
| + // TryAddToIncomingQueue() to make sure that posting task is properly |
| + // synchronized between different threads. |
| + // |
| + // Returns true if the task was successfully added to the queue, otherwise |
| + // returns false. In all cases, the ownership of |task| is transferred to the |
| + // called method. |
| + bool AddToIncomingQueue(const tracked_objects::Location& from_here, |
| + const Closure& task, |
| + TimeDelta delay, |
| + bool nestable); |
| + |
| + // Returns true if the message loop has high resolution timers enabled. |
| + // Provided for testing. |
| + 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.
|
| + |
| + // Returns true if the message loop is "idle". Provided for testing. |
| + bool IsIdleForTest(); |
| + |
| + // Takes the incoming queue lock, signals |caller_wait| and waits until |
| + // |caller_signal| is signalled. |
| + void LockWaitUnLockForTest(WaitableEvent* caller_wait, |
| + WaitableEvent* caller_signal); |
| + |
| + // Loads tasks from the |incoming_queue_| into |*work_queue|. Must be called |
| + // from the thread that is running the loop. |
| + void ReloadWorkQueue(TaskQueue* work_queue); |
| + |
| + // Same as AddToIncomingQueue() except that it will avoid blocking if the lock |
| + // is already held, and will in that case (when the lock is contended) fail to |
| + // add the task, and will return false. |
| + 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.
|
| + const Closure& task); |
| + |
| + // Disconnects |this| from the parent message loop. |
| + void WillDestroyCurrentMessageLoop(); |
| + |
| + private: |
| + friend class RefCountedThreadSafe<IncomingTaskQueue>; |
| + virtual ~IncomingTaskQueue(); |
| + |
| + // Calculates the time at which a PendingTask should run. |
| + TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
| + |
| + // Adds a task to |incoming_queue_|. The caller retains ownership of |
| + // |pending_task|, but this function will reset the value of |
| + // |pending_task->task|. This is needed to ensure that the posting call stack |
| + // does not retain |pending_task->task| beyond this function call. |
| + bool PostPendingTask(PendingTask* pending_task); |
| + |
| +#if defined(OS_WIN) |
| + TimeTicks high_resolution_timer_expiration_; |
| +#endif |
| + |
| + // The lock that protects access to |incoming_queue_|, |message_loop_| and |
| + // |next_sequence_num_|. |
| + base::Lock incoming_queue_lock_; |
| + |
| + // An incoming queue of tasks that are acquired under a mutex for processing |
| + // on this instance's thread. These tasks have not yet been been pushed to |
| + // |message_loop_|. |
| + TaskQueue incoming_queue_; |
| + |
| + // Points to the message loop that owns |this|. |
| + MessageLoop* message_loop_; |
| + |
| + // The next sequence number to use for delayed tasks. |
| + int next_sequence_num_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(IncomingTaskQueue); |
| +}; |
| + |
| +} // namespace internal |
| +} // namespace base |
| + |
| +#endif // BASE_INCOMING_TASK_QUEUE_H_ |