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

Side by Side 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: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #include "base/message_loop_proxy.h" 5 #include "base/message_loop_proxy.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/threading/post_task_and_reply_impl.h"
9 10
10 namespace base { 11 namespace base {
11 12
12 namespace { 13 namespace {
13 14
14 // This relay class remembers the MessageLoop that it was created on, and 15 class PostTaskAndReplyMessageLoopProxy : public internal::PostTaskAndReplyImpl {
15 // ensures that both the |task| and |reply| Closures are deleted on this same
16 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or
17 // deleted.
18 //
19 // If this is not possible because the originating MessageLoop is no longer
20 // available, the the |task| and |reply| Closures are leaked. Leaking is
21 // considered preferable to having a thread-safetey violations caused by
22 // invoking the Closure destructor on the wrong thread.
23 class PostTaskAndReplyRelay {
24 public: 16 public:
25 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, 17 PostTaskAndReplyMessageLoopProxy(MessageLoopProxy* destination)
26 const Closure& task, const Closure& reply) 18 : destination_(destination) {
27 : from_here_(from_here),
28 origin_loop_(MessageLoopProxy::current()) {
29 task_ = task;
30 reply_ = reply;
31 } 19 }
32 20
33 ~PostTaskAndReplyRelay() { 21 protected:
34 DCHECK(origin_loop_->BelongsToCurrentThread()); 22 bool PostTask(const tracked_objects::Location& from_here,
willchan no longer on Chromium 2011/10/13 22:33:45 Please use the virtual keyword for this guy (style
Jói 2011/10/13 22:46:07 Done.
35 task_.Reset(); 23 const base::Closure& task) {
36 reply_.Reset(); 24 return destination_->PostTask(from_here, task);
37 } 25 }
38 26
39 void Run() { 27 // Non-owning.
40 task_.Run(); 28 MessageLoopProxy* destination_;
41 origin_loop_->PostTask(
42 from_here_,
43 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
44 base::Unretained(this)));
45 }
46
47 private:
48 void RunReplyAndSelfDestruct() {
49 DCHECK(origin_loop_->BelongsToCurrentThread());
50
51 // Force |task_| to be released before |reply_| is to ensure that no one
52 // accidentally depends on |task_| keeping one of its arguments alive while
53 // |reply_| is executing.
54 task_.Reset();
55
56 reply_.Run();
57
58 // Cue mission impossible theme.
59 delete this;
60 }
61
62 tracked_objects::Location from_here_;
63 scoped_refptr<MessageLoopProxy> origin_loop_;
64 Closure reply_;
65 Closure task_;
66 }; 29 };
67 30
68 } // namespace 31 } // namespace
69 32
70 MessageLoopProxy::MessageLoopProxy() { 33 MessageLoopProxy::MessageLoopProxy() {
71 } 34 }
72 35
73 MessageLoopProxy::~MessageLoopProxy() { 36 MessageLoopProxy::~MessageLoopProxy() {
74 } 37 }
75 38
76 bool MessageLoopProxy::PostTaskAndReply( 39 bool MessageLoopProxy::PostTaskAndReply(
77 const tracked_objects::Location& from_here, 40 const tracked_objects::Location& from_here,
78 const Closure& task, 41 const Closure& task,
79 const Closure& reply) { 42 const Closure& reply) {
80 PostTaskAndReplyRelay* relay = 43 return PostTaskAndReplyMessageLoopProxy(this).PostTaskAndReply(
81 new PostTaskAndReplyRelay(from_here, task, reply); 44 from_here, task, reply);
82 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run,
83 Unretained(relay)))) {
84 delete relay;
85 return false;
86 }
87
88 return true;
89 } 45 }
90 46
91 void MessageLoopProxy::OnDestruct() const { 47 void MessageLoopProxy::OnDestruct() const {
92 delete this; 48 delete this;
93 } 49 }
94 50
95 } // namespace base 51 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698