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" | 8 #include "base/callback.h" |
9 #include "base/debug/leak_annotations.h" | 9 #include "base/debug/leak_annotations.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 // that both the |task| and |reply| Closures are deleted on this same sequence. | 21 // 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. | 22 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. |
23 // | 23 // |
24 // If RunReplyAndSelfDestruct() doesn't run because the originating execution | 24 // If RunReplyAndSelfDestruct() doesn't run because the originating execution |
25 // context is no longer available, then the |task| and |reply| Closures are | 25 // context is no longer available, then the |task| and |reply| Closures are |
26 // leaked. Leaking is considered preferable to having a thread-safetey | 26 // leaked. Leaking is considered preferable to having a thread-safetey |
27 // violations caused by invoking the Closure destructor on the wrong sequence. | 27 // violations caused by invoking the Closure destructor on the wrong sequence. |
28 class PostTaskAndReplyRelay { | 28 class PostTaskAndReplyRelay { |
29 public: | 29 public: |
30 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, | 30 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
31 const Closure& task, | 31 OnceClosure task, |
32 const Closure& reply) | 32 OnceClosure reply) |
33 : sequence_checker_(), | 33 : sequence_checker_(), |
34 from_here_(from_here), | 34 from_here_(from_here), |
35 origin_task_runner_(SequencedTaskRunnerHandle::Get()), | 35 origin_task_runner_(SequencedTaskRunnerHandle::Get()), |
36 reply_(reply), | 36 reply_(std::move(reply)), |
37 task_(task) {} | 37 task_(std::move(task)) {} |
38 | 38 |
39 ~PostTaskAndReplyRelay() { | 39 ~PostTaskAndReplyRelay() { |
40 DCHECK(sequence_checker_.CalledOnValidSequence()); | 40 DCHECK(sequence_checker_.CalledOnValidSequence()); |
41 task_.Reset(); | 41 task_.Reset(); |
42 reply_.Reset(); | 42 reply_.Reset(); |
43 } | 43 } |
44 | 44 |
45 void RunTaskAndPostReply() { | 45 void RunTaskAndPostReply() { |
46 task_.Run(); | 46 std::move(task_).Run(); |
47 origin_task_runner_->PostTask( | 47 origin_task_runner_->PostTask( |
48 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, | 48 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, |
49 base::Unretained(this))); | 49 base::Unretained(this))); |
50 } | 50 } |
51 | 51 |
52 private: | 52 private: |
53 void RunReplyAndSelfDestruct() { | 53 void RunReplyAndSelfDestruct() { |
54 DCHECK(sequence_checker_.CalledOnValidSequence()); | 54 DCHECK(sequence_checker_.CalledOnValidSequence()); |
55 | 55 |
56 // Force |task_| to be released before |reply_| is to ensure that no one | 56 // Force |task_| to be released before |reply_| is to ensure that no one |
57 // accidentally depends on |task_| keeping one of its arguments alive while | 57 // accidentally depends on |task_| keeping one of its arguments alive while |
58 // |reply_| is executing. | 58 // |reply_| is executing. |
59 task_.Reset(); | 59 std::move(task_).Reset(); |
60 | 60 |
61 reply_.Run(); | 61 std::move(reply_).Run(); |
62 | 62 |
63 // Cue mission impossible theme. | 63 // Cue mission impossible theme. |
64 delete this; | 64 delete this; |
65 } | 65 } |
66 | 66 |
67 const SequenceChecker sequence_checker_; | 67 const SequenceChecker sequence_checker_; |
68 const tracked_objects::Location from_here_; | 68 const tracked_objects::Location from_here_; |
69 const scoped_refptr<SequencedTaskRunner> origin_task_runner_; | 69 const scoped_refptr<SequencedTaskRunner> origin_task_runner_; |
70 Closure reply_; | 70 OnceClosure reply_; |
71 Closure task_; | 71 OnceClosure task_; |
72 }; | 72 }; |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 namespace internal { | 76 namespace internal { |
77 | 77 |
78 bool PostTaskAndReplyImpl::PostTaskAndReply( | 78 bool PostTaskAndReplyImpl::PostTaskAndReply( |
79 const tracked_objects::Location& from_here, | 79 const tracked_objects::Location& from_here, |
80 const Closure& task, | 80 OnceClosure task, |
81 const Closure& reply) { | 81 OnceClosure reply) { |
82 DCHECK(!task.is_null()) << from_here.ToString(); | 82 DCHECK(!task.is_null()) << from_here.ToString(); |
83 DCHECK(!reply.is_null()) << from_here.ToString(); | 83 DCHECK(!reply.is_null()) << from_here.ToString(); |
84 PostTaskAndReplyRelay* relay = | 84 PostTaskAndReplyRelay* relay = |
85 new PostTaskAndReplyRelay(from_here, task, reply); | 85 new PostTaskAndReplyRelay(from_here, std::move(task), std::move(reply)); |
86 // PostTaskAndReplyRelay self-destructs after executing |reply|. On the flip | 86 // PostTaskAndReplyRelay self-destructs after executing |reply|. On the flip |
87 // side though, it is intentionally leaked if the |task| doesn't complete | 87 // side though, it is intentionally leaked if the |task| doesn't complete |
88 // before the origin sequence stops executing tasks. Annotate |relay| as leaky | 88 // before the origin sequence stops executing tasks. Annotate |relay| as leaky |
89 // to avoid having to suppress every callsite which happens to flakily trigger | 89 // to avoid having to suppress every callsite which happens to flakily trigger |
90 // this race. | 90 // this race. |
91 ANNOTATE_LEAKING_OBJECT_PTR(relay); | 91 ANNOTATE_LEAKING_OBJECT_PTR(relay); |
92 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply, | 92 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply, |
93 Unretained(relay)))) { | 93 Unretained(relay)))) { |
94 delete relay; | 94 delete relay; |
95 return false; | 95 return false; |
96 } | 96 } |
97 | 97 |
98 return true; | 98 return true; |
99 } | 99 } |
100 | 100 |
101 } // namespace internal | 101 } // namespace internal |
102 | 102 |
103 } // namespace base | 103 } // namespace base |
OLD | NEW |