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 // Implementation note:
62 // An important point to note is that Future<T> is a final class. Implementation
63 // of Future<T>::Poll happens through an indirection layer by implementing the
64 // FuturePollable<T> interface. This allows for the
65 // unique_ptr<FuturePollable<T>> to be hidden, making callsites nicer while
66 // also allowing useful "helper" functions like |ContinueWith| to live on the
67 // class rather than as free functions.
68 template <typename T>
69 class Future final {
70 public:
71 using PollT = T;
72
73 // Creates a Future from a |FuturePollable<T>|. Prefer using |MakeFuture|
74 // instead of this function.
Future(std::unique_ptr<FuturePollable<T>> pollable)75 explicit Future(std::unique_ptr<FuturePollable<T>> pollable)
76 : pollable_(std::move(pollable)) {}
77
78 // Intentionally implicit to allow for ergonomic definition of functions
79 // returning Future<T> for any T.
Future(T item)80 Future(T item) : pollable_(new ImmediateImpl<T>(std::move(item))) {}
81
82 // Intentionally implicit to allow for egonomic definition of functions
83 // returning Future<StatusOr<T>> by simply returning ErrStatus.
84 // The enable_if is necessary because this definition is the same as the above
85 // constructor in cases where T = base::Status.
86 template <typename U = T,
87 typename = std::enable_if_t<!std::is_same_v<Status, U>>>
Future(Status status)88 Future(Status status) : Future(T(std::move(status))) {}
89
90 // Intentionally implicit to allow for egonomic definition of functions
91 // returning Future<StatusOr<T>> by simply returning T.
92 template <typename U = T, typename = typename U::value_type>
Future(typename U::value_type val)93 Future(typename U::value_type val) : Future(T(std::move(val))) {}
94
95 // Operator used to chain operations on Futures. The result T produced by
96 // |this| is passed to |fn| which itself returns a Future<U>. The return value
97 // of this function is a Future<U> which encapsulates both the operation done
98 // by |this| as well as by the Future<U> returned by |fn|.
99 //
100 // Usage:
101 // ```
102 // Future<int> MySpecialFutureFn();
103 // Future<std::string> IntToStringInBackground(int);
104 //
105 // MySpecialFutureFn().ContinueWith([](int x) -> Future<std::string> {
106 // return IntToStringInBackground(x);
107 // });
108 // ```
109 template <typename Function /* Future<U>(T) */,
110 typename U = FutureReturn<Function, T>>
ContinueWith(Function fn)111 Future<U> ContinueWith(Function fn) && {
112 return MakeFuture<ContinueWithImpl<Function, T>>(std::move(*this),
113 std::move(fn));
114 }
115
116 // Checks if the computation backing this Future<T> has finished.
117 //
118 // Returns a FuturePollResult<T> which is a essentially a
119 // variant<PendingPollResult, T>. If PendingPollResult is returned, |ctx| will
120 // be used to register interest in the various fds which are "blocking" this
121 // future from finishing. If T is returned, Poll *must not* be called again.
Poll(PollContext * ctx)122 FuturePollResult<T> Poll(PollContext* ctx) { return pollable_->Poll(ctx); }
123
124 private:
125 // TOOD(lalitm): if performance becomes a problem, this can be changed to
126 // something more efficient e.g. either storage in a stack allocated buffer
127 // or with bump-pointer allocation. In the current usage this is not a
128 // performance bottleneck and so this is not important enough to invest time
129 // into fixing.
130 std::unique_ptr<FuturePollable<T>> pollable_;
131 };
132
133 // Alias to shorten type defintions for Future<Status> which is common in
134 // the codebase.
135 using StatusFuture = Future<Status>;
136
137 // Alias to shorten type defintions for Future<StatusOr<T>> which is common
138 // in the codebase.
139 template <typename T>
140 using StatusOrFuture = Future<StatusOr<T>>;
141
142 } // namespace base
143 } // namespace perfetto
144
145 #endif // INCLUDE_PERFETTO_EXT_BASE_THREADING_FUTURE_H_
146