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 |
34 // Acquires |incoming_queue_lock_| and appends a task to |incoming_queue_|. | |
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); | |
46 | |
47 // Returns true if the message loop has high resolution timers enabled. | |
48 // Provided for testing. | |
49 bool IsHishResolutionTimersEnabledForTest(); | |
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 // Diconnects |this| from the parent message loop. | |
rvargas (doing something else)
2013/07/10 00:24:42
type: disconnects
alexeypa (please no reviews)
2013/07/10 16:14:27
Done.
| |
65 void WillDestroyCurrentMessageLoop(); | |
66 | |
30 protected: | 67 protected: |
31 virtual ~MessageLoopProxyImpl(); | 68 virtual ~ProxyImpl(); |
rvargas (doing something else)
2013/07/10 00:24:42
nit: send it to the private section?
alexeypa (please no reviews)
2013/07/10 16:14:27
Done.
| |
32 | |
33 // Override OnDestruct so that we can delete the object on the target message | |
34 // loop if it still exists. | |
35 virtual void OnDestruct() const OVERRIDE; | |
36 | 69 |
37 private: | 70 private: |
38 // Allow the MessageLoop to create a MessageLoopProxyImpl. | 71 friend class MessageLoopLockTest; |
39 friend class MessageLoop; | 72 friend class DeleteHelper<ProxyImpl>; |
40 friend class DeleteHelper<MessageLoopProxyImpl>; | |
41 | 73 |
42 MessageLoopProxyImpl(); | 74 // Calculates the time at which a PendingTask should run. |
75 TimeTicks CalculateDelayedRuntime(TimeDelta delay); | |
43 | 76 |
44 // Called directly by MessageLoop::~MessageLoop. | 77 // Adds a task to |incoming_queue_|. The caller retains ownership of |
45 virtual void WillDestroyCurrentMessageLoop(); | 78 // |pending_task|, but this function will reset the value of |
79 // |pending_task->task|. This is needed to ensure that the posting call stack | |
80 // does not retain |pending_task->task| beyond this function call. | |
81 bool PostPendingTask(PendingTask* pending_task); | |
46 | 82 |
83 #if defined(OS_WIN) | |
84 TimeTicks high_resolution_timer_expiration_; | |
85 #endif | |
47 | 86 |
48 bool PostTaskHelper(const tracked_objects::Location& from_here, | 87 // The lock that protects access to |incoming_queue_|, |message_loop_| and |
49 const base::Closure& task, | 88 // |next_sequence_num_|. |
50 base::TimeDelta delay, | 89 mutable base::Lock incoming_queue_lock_; |
51 bool nestable); | |
52 | 90 |
53 // The lock that protects access to target_message_loop_. | 91 // An incoming queue of tasks that are acquired under a mutex for processing |
54 mutable base::Lock message_loop_lock_; | 92 // on this instance's thread. These tasks have not yet been been pushed to |
55 MessageLoop* target_message_loop_; | 93 // |message_loop_|. |
94 TaskQueue incoming_queue_; | |
56 | 95 |
57 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); | 96 // Points to the message loop that owns |this|. |
97 MessageLoop* message_loop_; | |
98 | |
99 // The next sequence number to use for delayed tasks. | |
100 int next_sequence_num_; | |
101 | |
102 DISALLOW_COPY_AND_ASSIGN(ProxyImpl); | |
58 }; | 103 }; |
59 | 104 |
60 } // namespace base | 105 } // namespace base |
61 | 106 |
62 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ | 107 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
OLD | NEW |