aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/threading/future/core/future.h
blob: 598336282a06e9efbd9ba1bff5af05a1882c7520 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#pragma once

#include "fwd.h"

#include <library/cpp/deprecated/atomic/atomic.h>

#include <util/datetime/base.h>
#include <util/generic/function.h>
#include <util/generic/maybe.h>
#include <util/generic/ptr.h>
#include <util/generic/vector.h>
#include <util/generic/yexception.h>
#include <util/system/event.h>
#include <util/system/spinlock.h>

namespace NThreading {
    ////////////////////////////////////////////////////////////////////////////////

    struct TFutureException: public yexception {};

    // creates unset promise
    template <typename T>
    TPromise<T> NewPromise();
    TPromise<void> NewPromise();

    // creates preset future
    template <typename T>
    TFuture<T> MakeFuture(const T& value);
    template <typename T>
    TFuture<std::remove_reference_t<T>> MakeFuture(T&& value);
    template <typename T>
    TFuture<T> MakeFuture();
    template <typename T>
    TFuture<T> MakeErrorFuture(std::exception_ptr exception);
    TFuture<void> MakeFuture();

    ////////////////////////////////////////////////////////////////////////////////

    namespace NImpl {
        template <typename T>
        class TFutureState;

        template <typename T>
        struct TFutureType {
            using TType = T;
        };

        template <typename T>
        struct TFutureType<TFuture<T>> {
            using TType = typename TFutureType<T>::TType;
        };

        template <typename F, typename T>
        struct TFutureCallResult {
            // NOTE: separate class for msvc compatibility
            using TType = decltype(std::declval<F&>()(std::declval<const TFuture<T>&>()));
        };
    }

    template <typename F>
    using TFutureType = typename NImpl::TFutureType<F>::TType;

    template <typename F, typename T>
    using TFutureCallResult = typename NImpl::TFutureCallResult<F, T>::TType;

    //! Type of the future/promise state identifier
    class TFutureStateId;

    ////////////////////////////////////////////////////////////////////////////////

    template <typename T>
    class TFuture {
        using TFutureState = NImpl::TFutureState<T>;

    private:
        TIntrusivePtr<TFutureState> State;

    public:
        using value_type = T;

        TFuture() noexcept = default;
        TFuture(const TFuture<T>& other) noexcept = default;
        TFuture(TFuture<T>&& other) noexcept = default;
        TFuture(const TIntrusivePtr<TFutureState>& state) noexcept;

        TFuture<T>& operator=(const TFuture<T>& other) noexcept = default;
        TFuture<T>& operator=(TFuture<T>&& other) noexcept = default;
        void Swap(TFuture<T>& other);

        bool Initialized() const;

        bool HasValue() const;
        const T& GetValue(TDuration timeout = TDuration::Zero()) const;
        const T& GetValueSync() const;
        T ExtractValue(TDuration timeout = TDuration::Zero());
        T ExtractValueSync();

        void TryRethrow() const;
        bool HasException() const;

        void Wait() const;
        bool Wait(TDuration timeout) const;
        bool Wait(TInstant deadline) const;

        template <typename F>
        const TFuture<T>& Subscribe(F&& callback) const;

        // precondition: EnsureInitialized() passes
        // postcondition: std::terminate is highly unlikely
        template <typename F>
        const TFuture<T>& NoexceptSubscribe(F&& callback) const noexcept;

        template <typename F>
        TFuture<TFutureType<TFutureCallResult<F, T>>> Apply(F&& func) const;

        TFuture<void> IgnoreResult() const;

        //! If the future is initialized returns the future state identifier. Otherwise returns an empty optional
        /** The state identifier is guaranteed to be unique during the future state lifetime and could be reused after its death
        **/
        TMaybe<TFutureStateId> StateId() const noexcept;

        void EnsureInitialized() const;
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <>
    class TFuture<void> {
        using TFutureState = NImpl::TFutureState<void>;

    private:
        TIntrusivePtr<TFutureState> State = nullptr;

    public:
        using value_type = void;

        TFuture() noexcept = default;
        TFuture(const TFuture<void>& other) noexcept = default;
        TFuture(TFuture<void>&& other) noexcept = default;
        TFuture(const TIntrusivePtr<TFutureState>& state) noexcept;

        TFuture<void>& operator=(const TFuture<void>& other) noexcept = default;
        TFuture<void>& operator=(TFuture<void>&& other) noexcept = default;
        void Swap(TFuture<void>& other);

        bool Initialized() const;

        bool HasValue() const;
        void GetValue(TDuration timeout = TDuration::Zero()) const;
        void GetValueSync() const;

        void TryRethrow() const;
        bool HasException() const;

        void Wait() const;
        bool Wait(TDuration timeout) const;
        bool Wait(TInstant deadline) const;

        template <typename F>
        const TFuture<void>& Subscribe(F&& callback) const;

        // precondition: EnsureInitialized() passes
        // postcondition: std::terminate is highly unlikely
        template <typename F>
        const TFuture<void>& NoexceptSubscribe(F&& callback) const noexcept;

        template <typename F>
        TFuture<TFutureType<TFutureCallResult<F, void>>> Apply(F&& func) const;

        template <typename R>
        TFuture<std::remove_cvref_t<R>> Return(R&& value) const;

        TFuture<void> IgnoreResult() const {
            return *this;
        }

        //! If the future is initialized returns the future state identifier. Otherwise returns an empty optional
        /** The state identifier is guaranteed to be unique during the future state lifetime and could be reused after its death
        **/
        TMaybe<TFutureStateId> StateId() const noexcept;

        void EnsureInitialized() const;
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <typename T>
    class TPromise {
        using TFutureState = NImpl::TFutureState<T>;

    private:
        TIntrusivePtr<TFutureState> State = nullptr;

    public:
        TPromise() noexcept = default;
        TPromise(const TPromise<T>& other) noexcept = default;
        TPromise(TPromise<T>&& other) noexcept = default;
        TPromise(const TIntrusivePtr<TFutureState>& state) noexcept;

        TPromise<T>& operator=(const TPromise<T>& other) noexcept = default;
        TPromise<T>& operator=(TPromise<T>&& other) noexcept = default;
        void Swap(TPromise<T>& other);

        bool Initialized() const;

        bool HasValue() const;
        const T& GetValue() const;
        T ExtractValue();

        void SetValue(const T& value);
        void SetValue(T&& value);

        bool TrySetValue(const T& value);
        bool TrySetValue(T&& value);

        void TryRethrow() const;
        bool HasException() const;
        void SetException(const TString& e);
        void SetException(std::exception_ptr e);
        bool TrySetException(std::exception_ptr e);

        TFuture<T> GetFuture() const;
        operator TFuture<T>() const;

    private:
        void EnsureInitialized() const;
    };

    ////////////////////////////////////////////////////////////////////////////////

    template <>
    class TPromise<void> {
        using TFutureState = NImpl::TFutureState<void>;

    private:
        TIntrusivePtr<TFutureState> State;

    public:
        TPromise() noexcept = default;
        TPromise(const TPromise<void>& other) noexcept = default;
        TPromise(TPromise<void>&& other) noexcept = default;
        TPromise(const TIntrusivePtr<TFutureState>& state) noexcept;

        TPromise<void>& operator=(const TPromise<void>& other) noexcept = default;
        TPromise<void>& operator=(TPromise<void>&& other) noexcept = default;
        void Swap(TPromise<void>& other);

        bool Initialized() const;

        bool HasValue() const;
        void GetValue() const;

        void SetValue();
        bool TrySetValue();

        void TryRethrow() const;
        bool HasException() const;
        void SetException(const TString& e);
        void SetException(std::exception_ptr e);
        bool TrySetException(std::exception_ptr e);

        TFuture<void> GetFuture() const;
        operator TFuture<void>() const;

    private:
        void EnsureInitialized() const;
    };

}

#define INCLUDE_FUTURE_INL_H
#include "future-inl.h"
#undef INCLUDE_FUTURE_INL_H