From 2837654227eb7ae572725fa55fb0cc33d922fd7b Mon Sep 17 00:00:00 2001 From: "joi@chromium.org" Date: Fri, 14 Oct 2011 17:30:37 +0000 Subject: Add WorkerPool::PostTaskAndReply and use in DHCP code. This factors out the PostTaskAndReply implementation out of MessageLoopProxy so that it can be used for any destination thread accessible via a PostTask-like interface, and uses that code from both MessageLoopProxy and WorkerPool. The DhcpProxyScriptFetcherWin and DhcpProxyScriptAdapterFetcher classes were both using a PostTaskAndReply-like mechanism with a WorkerPool thread, and on inspection it looks like there are several places in net/ where this is done, and this motivated the larger change (vs. patch set 1 which was a mechanical switch to base::Bind from NewRunnableMethod). BUG=97516 TEST=net_unittests Review URL: http://codereview.chromium.org/8139028 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@105512 0039d316-1c4b-4281-b951-d872f2087c98 --- base/message_loop_proxy.cc | 67 +++++++++------------------------------------- 1 file changed, 12 insertions(+), 55 deletions(-) (limited to 'base/message_loop_proxy.cc') diff --git a/base/message_loop_proxy.cc b/base/message_loop_proxy.cc index c821704..f9b4ad8 100644 --- a/base/message_loop_proxy.cc +++ b/base/message_loop_proxy.cc @@ -5,64 +5,28 @@ #include "base/message_loop_proxy.h" #include "base/bind.h" +#include "base/compiler_specific.h" #include "base/location.h" +#include "base/threading/post_task_and_reply_impl.h" namespace base { namespace { -// 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 { +class PostTaskAndReplyMessageLoopProxy : public internal::PostTaskAndReplyImpl { public: - PostTaskAndReplyRelay(const tracked_objects::Location& from_here, - const Closure& task, const Closure& reply) - : from_here_(from_here), - origin_loop_(MessageLoopProxy::current()) { - task_ = task; - reply_ = reply; - } - - ~PostTaskAndReplyRelay() { - DCHECK(origin_loop_->BelongsToCurrentThread()); - task_.Reset(); - reply_.Reset(); - } - - void Run() { - task_.Run(); - origin_loop_->PostTask( - from_here_, - Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, - base::Unretained(this))); + PostTaskAndReplyMessageLoopProxy(MessageLoopProxy* destination) + : destination_(destination) { } private: - void RunReplyAndSelfDestruct() { - DCHECK(origin_loop_->BelongsToCurrentThread()); - - // Force |task_| to be released before |reply_| is to ensure that no one - // accidentally depends on |task_| keeping one of its arguments alive while - // |reply_| is executing. - task_.Reset(); - - reply_.Run(); - - // Cue mission impossible theme. - delete this; + virtual bool PostTask(const tracked_objects::Location& from_here, + const base::Closure& task) OVERRIDE { + return destination_->PostTask(from_here, task); } - tracked_objects::Location from_here_; - scoped_refptr origin_loop_; - Closure reply_; - Closure task_; + // Non-owning. + MessageLoopProxy* destination_; }; } // namespace @@ -77,15 +41,8 @@ bool MessageLoopProxy::PostTaskAndReply( const tracked_objects::Location& from_here, const Closure& task, const Closure& reply) { - PostTaskAndReplyRelay* relay = - new PostTaskAndReplyRelay(from_here, task, reply); - if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run, - Unretained(relay)))) { - delete relay; - return false; - } - - return true; + return PostTaskAndReplyMessageLoopProxy(this).PostTaskAndReply( + from_here, task, reply); } void MessageLoopProxy::OnDestruct() const { -- cgit v1.1