OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_PROXY_H_ | 5 #ifndef BASE_MESSAGE_LOOP_PROXY_H_ |
6 #define BASE_MESSAGE_LOOP_PROXY_H_ | 6 #define BASE_MESSAGE_LOOP_PROXY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const base::Closure& task) = 0; | 63 const base::Closure& task) = 0; |
64 virtual bool PostNonNestableDelayedTask( | 64 virtual bool PostNonNestableDelayedTask( |
65 const tracked_objects::Location& from_here, | 65 const tracked_objects::Location& from_here, |
66 const base::Closure& task, | 66 const base::Closure& task, |
67 int64 delay_ms) = 0; | 67 int64 delay_ms) = 0; |
68 | 68 |
69 // A method which checks if the caller is currently running in the thread that | 69 // A method which checks if the caller is currently running in the thread that |
70 // this proxy represents. | 70 // this proxy represents. |
71 virtual bool BelongsToCurrentThread() = 0; | 71 virtual bool BelongsToCurrentThread() = 0; |
72 | 72 |
| 73 // Executes |task| on the given MessageLoopProxy. On completion, |reply| |
| 74 // is passed back to the MessageLoopProxy for the thread that called |
| 75 // PostTaskAndReply(). |
| 76 // |
| 77 // Both |task| and |reply| are guaranteed to be deleted on the thread from |
| 78 // which PostTaskAndReply() is invoked. |
| 79 // |
| 80 // This simplifies logic when an operation needs to be executed on another |
| 81 // thread, but processing of the result needs to occur on the current thread. |
| 82 // |
| 83 // Note that because of the lifetime guarantees on the |reply| Closure, it is |
| 84 // perfectly valid to use a WeakPtr<> in the bound parameters for |reply|. |
| 85 // This is useful if |reply| needs to be canceled on the originating thread |
| 86 // while |task| may still be executing. |
| 87 bool PostTaskAndReply(const tracked_objects::Location& from_here, |
| 88 const Closure& task, |
| 89 const Closure& reply); |
| 90 |
73 template <class T> | 91 template <class T> |
74 bool DeleteSoon(const tracked_objects::Location& from_here, | 92 bool DeleteSoon(const tracked_objects::Location& from_here, |
75 T* object) { | 93 T* object) { |
76 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); | 94 return PostNonNestableTask(from_here, new DeleteTask<T>(object)); |
77 } | 95 } |
78 template <class T> | 96 template <class T> |
79 bool ReleaseSoon(const tracked_objects::Location& from_here, | 97 bool ReleaseSoon(const tracked_objects::Location& from_here, |
80 T* object) { | 98 T* object) { |
81 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); | 99 return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); |
82 } | 100 } |
(...skipping 13 matching lines...) Expand all Loading... |
96 // to provide deletion on specific threads. | 114 // to provide deletion on specific threads. |
97 virtual void OnDestruct() const; | 115 virtual void OnDestruct() const; |
98 }; | 116 }; |
99 | 117 |
100 struct MessageLoopProxyTraits { | 118 struct MessageLoopProxyTraits { |
101 static void Destruct(const MessageLoopProxy* proxy) { | 119 static void Destruct(const MessageLoopProxy* proxy) { |
102 proxy->OnDestruct(); | 120 proxy->OnDestruct(); |
103 } | 121 } |
104 }; | 122 }; |
105 | 123 |
| 124 namespace internal { |
| 125 |
| 126 // This relay class remembers the MessageLoop that it was created on, and |
| 127 // ensures that both the |task| and |reply| Closures are deleted on this same |
| 128 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or |
| 129 // deleted. |
| 130 // |
| 131 // If this is not possible because the originating MessageLoop is no longer |
| 132 // available, the the |task| and |reply| Closures are leaked. Leaking is |
| 133 // considered preferable to having a thread-safetey violations caused by |
| 134 // invoking the Closure destructor on the wrong thread. |
| 135 class PostTaskAndReplyRelay { |
| 136 public: |
| 137 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
| 138 const Closure& task, const Closure& reply); |
| 139 |
| 140 ~PostTaskAndReplyRelay(); |
| 141 |
| 142 void Run(); |
| 143 |
| 144 private: |
| 145 void RunReplyAndSelfDestruct(); |
| 146 |
| 147 tracked_objects::Location from_here_; |
| 148 scoped_refptr<MessageLoopProxy> origin_loop_; |
| 149 Closure reply_; |
| 150 Closure task_; |
| 151 }; |
| 152 |
| 153 } // namespace internal |
| 154 |
106 } // namespace base | 155 } // namespace base |
107 | 156 |
108 #endif // BASE_MESSAGE_LOOP_PROXY_H_ | 157 #endif // BASE_MESSAGE_LOOP_PROXY_H_ |
OLD | NEW |