NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
future.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_ASYNC_FUTURE_HPP__
2#define NEFORCE_CORE_ASYNC_FUTURE_HPP__
3
10
21NEFORCE_BEGIN_NAMESPACE__
22
28
33struct future_exception final : exception {
34 explicit future_exception(const char* info = "Future Operation Failed.", const char* type = static_type,
35 const int code = 0) noexcept :
36 exception(info, type, code) {}
37
38 explicit future_exception(const exception& e) :
39 exception(e) {}
40
41 ~future_exception() override = default;
42 static constexpr auto static_type = "future_exception";
43};
44
45 // Exceptions
47
53
59
60template <typename Res>
61class future;
62
63template <typename Res>
64class shared_future;
65
66template <typename Sign>
67class packaged_task;
68
69template <typename Res>
70class promise;
71
72
85
97
98
105enum class launch {
106 async = 1,
108};
109
110constexpr launch operator&(const launch left, const launch right) noexcept {
111 return static_cast<launch>(static_cast<int>(left) & static_cast<int>(right));
112}
113constexpr launch operator|(const launch left, const launch right) noexcept {
114 return static_cast<launch>(static_cast<int>(left) | static_cast<int>(right));
115}
116constexpr launch operator^(const launch left, const launch right) noexcept {
117 return static_cast<launch>(static_cast<int>(left) ^ static_cast<int>(right));
118}
119constexpr launch operator~(const launch value) noexcept { return static_cast<launch>(~static_cast<int>(value)); }
120
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; }
124
125
132template <typename Func, typename... Args>
134
135template <typename Func, typename... Args>
136future<async_result_t<Func, Args...>> async(launch policy, Func&& function, Args&&... args);
137
138template <typename Func, typename... Args>
139future<async_result_t<Func, Args...>> async(Func&& function, Args&&... args);
140
141
143NEFORCE_BEGIN_INNER__
144
145constexpr const char* future_errc_cstr(const future_errc code) {
146 switch (code) {
148 return "future_already_retrieved";
149 }
151 return "promise_already_satisfied";
152 }
154 return "no_state";
155 }
157 return "broken_promise";
158 }
159 default: {
160 unreachable();
161 }
162 }
163}
164
171struct __future_base {
178 struct result_base {
179 exception_ptr error_ptr{nullptr};
180
181 protected:
182 result_base() noexcept {}
183 virtual ~result_base() = default;
184
185 public:
186 result_base(const result_base&) = delete;
187 result_base& operator=(const result_base&) = delete;
188
193 virtual void destroy() = 0;
194
199 struct Deleter {
200 void operator()(result_base* result) const { result->destroy(); }
201 };
202 };
203
204 template <typename Res>
205 using Ptr = unique_ptr<Res, result_base::Deleter>;
206
214 template <typename Res>
215 struct basic_result : result_base {
216 private:
217 aligned_buffer<Res> storage;
218 bool initialized;
219
221 void destroy() override { delete this; }
222
223 public:
224 using result_type = Res;
225
229 basic_result() noexcept :
230 initialized() {}
231
236 ~basic_result() override {
237 if (initialized) {
238 value().~Res();
239 }
240 }
241
246 Res& value() noexcept { return *storage.ptr(); }
247
252 void set(Res&& result) {
253 new (storage.addr()) Res(_NEFORCE forward<Res>(result));
254 initialized = true;
255 }
256 };
257
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>;
269
274 explicit allocated_result(const Alloc& alloc) :
275 basic_result<Res>(),
276 Alloc(alloc) {}
277
278 private:
282 void destroy() override {
283 allocator_type alloc(*this);
284 allocated_ptr<allocator_type> guard_ptr{alloc, this};
285 this->~AllocatedResult();
286 }
287 };
288
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);
300 auto guard = _NEFORCE allocate_guarded(alloc2);
301 result_type* ptr = new (static_cast<void*>(guard.get())) result_type{alloc};
302 guard = nullptr;
303 return Ptr<result_type>(ptr);
304 }
305
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>);
315 }
316
323 class state_base {
324 public:
325 using PtrType = Ptr<result_base>;
326
327 private:
329 enum status : uint32_t {
330 not_ready,
331 ready
332 };
333
334 PtrType result_ptr;
335 atomic_futex<> status;
336 atomic_flag retrieved;
337 once_flag flag;
338
339 public:
343 state_base() noexcept :
344 status(status::not_ready),
345 retrieved(false) {
346 retrieved.clear();
347 }
348
349 state_base(const state_base&) = delete;
350 state_base& operator=(const state_base&) = delete;
351 virtual ~state_base() = default;
352
358 result_base& wait() {
359 complete_async();
360 status.load_when_equal(status::ready, memory_order_acquire);
361 return *result_ptr;
362 }
363
371 template <typename Rep, typename Period>
372 future_status wait_for(const duration<Rep, Period>& relative_time) {
373 if (status.load(memory_order_acquire) == status::ready) {
375 }
376
377 if (is_deferred_future()) {
379 }
380
383 complete_async();
385 }
387 }
388
396 template <typename Clock, typename Dur>
397 future_status wait_until(const time_point<Clock, Dur>& absolute_time) {
398 static_assert(is_clock_v<Clock>, "Clock type must be clock_t");
399 if (status.load(memory_order_acquire) == status::ready) {
401 }
402
403 if (is_deferred_future()) {
405 }
406
407 if (status.load_when_equal_until(status::ready, memory_order_acquire, absolute_time)) {
408 complete_async();
410 }
412 }
413
421 template <typename Callable>
422 void set_result(Callable&& result_func, const bool ignore_failure = false) {
423 bool did_set = false;
424 function<PtrType()> func = _NEFORCE forward<Callable>(result_func);
425 call_once(flag, &state_base::do_set, this, _NEFORCE addressof(func), _NEFORCE addressof(did_set));
426 if (did_set) {
427 status.store_notify_all(status::ready, memory_order_release);
428 } else if (!ignore_failure) {
429 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
430 }
431 }
432
441 template <typename Callable>
442 void set_delayed_result(Callable&& result_func, weak_ptr<state_base> self) {
443 bool did_set = false;
444 unique_ptr<make_ready> mr = make_unique<make_ready>();
445 function<PtrType()> func = _NEFORCE forward<Callable>(result_func);
446 call_once(flag, &state_base::do_set, this, _NEFORCE addressof(func), _NEFORCE addressof(did_set));
447 if (!did_set) {
448 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
449 }
450 mr->shared_state = _NEFORCE move(self);
451 mr->set();
452 mr.release();
453 }
454
460 void break_promise(PtrType result) {
461 if (static_cast<bool>(result)) {
462 result->error_ptr = make_exception_ptr(future_exception(future_errc_cstr(future_errc::broken_promise)));
463 result_ptr.swap(result);
464 status.store_notify_all(status::ready, memory_order_release);
465 }
466 }
467
472 void set_retrieved_flag() {
473 if (retrieved.test_and_set(memory_order_acquire)) {
474 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::future_already_retrieved)));
475 }
476 }
477
485 template <typename Res, typename Arg>
486 struct setter;
487
491 template <typename Res, typename Arg>
492 struct setter<Res, Arg&> {
493 static_assert(is_same_v<Res, Arg&> || is_same_v<const Res, Arg>, "Invalid specialisation");
494
495 typename promise<Res>::PtrType operator()() const {
496 promise_ptr->storage->set(*arg_ptr);
497 return _NEFORCE move(promise_ptr->storage);
498 }
499 promise<Res>* promise_ptr;
500 Arg* arg_ptr;
501 };
502
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);
511 }
512 promise<Res>* promise_ptr;
513 Res* arg_ptr;
514 };
515
519 template <typename Res>
520 struct setter<Res, void> {
521 static_assert(is_void_v<Res>, "Only used for promise<void>");
522
523 typename promise<Res>::ptr_type operator()() const { return _NEFORCE move(promise_ptr->storage); }
524 promise<Res>* promise_ptr;
525 };
526
527 struct exception_ptr_tag {};
528
532 template <typename Res>
533 struct setter<Res, exception_ptr_tag> {
534 typename promise<Res>::ptr_type operator()() const {
535 promise_ptr->storage->error_ptr = *exp_ptr;
536 return _NEFORCE move(promise_ptr->storage);
537 }
538 promise<Res>* promise_ptr;
539 exception_ptr* exp_ptr;
540 };
541
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)};
548 }
549
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};
557 }
558
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};
565 }
566
573 template <typename T>
574 static void check(const shared_ptr<T>& ptr) {
575 if (!static_cast<bool>(ptr)) {
576 NEFORCE_THROW_EXCEPTION(future_exception(future_errc_cstr(future_errc::no_state)));
577 }
578 }
579
580 private:
586 void do_set(function<PtrType()>* func, bool* did_set) {
587 PtrType result = (*func)();
588 *did_set = true;
589 result_ptr.swap(result);
590 }
591
596 virtual void complete_async() {}
597
602 virtual bool is_deferred_future() const { return false; }
603
609 struct make_ready final : at_thread_exit_elt {
610 weak_ptr<state_base> shared_state;
611
616 static void run(void* ptr) {
617 const auto self = static_cast<make_ready*>(ptr);
618 const auto state = self->shared_state.lock();
619 if (state) {
620 state->status.store_notify_all(status::ready, memory_order_release);
621 }
622 delete self;
623 }
624
628 void set() { thread_exit_register(this, &make_ready::run); }
629 };
630 };
631
632private:
637 template <typename Ptr>
638 struct get_result_type {
639 using type = typename Ptr::element_type::result_type;
640 };
641
642 template <typename Ptr>
643 using result_res_t = typename get_result_type<Ptr>::type;
644
645public:
646 class async_state_common;
647
648 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
649 class deferred_state;
650
651 template <typename BoundFunc, typename Res = decltype(_NEFORCE declval<BoundFunc&>()())>
652 class async_state_impl;
653
654 template <typename Sign>
655 class task_state_base;
656
657 template <typename Func, typename Alloc, typename Sign>
658 class task_state;
659
660 template <typename ResPtrT, typename Func, typename ResT = result_res_t<ResPtrT>>
661 struct task_setter;
662
666 template <typename ResPtr, typename BoundFunc>
667 static task_setter<ResPtr, BoundFunc> create_task_setter(ResPtr& ptr, BoundFunc& call) {
668 return {_NEFORCE addressof(ptr), _NEFORCE addressof(call)};
669 }
670};
671
676template <typename Res>
677struct __future_base::basic_result<Res&> : __future_base::result_base {
678private:
679 Res* value_ptr;
680
681 void destroy() override { delete this; }
682
683public:
684 using result_type = Res&;
685
689 basic_result() noexcept :
690 value_ptr() {}
691
696 void set(Res& result) noexcept { value_ptr = _NEFORCE addressof(result); }
697
702 NEFORCE_NODISCARD Res& get() noexcept { return *value_ptr; }
703};
704
708template <>
709struct __future_base::basic_result<void> : __future_base::result_base {
710 using result_type = void;
711
712private:
713 void destroy() override { delete this; }
714};
715
716
723template <typename Res>
724class __basic_future : public __future_base {
725protected:
726 using state_type = shared_ptr<state_base>;
727 using result_type = __future_base::basic_result<Res>&;
728
729private:
730 state_type state_ptr;
731
732public:
733 __basic_future(const __basic_future&) = delete;
734 __basic_future& operator=(const __basic_future&) = delete;
735
740 bool valid() const noexcept { return static_cast<bool>(state_ptr); }
741
746 void wait() const {
747 state_base::check(state_ptr);
748 state_ptr->wait();
749 }
750
759 template <typename Rep, typename Period>
760 future_status wait_for(const duration<Rep, Period>& relative_time) const {
761 state_base::check(state_ptr);
762 return state_ptr->wait_for(relative_time);
763 }
764
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);
777 }
778
779protected:
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) {
788 rethrow_exception(result.error_ptr);
789 }
790 return static_cast<result_type>(result);
791 }
792
797 void swap(__basic_future& other) noexcept { state_ptr.swap(other.state_ptr); }
798
803 explicit __basic_future(const state_type& state) :
804 state_ptr(state) {
805 state_base::check(state_ptr);
806 state_ptr->set_retrieved_flag();
807 }
808
809 explicit __basic_future(const shared_future<Res>&) noexcept;
810 explicit __basic_future(shared_future<Res>&&) noexcept;
811 explicit __basic_future(future<Res>&&) noexcept;
812
816 constexpr __basic_future() noexcept {}
817
823 struct reset {
824 __basic_future& future_ref;
825
830 explicit reset(__basic_future& future) noexcept :
831 future_ref(future) {}
832
836 ~reset() { future_ref.state_ptr.reset(); }
837 };
838};
839
840NEFORCE_END_INNER__
842
843
844template <typename Res, typename Arg>
845struct is_location_invariant<inner::__future_base::state_base::setter<Res, Arg>> : true_type {};
846
847template <typename ResPtr, typename Func, typename Res>
848struct is_location_invariant<inner::__future_base::task_setter<ResPtr, Func, Res>> : true_type {};
849
850
859template <typename Res>
860class future : public inner::__basic_future<Res> {
861 static_assert(!is_array_v<Res>, "result type must not be an array");
862 static_assert(!is_function_v<Res>, "result type must not be a function");
863 static_assert(is_destructible_v<Res>, "result type must be destructible");
864
865 friend class promise<Res>;
866
867 template <typename Sign>
868 friend class packaged_task;
869
870 template <typename Function, typename... Args>
871 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
872
873 using base_type = inner::__basic_future<Res>;
874 using state_type = typename base_type::state_type;
875
879 explicit future(const state_type& state) :
880 base_type(state) {}
881
882public:
886 constexpr future() noexcept :
887 base_type() {}
888
893 future(future&& other) noexcept :
894 base_type(_NEFORCE move(other)) {}
895
896 future(const future&) = delete;
897 future& operator=(const future&) = delete;
898
904 future& operator=(future&& other) noexcept {
905 future(_NEFORCE move(other)).swap(*this);
906 return *this;
907 }
908
914 Res get() {
915 typename base_type::reset reset_(*this);
916 return _NEFORCE move(this->get_result().value());
917 }
918
924};
925
930template <typename Res>
931class future<Res&> : public inner::__basic_future<Res&> {
932 friend class promise<Res&>;
933
934 template <typename Sign>
935 friend class packaged_task;
936
937 template <typename Function, typename... Args>
938 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
939
940 typedef inner::__basic_future<Res&> base_type;
941 typedef typename base_type::state_type state_type;
942
943 explicit future(const state_type& state) :
944 base_type(state) {}
945
946public:
947 constexpr future() noexcept :
948 base_type() {}
949 future(future&& other) noexcept :
950 base_type(_NEFORCE move(other)) {}
951
952 future(const future&) = delete;
953 future& operator=(const future&) = delete;
954
955 future& operator=(future&& other) noexcept {
956 future(_NEFORCE move(other)).swap(*this);
957 return *this;
958 }
959
965 Res& get() {
966 typename base_type::reset reset_(*this);
967 return this->get_result().get();
968 }
969
970 shared_future<Res&> share() noexcept;
971};
972
976template <>
977class future<void> : public inner::__basic_future<void> {
978 friend class promise<void>;
979
980 template <typename>
981 friend class packaged_task;
982
983 template <typename Function, typename... Args>
984 friend future<async_result_t<Function, Args...>> async(launch, Function&&, Args&&...);
985
986 using base_type = inner::__basic_future<void>;
987 using state_type = base_type::state_type;
988
989 explicit future(const state_type& state) :
990 base_type(state) {}
991
992public:
993 constexpr future() noexcept {}
994 future(future&& other) noexcept :
995 base_type(_NEFORCE move(other)) {}
996
997 future(const future&) = delete;
998 future& operator=(const future&) = delete;
999
1000 future& operator=(future&& other) noexcept {
1001 future(_NEFORCE move(other)).swap(*this);
1002 return *this;
1003 }
1004
1009 void get() {
1010 base_type::reset reset(*this);
1011 ignore = this->get_result();
1012 }
1013
1014 shared_future<void> share() noexcept;
1015};
1016
1024template <typename Res>
1025class shared_future : public inner::__basic_future<Res> {
1026 static_assert(!is_array_v<Res>, "result type must not be an array");
1027 static_assert(!is_function_v<Res>, "result type must not be a function");
1028 static_assert(is_destructible_v<Res>, "result type must be destructible");
1029
1030 using base_type = inner::__basic_future<Res>;
1031
1032public:
1033 constexpr shared_future() noexcept :
1034 base_type() {}
1035 shared_future(const shared_future& other) noexcept :
1036 base_type(other) {}
1037 shared_future(future<Res>&& other) noexcept :
1038 base_type(_NEFORCE move(other)) {}
1039 shared_future(shared_future&& other) noexcept :
1040 base_type(_NEFORCE move(other)) {}
1041
1042 shared_future& operator=(const shared_future& other) noexcept {
1043 shared_future(other).swap(*this);
1044 return *this;
1045 }
1046
1047 shared_future& operator=(shared_future&& other) noexcept {
1048 shared_future(_NEFORCE move(other)).swap(*this);
1049 return *this;
1050 }
1051
1057 const Res& get() const { return this->get_result().value(); }
1058};
1059
1064template <typename Res>
1065class shared_future<Res&> : public inner::__basic_future<Res&> {
1066 using base_type = inner::__basic_future<Res&>;
1067
1068public:
1069 constexpr shared_future() noexcept :
1070 base_type() {}
1071 shared_future(const shared_future& other) :
1072 base_type(other) {}
1073 shared_future(future<Res&>&& other) noexcept :
1074 base_type(_NEFORCE move(other)) {}
1075 shared_future(shared_future&& other) noexcept :
1076 base_type(_NEFORCE move(other)) {}
1077
1078 shared_future& operator=(const shared_future& other) {
1079 shared_future(other).swap(*this);
1080 return *this;
1081 }
1082
1083 shared_future& operator=(shared_future&& other) noexcept {
1084 shared_future(_NEFORCE move(other)).swap(*this);
1085 return *this;
1086 }
1087
1092 Res& get() const { return this->get_result().get(); }
1093};
1094
1098template <>
1099class shared_future<void> : public inner::__basic_future<void> {
1100 using base_type = inner::__basic_future<void>;
1101
1102public:
1103 constexpr shared_future() noexcept {}
1104 shared_future(const shared_future& other) :
1105 base_type(other) {}
1106 shared_future(future<void>&& other) noexcept :
1107 base_type(_NEFORCE move(other)) {}
1108 shared_future(shared_future&& other) noexcept :
1109 base_type(_NEFORCE move(other)) {}
1110
1111 shared_future& operator=(const shared_future& other) {
1112 shared_future(other).swap(*this);
1113 return *this;
1114 }
1115
1116 shared_future& operator=(shared_future&& other) noexcept {
1117 shared_future(_NEFORCE move(other)).swap(*this);
1118 return *this;
1119 }
1120
1124 void get() const { ignore = this->get_result(); }
1125};
1126
1128
1129template <typename Res>
1130inner::__basic_future<Res>::__basic_future(const shared_future<Res>& other) noexcept :
1131state_ptr(other.state_ptr) {}
1132
1133template <typename Res>
1134inner::__basic_future<Res>::__basic_future(shared_future<Res>&& other) noexcept :
1135state_ptr(_NEFORCE move(other.state_ptr)) {}
1136
1137template <typename Res>
1138inner::__basic_future<Res>::__basic_future(future<Res>&& other) noexcept :
1139state_ptr(_NEFORCE move(other.state_ptr)) {}
1140
1141
1142template <typename Result>
1143shared_future<Result> future<Result>::share() noexcept {
1144 return shared_future<Result>(_NEFORCE move(*this));
1145}
1146
1147template <typename Res>
1148shared_future<Res&> future<Res&>::share() noexcept {
1149 return shared_future<Res&>(_NEFORCE move(*this));
1150}
1151
1152inline shared_future<void> future<void>::share() noexcept { return shared_future<void>(_NEFORCE move(*this)); }
1153
1155
1162template <typename T>
1164 using type = T;
1165};
1166
1170template <>
1171struct future_result<void> {
1172 using type = none_t;
1173};
1174
1179template <typename T>
1180using future_result_t = typename future_result<T>::type;
1181
1182
1189template <typename T>
1191 f.get();
1192 return none;
1193}
1194
1201template <typename T>
1203 return f.get();
1204}
1205 // Async
1207 // AsyncComponents
1209
1210NEFORCE_END_NAMESPACE__
1211#endif // NEFORCE_CORE_ASYNC_FUTURE_HPP__
对齐缓冲区实现
分配器指针包装器
线程退出回调支持
原子快速用户态互斥锁实现
单次调用
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
原子加载数据
函数包装器主模板声明
Res & get()
获取结果引用
void get()
获取结果
独占future类模板
Res get()
获取结果
future & operator=(future &&other) noexcept
移动赋值运算符
constexpr future() noexcept
默认构造函数
future(future &&other) noexcept
移动构造函数
shared_future< Res > share() noexcept
转换为共享future
Promise类模板
inner::__future_base::Ptr< result_type > ptr_type
结果指针类型
集合容器
Res & get() const
获取结果引用
void get() const
获取结果
共享future类模板
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结果获取函数
future_errc
期值错误码枚举
invoke_result_t< decay_t< Func >, decay_t< Args >... > async_result_t
异步调用结果类型推导
launch
异步启动策略枚举
NEFORCE_NODISCARD future< async_result_t< Func, Args... > > async(launch policy, Func &&function, Args &&... args)
异步执行函数(指定策略)
future_status
期值状态枚举
typename future_result< T >::type future_result_t
future结果类型别名
@ broken_promise
承诺被破坏
@ future_already_retrieved
future已经被获取
@ promise_already_satisfied
promise已经被满足
@ no_state
没有关联的状态对象
@ deferred
延迟执行
@ timeout
等待超时
@ ready
结果已就绪
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
创建异常指针
@ wait
等待操作
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
获取异常类型
future结果类型转换
判断类型是否是位置不变的
空状态类型
弱智能指针实现