1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file defines a common helper encapsulating the tricky bits of
6 // implementing PostTaskAndReply(). These tricky bits are specifically around
7 // handling of the reply callback and ensuring it is deleted on the correct
8 // sequence.
9
10 #ifndef BASE_THREADING_POST_TASK_AND_REPLY_IMPL_H_
11 #define BASE_THREADING_POST_TASK_AND_REPLY_IMPL_H_
12
13 #include <utility>
14
15 #include "base/base_export.h"
16 #include "base/check_op.h"
17 #include "base/functional/bind.h"
18 #include "base/functional/callback.h"
19 #include "base/location.h"
20 #include "base/memory/scoped_refptr.h"
21 #include "base/task/sequenced_task_runner.h"
22
23 namespace base::internal {
24
25 class BASE_EXPORT PostTaskAndReplyRelay {
26 public:
27 PostTaskAndReplyRelay(const Location& from_here,
28 OnceClosure task,
29 OnceClosure reply,
30 scoped_refptr<SequencedTaskRunner> reply_task_runner);
31
32 PostTaskAndReplyRelay(PostTaskAndReplyRelay&&);
33 // No assignment operator because of const member.
34 PostTaskAndReplyRelay& operator=(PostTaskAndReplyRelay&&) = delete;
35
36 PostTaskAndReplyRelay(const PostTaskAndReplyRelay&) = delete;
37 PostTaskAndReplyRelay& operator=(const PostTaskAndReplyRelay&) = delete;
38
39 ~PostTaskAndReplyRelay();
40
41 // Static function is used because it is not possible to bind a method call to
42 // a non-pointer type.
RunTaskAndPostReply(PostTaskAndReplyRelay relay)43 static void RunTaskAndPostReply(PostTaskAndReplyRelay relay) {
44 DCHECK(relay.task_);
45 std::move(relay.task_).Run();
46
47 // Keep a pointer to the reply TaskRunner for the PostTask() call before
48 // |relay| is moved into a callback.
49 SequencedTaskRunner* reply_task_runner_raw = relay.reply_task_runner_.get();
50
51 const Location from_here = relay.from_here_;
52 reply_task_runner_raw->PostTask(
53 from_here,
54 BindOnce(&PostTaskAndReplyRelay::RunReply, std::move(relay)));
55 }
56
57 private:
58 // Static function is used because it is not possible to bind a method call to
59 // a non-pointer type.
RunReply(PostTaskAndReplyRelay relay)60 static void RunReply(PostTaskAndReplyRelay relay) {
61 DCHECK(!relay.task_);
62 DCHECK(relay.reply_);
63 std::move(relay.reply_).Run();
64 }
65
66 const Location from_here_;
67 OnceClosure task_;
68 OnceClosure reply_;
69 // Not const to allow moving.
70 scoped_refptr<SequencedTaskRunner> reply_task_runner_;
71 };
72
73 // Precondition: `SequencedTaskRunner::HasCurrentDefault()` must be true.
74 //
75 // Posts `task` by calling `task_poster`. On completion, posts `reply` to the
76 // origin sequence. Each callback is deleted synchronously after running, or
77 // scheduled for asynchronous deletion on the origin sequence if it can't run
78 // (e.g. if a TaskRunner skips it on shutdown). In this case, the callback may
79 // leak: see `SequencedTaskRunner::DeleteSoon()` for details about when objects
80 // scheduled for asynchronous deletion can be leaked.
81 //
82 // Note: All //base task posting APIs require callbacks to support deletion on
83 // the posting sequence if they can't be scheduled.
84 template <typename TaskPoster>
PostTaskAndReplyImpl(TaskPoster task_poster,const Location & from_here,OnceClosure task,OnceClosure reply)85 bool PostTaskAndReplyImpl(TaskPoster task_poster,
86 const Location& from_here,
87 OnceClosure task,
88 OnceClosure reply) {
89 DCHECK(task) << from_here.ToString();
90 DCHECK(reply) << from_here.ToString();
91
92 const bool has_sequenced_context = SequencedTaskRunner::HasCurrentDefault();
93
94 const bool post_task_success = task_poster(
95 from_here, BindOnce(&PostTaskAndReplyRelay::RunTaskAndPostReply,
96 PostTaskAndReplyRelay(
97 from_here, std::move(task), std::move(reply),
98 has_sequenced_context
99 ? SequencedTaskRunner::GetCurrentDefault()
100 : nullptr)));
101
102 // PostTaskAndReply() requires a SequencedTaskRunner::CurrentDefaultHandle to
103 // post the reply. Having no SequencedTaskRunner::CurrentDefaultHandle is
104 // allowed when posting the task fails, to simplify calls during shutdown
105 // (https://crbug.com/922938).
106 CHECK(has_sequenced_context || !post_task_success);
107
108 return post_task_success;
109 }
110
111 } // namespace base::internal
112
113 #endif // BASE_THREADING_POST_TASK_AND_REPLY_IMPL_H_
114