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/location.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/thread_task_runner_handle.h" |
10 | 11 |
11 namespace base { | 12 namespace base { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 // This relay class remembers the MessageLoop that it was created on, and | 16 // This relay class remembers the MessageLoop that it was created on, and |
16 // ensures that both the |task| and |reply| Closures are deleted on this same | 17 // ensures that both the |task| and |reply| Closures are deleted on this same |
17 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or | 18 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or |
18 // deleted. | 19 // deleted. |
19 // | 20 // |
20 // If this is not possible because the originating MessageLoop is no longer | 21 // If this is not possible because the originating MessageLoop is no longer |
21 // available, the the |task| and |reply| Closures are leaked. Leaking is | 22 // available, the the |task| and |reply| Closures are leaked. Leaking is |
22 // considered preferable to having a thread-safetey violations caused by | 23 // considered preferable to having a thread-safetey violations caused by |
23 // invoking the Closure destructor on the wrong thread. | 24 // invoking the Closure destructor on the wrong thread. |
24 class PostTaskAndReplyRelay { | 25 class PostTaskAndReplyRelay { |
25 public: | 26 public: |
26 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, | 27 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, |
27 const Closure& task, const Closure& reply) | 28 const Closure& task, const Closure& reply) |
28 : from_here_(from_here), | 29 : from_here_(from_here), |
29 origin_loop_(MessageLoopProxy::current()) { | 30 origin_loop_(ThreadTaskRunnerHandle::Get()) { |
30 task_ = task; | 31 task_ = task; |
31 reply_ = reply; | 32 reply_ = reply; |
32 } | 33 } |
33 | 34 |
34 ~PostTaskAndReplyRelay() { | 35 ~PostTaskAndReplyRelay() { |
35 DCHECK(origin_loop_->BelongsToCurrentThread()); | 36 DCHECK(origin_loop_->BelongsToCurrentThread()); |
36 task_.Reset(); | 37 task_.Reset(); |
37 reply_.Reset(); | 38 reply_.Reset(); |
38 } | 39 } |
39 | 40 |
(...skipping 14 matching lines...) Expand all Loading... |
54 // |reply_| is executing. | 55 // |reply_| is executing. |
55 task_.Reset(); | 56 task_.Reset(); |
56 | 57 |
57 reply_.Run(); | 58 reply_.Run(); |
58 | 59 |
59 // Cue mission impossible theme. | 60 // Cue mission impossible theme. |
60 delete this; | 61 delete this; |
61 } | 62 } |
62 | 63 |
63 tracked_objects::Location from_here_; | 64 tracked_objects::Location from_here_; |
64 scoped_refptr<MessageLoopProxy> origin_loop_; | 65 scoped_refptr<SingleThreadTaskRunner> origin_loop_; |
65 Closure reply_; | 66 Closure reply_; |
66 Closure task_; | 67 Closure task_; |
67 }; | 68 }; |
68 | 69 |
69 } // namespace | 70 } // namespace |
70 | 71 |
71 namespace internal { | 72 namespace internal { |
72 | 73 |
73 bool PostTaskAndReplyImpl::PostTaskAndReply( | 74 bool PostTaskAndReplyImpl::PostTaskAndReply( |
74 const tracked_objects::Location& from_here, | 75 const tracked_objects::Location& from_here, |
75 const Closure& task, | 76 const Closure& task, |
76 const Closure& reply) { | 77 const Closure& reply) { |
77 PostTaskAndReplyRelay* relay = | 78 PostTaskAndReplyRelay* relay = |
78 new PostTaskAndReplyRelay(from_here, task, reply); | 79 new PostTaskAndReplyRelay(from_here, task, reply); |
79 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run, | 80 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run, |
80 Unretained(relay)))) { | 81 Unretained(relay)))) { |
81 delete relay; | 82 delete relay; |
82 return false; | 83 return false; |
83 } | 84 } |
84 | 85 |
85 return true; | 86 return true; |
86 } | 87 } |
87 | 88 |
88 } // namespace internal | 89 } // namespace internal |
89 | 90 |
90 } // namespace base | 91 } // namespace base |
OLD | NEW |