| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/debug/leak_annotations.h" | 10 #include "base/debug/leak_annotations.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // that both the |task| and |reply| Closures are deleted on this same sequence. | 22 // that both the |task| and |reply| Closures are deleted on this same sequence. |
| 23 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. | 23 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. |
| 24 // | 24 // |
| 25 // If RunReplyAndSelfDestruct() doesn't run because the originating execution | 25 // If RunReplyAndSelfDestruct() doesn't run because the originating execution |
| 26 // context is no longer available, then the |task| and |reply| Closures are | 26 // context is no longer available, then the |task| and |reply| Closures are |
| 27 // leaked. Leaking is considered preferable to having a thread-safetey | 27 // leaked. Leaking is considered preferable to having a thread-safetey |
| 28 // violations caused by invoking the Closure destructor on the wrong sequence. | 28 // violations caused by invoking the Closure destructor on the wrong sequence. |
| 29 class PostTaskAndReplyRelay { | 29 class PostTaskAndReplyRelay { |
| 30 public: | 30 public: |
| 31 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, | 31 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
| 32 Closure task, | 32 OnceClosure task, |
| 33 Closure reply) | 33 OnceClosure reply) |
| 34 : sequence_checker_(), | 34 : sequence_checker_(), |
| 35 from_here_(from_here), | 35 from_here_(from_here), |
| 36 origin_task_runner_(SequencedTaskRunnerHandle::Get()), | 36 origin_task_runner_(SequencedTaskRunnerHandle::Get()), |
| 37 reply_(std::move(reply)), | 37 reply_(std::move(reply)), |
| 38 task_(std::move(task)) {} | 38 task_(std::move(task)) {} |
| 39 | 39 |
| 40 ~PostTaskAndReplyRelay() { | 40 ~PostTaskAndReplyRelay() { |
| 41 DCHECK(sequence_checker_.CalledOnValidSequence()); | 41 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 void RunTaskAndPostReply() { | 44 void RunTaskAndPostReply() { |
| 45 task_.Run(); | 45 std::move(task_).Run(); |
| 46 task_.Reset(); | |
| 47 origin_task_runner_->PostTask( | 46 origin_task_runner_->PostTask( |
| 48 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, | 47 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, |
| 49 base::Unretained(this))); | 48 base::Unretained(this))); |
| 50 } | 49 } |
| 51 | 50 |
| 52 private: | 51 private: |
| 53 void RunReplyAndSelfDestruct() { | 52 void RunReplyAndSelfDestruct() { |
| 54 DCHECK(sequence_checker_.CalledOnValidSequence()); | 53 DCHECK(sequence_checker_.CalledOnValidSequence()); |
| 55 | 54 |
| 56 // Ensure |task_| has already been released before |reply_| to ensure that | 55 // Ensure |task_| has already been released before |reply_| to ensure that |
| 57 // no one accidentally depends on |task_| keeping one of its arguments alive | 56 // no one accidentally depends on |task_| keeping one of its arguments alive |
| 58 // while |reply_| is executing. | 57 // while |reply_| is executing. |
| 59 DCHECK(!task_); | 58 DCHECK(!task_); |
| 60 | 59 |
| 61 reply_.Run(); | 60 std::move(reply_).Run(); |
| 62 | 61 |
| 63 // Cue mission impossible theme. | 62 // Cue mission impossible theme. |
| 64 delete this; | 63 delete this; |
| 65 } | 64 } |
| 66 | 65 |
| 67 const SequenceChecker sequence_checker_; | 66 const SequenceChecker sequence_checker_; |
| 68 const tracked_objects::Location from_here_; | 67 const tracked_objects::Location from_here_; |
| 69 const scoped_refptr<SequencedTaskRunner> origin_task_runner_; | 68 const scoped_refptr<SequencedTaskRunner> origin_task_runner_; |
| 70 Closure reply_; | 69 OnceClosure reply_; |
| 71 Closure task_; | 70 OnceClosure 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 Closure task, | 79 OnceClosure task, |
| 81 Closure reply) { | 80 OnceClosure 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, std::move(task), std::move(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 |