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/threading/post_task_and_reply_impl.h" | 5 #include "base/threading/post_task_and_reply_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | |
| 9 #include "base/debug/leak_annotations.h" | 8 #include "base/debug/leak_annotations.h" |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 12 #include "base/sequence_checker.h" | 11 #include "base/sequence_checker.h" |
| 13 #include "base/sequenced_task_runner.h" | 12 #include "base/sequenced_task_runner.h" |
| 14 #include "base/threading/sequenced_task_runner_handle.h" | 13 #include "base/threading/sequenced_task_runner_handle.h" |
| 15 | 14 |
| 16 namespace base { | 15 namespace base { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // This relay class remembers the sequence that it was created on, and ensures | 19 // This relay class remembers the sequence that it was created on, and ensures |
| 21 // that both the |task| and |reply| Closures are deleted on this same sequence. | 20 // that both the |task| and |reply| Closures are deleted on this same sequence. |
| 22 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. | 21 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. |
| 23 // | 22 // |
| 24 // If RunReplyAndSelfDestruct() doesn't run because the originating execution | 23 // If RunReplyAndSelfDestruct() doesn't run because the originating execution |
| 25 // context is no longer available, then the |task| and |reply| Closures are | 24 // context is no longer available, then the |task| and |reply| Closures are |
| 26 // leaked. Leaking is considered preferable to having a thread-safetey | 25 // leaked. Leaking is considered preferable to having a thread-safetey |
| 27 // violations caused by invoking the Closure destructor on the wrong sequence. | 26 // violations caused by invoking the Closure destructor on the wrong sequence. |
| 28 class PostTaskAndReplyRelay { | 27 class PostTaskAndReplyRelay { |
| 29 public: | 28 public: |
| 30 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, | 29 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
| 31 const Closure& task, | 30 Closure task, |
| 32 const Closure& reply) | 31 Closure reply) |
| 33 : sequence_checker_(), | 32 : sequence_checker_(), |
| 34 from_here_(from_here), | 33 from_here_(from_here), |
| 35 origin_task_runner_(SequencedTaskRunnerHandle::Get()), | 34 origin_task_runner_(SequencedTaskRunnerHandle::Get()), |
| 36 reply_(reply), | 35 reply_(std::move(reply)), |
| 37 task_(task) {} | 36 task_(std::move(task)) {} |
|
gab
2017/02/07 15:46:08
#include <utility>
tzik
2017/02/08 01:39:32
Done.
| |
| 38 | 37 |
| 39 ~PostTaskAndReplyRelay() { | 38 ~PostTaskAndReplyRelay() { |
| 40 DCHECK(sequence_checker_.CalledOnValidSequence()); | 39 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 41 task_.Reset(); | 40 task_.Reset(); |
| 42 reply_.Reset(); | 41 reply_.Reset(); |
| 43 } | 42 } |
| 44 | 43 |
| 45 void RunTaskAndPostReply() { | 44 void RunTaskAndPostReply() { |
| 46 task_.Run(); | 45 task_.Run(); |
| 47 origin_task_runner_->PostTask( | 46 origin_task_runner_->PostTask( |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 70 Closure reply_; | 69 Closure reply_; |
| 71 Closure task_; | 70 Closure task_; |
| 72 }; | 71 }; |
| 73 | 72 |
| 74 } // namespace | 73 } // namespace |
| 75 | 74 |
| 76 namespace internal { | 75 namespace internal { |
| 77 | 76 |
| 78 bool PostTaskAndReplyImpl::PostTaskAndReply( | 77 bool PostTaskAndReplyImpl::PostTaskAndReply( |
| 79 const tracked_objects::Location& from_here, | 78 const tracked_objects::Location& from_here, |
| 80 const Closure& task, | 79 Closure task, |
| 81 const Closure& reply) { | 80 Closure reply) { |
| 82 DCHECK(!task.is_null()) << from_here.ToString(); | 81 DCHECK(!task.is_null()) << from_here.ToString(); |
| 83 DCHECK(!reply.is_null()) << from_here.ToString(); | 82 DCHECK(!reply.is_null()) << from_here.ToString(); |
| 84 PostTaskAndReplyRelay* relay = | 83 PostTaskAndReplyRelay* relay = |
| 85 new PostTaskAndReplyRelay(from_here, task, reply); | 84 new PostTaskAndReplyRelay(from_here, std::move(task), std::move(reply)); |
| 86 // PostTaskAndReplyRelay self-destructs after executing |reply|. On the flip | 85 // PostTaskAndReplyRelay self-destructs after executing |reply|. On the flip |
| 87 // side though, it is intentionally leaked if the |task| doesn't complete | 86 // side though, it is intentionally leaked if the |task| doesn't complete |
| 88 // before the origin sequence stops executing tasks. Annotate |relay| as leaky | 87 // before the origin sequence stops executing tasks. Annotate |relay| as leaky |
| 89 // to avoid having to suppress every callsite which happens to flakily trigger | 88 // to avoid having to suppress every callsite which happens to flakily trigger |
| 90 // this race. | 89 // this race. |
| 91 ANNOTATE_LEAKING_OBJECT_PTR(relay); | 90 ANNOTATE_LEAKING_OBJECT_PTR(relay); |
| 92 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply, | 91 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply, |
| 93 Unretained(relay)))) { | 92 Unretained(relay)))) { |
| 94 delete relay; | 93 delete relay; |
| 95 return false; | 94 return false; |
| 96 } | 95 } |
| 97 | 96 |
| 98 return true; | 97 return true; |
| 99 } | 98 } |
| 100 | 99 |
| 101 } // namespace internal | 100 } // namespace internal |
| 102 | 101 |
| 103 } // namespace base | 102 } // namespace base |
| OLD | NEW |