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

Side by Side Diff: base/message_loop_proxy.cc

Issue 7210053: Implementation of PostTaskAndReply() in MessageLoopProxy and BrowserThread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: copyright Created 9 years, 4 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/message_loop_proxy.h ('k') | base/message_loop_proxy_unittest.cc » ('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) 2010 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"
8
7 namespace base { 9 namespace base {
8 10
11 namespace {
12
13 // This relay class remembers the MessageLoop that it was created on, and
14 // ensures that both the |task| and |reply| Closures are deleted on this same
15 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or
16 // deleted.
17 //
18 // If this is not possible because the originating MessageLoop is no longer
19 // available, the the |task| and |reply| Closures are leaked. Leaking is
20 // considered preferable to having a thread-safetey violations caused by
21 // invoking the Closure destructor on the wrong thread.
22 class PostTaskAndReplyRelay {
23 public:
24 PostTaskAndReplyRelay(const tracked_objects::Location& from_here,
25 const Closure& task, const Closure& reply)
26 : from_here_(from_here),
27 origin_loop_(MessageLoopProxy::current()) {
28 task_ = task;
29 reply_ = reply;
30 }
31
32 ~PostTaskAndReplyRelay() {
33 DCHECK(origin_loop_->BelongsToCurrentThread());
34 task_.Reset();
35 reply_.Reset();
36 }
37
38 void Run() {
39 task_.Run();
40 origin_loop_->PostTask(
41 from_here_,
42 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
43 base::Unretained(this)));
44 }
45
46 private:
47 void RunReplyAndSelfDestruct() {
48 DCHECK(origin_loop_->BelongsToCurrentThread());
49
50 // Force |task_| to be released before |reply_| is to ensure that no one
51 // accidentally depends on |task_| keeping one of its arguments alive while
52 // |reply_| is executing.
53 task_.Reset();
54
55 reply_.Run();
56
57 // Cue mission impossible theme.
58 delete this;
59 }
60
61 tracked_objects::Location from_here_;
62 scoped_refptr<MessageLoopProxy> origin_loop_;
63 Closure reply_;
64 Closure task_;
65 };
66
67 } // namespace
68
9 MessageLoopProxy::MessageLoopProxy() { 69 MessageLoopProxy::MessageLoopProxy() {
10 } 70 }
11 71
12 MessageLoopProxy::~MessageLoopProxy() { 72 MessageLoopProxy::~MessageLoopProxy() {
13 } 73 }
14 74
75 bool MessageLoopProxy::PostTaskAndReply(
76 const tracked_objects::Location& from_here,
77 const Closure& task,
78 const Closure& reply) {
79 PostTaskAndReplyRelay* relay =
80 new PostTaskAndReplyRelay(from_here, task, reply);
81 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run,
82 Unretained(relay)))) {
83 delete relay;
84 return false;
85 }
86
87 return true;
88 }
89
15 void MessageLoopProxy::OnDestruct() const { 90 void MessageLoopProxy::OnDestruct() const {
16 delete this; 91 delete this;
17 } 92 }
18 93
19 } // namespace base 94 } // namespace base
OLDNEW
« no previous file with comments | « base/message_loop_proxy.h ('k') | base/message_loop_proxy_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698