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

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

Issue 2180953002: Support PostTaskAndReply from a sequenced task. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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/location.h" 8 #include "base/bind_helpers.h"
gab 2016/07/25 17:57:44 Ditto
fdoray 2016/07/25 20:40:04 Done.
9 #include "base/single_thread_task_runner.h" 9 #include "base/callback.h"
10 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/logging.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/sequence_checker.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/threading/sequenced_task_runner_handle.h"
11 15
12 namespace base { 16 namespace base {
13 17
14 namespace { 18 namespace {
15 19
16 // This relay class remembers the MessageLoop that it was created on, and 20 // This relay class remembers the sequence it was created on and ensures that
17 // ensures that both the |task| and |reply| Closures are deleted on this same 21 // both the |task| and |reply| Closures are deleted on this same sequence. Also,
18 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or 22 // |task| is guaranteed to be deleted before |reply| is run or deleted.
19 // deleted.
20 // 23 //
21 // If this is not possible because the originating MessageLoop is no longer 24 // If |reply| doesn't run because the originating execution context is no longer
22 // available, the the |task| and |reply| Closures are leaked. Leaking is 25 // available, then the |task| and |reply| Closures are leaked. Leaking is
23 // considered preferable to having a thread-safetey violations caused by 26 // considered preferable to having a thread-safetey violations caused by
24 // invoking the Closure destructor on the wrong thread. 27 // invoking the Closure destructor on the wrong sequence.
25 class PostTaskAndReplyRelay { 28 class PostTaskAndReplyRelay {
26 public: 29 public:
27 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, 30 PostTaskAndReplyRelay(const tracked_objects::Location& from_here,
28 const Closure& task, 31 const Closure& task,
29 const Closure& reply) 32 const Closure& reply)
30 : from_here_(from_here), 33 : from_here_(from_here),
31 origin_task_runner_(ThreadTaskRunnerHandle::Get()) { 34 origin_task_runner_(SequencedTaskRunnerHandle::Get()),
32 task_ = task; 35 reply_(reply),
33 reply_ = reply; 36 task_(task) {}
34 }
35 37
36 ~PostTaskAndReplyRelay() { 38 ~PostTaskAndReplyRelay() {
37 DCHECK(origin_task_runner_->BelongsToCurrentThread()); 39 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
38 task_.Reset(); 40 task_.Reset();
39 reply_.Reset(); 41 reply_.Reset();
40 } 42 }
41 43
42 void Run() { 44 void RunTaskAndPostReply() {
43 task_.Run(); 45 task_.Run();
44 origin_task_runner_->PostTask( 46 origin_task_runner_->PostTask(
45 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, 47 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
46 base::Unretained(this))); 48 base::Unretained(this)));
47 } 49 }
48 50
49 private: 51 private:
50 void RunReplyAndSelfDestruct() { 52 void RunReplyAndSelfDestruct() {
51 DCHECK(origin_task_runner_->BelongsToCurrentThread()); 53 DCHECK(sequence_checker_.CalledOnValidSequencedThread());
52 54
53 // Force |task_| to be released before |reply_| is to ensure that no one 55 // Force |task_| to be released before |reply_| is to ensure that no one
54 // accidentally depends on |task_| keeping one of its arguments alive while 56 // accidentally depends on |task_| keeping one of its arguments alive while
55 // |reply_| is executing. 57 // |reply_| is executing.
56 task_.Reset(); 58 task_.Reset();
57 59
58 reply_.Run(); 60 reply_.Run();
59 61
60 // Cue mission impossible theme.
gab 2016/07/25 17:57:44 Aaahhh... this has been my favorite comment in Chr
61 delete this; 62 delete this;
62 } 63 }
63 64
64 tracked_objects::Location from_here_; 65 SequenceChecker sequence_checker_;
gab 2016/07/25 17:57:44 const (never detached)
fdoray 2016/07/25 20:40:04 Done.
65 scoped_refptr<SingleThreadTaskRunner> origin_task_runner_; 66 const tracked_objects::Location from_here_;
67 const scoped_refptr<SequencedTaskRunner> origin_task_runner_;
66 Closure reply_; 68 Closure reply_;
67 Closure task_; 69 Closure task_;
68 }; 70 };
69 71
70 } // namespace 72 } // namespace
71 73
72 namespace internal { 74 namespace internal {
73 75
74 bool PostTaskAndReplyImpl::PostTaskAndReply( 76 bool PostTaskAndReply(const tracked_objects::Location& from_here,
75 const tracked_objects::Location& from_here, 77 const Closure& task,
76 const Closure& task, 78 const Closure& reply,
77 const Closure& reply) { 79 const PostTaskCallback& post_task_callback) {
78 // TODO(tzik): Use DCHECK here once the crash is gone. http://crbug.com/541319 80 DCHECK(!task.is_null()) << from_here.ToString();
79 CHECK(!task.is_null()) << from_here.ToString(); 81 DCHECK(!reply.is_null()) << from_here.ToString();
80 CHECK(!reply.is_null()) << from_here.ToString(); 82
81 PostTaskAndReplyRelay* relay = 83 PostTaskAndReplyRelay* relay =
82 new PostTaskAndReplyRelay(from_here, task, reply); 84 new PostTaskAndReplyRelay(from_here, task, reply);
83 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run, 85 if (!post_task_callback.Run(from_here,
84 Unretained(relay)))) { 86 Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply,
87 Unretained(relay)))) {
85 delete relay; 88 delete relay;
86 return false; 89 return false;
87 } 90 }
88 91
89 return true; 92 return true;
90 } 93 }
91 94
92 } // namespace internal 95 } // namespace internal
93 96
94 } // namespace base 97 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698