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