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/task.h" | 5 #include "base/task.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/message_loop_proxy.h" | |
| 8 | 10 |
| 9 Task::Task() { | 11 Task::Task() { |
| 10 } | 12 } |
| 11 | 13 |
| 12 Task::~Task() { | 14 Task::~Task() { |
| 13 } | 15 } |
| 14 | 16 |
| 15 CancelableTask::CancelableTask() { | 17 CancelableTask::CancelableTask() { |
| 16 } | 18 } |
| 17 | 19 |
| 18 CancelableTask::~CancelableTask() { | 20 CancelableTask::~CancelableTask() { |
| 19 } | 21 } |
| 20 | 22 |
| 21 namespace base { | 23 namespace base { |
| 22 | 24 |
| 25 namespace internal { | |
| 26 | |
| 27 PostTaskAndReplyRelay::PostTaskAndReplyRelay( | |
| 28 const tracked_objects::Location& from_here, | |
| 29 const Closure& task, | |
| 30 const Closure& reply) | |
| 31 : from_here_(from_here), | |
| 32 origin_loop_(MessageLoopProxy::CreateForCurrentThread()) { | |
| 33 task_ = task; | |
| 34 reply_ = reply; | |
| 35 } | |
| 36 | |
| 37 PostTaskAndReplyRelay::~PostTaskAndReplyRelay() { | |
| 38 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
| 39 } | |
| 40 | |
| 41 void PostTaskAndReplyRelay::Run() { | |
| 42 task_.Run(); | |
| 43 origin_loop_->PostTask( | |
| 44 from_here_, | |
| 45 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, this)); | |
| 46 } | |
| 47 | |
| 48 void PostTaskAndReplyRelay::RunReplyAndSelfDestruct() { | |
| 49 DCHECK(origin_loop_->BelongsToCurrentThread()); | |
| 50 reply_.Run(); | |
| 51 | |
| 52 // Cue mission impossible theme. | |
| 53 // | |
| 54 // Even though PostTaskAndReplyRelay is refcounted, dropping the references | |
| 55 // here is preferable because it eliminates the chance that other tasks can | |
| 56 // interceed between the execution of reply_ and the destruction of the | |
| 57 // Closures. | |
| 58 task_.Reset(); | |
| 59 reply_.Reset(); | |
| 60 } | |
| 61 | |
| 62 void PostTaskAndReplyRelay::DoManualDestruction( | |
| 63 const PostTaskAndReplyRelay* obj) { | |
| 64 obj->origin_loop_->DeleteSoon(obj->from_here_, obj); | |
| 65 } | |
| 66 | |
| 67 } // namespace internal | |
| 68 | |
| 23 ScopedTaskRunner::ScopedTaskRunner(Task* task) : task_(task) { | 69 ScopedTaskRunner::ScopedTaskRunner(Task* task) : task_(task) { |
| 24 } | 70 } |
| 25 | 71 |
| 26 ScopedTaskRunner::~ScopedTaskRunner() { | 72 ScopedTaskRunner::~ScopedTaskRunner() { |
| 27 if (task_) { | 73 if (task_) { |
| 28 task_->Run(); | 74 task_->Run(); |
| 29 delete task_; | 75 delete task_; |
| 30 } | 76 } |
| 31 } | 77 } |
| 32 | 78 |
| 33 Task* ScopedTaskRunner::Release() { | 79 Task* ScopedTaskRunner::Release() { |
| 34 Task* tmp = task_; | 80 Task* tmp = task_; |
| 35 task_ = NULL; | 81 task_ = NULL; |
| 36 return tmp; | 82 return tmp; |
| 37 } | 83 } |
| 38 | 84 |
| 39 namespace internal { | 85 void PostTaskAndReply(MessageLoop* loop, |
|
willchan no longer on Chromium
2011/07/06 22:10:26
We should use MessageLoopProxy here.
awong
2011/08/15 22:07:55
New CL side-steps this issue.
| |
| 40 | 86 const tracked_objects::Location& from_here, |
| 41 PostTaskAndReplyRelay::PostTaskAndReplyRelay(const Closure& task, | 87 const Closure& task, |
| 42 const Closure& reply) | 88 const Closure& reply) { |
| 43 : task_(task), | 89 internal::PostTaskAndReplyRelay* relay = |
| 44 reply_(reply), | 90 new internal::PostTaskAndReplyRelay(from_here, task, reply); |
| 45 origin_loop_(MessageLoop::Current()) { | 91 loop->PostTask(from_here, |
| 92 Bind(&internal::PostTaskAndReplyRelay::Run, relay)); | |
| 46 } | 93 } |
| 47 | 94 |
| 48 PostTaskAndReplyRelay::~PostTaskAndReplyRelay() { | |
| 49 DCHECK(origin_loop_ == MessageLoop::Current()); | |
| 50 } | |
| 51 | |
| 52 void PostTaskAndReplyRelay::Run() { | |
| 53 task_.Run(); | |
| 54 origin_loop_->PostTask( | |
| 55 base::Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, | |
| 56 base::Unretained(this))); | |
| 57 } | |
| 58 | |
| 59 void PostTaskAndReplyRelay::RunReplyAndSelfDestruct() { | |
| 60 reply_.Run(); | |
| 61 delete this; | |
| 62 } | |
| 63 } // namespace internal | |
| 64 | |
| 65 | |
| 66 } // namespace base | 95 } // namespace base |
| OLD | NEW |