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

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

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

Powered by Google App Engine
This is Rietveld 408576698