1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_H_
18 #define INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_H_
19
20 #include <memory>
21 #include <type_traits>
22
23 #include "perfetto/ext/base/status_or.h"
24 #include "perfetto/ext/base/threading/future_combinators.h"
25 #include "perfetto/ext/base/threading/poll.h"
26
27 namespace perfetto {
28 namespace base {
29
30 // Creates a Future<T> from P, a subclass of FuturePollable<T>.
31 //
32 // T generally is a primitive (e.g. int, string, double) or structs of
33 // primitives but any can also be any moveable type.
34 //
35 // This function follows the same pattern of std::make_unique, std::make_shared
36 // etc.
37 template <typename P, typename... Args, typename T = typename P::PollT>
MakeFuture(Args...args)38 Future<T> MakeFuture(Args... args) {
39 return Future<T>(
40 std::unique_ptr<FuturePollable<T>>(new P(std::forward<Args>(args)...)));
41 }
42
43 // A value of type T which is computed asynchronously.
44 //
45 // The result of long running compute/IO operations may not be available
46 // immediately. This class acts as a representation of the value which will be
47 // produced at some point in the future. Callers can then be notified of the
48 // result once it's available to be processed.
49 //
50 // This class takes heavy inspiration from the implementation of Futures in
51 // Rust. Specifically, this implementation is:
52 // - pull-based/lazy: Futures do nothing until "polled" i.e. driven to
53 // completion by a base::TaskRunner. The implementation of this is provided
54 // by base::TaskRunnerPoller.
55 // - backpressured: because futures are "polled", the result is only
56 // requested when it can be processed on the base::TaskRunner thread.
57 // - cancellable: by just destroying the future the computation can be
58 // cancelled. Note, that the implementation of the source future still needs
59 // to propogate cancellation across thread/socket/pipe boundary.
60 //
61 // Note: Futures *must* be polled on the same thread on which they were created.
62 // The |SpawnResultFuture| can be used to move the results of Futures between
63 // threads in a safe manner.
64 //
65 // Implementation note:
66 // An important point to note is that Future<T> is a final class. Implementation
67 // of Future<T>::Poll happens through an indirection layer by implementing the
68 // FuturePollable<T> interface. This allows for the
69 // unique_ptr<FuturePollable<T>> to be hidden, making callsites nicer while
70 // also allowing useful "helper" functions like |ContinueWith| to live on the
71 // class rather than as free functions.
72 template <typename T>
73 class Future final {
74 public:
75 using PollT = T;
76
77 // Creates a Future from a |FuturePollable<T>|. Prefer using |MakeFuture|
78 // instead of this function.
Future(std::unique_ptr<FuturePollable<T>> pollable)79 explicit Future(std::unique_ptr<FuturePollable<T>> pollable)
80 : pollable_(std::move(pollable)) {}
81
82 // Intentionally implicit to allow for ergonomic definition of functions
83 // returning Future<T> for any T.
Future(T item)84 Future(T item) : pollable_(new ImmediateImpl<T>(std::move(item))) {}
85
86 // Intentionally implicit to allow for egonomic definition of functions
87 // returning Future<StatusOr<T>> by simply returning ErrStatus.
88 // The enable_if is necessary because this definition is the same as the above
89 // constructor in cases where T = base::Status.
90 template <typename U = T,
91 typename = std::enable_if_t<!std::is_same_v<Status, U>>>
Future(Status status)92 Future(Status status) : Future(T(std::move(status))) {}
93
94 // Intentionally implicit to allow for egonomic definition of functions
95 // returning Future<StatusOr<T>> by simply returning T.
96 template <typename U = T, typename = typename U::value_type>
Future(typename U::value_type val)97 Future(typename U::value_type val) : Future(T(std::move(val))) {}
98
99 // Operator used to chain operations on Futures. The result T produced by
100 // |this| is passed to |fn| which itself returns a Future<U>. The return value
101 // of this function is a Future<U> which encapsulates both the operation done
102 // by |this| as well as by the Future<U> returned by |fn|.
103 //
104 // Usage:
105 // ```
106 // Future<int> MySpecialFutureFn();
107 // Future<std::string> IntToStringInBackground(int);
108 //
109 // MySpecialFutureFn().ContinueWith([](int x) -> Future<std::string> {
110 // return IntToStringInBackground(x);
111 // });
112 // ```
113 template <typename Function /* Future<U>(T) */,
114 typename U = FutureReturn<Function, T>>
ContinueWith(Function fn)115 Future<U> ContinueWith(Function fn) && {
116 return MakeFuture<ContinueWithImpl<Function, T>>(std::move(*this),
117 std::move(fn));
118 }
119
120 // Checks if the computation backing this Future<T> has finished.
121 //
122 // Returns a FuturePollResult<T> which is a essentially a
123 // variant<PendingPollResult, T>. If PendingPollResult is returned, |ctx| will
124 // be used to register interest in the various fds which are "blocking" this
125 // future from finishing. If T is returned, Poll *must not* be called again.
Poll(PollContext * ctx)126 FuturePollResult<T> Poll(PollContext* ctx) { return pollable_->Poll(ctx); }
127
128 private:
129 // TOOD(lalitm): if performance becomes a problem, this can be changed to
130 // something more efficient e.g. either storage in a stack allocated buffer
131 // or with bump-pointer allocation. In the current usage this is not a
132 // performance bottleneck and so this is not important enough to invest time
133 // into fixing.
134 std::unique_ptr<FuturePollable<T>> pollable_;
135 };
136
137 // Alias to shorten type defintions for Future<Status> which is common in
138 // the codebase.
139 using StatusFuture = Future<Status>;
140
141 // Alias to shorten type defintions for Future<StatusOr<T>> which is common
142 // in the codebase.
143 template <typename T>
144 using StatusOrFuture = Future<StatusOr<T>>;
145
146 } // namespace base
147 } // namespace perfetto
148
149 #endif // INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_H_
150