Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Side by Side Diff: base/threading/post_task_and_reply_impl.cc

Issue 2657603004: Clear PostTaskAndReply task on the destination thread (3) (Closed)
Patch Set: rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 Closure task,
gab 2017/01/31 19:34:59 Can this CL be focused on this change only? (i.e.
tzik 2017/02/07 08:05:35 Sure. Let me split them to a separate CL.
32 const Closure& reply) 32 Closure 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();
42 reply_.Reset();
43 } 41 }
44 42
45 void RunTaskAndPostReply() { 43 void RunTaskAndPostReply() {
46 task_.Run(); 44 task_.Run();
45 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 // Force |task_| to be released before |reply_| is to ensure that no one 55 // Ensure |task_| to be released before |reply_| is to ensure that no one
dcheng 2017/02/07 07:28:44 Nit: reword to fix some minor typos in the rest of
tzik 2017/02/07 08:05:34 Acknowledged.
tzik 2017/02/07 08:33:26 Done.
57 // accidentally depends on |task_| keeping one of its arguments alive while 56 // accidentally depends on |task_| keeping one of its arguments alive while
58 // |reply_| is executing. 57 // |reply_| is executing.
59 task_.Reset(); 58 DCHECK(!task_);
60 59
61 reply_.Run(); 60 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 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698