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/location.h" | 8 #include "base/callback.h" |
9 #include "base/single_thread_task_runner.h" | 9 #include "base/logging.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/sequence_checker.h" | |
12 #include "base/sequenced_task_runner.h" | |
13 #include "base/threading/sequenced_task_runner_handle.h" | |
11 | 14 |
12 namespace base { | 15 namespace base { |
13 | 16 |
14 namespace { | 17 namespace { |
15 | 18 |
16 // This relay class remembers the MessageLoop that it was created on, and | 19 // This relay class remembers the sequence that it was created on, and ensures |
17 // ensures that both the |task| and |reply| Closures are deleted on this same | 20 // that both the |task| and |reply| Closures are deleted on this same sequence. |
18 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or | 21 // Also, |task| is guaranteed to be deleted before |reply| is run or deleted. |
19 // deleted. | |
20 // | 22 // |
21 // If this is not possible because the originating MessageLoop is no longer | 23 // If RunReplyAndSelfDestruct() doesn't run because the originating execution |
22 // available, the the |task| and |reply| Closures are leaked. Leaking is | 24 // context is no longer available, then the |task| and |reply| Closures are |
23 // considered preferable to having a thread-safetey violations caused by | 25 // leaked. Leaking is considered preferable to having a thread-safetey |
24 // invoking the Closure destructor on the wrong thread. | 26 // violations caused by invoking the Closure destructor on the wrong sequence. |
25 class PostTaskAndReplyRelay { | 27 class PostTaskAndReplyRelay { |
26 public: | 28 public: |
27 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, | 29 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
28 const Closure& task, | 30 const Closure& task, |
29 const Closure& reply) | 31 const Closure& reply) |
30 : from_here_(from_here), | 32 : sequence_checker_(), |
gab
2016/07/30 16:57:54
Not necessary, member should be automatically init
fdoray
2016/08/01 13:22:22
Build error with clang when const member is not ex
gab
2016/08/01 13:26:21
Uh, that surprises me, I don't see why that's nece
| |
31 origin_task_runner_(ThreadTaskRunnerHandle::Get()) { | 33 from_here_(from_here), |
32 task_ = task; | 34 origin_task_runner_(SequencedTaskRunnerHandle::Get()), |
33 reply_ = reply; | 35 reply_(reply), |
34 } | 36 task_(task) {} |
35 | 37 |
36 ~PostTaskAndReplyRelay() { | 38 ~PostTaskAndReplyRelay() { |
37 DCHECK(origin_task_runner_->BelongsToCurrentThread()); | 39 DCHECK(sequence_checker_.CalledOnValidSequence()); |
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_.CalledOnValidSequence()); |
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. | 62 // Cue mission impossible theme. |
61 delete this; | 63 delete this; |
62 } | 64 } |
63 | 65 |
64 tracked_objects::Location from_here_; | 66 const SequenceChecker sequence_checker_; |
65 scoped_refptr<SingleThreadTaskRunner> origin_task_runner_; | 67 const tracked_objects::Location from_here_; |
68 const scoped_refptr<SequencedTaskRunner> origin_task_runner_; | |
66 Closure reply_; | 69 Closure reply_; |
67 Closure task_; | 70 Closure task_; |
68 }; | 71 }; |
69 | 72 |
70 } // namespace | 73 } // namespace |
71 | 74 |
72 namespace internal { | 75 namespace internal { |
73 | 76 |
74 bool PostTaskAndReplyImpl::PostTaskAndReply( | 77 bool PostTaskAndReplyImpl::PostTaskAndReply( |
75 const tracked_objects::Location& from_here, | 78 const tracked_objects::Location& from_here, |
76 const Closure& task, | 79 const Closure& task, |
77 const Closure& reply) { | 80 const Closure& reply) { |
78 // TODO(tzik): Use DCHECK here once the crash is gone. http://crbug.com/541319 | 81 DCHECK(!task.is_null()) << from_here.ToString(); |
79 CHECK(!task.is_null()) << from_here.ToString(); | 82 DCHECK(!reply.is_null()) << from_here.ToString(); |
80 CHECK(!reply.is_null()) << from_here.ToString(); | |
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 (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::RunTaskAndPostReply, |
84 Unretained(relay)))) { | 86 Unretained(relay)))) { |
85 delete relay; | 87 delete relay; |
86 return false; | 88 return false; |
87 } | 89 } |
88 | 90 |
89 return true; | 91 return true; |
90 } | 92 } |
91 | 93 |
92 } // namespace internal | 94 } // namespace internal |
93 | 95 |
94 } // namespace base | 96 } // namespace base |
OLD | NEW |