OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_MESSAGE_LOOP_PROXY_IMPL_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
11 #include "base/pending_task.h" | |
11 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 #include "base/time/time.h" | |
12 | 14 |
13 namespace base { | 15 namespace base { |
14 | 16 |
15 // A stock implementation of MessageLoopProxy that is created and managed by a | 17 // A stock implementation of MessageLoopProxy that is created and managed by a |
16 // MessageLoop. For now a MessageLoopProxyImpl can only be created as part of a | 18 // MessageLoop. For now a ProxyImpl can only be created as part of a |
17 // MessageLoop. | 19 // MessageLoop. |
18 class BASE_EXPORT MessageLoopProxyImpl : public MessageLoopProxy { | 20 class BASE_EXPORT MessageLoop::ProxyImpl : public MessageLoopProxy { |
19 public: | 21 public: |
22 ProxyImpl(); | |
23 | |
20 // MessageLoopProxy implementation | 24 // MessageLoopProxy implementation |
21 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 25 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
22 const base::Closure& task, | 26 const base::Closure& task, |
23 base::TimeDelta delay) OVERRIDE; | 27 base::TimeDelta delay) OVERRIDE; |
24 virtual bool PostNonNestableDelayedTask( | 28 virtual bool PostNonNestableDelayedTask( |
25 const tracked_objects::Location& from_here, | 29 const tracked_objects::Location& from_here, |
26 const base::Closure& task, | 30 const base::Closure& task, |
27 base::TimeDelta delay) OVERRIDE; | 31 base::TimeDelta delay) OVERRIDE; |
28 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | 32 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
29 | 33 |
30 protected: | 34 // Acquires |incoming_queue_lock_| and appends a task to |incoming_queue_|. |
31 virtual ~MessageLoopProxyImpl(); | 35 // Posting of all tasks is routed though AddToIncomingQueue() or |
36 // TryAddToIncomingQueue() to make sure that posting task is properly | |
37 // synchronized between different threads. | |
38 // | |
39 // Returns true if the task was successfully added to the queue, otherwise | |
40 // returns false. In all cases, the ownership of |task| is transferred to the | |
41 // called method. | |
42 bool AddToIncomingQueue(const tracked_objects::Location& from_here, | |
43 const Closure& task, | |
44 TimeDelta delay, | |
45 bool nestable); | |
32 | 46 |
33 // Override OnDestruct so that we can delete the object on the target message | 47 // Returns true if the message loop has high resolution timers enabled. |
34 // loop if it still exists. | 48 // Provided for testing. |
35 virtual void OnDestruct() const OVERRIDE; | 49 bool IsHishResolutionTimersEnabledForTest(); |
DaleCurtis
2013/07/10 17:31:08
DItto.
alexeypa (please no reviews)
2013/07/10 19:39:59
Done.
| |
50 | |
51 // Returns true if the message loop is "idle". Provided for testing. | |
52 bool IsIdleForTest(); | |
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, | |
62 const Closure& task); | |
63 | |
64 // Disconnects |this| from the parent message loop. | |
65 void WillDestroyCurrentMessageLoop(); | |
36 | 66 |
37 private: | 67 private: |
38 // Allow the MessageLoop to create a MessageLoopProxyImpl. | 68 friend class MessageLoopLockTest; |
39 friend class MessageLoop; | 69 friend class DeleteHelper<ProxyImpl>; |
40 friend class DeleteHelper<MessageLoopProxyImpl>; | |
41 | 70 |
42 MessageLoopProxyImpl(); | 71 virtual ~ProxyImpl(); |
43 | 72 |
44 // Called directly by MessageLoop::~MessageLoop. | 73 // Calculates the time at which a PendingTask should run. |
45 virtual void WillDestroyCurrentMessageLoop(); | 74 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
46 | 75 |
76 // Adds a task to |incoming_queue_|. The caller retains ownership of | |
77 // |pending_task|, but this function will reset the value of | |
78 // |pending_task->task|. This is needed to ensure that the posting call stack | |
79 // does not retain |pending_task->task| beyond this function call. | |
80 bool PostPendingTask(PendingTask* pending_task); | |
47 | 81 |
48 bool PostTaskHelper(const tracked_objects::Location& from_here, | 82 #if defined(OS_WIN) |
49 const base::Closure& task, | 83 TimeTicks high_resolution_timer_expiration_; |
50 base::TimeDelta delay, | 84 #endif |
51 bool nestable); | |
52 | 85 |
53 // The lock that protects access to target_message_loop_. | 86 // The lock that protects access to |incoming_queue_|, |message_loop_| and |
54 mutable base::Lock message_loop_lock_; | 87 // |next_sequence_num_|. |
55 MessageLoop* target_message_loop_; | 88 mutable base::Lock incoming_queue_lock_; |
56 | 89 |
57 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); | 90 // An incoming queue of tasks that are acquired under a mutex for processing |
91 // on this instance's thread. These tasks have not yet been been pushed to | |
92 // |message_loop_|. | |
93 TaskQueue incoming_queue_; | |
94 | |
95 // Points to the message loop that owns |this|. | |
96 MessageLoop* message_loop_; | |
97 | |
98 // The next sequence number to use for delayed tasks. | |
99 int next_sequence_num_; | |
100 | |
101 DISALLOW_COPY_AND_ASSIGN(ProxyImpl); | |
58 }; | 102 }; |
59 | 103 |
60 } // namespace base | 104 } // namespace base |
61 | 105 |
62 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ | 106 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
OLD | NEW |