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 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 | |
34 // loop if it still exists. | |
35 virtual void OnDestruct() const OVERRIDE; | |
36 | |
37 private: | 35 private: |
38 // Allow the MessageLoop to create a MessageLoopProxyImpl. | 36 // Allow the MessageLoop to create a MessageLoopProxyImpl. |
39 friend class MessageLoop; | 37 friend class MessageLoop; |
38 friend class MessageLoopLockTest; | |
40 friend class DeleteHelper<MessageLoopProxyImpl>; | 39 friend class DeleteHelper<MessageLoopProxyImpl>; |
41 | 40 |
42 MessageLoopProxyImpl(); | 41 MessageLoopProxyImpl(); |
43 | 42 |
44 // Called directly by MessageLoop::~MessageLoop. | 43 // Called directly by MessageLoop::~MessageLoop. |
45 virtual void WillDestroyCurrentMessageLoop(); | 44 virtual void WillDestroyCurrentMessageLoop(); |
46 | 45 |
46 // Acquires |incoming_queue_lock_| and appends a task to |incoming_queue_|. | |
47 // Posting of all tasks is routed though AddToIncomingQueue() or | |
48 // TryAddToIncomingQueue() to make sure that posting task is properly | |
49 // synchronized between different threads. | |
50 // | |
51 // Returns true if the task was successfully added to the queue, otherwise | |
52 // returns false. In all cases, the ownership of |task| is transferred to the | |
53 // called method. | |
rvargas (doing something else)
2013/07/09 21:46:09
nit: Given the number of private methods we should
alexeypa (please no reviews)
2013/07/09 23:37:40
I converted MessageLoopProxyImpl into a private ne
| |
54 bool AddToIncomingQueue(const tracked_objects::Location& from_here, | |
55 const Closure& task, | |
56 TimeDelta delay, | |
57 bool nestable); | |
47 | 58 |
48 bool PostTaskHelper(const tracked_objects::Location& from_here, | 59 // Calculates the time at which a PendingTask should run. |
49 const base::Closure& task, | 60 TimeTicks CalculateDelayedRuntime(TimeDelta delay); |
50 base::TimeDelta delay, | |
51 bool nestable); | |
52 | 61 |
53 // The lock that protects access to target_message_loop_. | 62 // Returns true if the message loop has high resolution timers enabled. |
54 mutable base::Lock message_loop_lock_; | 63 // Provided for testing. |
55 MessageLoop* target_message_loop_; | 64 bool IsHishResolutionTimersEnabledForTest(); |
65 | |
66 // Returns true if the message loop is "idle". Provided for testing. | |
67 bool IsIdleForTest(); | |
68 | |
69 // Same as AddToIncomingQueue() except that it will avoid blocking if the lock | |
70 // is already held, and will in that case (when the lock is contended) fail to | |
71 // add the task, and will return false. | |
72 bool TryAddToIncomingQueue(const tracked_objects::Location& from_here, | |
73 const Closure& task); | |
74 | |
75 // Adds a task to |incoming_queue_|. This method can be called on any thread. | |
76 // The caller is responsible for synchronizing calls to this method. | |
rvargas (doing something else)
2013/07/09 21:46:09
nit: This is a private method not used by anyone e
alexeypa (please no reviews)
2013/07/09 23:37:40
Done.
| |
77 // | |
78 // In all cases, the caller retains ownership of |pending_task|, but this | |
79 // function will reset the value of |pending_task->task|. This is needed to | |
80 // ensure that the posting call stack does not retain |pending_task->task| | |
81 // beyond this function call. | |
82 bool PostPendingTask(PendingTask* pending_task); | |
83 | |
84 // Loads tasks from the |incoming_queue_| into |*work_queue|. |*work_queue| | |
85 // must be empty. Accessing |incoming_queue_| requires a lock. The caller | |
86 // arranges synchronization for |*work_queue|. | |
rvargas (doing something else)
2013/07/09 21:46:09
nit: the last sentence is confusing. I understand
alexeypa (please no reviews)
2013/07/09 23:37:40
Done.
| |
87 void ReloadWorkQueue(TaskQueue* work_queue); | |
88 | |
89 #if defined(OS_WIN) | |
90 TimeTicks high_resolution_timer_expiration_; | |
91 #endif | |
92 | |
93 // The lock that protects access to |incoming_queue_|, |message_loop_| and | |
94 // |next_sequence_num_|. | |
95 mutable base::Lock incoming_queue_lock_; | |
rvargas (doing something else)
2013/07/09 21:46:09
nit: it would be nice to have a better name here (
alexeypa (please no reviews)
2013/07/09 23:37:40
I prefer to keep it |incoming_queue_lock_| unless
| |
96 | |
97 // An incoming queue of tasks that are acquired under a mutex for processing | |
98 // on this instance's thread. These tasks have not yet been been pushed to | |
99 // |message_loop_|. | |
100 TaskQueue incoming_queue_; | |
101 | |
102 // Points to the message loop that owns |this|. | |
103 MessageLoop* message_loop_; | |
104 | |
105 // The next sequence number to use for delayed tasks. | |
106 int next_sequence_num_; | |
56 | 107 |
57 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); | 108 DISALLOW_COPY_AND_ASSIGN(MessageLoopProxyImpl); |
58 }; | 109 }; |
59 | 110 |
60 } // namespace base | 111 } // namespace base |
61 | 112 |
62 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ | 113 #endif // BASE_MESSAGE_LOOP_MESSAGE_LOOP_PROXY_IMPL_H_ |
OLD | NEW |