Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 } | 20 } |
| 32 | 21 |
| 33 ~PostTaskAndReplyRelay() { | 22 protected: |
|
willchan no longer on Chromium
2011/10/13 22:59:56
private
Jói
2011/10/13 23:02:49
Done.
| |
| 34 DCHECK(origin_loop_->BelongsToCurrentThread()); | 23 virtual bool PostTask(const tracked_objects::Location& from_here, |
| 35 task_.Reset(); | 24 const base::Closure& task) OVERRIDE { |
| 36 reply_.Reset(); | 25 return destination_->PostTask(from_here, task); |
| 37 } | 26 } |
| 38 | 27 |
| 39 void Run() { | 28 // Non-owning. |
| 40 task_.Run(); | 29 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 }; | 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 |
| OLD | NEW |