forked from scylladb/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
future.hh
1340 lines (1219 loc) · 45 KB
/
future.hh
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#ifndef FUTURE_HH_
#define FUTURE_HH_
#include "apply.hh"
#include "task.hh"
#include "preempt.hh"
#include "thread_impl.hh"
#include <stdexcept>
#include <atomic>
#include <memory>
#include <type_traits>
#include <assert.h>
#include <cstdlib>
#include "function_traits.hh"
/// \defgroup future-module Futures and Promises
///
/// \brief
/// Futures and promises are the basic tools for asynchronous
/// programming in seastar. A future represents a result that
/// may not have been computed yet, for example a buffer that
/// is being read from the disk, or the result of a function
/// that is executed on another cpu. A promise object allows
/// the future to be eventually resolved by assigning it a value.
///
/// \brief
/// Another way to look at futures and promises are as the reader
/// and writer sides, respectively, of a single-item, single use
/// queue. You read from the future, and write to the promise,
/// and the system takes care that it works no matter what the
/// order of operations is.
///
/// \brief
/// The normal way of working with futures is to chain continuations
/// to them. A continuation is a block of code (usually a lamdba)
/// that is called when the future is assigned a value (the future
/// is resolved); the continuation can then access the actual value.
///
/// \defgroup future-util Future Utilities
///
/// \brief
/// These utilities are provided to help perform operations on futures.
/// \addtogroup future-module
/// @{
template <class... T>
class promise;
template <class... T>
class future;
/// \brief Creates a \ref future in an available, value state.
///
/// Creates a \ref future object that is already resolved. This
/// is useful when it is determined that no I/O needs to be performed
/// to perform a computation (for example, because the data is cached
/// in some buffer).
template <typename... T, typename... A>
future<T...> make_ready_future(A&&... value);
/// \brief Creates a \ref future in an available, failed state.
///
/// Creates a \ref future object that is already resolved in a failed
/// state. This is useful when no I/O needs to be performed to perform
/// a computation (for example, because the connection is closed and
/// we cannot read from it).
template <typename... T>
future<T...> make_exception_future(std::exception_ptr value) noexcept;
/// \cond internal
void engine_exit(std::exception_ptr eptr = {});
void report_failed_future(std::exception_ptr ex);
/// \endcond
//
// A future/promise pair maintain one logical value (a future_state).
// To minimize allocations, the value is stored in exactly one of three
// locations:
//
// - in the promise (so long as it exists, and before a .then() is called)
//
// - in the task associated with the .then() clause (after .then() is called,
// if a value was not set)
//
// - in the future (if the promise was destroyed, or if it never existed, as
// with make_ready_future()), before .then() is called
//
// Both promise and future maintain a pointer to the state, which is modified
// the the state moves to a new location due to events (such as .then() being
// called) or due to the promise or future being mobved around.
//
/// \cond internal
template <typename... T>
struct future_state {
static constexpr bool copy_noexcept = std::is_nothrow_copy_constructible<std::tuple<T...>>::value;
static_assert(std::is_nothrow_move_constructible<std::tuple<T...>>::value,
"Types must be no-throw move constructible");
static_assert(std::is_nothrow_destructible<std::tuple<T...>>::value,
"Types must be no-throw destructible");
static_assert(std::is_nothrow_copy_constructible<std::exception_ptr>::value,
"std::exception_ptr's copy constructor must not throw");
static_assert(std::is_nothrow_move_constructible<std::exception_ptr>::value,
"std::exception_ptr's move constructor must not throw");
enum class state {
invalid,
future,
result,
exception,
} _state = state::future;
union any {
any() {}
~any() {}
std::tuple<T...> value;
std::exception_ptr ex;
} _u;
future_state() noexcept {}
[[gnu::always_inline]]
future_state(future_state&& x) noexcept
: _state(x._state) {
switch (_state) {
case state::future:
break;
case state::result:
new (&_u.value) std::tuple<T...>(std::move(x._u.value));
x._u.value.~tuple();
break;
case state::exception:
new (&_u.ex) std::exception_ptr(std::move(x._u.ex));
x._u.ex.~exception_ptr();
break;
case state::invalid:
break;
default:
abort();
}
x._state = state::invalid;
}
__attribute__((always_inline))
~future_state() noexcept {
switch (_state) {
case state::invalid:
break;
case state::future:
break;
case state::result:
_u.value.~tuple();
break;
case state::exception:
_u.ex.~exception_ptr();
break;
default:
abort();
}
}
future_state& operator=(future_state&& x) noexcept {
if (this != &x) {
this->~future_state();
new (this) future_state(std::move(x));
}
return *this;
}
bool available() const noexcept { return _state == state::result || _state == state::exception; }
bool failed() const noexcept { return _state == state::exception; }
void wait();
void set(const std::tuple<T...>& value) noexcept {
assert(_state == state::future);
new (&_u.value) std::tuple<T...>(value);
_state = state::result;
}
void set(std::tuple<T...>&& value) noexcept {
assert(_state == state::future);
new (&_u.value) std::tuple<T...>(std::move(value));
_state = state::result;
}
template <typename... A>
void set(A&&... a) {
assert(_state == state::future);
new (&_u.value) std::tuple<T...>(std::forward<A>(a)...);
_state = state::result;
}
void set_exception(std::exception_ptr ex) noexcept {
assert(_state == state::future);
new (&_u.ex) std::exception_ptr(ex);
_state = state::exception;
}
std::exception_ptr get_exception() && noexcept {
assert(_state == state::exception);
// Move ex out so future::~future() knows we've handled it
_state = state::invalid;
auto ex = std::move(_u.ex);
_u.ex.~exception_ptr();
return ex;
}
std::exception_ptr get_exception() const& noexcept {
assert(_state == state::exception);
return _u.ex;
}
std::tuple<T...> get_value() && noexcept {
assert(_state == state::result);
return std::move(_u.value);
}
template<typename U = std::tuple<T...>>
std::enable_if_t<std::is_copy_constructible<U>::value, U> get_value() const& noexcept(copy_noexcept) {
assert(_state == state::result);
return _u.value;
}
std::tuple<T...> get() && {
assert(_state != state::future);
if (_state == state::exception) {
_state = state::invalid;
auto ex = std::move(_u.ex);
_u.ex.~exception_ptr();
// Move ex out so future::~future() knows we've handled it
std::rethrow_exception(std::move(ex));
}
return std::move(_u.value);
}
std::tuple<T...> get() const& {
assert(_state != state::future);
if (_state == state::exception) {
std::rethrow_exception(_u.ex);
}
return _u.value;
}
void ignore() noexcept {
assert(_state != state::future);
this->~future_state();
_state = state::invalid;
}
using get0_return_type = std::tuple_element_t<0, std::tuple<T...>>;
static get0_return_type get0(std::tuple<T...>&& x) {
return std::get<0>(std::move(x));
}
void forward_to(promise<T...>& pr) noexcept {
assert(_state != state::future);
if (_state == state::exception) {
pr.set_urgent_exception(std::move(_u.ex));
_u.ex.~exception_ptr();
} else {
pr.set_urgent_value(std::move(_u.value));
_u.value.~tuple();
}
_state = state::invalid;
}
};
// Specialize future_state<> to overlap the state enum with the exception, as there
// is no value to hold.
//
// Assumes std::exception_ptr is really a pointer.
template <>
struct future_state<> {
static_assert(sizeof(std::exception_ptr) == sizeof(void*), "exception_ptr not a pointer");
static_assert(std::is_nothrow_copy_constructible<std::exception_ptr>::value,
"std::exception_ptr's copy constructor must not throw");
static_assert(std::is_nothrow_move_constructible<std::exception_ptr>::value,
"std::exception_ptr's move constructor must not throw");
static constexpr bool copy_noexcept = true;
enum class state : uintptr_t {
invalid = 0,
future = 1,
result = 2,
exception_min = 3, // or anything greater
};
union any {
any() { st = state::future; }
~any() {}
state st;
std::exception_ptr ex;
} _u;
future_state() noexcept {}
[[gnu::always_inline]]
future_state(future_state&& x) noexcept {
if (x._u.st < state::exception_min) {
_u.st = x._u.st;
} else {
// Move ex out so future::~future() knows we've handled it
// Moving it will reset us to invalid state
new (&_u.ex) std::exception_ptr(std::move(x._u.ex));
x._u.ex.~exception_ptr();
}
x._u.st = state::invalid;
}
[[gnu::always_inline]]
~future_state() noexcept {
if (_u.st >= state::exception_min) {
_u.ex.~exception_ptr();
}
}
future_state& operator=(future_state&& x) noexcept {
if (this != &x) {
this->~future_state();
new (this) future_state(std::move(x));
}
return *this;
}
bool available() const noexcept { return _u.st == state::result || _u.st >= state::exception_min; }
bool failed() const noexcept { return _u.st >= state::exception_min; }
void set(const std::tuple<>& value) noexcept {
assert(_u.st == state::future);
_u.st = state::result;
}
void set(std::tuple<>&& value) noexcept {
assert(_u.st == state::future);
_u.st = state::result;
}
void set() {
assert(_u.st == state::future);
_u.st = state::result;
}
void set_exception(std::exception_ptr ex) noexcept {
assert(_u.st == state::future);
new (&_u.ex) std::exception_ptr(ex);
assert(_u.st >= state::exception_min);
}
std::tuple<> get() && {
assert(_u.st != state::future);
if (_u.st >= state::exception_min) {
// Move ex out so future::~future() knows we've handled it
// Moving it will reset us to invalid state
std::rethrow_exception(std::move(_u.ex));
}
return {};
}
std::tuple<> get() const& {
assert(_u.st != state::future);
if (_u.st >= state::exception_min) {
std::rethrow_exception(_u.ex);
}
return {};
}
void ignore() noexcept {
assert(_u.st != state::future);
this->~future_state();
_u.st = state::invalid;
}
using get0_return_type = void;
static get0_return_type get0(std::tuple<>&&) {
return;
}
std::exception_ptr get_exception() && noexcept {
assert(_u.st >= state::exception_min);
// Move ex out so future::~future() knows we've handled it
// Moving it will reset us to invalid state
return std::move(_u.ex);
}
std::exception_ptr get_exception() const& noexcept {
assert(_u.st >= state::exception_min);
return _u.ex;
}
std::tuple<> get_value() const noexcept {
assert(_u.st == state::result);
return {};
}
void forward_to(promise<>& pr) noexcept;
};
template <typename Func, typename... T>
struct continuation final : task {
continuation(Func&& func, future_state<T...>&& state) : _state(std::move(state)), _func(std::move(func)) {}
continuation(Func&& func) : _func(std::move(func)) {}
virtual void run() noexcept override {
_func(std::move(_state));
}
future_state<T...> _state;
Func _func;
};
/// \endcond
/// \brief promise - allows a future value to be made available at a later time.
///
///
template <typename... T>
class promise {
enum class urgent { no, yes };
future<T...>* _future = nullptr;
future_state<T...> _local_state;
future_state<T...>* _state;
std::unique_ptr<task> _task;
static constexpr bool copy_noexcept = future_state<T...>::copy_noexcept;
public:
/// \brief Constructs an empty \c promise.
///
/// Creates promise with no associated future yet (see get_future()).
promise() noexcept : _state(&_local_state) {}
/// \brief Moves a \c promise object.
promise(promise&& x) noexcept : _future(x._future), _state(x._state), _task(std::move(x._task)) {
if (_state == &x._local_state) {
_state = &_local_state;
_local_state = std::move(x._local_state);
}
x._future = nullptr;
x._state = nullptr;
migrated();
}
promise(const promise&) = delete;
__attribute__((always_inline))
~promise() noexcept {
abandoned();
}
promise& operator=(promise&& x) noexcept {
if (this != &x) {
this->~promise();
new (this) promise(std::move(x));
}
return *this;
}
void operator=(const promise&) = delete;
/// \brief Gets the promise's associated future.
///
/// The future and promise will be remember each other, even if either or
/// both are moved. When \c set_value() or \c set_exception() are called
/// on the promise, the future will be become ready, and if a continuation
/// was attached to the future, it will run.
future<T...> get_future() noexcept;
/// \brief Sets the promise's value (as tuple; by copying)
///
/// Copies the tuple argument and makes it available to the associated
/// future. May be called either before or after \c get_future().
void set_value(const std::tuple<T...>& result) noexcept(copy_noexcept) {
do_set_value<urgent::no>(result);
}
/// \brief Sets the promises value (as tuple; by moving)
///
/// Moves the tuple argument and makes it available to the associated
/// future. May be called either before or after \c get_future().
void set_value(std::tuple<T...>&& result) noexcept {
do_set_value<urgent::no>(std::move(result));
}
/// \brief Sets the promises value (variadic)
///
/// Forwards the arguments and makes them available to the associated
/// future. May be called either before or after \c get_future().
template <typename... A>
void set_value(A&&... a) noexcept {
assert(_state);
_state->set(std::forward<A>(a)...);
make_ready<urgent::no>();
}
/// \brief Marks the promise as failed
///
/// Forwards the exception argument to the future and makes it
/// available. May be called either before or after \c get_future().
void set_exception(std::exception_ptr ex) noexcept {
do_set_exception<urgent::no>(std::move(ex));
}
/// \brief Marks the promise as failed
///
/// Forwards the exception argument to the future and makes it
/// available. May be called either before or after \c get_future().
template<typename Exception>
void set_exception(Exception&& e) noexcept {
set_exception(make_exception_ptr(std::forward<Exception>(e)));
}
private:
template<urgent Urgent>
void do_set_value(std::tuple<T...> result) noexcept {
assert(_state);
_state->set(std::move(result));
make_ready<Urgent>();
}
void set_urgent_value(const std::tuple<T...>& result) noexcept(copy_noexcept) {
do_set_value<urgent::yes>(result);
}
void set_urgent_value(std::tuple<T...>&& result) noexcept {
do_set_value<urgent::yes>(std::move(result));
}
template<urgent Urgent>
void do_set_exception(std::exception_ptr ex) noexcept {
assert(_state);
_state->set_exception(std::move(ex));
make_ready<Urgent>();
}
void set_urgent_exception(std::exception_ptr ex) noexcept {
do_set_exception<urgent::yes>(std::move(ex));
}
private:
template <typename Func>
void schedule(Func&& func) {
auto tws = std::make_unique<continuation<Func, T...>>(std::move(func));
_state = &tws->_state;
_task = std::move(tws);
}
template<urgent Urgent>
__attribute__((always_inline))
void make_ready() noexcept;
void migrated() noexcept;
void abandoned() noexcept;
template <typename... U>
friend class future;
friend class future_state<T...>;
};
/// \brief Specialization of \c promise<void>
///
/// This is an alias for \c promise<>, for generic programming purposes.
/// For example, You may have a \c promise<T> where \c T can legally be
/// \c void.
template<>
class promise<void> : public promise<> {};
/// @}
/// \addtogroup future-util
/// @{
/// \brief Check whether a type is a future
///
/// This is a type trait evaluating to \c true if the given type is a
/// future.
///
template <typename... T> struct is_future : std::false_type {};
/// \cond internal
/// \addtogroup future-util
template <typename... T> struct is_future<future<T...>> : std::true_type {};
struct ready_future_marker {};
struct ready_future_from_tuple_marker {};
struct exception_future_marker {};
/// \endcond
/// \brief Converts a type to a future type, if it isn't already.
///
/// \return Result in member type 'type'.
template <typename T>
struct futurize;
template <typename T>
struct futurize {
/// If \c T is a future, \c T; otherwise \c future<T>
using type = future<T>;
/// The promise type associated with \c type.
using promise_type = promise<T>;
/// The value tuple type associated with \c type
using value_type = std::tuple<T>;
/// Apply a function to an argument list (expressed as a tuple)
/// and return the result, as a future (if it wasn't already).
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, std::tuple<FuncArgs...>&& args) noexcept;
/// Apply a function to an argument list
/// and return the result, as a future (if it wasn't already).
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, FuncArgs&&... args) noexcept;
/// Convert a value or a future to a future
static inline type convert(T&& value) { return make_ready_future<T>(std::move(value)); }
static inline type convert(type&& value) { return std::move(value); }
/// Convert the tuple representation into a future
static type from_tuple(value_type&& value);
/// Convert the tuple representation into a future
static type from_tuple(const value_type& value);
/// Makes an exceptional future of type \ref type.
template <typename Arg>
static type make_exception_future(Arg&& arg);
};
/// \cond internal
template <>
struct futurize<void> {
using type = future<>;
using promise_type = promise<>;
using value_type = std::tuple<>;
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, std::tuple<FuncArgs...>&& args) noexcept;
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, FuncArgs&&... args) noexcept;
static inline type from_tuple(value_type&& value);
static inline type from_tuple(const value_type& value);
template <typename Arg>
static type make_exception_future(Arg&& arg);
};
template <typename... Args>
struct futurize<future<Args...>> {
using type = future<Args...>;
using promise_type = promise<Args...>;
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, std::tuple<FuncArgs...>&& args) noexcept;
template<typename Func, typename... FuncArgs>
static inline type apply(Func&& func, FuncArgs&&... args) noexcept;
static inline type convert(Args&&... values) { return make_ready_future<Args...>(std::move(values)...); }
static inline type convert(type&& value) { return std::move(value); }
template <typename Arg>
static type make_exception_future(Arg&& arg);
};
/// \endcond
// Converts a type to a future type, if it isn't already.
template <typename T>
using futurize_t = typename futurize<T>::type;
/// @}
/// \addtogroup future-module
/// @{
/// \brief A representation of a possibly not-yet-computed value.
///
/// A \c future represents a value that has not yet been computed
/// (an asynchronous computation). It can be in one of several
/// states:
/// - unavailable: the computation has not been completed yet
/// - value: the computation has been completed successfully and a
/// value is available.
/// - failed: the computation completed with an exception.
///
/// methods in \c future allow querying the state and, most importantly,
/// scheduling a \c continuation to be executed when the future becomes
/// available. Only one such continuation may be scheduled.
template <typename... T>
class future {
promise<T...>* _promise;
future_state<T...> _local_state; // valid if !_promise
static constexpr bool copy_noexcept = future_state<T...>::copy_noexcept;
private:
future(promise<T...>* pr) noexcept : _promise(pr) {
_promise->_future = this;
}
template <typename... A>
future(ready_future_marker, A&&... a) : _promise(nullptr) {
_local_state.set(std::forward<A>(a)...);
}
template <typename... A>
future(ready_future_from_tuple_marker, std::tuple<A...>&& data) : _promise(nullptr) {
_local_state.set(std::move(data));
}
future(exception_future_marker, std::exception_ptr ex) noexcept : _promise(nullptr) {
_local_state.set_exception(std::move(ex));
}
[[gnu::always_inline]]
explicit future(future_state<T...>&& state) noexcept
: _promise(nullptr), _local_state(std::move(state)) {
}
[[gnu::always_inline]]
future_state<T...>* state() noexcept {
return _promise ? _promise->_state : &_local_state;
}
template <typename Func>
void schedule(Func&& func) {
if (state()->available()) {
::schedule(std::make_unique<continuation<Func, T...>>(std::move(func), std::move(*state())));
} else {
assert(_promise);
_promise->schedule(std::move(func));
_promise->_future = nullptr;
_promise = nullptr;
}
}
[[gnu::always_inline]]
future_state<T...> get_available_state() noexcept {
auto st = state();
if (_promise) {
_promise->_future = nullptr;
_promise = nullptr;
}
return std::move(*st);
}
[[gnu::noinline]]
future<T...> rethrow_with_nested() {
if (!failed()) {
return make_exception_future<T...>(std::current_exception());
} else {
//
// Encapsulate the current exception into the
// std::nested_exception because the current libstdc++
// implementation has a bug requiring the value of a
// std::throw_with_nested() parameter to be of a polymorphic
// type.
//
std::nested_exception f_ex;
try {
get();
} catch (...) {
std::throw_with_nested(f_ex);
}
}
assert(0 && "we should not be here");
}
template<typename... U>
friend class shared_future;
public:
/// \brief The data type carried by the future.
using value_type = std::tuple<T...>;
/// \brief The data type carried by the future.
using promise_type = promise<T...>;
/// \brief Moves the future into a new object.
[[gnu::always_inline]]
future(future&& x) noexcept : _promise(x._promise) {
if (!_promise) {
_local_state = std::move(x._local_state);
}
x._promise = nullptr;
if (_promise) {
_promise->_future = this;
}
}
future(const future&) = delete;
future& operator=(future&& x) noexcept {
if (this != &x) {
this->~future();
new (this) future(std::move(x));
}
return *this;
}
void operator=(const future&) = delete;
__attribute__((always_inline))
~future() {
if (_promise) {
_promise->_future = nullptr;
}
if (failed()) {
report_failed_future(state()->get_exception());
}
}
/// \brief gets the value returned by the computation
///
/// Requires that the future be available. If the value
/// was computed successfully, it is returned (as an
/// \c std::tuple). Otherwise, an exception is thrown.
///
/// If get() is called in a \ref seastar::thread context,
/// then it need not be available; instead, the thread will
/// be paused until the future becomes available.
[[gnu::always_inline]]
std::tuple<T...> get() {
if (!state()->available()) {
wait();
} else if (seastar::thread_impl::get() && seastar::thread_impl::should_yield()) {
seastar::thread_impl::yield();
}
return get_available_state().get();
}
[[gnu::always_inline]]
std::exception_ptr get_exception() {
return get_available_state().get_exception();
}
/// Gets the value returned by the computation.
///
/// Similar to \ref get(), but instead of returning a
/// tuple, returns the first value of the tuple. This is
/// useful for the common case of a \c future<T> with exactly
/// one type parameter.
///
/// Equivalent to: \c std::get<0>(f.get()).
typename future_state<T...>::get0_return_type get0() {
return future_state<T...>::get0(get());
}
/// \cond internal
void wait() {
auto thread = seastar::thread_impl::get();
assert(thread);
schedule([this, thread] (future_state<T...>&& new_state) {
*state() = std::move(new_state);
seastar::thread_impl::switch_in(thread);
});
seastar::thread_impl::switch_out(thread);
}
/// \endcond
/// \brief Checks whether the future is available.
///
/// \return \c true if the future has a value, or has failed.
[[gnu::always_inline]]
bool available() noexcept {
return state()->available();
}
/// \brief Checks whether the future has failed.
///
/// \return \c true if the future is availble and has failed.
[[gnu::always_inline]]
bool failed() noexcept {
return state()->failed();
}
/// \brief Schedule a block of code to run when the future is ready.
///
/// Schedules a function (often a lambda) to run when the future becomes
/// available. The function is called with the result of this future's
/// computation as parameters. The return value of the function becomes
/// the return value of then(), itself as a future; this allows then()
/// calls to be chained.
///
/// If the future failed, the function is not called, and the exception
/// is propagated into the return value of then().
///
/// \param func - function to be called when the future becomes available,
/// unless it has failed.
/// \return a \c future representing the return value of \c func, applied
/// to the eventual value of this future.
template <typename Func, typename Result = futurize_t<std::result_of_t<Func(T&&...)>>>
Result
then(Func&& func) noexcept {
using futurator = futurize<std::result_of_t<Func(T&&...)>>;
if (available() && !need_preempt()) {
if (failed()) {
return futurator::make_exception_future(get_available_state().get_exception());
} else {
return futurator::apply(std::forward<Func>(func), get_available_state().get_value());
}
}
typename futurator::promise_type pr;
auto fut = pr.get_future();
try {
schedule([pr = std::move(pr), func = std::forward<Func>(func)] (auto&& state) mutable {
if (state.failed()) {
pr.set_exception(std::move(state).get_exception());
} else {
futurator::apply(std::forward<Func>(func), std::move(state).get_value()).forward_to(std::move(pr));
}
});
} catch (...) {
// catch possible std::bad_alloc in schedule() above
// nothing can be done about it, we cannot break future chain by returning
// ready future while 'this' future is not ready
abort();
}
return fut;
}
/// \brief Schedule a block of code to run when the future is ready, allowing
/// for exception handling.
///
/// Schedules a function (often a lambda) to run when the future becomes
/// available. The function is called with the this future as a parameter;
/// it will be in an available state. The return value of the function becomes
/// the return value of then_wrapped(), itself as a future; this allows
/// then_wrapped() calls to be chained.
///
/// Unlike then(), the function will be called for both value and exceptional
/// futures.
///
/// \param func - function to be called when the future becomes available,
/// \return a \c future representing the return value of \c func, applied
/// to the eventual value of this future.
template <typename Func, typename Result = futurize_t<std::result_of_t<Func(future)>>>
Result
then_wrapped(Func&& func) noexcept {
using futurator = futurize<std::result_of_t<Func(future)>>;
if (available() && !need_preempt()) {
return futurator::apply(std::forward<Func>(func), future(get_available_state()));
}
typename futurator::promise_type pr;
auto fut = pr.get_future();
try {
schedule([pr = std::move(pr), func = std::forward<Func>(func)] (auto&& state) mutable {
futurator::apply(std::forward<Func>(func), future(std::move(state))).forward_to(std::move(pr));
});
} catch (...) {
// catch possible std::bad_alloc in schedule() above
// nothing can be done about it, we cannot break future chain by returning
// ready future while 'this' future is not ready
abort();
}
return fut;
}
/// \brief Satisfy some \ref promise object with this future as a result.
///
/// Arranges so that when this future is resolve, it will be used to
/// satisfy an unrelated promise. This is similar to scheduling a
/// continuation that moves the result of this future into the promise
/// (using promise::set_value() or promise::set_exception(), except
/// that it is more efficient.
///
/// \param pr a promise that will be fulfilled with the results of this
/// future.
void forward_to(promise<T...>&& pr) noexcept {
if (state()->available()) {
state()->forward_to(pr);
} else {
_promise->_future = nullptr;
*_promise = std::move(pr);
_promise = nullptr;
}
}
/**
* Finally continuation for statements that require waiting for the result.
* I.e. you need to "finally" call a function that returns a possibly
* unavailable future. The returned future will be "waited for", any
* exception generated will be propagated, but the return value is ignored.
* I.e. the original return value (the future upon which you are making this
* call) will be preserved.
*
* If the original return value or the callback return value is an
* exceptional future it will be propagated.
*
* If both of them are exceptional - the std::nested_exception exception
* with the callback exception on top and the original future exception
* nested will be propagated.
*/
template <typename Func>
future<T...> finally(Func&& func) noexcept {
return then_wrapped(finally_body<Func, is_future<std::result_of_t<Func()>>::value>(std::forward<Func>(func)));
}
template <typename Func, bool FuncReturnsFuture>
struct finally_body;
template <typename Func>
struct finally_body<Func, true> {
Func _func;
finally_body(Func&& func) : _func(std::forward<Func>(func))
{ }
future<T...> operator()(future<T...>&& result) {
using futurator = futurize<std::result_of_t<Func()>>;
return futurator::apply(_func).then_wrapped([result = std::move(result)](auto f_res) mutable {
if (!f_res.failed()) {
return std::move(result);
} else {
try {
f_res.get();
} catch (...) {
return result.rethrow_with_nested();
}
assert(0 && "we should not be here");
}
});
}
};
template <typename Func>
struct finally_body<Func, false> {
Func _func;
finally_body(Func&& func) : _func(std::forward<Func>(func))
{ }
future<T...> operator()(future<T...>&& result) {
try {