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
3 // found in the LICENSE file.
5 #include "base/threading/post_task_and_reply_impl.h"
8 #include "base/location.h"
9 #include "base/message_loop_proxy.h"
15 // 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 // thread. Also, |task| is guaranteed to be deleted before |reply| is run or
20 // 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 // considered preferable to having a thread-safetey violations caused by
23 // invoking the Closure destructor on the wrong thread.
24 class PostTaskAndReplyRelay
{
26 PostTaskAndReplyRelay(const tracked_objects::Location
& from_here
,
27 const Closure
& task
, const Closure
& reply
)
28 : from_here_(from_here
),
29 origin_loop_(MessageLoopProxy::current()) {
34 ~PostTaskAndReplyRelay() {
35 DCHECK(origin_loop_
->BelongsToCurrentThread());
42 origin_loop_
->PostTask(
44 Bind(&PostTaskAndReplyRelay::RunReplyAndSelfDestruct
,
45 base::Unretained(this)));
49 void RunReplyAndSelfDestruct() {
50 DCHECK(origin_loop_
->BelongsToCurrentThread());
52 // Force |task_| to be released before |reply_| is to ensure that no one
53 // accidentally depends on |task_| keeping one of its arguments alive while
54 // |reply_| is executing.
59 // Cue mission impossible theme.
63 tracked_objects::Location from_here_
;
64 scoped_refptr
<MessageLoopProxy
> origin_loop_
;
73 bool PostTaskAndReplyImpl::PostTaskAndReply(
74 const tracked_objects::Location
& from_here
,
76 const Closure
& reply
) {
77 PostTaskAndReplyRelay
* relay
=
78 new PostTaskAndReplyRelay(from_here
, task
, reply
);
79 if (!PostTask(from_here
, Bind(&PostTaskAndReplyRelay::Run
,
80 Unretained(relay
)))) {
88 } // namespace internal