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.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 MessageLoopProxyImpl can only be created as part of a |
17 // MessageLoop. | 19 // MessageLoop. |
18 class BASE_EXPORT MessageLoopProxyImpl : public MessageLoopProxy { | 20 class BASE_EXPORT MessageLoopProxyImpl : public MessageLoopProxy { |
19 public: | 21 public: |
20 // MessageLoopProxy implementation | 22 // MessageLoopProxy implementation |
21 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, | 23 virtual bool PostDelayedTask(const tracked_objects::Location& from_here, |
22 const base::Closure& task, | 24 const base::Closure& task, |
23 base::TimeDelta delay) OVERRIDE; | 25 base::TimeDelta delay) OVERRIDE; |
24 virtual bool PostNonNestableDelayedTask( | 26 virtual bool PostNonNestableDelayedTask( |
25 const tracked_objects::Location& from_here, | 27 const tracked_objects::Location& from_here, |
26 const base::Closure& task, | 28 const base::Closure& task, |
27 base::TimeDelta delay) OVERRIDE; | 29 base::TimeDelta delay) OVERRIDE; |
28 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; | 30 virtual bool RunsTasksOnCurrentThread() const OVERRIDE; |
29 | 31 |
30 protected: | 32 protected: |
31 virtual ~MessageLoopProxyImpl(); | 33 virtual ~MessageLoopProxyImpl(); |
32 | 34 |
33 // Override OnDestruct so that we can delete the object on the target message | 35 // Override OnDestruct so that we can delete the object on the target message |
34 // loop if it still exists. | 36 // loop if it still exists. |
35 virtual void OnDestruct() const OVERRIDE; | 37 virtual void OnDestruct() const OVERRIDE; |
36 | 38 |
37 private: | 39 private: |
38 // Allow the MessageLoop to create a MessageLoopProxyImpl. | 40 // Allow the MessageLoop to create a MessageLoopProxyImpl. |
39 friend class MessageLoop; | 41 friend class MessageLoop; |
42 friend class MessageLoopLockTest; | |
40 friend class DeleteHelper<MessageLoopProxyImpl>; | 43 friend class DeleteHelper<MessageLoopProxyImpl>; |
41 | 44 |
42 MessageLoopProxyImpl(); | 45 MessageLoopProxyImpl(); |
43 | 46 |
44 // Called directly by MessageLoop::~MessageLoop. | 47 // Called directly by MessageLoop::~MessageLoop. |
45 virtual void WillDestroyCurrentMessageLoop(); | 48 virtual void WillDestroyCurrentMessageLoop(); |
46 | 49 |
50 // Acquires |incoming_queue_lock_| and appends a task to |incoming_queue_|. | |
51 // Posting of all tasks is routed though AddToIncomingQueue() or | |
52 // TryAddToIncomingQueue() to make sure that posting task is properly | |
53 // synchronized between different threads. | |
54 // | |
55 // Returns true if the task was successfully added to the queue, otherwise | |
56 // returns false. In all cases, the ownership of |task| is transferred to the | |
57 // called method. | |
58 bool AddToIncomingQueue(const tracked_objects::Location& from_here, | |
59 const Closure& task, | |
60 TimeDelta delay, | |
61 bool nestable); | |
47 | 62 |
48 bool PostTaskHelper(const tracked_objects::Location& from_here, | 63 // Calculates the time at which a PendingTask should run. |
49 const base::Closure& task, | 64 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
50 base::TimeDelta delay, | |
51 bool nestable); | |
52 | 65 |
53 // The lock that protects access to target_message_loop_. | 66 // Returns true if the message loop has high resolution timers enabled. |
54 mutable base::Lock message_loop_lock_; | 67 // Provided for testing. |
55 MessageLoop* target_message_loop_; | 68 bool IsHishResolutionTimersEnabledForTest(); |
69 | |
70 // Returns true if the message loop is "idle". Provided for testing. | |
71 bool IsIdleForTest(); | |
72 | |
73 // Same as AddToIncomingQueue() except that it will avoid blocking if the lock | |
74 // is already held, and will in that case (when the lock is contended) fail to | |
75 // add the task, and will return false. | |
76 bool TryAddToIncomingQueue(const tracked_objects::Location& from_here, | |
rvargas (doing something else)
2013/06/28 22:59:44
it would be nice to have some sort of indication o
| |
77 const Closure& task); | |
78 | |
79 // Adds a task to |incoming_queue_|. This method can be called on any thread. | |
80 // The caller is responsible for synchronizing calls to this method. | |
81 // | |
82 // In all cases, the caller retains ownership of |pending_task|, but this | |
83 // function will reset the value of |pending_task->task|. This is needed to | |
84 // ensure that the posting call stack does not retain |pending_task->task| | |
85 // beyond this function call. | |
86 bool PostPendingTask(PendingTask* pending_task); | |
87 | |
88 // Load tasks from the |incoming_queue_| into |*work_queue| if the latter is | |
89 // empty. The former requires a lock to access, while the latter is directly | |
90 // accessible on this thread. | |
91 void ReloadWorkQueue(TaskQueue* work_queue); | |
92 | |
93 #if defined(OS_WIN) | |
94 TimeTicks high_resolution_timer_expiration_; | |
95 #endif | |
96 | |
97 // The lock that protects access to |incoming_queue_|, |message_loop_| and | |
98 // |next_sequence_num_|. | |
99 mutable base::Lock incoming_queue_lock_; | |
100 | |
101 // An incoming queue of tasks that are acquired under a mutex for processing | |
102 // on this instance's thread. These tasks have not yet been been pushed to | |
103 // |message_loop_|. | |
104 TaskQueue incoming_queue_; | |
105 | |
106 // Points to the message loop that owns |this|. | |
107 MessageLoop* message_loop_; | |
108 | |
109 // The next sequence number to use for delayed tasks. | |
110 int next_sequence_num_; | |
56 | 111 |
57 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); | 112 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); |
58 }; | 113 }; |
59 | 114 |
60 } // namespace base | 115 } // namespace base |
61 | 116 |
62 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ | 117 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
OLD | NEW |