1#ifndef NEFORCE_CORE_ASYNC_FUTURE_HPP__
2#define NEFORCE_CORE_ASYNC_FUTURE_HPP__
21NEFORCE_BEGIN_NAMESPACE__
34 explicit future_exception(
const char* info =
"Future Operation Failed.",
const char*
type = static_type,
35 const int code = 0) noexcept :
38 explicit future_exception(
const exception& e) :
41 ~future_exception()
override =
default;
42 static constexpr auto static_type =
"future_exception";
60template <
typename Res>
63template <
typename Res>
66template <
typename Sign>
69template <
typename Res>
111 return static_cast<launch>(
static_cast<int>(left) &
static_cast<int>(right));
114 return static_cast<launch>(
static_cast<int>(left) |
static_cast<int>(right));
117 return static_cast<launch>(
static_cast<int>(left) ^
static_cast<int>(right));
119constexpr launch operator~(
const launch value)
noexcept {
return static_cast<launch>(~static_cast<int>(value)); }
121constexpr launch& operator&=(
launch& left,
const launch right)
noexcept {
return left = left & right; }
122constexpr launch& operator|=(
launch& left,
const launch right)
noexcept {
return left = left | right; }
123constexpr launch& operator^=(
launch& left,
const launch right)
noexcept {
return left = left ^ right; }
132template <
typename Func,
typename... Args>
135template <
typename Func,
typename... Args>
138template <
typename Func,
typename... Args>
145constexpr const char* future_errc_cstr(
const future_errc code) {
148 return "future_already_retrieved";
151 return "promise_already_satisfied";
157 return "broken_promise";
171struct __future_base {
179 exception_ptr error_ptr{
nullptr};
182 result_base() noexcept {}
183 virtual ~result_base() =
default;
186 result_base(
const result_base&) =
delete;
187 result_base& operator=(
const result_base&) =
delete;
200 void operator()(result_base* result)
const { result->destroy(); }
204 template <
typename Res>
205 using Ptr = unique_ptr<Res, result_base::Deleter>;
214 template <
typename Res>
215 struct basic_result : result_base {
217 aligned_buffer<Res> storage;
221 void destroy()
override {
delete this; }
224 using result_type = Res;
229 basic_result() noexcept :
236 ~basic_result()
override {
246 Res& value() noexcept {
return *storage.
ptr(); }
252 void set(Res&& result) {
266 template <
typename Res,
typename Alloc>
267 struct allocated_result final : basic_result<Res>, Alloc {
268 using allocator_type = inner::__allocator_traits_base::alloc_rebind_t<Alloc, allocated_result>;
274 explicit allocated_result(
const Alloc& alloc) :
283 allocator_type alloc(*
this);
284 allocated_ptr<allocator_type> guard_ptr{alloc,
this};
285 this->~AllocatedResult();
296 template <
typename Res,
typename Alloc>
297 static Ptr<allocated_result<Res, Alloc>> allocate_result(
const Alloc& alloc) {
298 using result_type = allocated_result<Res, Alloc>;
299 typename result_type::allocator_type alloc2(alloc);
301 result_type* ptr =
new (
static_cast<void*
>(guard.get())) result_type{alloc};
303 return Ptr<result_type>(ptr);
312 template <
typename Res,
typename T>
313 static Ptr<basic_result<Res>> allocate_result(
const _NEFORCE
allocator<T>&) {
314 return Ptr<basic_result<Res>>(
new basic_result<Res>);
325 using PtrType = Ptr<result_base>;
335 atomic_futex<> status;
336 atomic_flag retrieved;
343 state_base() noexcept :
344 status(status::not_ready),
349 state_base(
const state_base&) =
delete;
350 state_base& operator=(
const state_base&) =
delete;
351 virtual ~state_base() =
default;
358 result_base&
wait() {
371 template <
typename Rep,
typename Period>
377 if (is_deferred_future()) {
396 template <
typename Clock,
typename Dur>
397 future_status wait_until(
const time_point<Clock, Dur>& absolute_time) {
403 if (is_deferred_future()) {
421 template <
typename Callable>
422 void set_result(Callable&& result_func,
const bool ignore_failure =
false) {
423 bool did_set =
false;
428 }
else if (!ignore_failure) {
441 template <
typename Callable>
442 void set_delayed_result(Callable&& result_func, weak_ptr<state_base> self) {
443 bool did_set =
false;
450 mr->shared_state = _NEFORCE
move(self);
460 void break_promise(PtrType result) {
461 if (
static_cast<bool>(result)) {
463 result_ptr.swap(result);
472 void set_retrieved_flag() {
485 template <
typename Res,
typename Arg>
491 template <
typename Res,
typename Arg>
492 struct setter<Res, Arg&> {
495 typename promise<Res>::PtrType operator()()
const {
496 promise_ptr->storage->set(*arg_ptr);
497 return _NEFORCE
move(promise_ptr->storage);
499 promise<Res>* promise_ptr;
506 template <
typename Res>
507 struct setter<Res, Res&&> {
508 typename promise<Res>::PtrType operator()()
const {
509 promise_ptr->storage->set(_NEFORCE
move(*arg_ptr));
510 return _NEFORCE
move(promise_ptr->storage);
512 promise<Res>* promise_ptr;
519 template <
typename Res>
520 struct setter<Res, void> {
524 promise<Res>* promise_ptr;
527 struct exception_ptr_tag {};
532 template <
typename Res>
533 struct setter<Res, exception_ptr_tag> {
535 promise_ptr->storage->error_ptr = *exp_ptr;
536 return _NEFORCE
move(promise_ptr->storage);
538 promise<Res>* promise_ptr;
539 exception_ptr* exp_ptr;
545 template <
typename Res,
typename Arg>
546 NEFORCE_ALWAYS_INLINE
static setter<Res, Arg&&> create_setter(promise<Res>* promise_ptr, Arg&& arg)
noexcept {
547 return setter<Res, Arg&&>{promise_ptr, _NEFORCE
addressof(arg)};
553 template <
typename Res>
554 NEFORCE_ALWAYS_INLINE
static setter<Res, exception_ptr_tag> create_setter(exception_ptr& exception,
555 promise<Res>* promise_ptr)
noexcept {
556 return setter<Res, exception_ptr_tag>{promise_ptr, &exception};
562 template <
typename Res>
563 NEFORCE_ALWAYS_INLINE
static setter<Res, void> create_setter(promise<Res>* promise_ptr)
noexcept {
564 return setter<Res, void>{promise_ptr};
573 template <
typename T>
574 static void check(
const shared_ptr<T>& ptr) {
575 if (!
static_cast<bool>(ptr)) {
586 void do_set(function<PtrType()>* func,
bool* did_set) {
587 PtrType result = (*func)();
589 result_ptr.swap(result);
596 virtual void complete_async() {}
602 virtual bool is_deferred_future()
const {
return false; }
609 struct make_ready final : at_thread_exit_elt {
610 weak_ptr<state_base> shared_state;
616 static void run(
void* ptr) {
617 const auto self =
static_cast<make_ready*
>(ptr);
618 const auto state = self->shared_state.
lock();
637 template <
typename Ptr>
638 struct get_result_type {
639 using type =
typename Ptr::element_type::result_type;
642 template <
typename Ptr>
643 using result_res_t =
typename get_result_type<Ptr>::type;
646 class async_state_common;
648 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
649 class deferred_state;
651 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
652 class async_state_impl;
654 template <
typename Sign>
655 class task_state_base;
657 template <
typename Func,
typename Alloc,
typename Sign>
660 template <
typename ResPtrT,
typename Func,
typename ResT = result_res_t<ResPtrT>>
666 template <
typename ResPtr,
typename BoundFunc>
667 static task_setter<ResPtr, BoundFunc> create_task_setter(ResPtr& ptr, BoundFunc& call) {
676template <
typename Res>
677struct __future_base::basic_result<Res&> : __future_base::result_base {
681 void destroy()
override {
delete this; }
684 using result_type = Res&;
689 basic_result() noexcept :
696 void set(Res& result)
noexcept { value_ptr = _NEFORCE
addressof(result); }
702 NEFORCE_NODISCARD Res&
get() noexcept {
return *value_ptr; }
709struct __future_base::basic_result<void> : __future_base::result_base {
710 using result_type = void;
713 void destroy()
override {
delete this; }
723template <
typename Res>
724class __basic_future :
public __future_base {
726 using state_type = shared_ptr<state_base>;
727 using result_type = __future_base::basic_result<Res>&;
730 state_type state_ptr;
733 __basic_future(
const __basic_future&) =
delete;
734 __basic_future& operator=(
const __basic_future&) =
delete;
740 bool valid() const noexcept {
return static_cast<bool>(state_ptr); }
747 state_base::check(state_ptr);
759 template <
typename Rep,
typename Period>
761 state_base::check(state_ptr);
773 template <
typename Clock,
typename Dur>
774 future_status wait_until(
const time_point<Clock, Dur>& absolute_time)
const {
775 state_base::check(state_ptr);
776 return state_ptr->wait_until(absolute_time);
784 result_type get_result()
const {
785 state_base::check(state_ptr);
786 result_base& result = state_ptr->wait();
787 if (result.error_ptr !=
nullptr) {
790 return static_cast<result_type
>(result);
797 void swap(__basic_future& other)
noexcept { state_ptr.swap(other.state_ptr); }
803 explicit __basic_future(
const state_type& state) :
805 state_base::check(state_ptr);
806 state_ptr->set_retrieved_flag();
809 explicit __basic_future(
const shared_future<Res>&)
noexcept;
810 explicit __basic_future(shared_future<Res>&&) noexcept;
811 explicit __basic_future(future<Res>&&) noexcept;
816 constexpr __basic_future() noexcept {}
824 __basic_future& future_ref;
830 explicit reset(__basic_future& future) noexcept :
831 future_ref(future) {}
836 ~reset() { future_ref.state_ptr.reset(); }
844template <
typename Res,
typename Arg>
847template <
typename ResPtr,
typename Func,
typename Res>
859template <
typename Res>
860class future :
public inner::__basic_future<Res> {
867 template <
typename Sign>
868 friend class packaged_task;
870 template <
typename Function,
typename... Args>
873 using base_type = inner::__basic_future<Res>;
874 using state_type =
typename base_type::state_type;
879 explicit future(
const state_type& state) :
894 base_type(_NEFORCE
move(other)) {}
915 typename base_type::reset reset_(*
this);
916 return _NEFORCE
move(this->get_result().value());
930template <typename Res>
931class future<Res&> : public inner::__basic_future<Res&> {
934 template <
typename Sign>
935 friend class packaged_task;
937 template <
typename Function,
typename... Args>
940 typedef inner::__basic_future<Res&> base_type;
941 typedef typename base_type::state_type state_type;
943 explicit future(
const state_type& state) :
947 constexpr future() noexcept :
949 future(future&& other) noexcept :
950 base_type(_NEFORCE
move(other)) {}
952 future(
const future&) =
delete;
953 future& operator=(
const future&) =
delete;
955 future& operator=(future&& other)
noexcept {
966 typename base_type::reset reset_(*
this);
967 return this->get_result().get();
977class future<
void> : public inner::__basic_future<
void> {
981 friend class packaged_task;
983 template <
typename Function,
typename... Args>
986 using base_type = inner::__basic_future<void>;
987 using state_type = base_type::state_type;
989 explicit future(
const state_type& state) :
993 constexpr future()
noexcept {}
994 future(future&& other) noexcept :
995 base_type(_NEFORCE
move(other)) {}
997 future(
const future&) =
delete;
998 future& operator=(
const future&) =
delete;
1000 future& operator=(future&& other)
noexcept {
1010 base_type::reset reset(*
this);
1011 ignore = this->get_result();
1024template <typename Res>
1025class shared_future : public inner::__basic_future<Res> {
1030 using base_type = inner::__basic_future<Res>;
1033 constexpr shared_future() noexcept :
1035 shared_future(
const shared_future& other) noexcept :
1038 base_type(_NEFORCE
move(other)) {}
1039 shared_future(shared_future&& other) noexcept :
1040 base_type(_NEFORCE
move(other)) {}
1042 shared_future& operator=(
const shared_future& other)
noexcept {
1043 shared_future(other).swap(*
this);
1047 shared_future& operator=(shared_future&& other)
noexcept {
1048 shared_future(_NEFORCE
move(other)).swap(*
this);
1057 const Res&
get()
const {
return this->get_result().value(); }
1064template <
typename Res>
1065class shared_future<Res&> :
public inner::__basic_future<Res&> {
1066 using base_type = inner::__basic_future<Res&>;
1069 constexpr shared_future() noexcept :
1071 shared_future(
const shared_future& other) :
1074 base_type(_NEFORCE
move(other)) {}
1075 shared_future(shared_future&& other) noexcept :
1076 base_type(_NEFORCE
move(other)) {}
1078 shared_future& operator=(
const shared_future& other) {
1079 shared_future(other).swap(*
this);
1083 shared_future& operator=(shared_future&& other)
noexcept {
1084 shared_future(_NEFORCE
move(other)).swap(*
this);
1092 Res&
get()
const {
return this->get_result().get(); }
1099class shared_future<void> :
public inner::__basic_future<void> {
1100 using base_type = inner::__basic_future<void>;
1103 constexpr shared_future()
noexcept {}
1104 shared_future(
const shared_future& other) :
1107 base_type(_NEFORCE
move(other)) {}
1108 shared_future(shared_future&& other) noexcept :
1109 base_type(_NEFORCE
move(other)) {}
1111 shared_future& operator=(
const shared_future& other) {
1112 shared_future(other).swap(*
this);
1116 shared_future& operator=(shared_future&& other)
noexcept {
1117 shared_future(_NEFORCE
move(other)).swap(*
this);
1124 void get()
const { ignore = this->get_result(); }
1129template <
typename Res>
1130inner::__basic_future<Res>::__basic_future(
const shared_future<Res>& other) noexcept :
1131state_ptr(other.state_ptr) {}
1133template <
typename Res>
1134inner::__basic_future<Res>::__basic_future(shared_future<Res>&& other) noexcept :
1135state_ptr(_NEFORCE
move(other.state_ptr)) {}
1137template <
typename Res>
1138inner::__basic_future<Res>::__basic_future(future<Res>&& other) noexcept :
1139state_ptr(_NEFORCE
move(other.state_ptr)) {}
1142template <
typename Result>
1144 return shared_future<Result>(_NEFORCE
move(*
this));
1147template <
typename Res>
1149 return shared_future<Res&>(_NEFORCE
move(*
this));
1152inline shared_future<void>
future<void>::share() noexcept {
return shared_future<void>(_NEFORCE
move(*
this)); }
1162template <
typename T>
1179template <
typename T>
1189template <
typename T>
1201template <
typename T>
1210NEFORCE_END_NAMESPACE__
NEFORCE_ALWAYS_INLINE void store_notify_all(const uint32_t value, const memory_order mo)
存储新值并通知所有等待线程
NEFORCE_ALWAYS_INLINE bool load_when_equal_for(const uint32_t value, const memory_order mo, const duration< Rep, Period > &rtime)
在指定时间内等待值等于指定值
NEFORCE_ALWAYS_INLINE void load_when_equal(const uint32_t value, const memory_order mo)
等待直到值等于指定值
NEFORCE_ALWAYS_INLINE bool load_when_equal_until(const uint32_t value, const memory_order mo, const time_point< Clock, Dur > &atime)
在指定时间点前等待值等于指定值(通用时钟)
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE uint32_t load(const memory_order mo) const
原子加载数据
future & operator=(future &&other) noexcept
移动赋值运算符
constexpr future() noexcept
默认构造函数
future(future &&other) noexcept
移动构造函数
shared_future< Res > share() noexcept
转换为共享future
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
const Res & get() const
获取常量引用结果
NEFORCE_CONSTEXPR20 pointer release() noexcept
释放所有权
NEFORCE_NODISCARD shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
allocated_ptr< Alloc > allocate_guarded(Alloc &alloc)
分配内存并创建allocated_ptr
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
NEFORCE_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
NEFORCE_ALWAYS_INLINE enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
invoke_result_t< decay_t< Func >, decay_t< Args >... > async_result_t
异步调用结果类型推导
NEFORCE_NODISCARD future< async_result_t< Func, Args... > > async(launch policy, Func &&function, Args &&... args)
异步执行函数(指定策略)
typename future_result< T >::type future_result_t
future结果类型别名
@ future_already_retrieved
future已经被获取
@ promise_already_satisfied
promise已经被满足
NEFORCE_INLINE17 constexpr bool is_void_v
is_void的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_array_v
is_array的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_function_v
is_function的便捷变量模板
void call_once(once_flag &flag, Callable &&func, Args &&... args)
单次调用函数
milliseconds NEFORCE_API relative_time(int64_t sec, int64_t nsec, bool is_monotonic=false) noexcept
将绝对时间戳转换为相对延迟毫秒数
NEFORCE_INLINE17 constexpr bool is_clock_v
is_clock的便捷变量模板
unsigned int uint32_t
32位无符号整数类型
NEFORCE_NORETURN NEFORCE_ALWAYS_INLINE_INLINE void unreachable() noexcept
标记不可达代码路径
void NEFORCE_API rethrow_exception(const exception_ptr &p)
重新抛出异常
exception_ptr make_exception_ptr(Ex ex) noexcept
创建异常指针
NEFORCE_CONSTEXPR20 void destroy(T *pointer) noexcept(is_nothrow_destructible_v< T >)
销毁单个对象
typename inner::__invoke_result_aux< F, Args... >::type invoke_result_t
invoke_result的便捷别名
standard_allocator< T > allocator
标准分配器别名
NEFORCE_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
NEFORCE_INLINE17 constexpr auto memory_order_acquire
获取内存顺序常量
NEFORCE_INLINE17 constexpr none_t none
默认空表示
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
void swap()=delete
删除无参数的swap重载
void NEFORCE_API thread_exit_register(at_thread_exit_elt *elt, void(*callback)(void *)) noexcept
注册线程退出回调
typename decay< T >::type decay_t
decay的便捷别名
NEFORCE_INLINE17 constexpr bool is_destructible_v
is_destructible的便捷变量模板
NEFORCE_INLINE17 constexpr bool is_same_v
is_same的便捷变量模板
bool_constant< true > true_type
表示true的类型
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
NEFORCE_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
void * addr() noexcept
获取缓冲区的原始地址
T * ptr() noexcept
获取缓冲区的类型化指针
NEFORCE_ALWAYS_INLINE void clear(const memory_order mo=memory_order_seq_cst) noexcept
清除标志
NEFORCE_ALWAYS_INLINE bool test_and_set(const memory_order mo=memory_order_seq_cst) noexcept
测试并设置标志
static constexpr duration zero() noexcept
获取零持续时间
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型