Index: base/message_loop_proxy.h |
diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h |
index 9a856bf65ae3c5cecfb8c824344515aec340e2b6..d705f27dcc5076da9aceed9ee5a1c34f2a5b6a56 100644 |
--- a/base/message_loop_proxy.h |
+++ b/base/message_loop_proxy.h |
@@ -70,6 +70,24 @@ class BASE_EXPORT MessageLoopProxy |
// this proxy represents. |
virtual bool BelongsToCurrentThread() = 0; |
+ // Executes |task| on the given MessageLoopProxy. On completion, |reply| |
+ // is passed back to the MessageLoopProxy for the thread that called |
+ // PostTaskAndReply(). |
+ // |
+ // Both |task| and |reply| are guaranteed to be deleted on the thread from |
+ // which PostTaskAndReply() is invoked. |
+ // |
+ // This simplifies logic when an operation needs to be executed on another |
+ // thread, but processing of the result needs to occur on the current thread. |
+ // |
+ // Note that because of the lifetime guarantees on the |reply| Closure, it is |
+ // perfectly valid to use a WeakPtr<> in the bound parameters for |reply|. |
+ // This is useful if |reply| needs to be canceled on the originating thread |
+ // while |task| may still be executing. |
+ bool PostTaskAndReply(const tracked_objects::Location& from_here, |
+ const Closure& task, |
+ const Closure& reply); |
+ |
template <class T> |
bool DeleteSoon(const tracked_objects::Location& from_here, |
T* object) { |
@@ -103,6 +121,37 @@ struct MessageLoopProxyTraits { |
} |
}; |
+namespace internal { |
+ |
+// This relay class remembers the MessageLoop that it was created on, and |
+// ensures that both the |task| and |reply| Closures are deleted on this same |
+// thread. Also, |task| is guaranteed to be deleted before |reply| is run or |
+// deleted. |
+// |
+// If this is not possible because the originating MessageLoop is no longer |
+// available, the the |task| and |reply| Closures are leaked. Leaking is |
+// considered preferable to having a thread-safetey violations caused by |
+// invoking the Closure destructor on the wrong thread. |
+class PostTaskAndReplyRelay { |
+ public: |
+ PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
+ const Closure& task, const Closure& reply); |
+ |
+ ~PostTaskAndReplyRelay(); |
+ |
+ void Run(); |
+ |
+ private: |
+ void RunReplyAndSelfDestruct(); |
+ |
+ tracked_objects::Location from_here_; |
+ scoped_refptr<MessageLoopProxy> origin_loop_; |
+ Closure reply_; |
+ Closure task_; |
+}; |
+ |
+} // namespace internal |
+ |
} // namespace base |
#endif // BASE_MESSAGE_LOOP_PROXY_H_ |