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

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: Mark private 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
« no previous file with comments | « base/base.gypi ('k') | base/threading/post_task_and_reply_impl.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/compiler_specific.h"
8 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/threading/post_task_and_reply_impl.h"
9 11
10 namespace base { 12 namespace base {
11 13
12 namespace { 14 namespace {
13 15
14 // This relay class remembers the MessageLoop that it was created on, and 16 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: 17 public:
25 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, 18 PostTaskAndReplyMessageLoopProxy(MessageLoopProxy* destination)
26 const Closure& task, const Closure& reply) 19 : destination_(destination) {
27 : from_here_(from_here),
28 origin_loop_(MessageLoopProxy::current()) {
29 task_ = task;
30 reply_ = reply;
31 }
32
33 ~PostTaskAndReplyRelay() {
34 DCHECK(origin_loop_->BelongsToCurrentThread());
35 task_.Reset();
36 reply_.Reset();
37 }
38
39 void Run() {
40 task_.Run();
41 origin_loop_->PostTask(
42 from_here_,
43 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
44 base::Unretained(this)));
45 } 20 }
46 21
47 private: 22 private:
48 void RunReplyAndSelfDestruct() { 23 virtual bool PostTask(const tracked_objects::Location& from_here,
49 DCHECK(origin_loop_->BelongsToCurrentThread()); 24 const base::Closure& task) OVERRIDE {
50 25 return destination_->PostTask(from_here, task);
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 } 26 }
61 27
62 tracked_objects::Location from_here_; 28 // Non-owning.
63 scoped_refptr<MessageLoopProxy> origin_loop_; 29 MessageLoopProxy* destination_;
64 Closure reply_;
65 Closure task_;
66 }; 30 };
67 31
68 } // namespace 32 } // namespace
69 33
70 MessageLoopProxy::MessageLoopProxy() { 34 MessageLoopProxy::MessageLoopProxy() {
71 } 35 }
72 36
73 MessageLoopProxy::~MessageLoopProxy() { 37 MessageLoopProxy::~MessageLoopProxy() {
74 } 38 }
75 39
76 bool MessageLoopProxy::PostTaskAndReply( 40 bool MessageLoopProxy::PostTaskAndReply(
77 const tracked_objects::Location& from_here, 41 const tracked_objects::Location& from_here,
78 const Closure& task, 42 const Closure& task,
79 const Closure& reply) { 43 const Closure& reply) {
80 PostTaskAndReplyRelay* relay = 44 return PostTaskAndReplyMessageLoopProxy(this).PostTaskAndReply(
81 new PostTaskAndReplyRelay(from_here, task, reply); 45 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 } 46 }
90 47
91 void MessageLoopProxy::OnDestruct() const { 48 void MessageLoopProxy::OnDestruct() const {
92 delete this; 49 delete this;
93 } 50 }
94 51
95 } // namespace base 52 } // namespace base
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/threading/post_task_and_reply_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698