Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1196)

Unified Diff: base/message_loop_proxy.cc

Issue 8139028: Add WorkerPool::PostTaskAndReply and use in DHCP code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address review comments. Fix compile issue after merge to lkgr. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/message_loop_proxy.cc
diff --git a/base/message_loop_proxy.cc b/base/message_loop_proxy.cc
index c8217042fe6a540c2df3309d59a3caf7c1a389a2..153412e81f93fbd255f3bd3f26c09b974626007e 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;
+ PostTaskAndReplyMessageLoopProxy(MessageLoopProxy* destination)
+ : destination_(destination) {
}
- ~PostTaskAndReplyRelay() {
- DCHECK(origin_loop_->BelongsToCurrentThread());
- task_.Reset();
- reply_.Reset();
+ protected:
willchan no longer on Chromium 2011/10/13 22:59:56 private
Jói 2011/10/13 23:02:49 Done.
+ virtual bool PostTask(const tracked_objects::Location& from_here,
+ const base::Closure& task) OVERRIDE {
+ return destination_->PostTask(from_here, task);
}
- void Run() {
- task_.Run();
- origin_loop_->PostTask(
- from_here_,
- Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
- base::Unretained(this)));
- }
-
- 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;
- }
-
- tracked_objects::Location from_here_;
- scoped_refptr<MessageLoopProxy> 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 {

Powered by Google App Engine
This is Rietveld 408576698