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

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

Issue 1100773004: base: Remove most uses of MessageLoopProxy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added some missing includes. Created 5 years, 7 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/location.h"
9 #include "base/single_thread_task_runner.h" 9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
11 11
12 namespace base { 12 namespace base {
13 13
14 namespace { 14 namespace {
15 15
16 // 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
17 // 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
18 // 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
19 // deleted. 19 // deleted.
20 // 20 //
21 // 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
22 // available, the the |task| and |reply| Closures are leaked. Leaking is 22 // available, the the |task| and |reply| Closures are leaked. Leaking is
23 // considered preferable to having a thread-safetey violations caused by 23 // considered preferable to having a thread-safetey violations caused by
24 // invoking the Closure destructor on the wrong thread. 24 // invoking the Closure destructor on the wrong thread.
25 class PostTaskAndReplyRelay { 25 class PostTaskAndReplyRelay {
26 public: 26 public:
27 PostTaskAndReplyRelay(const tracked_objects::Location& from_here, 27 PostTaskAndReplyRelay(const tracked_objects::Location& from_here,
28 const Closure& task, const Closure& reply) 28 const Closure& task,
29 const Closure& reply)
29 : from_here_(from_here), 30 : from_here_(from_here),
30 origin_loop_(ThreadTaskRunnerHandle::Get()) { 31 origin_task_runner_(ThreadTaskRunnerHandle::Get()) {
31 task_ = task; 32 task_ = task;
32 reply_ = reply; 33 reply_ = reply;
33 } 34 }
34 35
35 ~PostTaskAndReplyRelay() { 36 ~PostTaskAndReplyRelay() {
36 DCHECK(origin_loop_->BelongsToCurrentThread()); 37 DCHECK(origin_task_runner_->BelongsToCurrentThread());
37 task_.Reset(); 38 task_.Reset();
38 reply_.Reset(); 39 reply_.Reset();
39 } 40 }
40 41
41 void Run() { 42 void Run() {
42 task_.Run(); 43 task_.Run();
43 origin_loop_->PostTask( 44 origin_task_runner_->PostTask(
44 from_here_, 45 from_here_, Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct,
45 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct, 46 base::Unretained(this)));
46 base::Unretained(this)));
47 } 47 }
48 48
49 private: 49 private:
50 void RunReplyAndSelfDestruct() { 50 void RunReplyAndSelfDestruct() {
51 DCHECK(origin_loop_->BelongsToCurrentThread()); 51 DCHECK(origin_task_runner_->BelongsToCurrentThread());
52 52
53 // Force |task_| to be released before |reply_| is to ensure that no one 53 // 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 54 // accidentally depends on |task_| keeping one of its arguments alive while
55 // |reply_| is executing. 55 // |reply_| is executing.
56 task_.Reset(); 56 task_.Reset();
57 57
58 reply_.Run(); 58 reply_.Run();
59 59
60 // Cue mission impossible theme. 60 // Cue mission impossible theme.
61 delete this; 61 delete this;
62 } 62 }
63 63
64 tracked_objects::Location from_here_; 64 tracked_objects::Location from_here_;
65 scoped_refptr<SingleThreadTaskRunner> origin_loop_; 65 scoped_refptr<SingleThreadTaskRunner> origin_task_runner_;
66 Closure reply_; 66 Closure reply_;
67 Closure task_; 67 Closure task_;
68 }; 68 };
69 69
70 } // namespace 70 } // namespace
71 71
72 namespace internal { 72 namespace internal {
73 73
74 bool PostTaskAndReplyImpl::PostTaskAndReply( 74 bool PostTaskAndReplyImpl::PostTaskAndReply(
75 const tracked_objects::Location& from_here, 75 const tracked_objects::Location& from_here,
76 const Closure& task, 76 const Closure& task,
77 const Closure& reply) { 77 const Closure& reply) {
78 PostTaskAndReplyRelay* relay = 78 PostTaskAndReplyRelay* relay =
79 new PostTaskAndReplyRelay(from_here, task, reply); 79 new PostTaskAndReplyRelay(from_here, task, reply);
80 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run, 80 if (!PostTask(from_here, Bind(&PostTaskAndReplyRelay::Run,
81 Unretained(relay)))) { 81 Unretained(relay)))) {
82 delete relay; 82 delete relay;
83 return false; 83 return false;
84 } 84 }
85 85
86 return true; 86 return true;
87 } 87 }
88 88
89 } // namespace internal 89 } // namespace internal
90 90
91 } // namespace base 91 } // namespace base
OLDNEW
« no previous file with comments | « base/threading/post_task_and_reply_impl.h ('k') | base/threading/sequenced_worker_pool_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698