MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
future.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_ASYNC_FUTURE_HPP__
2#define MSTL_CORE_ASYNC_FUTURE_HPP__
3
10
21
27
34
35 // Exceptions
36
37
42
43template <typename Res>
44class future;
45
46template <typename Res>
47class shared_future;
48
49template <typename Sign>
50class packaged_task;
51
52template <typename Res>
53class promise;
54
55
68
80
81
88enum class launch {
89 async = 1,
91};
92
93constexpr launch operator &(const launch left, const launch right) noexcept {
94 return static_cast<launch>(static_cast<int>(left) & static_cast<int>(right));
95}
96constexpr launch operator |(const launch left, const launch right) noexcept {
97 return static_cast<launch>(static_cast<int>(left) | static_cast<int>(right));
98}
99constexpr launch operator ^(const launch left, const launch right) noexcept {
100 return static_cast<launch>(static_cast<int>(left) ^ static_cast<int>(right));
101}
102constexpr launch operator ~(const launch value) noexcept {
103 return static_cast<launch>(~static_cast<int>(value));
104}
105
106constexpr launch& operator &=(launch& left, const launch right) noexcept {
107 return left = left & right;
108}
109constexpr launch& operator |=(launch& left, const launch right) noexcept {
110 return left = left | right;
111}
112constexpr launch& operator ^=(launch& left, const launch right) noexcept {
113 return left = left ^ right;
114}
115
116
123template <typename Func, typename... Args>
125
126template <typename Func, typename... Args>
127future<async_result_t<Func, Args...>>
128async(launch policy, Func&& function, Args&&... args);
129
130template <typename Func, typename... Args>
131future<async_result_t<Func, Args...>>
132async(Func&& function, Args&&... args);
133
134
137
138constexpr const char* future_errc_cstr(const future_errc code) {
139 switch (code) {
141 return "future_already_retrieved";
142 }
144 return "promise_already_satisfied";
145 }
147 return "no_state";
148 }
150 return "broken_promise";
151 }
152 default: {
153 MSTL_UNREACHABLE;
154 }
155 }
156}
157
164struct __future_base {
171 struct result_base {
172 exception_ptr error_ptr{nullptr};
173
174 protected:
175 result_base() noexcept {}
176 virtual ~result_base() = default;
177
178 public:
179 result_base(const result_base&) = delete;
180 result_base& operator =(const result_base&) = delete;
181
186 virtual void destroy() = 0;
187
192 struct Deleter {
193 void operator ()(result_base* result) const {
194 result->destroy();
195 }
196 };
197 };
198
199 template <typename Res>
200 using Ptr = unique_ptr<Res, result_base::Deleter>;
201
209 template <typename Res>
210 struct basic_result : result_base {
211 private:
212 aligned_buffer<Res> storage;
213 bool initialized;
214
216 void destroy() override {
217 delete this;
218 }
219
220 public:
221 using result_type = Res;
222
226 basic_result() noexcept : initialized() {}
227
232 ~basic_result() override {
233 if (initialized) {
234 value().~Res();
235 }
236 }
237
242 Res& value() noexcept {
243 return *storage.ptr();
244 }
245
250 void set(Res&& result) {
251 new (storage.addr()) Res(_MSTL forward<Res>(result));
252 initialized = true;
253 }
254 };
255
264 template <typename Res, typename Alloc>
265 struct allocated_result final : basic_result<Res>, Alloc {
266 using allocator_type = _INNER __allocator_traits_base::alloc_rebind_t<Alloc, allocated_result>;
267
272 explicit allocated_result(const Alloc& alloc)
273 : basic_result<Res>(), Alloc(alloc) {}
274
275 private:
279 void destroy() override {
280 allocator_type alloc(*this);
281 allocated_ptr<allocator_type> guard_ptr{ alloc, this };
282 this->~AllocatedResult();
283 }
284 };
285
293 template <typename Res, typename Alloc>
294 static Ptr<allocated_result<Res, Alloc>>
295 allocate_result(const Alloc& alloc) {
296 using result_type = allocated_result<Res, Alloc>;
297 typename result_type::allocator_type alloc2(alloc);
298 auto guard = _MSTL allocate_guarded(alloc2);
299 result_type* ptr = new(static_cast<void*>(guard.get())) result_type{alloc};
300 guard = nullptr;
301 return Ptr<result_type>(ptr);
302 }
303
310 template <typename Res, typename T>
311 static Ptr<basic_result<Res>>
312 allocate_result(const _MSTL allocator<T>&) {
313 return Ptr<basic_result<Res>>(new basic_result<Res>);
314 }
315
322 class state_base {
323 public:
324 using PtrType = Ptr<result_base>;
325
326 private:
328 enum status : uint32_t {
329 not_ready,
330 ready
331 };
332
333 PtrType result_ptr;
334 atomic_futex<> status;
335 atomic_flag retrieved;
336 once_flag flag;
337
338 public:
342 state_base() noexcept
343 : status(status::not_ready), retrieved(false) {
344 retrieved.clear();
345 }
346
347 state_base(const state_base&) = delete;
348 state_base& operator =(const state_base&) = delete;
349 virtual ~state_base() = default;
350
356 result_base& wait() {
357 complete_async();
358 status.load_when_equal(status::ready, memory_order_acquire);
359 return *result_ptr;
360 }
361
369 template <typename Rep, typename Period>
370 future_status wait_for(const duration<Rep, Period>& relative_time) {
371 if (status.load(memory_order_acquire) == status::ready) {
373 }
374
375 if (is_deferred_future()) {
377 }
378
381 complete_async();
383 }
385 }
386
394 template <typename Clock, typename Dur>
395 future_status wait_until(const time_point<Clock, Dur>& absolute_time) {
396 static_assert(is_clock_v<Clock>, "Clock type must be clock_t");
397 if (status.load(memory_order_acquire) == status::ready) {
399 }
400
401 if (is_deferred_future()) {
403 }
404
405 if (status.load_when_equal_until(status::ready, memory_order_acquire, absolute_time)) {
406 complete_async();
408 }
410 }
411
419 template <typename Callable>
420 void set_result(Callable&& result_func, const bool ignore_failure = false) {
421 bool did_set = false;
422 function<PtrType()> func = _MSTL forward<Callable>(result_func);
423 call_once(flag, &state_base::do_set, this, _MSTL addressof(func), _MSTL addressof(did_set));
424 if (did_set) {
425 status.store_notify_all(status::ready, memory_order_release);
426 } else if (!ignore_failure) {
427 throw_exception(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
428 }
429 }
430
439 template <typename Callable>
440 void set_delayed_result(Callable&& result_func, weak_ptr<state_base> self) {
441 bool did_set = false;
442 unique_ptr<make_ready> mr = make_unique<make_ready>();
443 function<PtrType()> func = _MSTL forward<Callable>(result_func);
444 call_once(flag, &state_base::do_set, this, _MSTL addressof(func), _MSTL addressof(did_set));
445 if (!did_set) {
446 throw_exception(future_exception(future_errc_cstr(future_errc::promise_already_satisfied)));
447 }
448 mr->shared_state = _MSTL move(self);
449 mr->set();
450 mr.release();
451 }
452
458 void break_promise(PtrType result) {
459 if (static_cast<bool>(result)) {
460 result->error_ptr = make_exception_ptr(
461 future_exception(future_errc_cstr(future_errc::broken_promise)));
462 result_ptr.swap(result);
463 status.store_notify_all(status::ready, memory_order_release);
464 }
465 }
466
471 void set_retrieved_flag() {
472 if (retrieved.test_and_set(memory_order_acquire)) {
473 throw_exception(future_exception(future_errc_cstr(future_errc::future_already_retrieved)));
474 }
475 }
476
484 template <typename Res, typename Arg>
485 struct setter;
486
490 template <typename Res, typename Arg>
491 struct setter<Res, Arg&> {
492 static_assert(is_same_v<Res, Arg&> || is_same_v<const Res, Arg>, "Invalid specialisation");
493
494 typename promise<Res>::PtrType operator ()() const {
495 promise_ptr->storage->set(*arg_ptr);
496 return _MSTL move(promise_ptr->storage);
497 }
498 promise<Res>* promise_ptr;
499 Arg* arg_ptr;
500 };
501
505 template <typename Res>
506 struct setter<Res, Res&&> {
507 typename promise<Res>::PtrType operator ()() const {
508 promise_ptr->storage->set(_MSTL move(*arg_ptr));
509 return _MSTL move(promise_ptr->storage);
510 }
511 promise<Res>* promise_ptr;
512 Res* arg_ptr;
513 };
514
518 template <typename Res>
519 struct setter<Res, void> {
520 static_assert(is_void_v<Res>, "Only used for promise<void>");
521
522 typename promise<Res>::ptr_type operator ()() const {
523 return _MSTL move(promise_ptr->storage);
524 }
525 promise<Res>* promise_ptr;
526 };
527
528 struct exception_ptr_tag {};
529
533 template <typename Res>
534 struct setter<Res, exception_ptr_tag> {
535 typename promise<Res>::ptr_type operator ()() const {
536 promise_ptr->storage->error_ptr = *exp_ptr;
537 return _MSTL move(promise_ptr->storage);
538 }
539 promise<Res>* promise_ptr;
540 exception_ptr* exp_ptr;
541 };
542
546 template <typename Res, typename Arg>
547 MSTL_ALWAYS_INLINE static setter<Res, Arg&&>
548 create_setter(promise<Res>* promise_ptr, Arg&& arg) noexcept {
549 return setter<Res, Arg&&>{ promise_ptr, _MSTL addressof(arg) };
550 }
551
555 template <typename Res>
556 MSTL_ALWAYS_INLINE static setter<Res, exception_ptr_tag>
557 create_setter(exception_ptr& exception, promise<Res>* promise_ptr) noexcept {
558 return setter<Res, exception_ptr_tag>{ promise_ptr, &exception };
559 }
560
564 template <typename Res>
565 MSTL_ALWAYS_INLINE static setter<Res, void>
566 create_setter(promise<Res>* promise_ptr) noexcept {
567 return setter<Res, void>{ promise_ptr };
568 }
569
576 template <typename T>
577 static void check(const shared_ptr<T>& ptr) {
578 if (!static_cast<bool>(ptr)) {
579 throw_exception(future_exception(future_errc_cstr(future_errc::no_state)));
580 }
581 }
582
583 private:
589 void do_set(function<PtrType()>* func, bool* did_set) {
590 PtrType result = (*func)();
591 *did_set = true;
592 result_ptr.swap(result);
593 }
594
599 virtual void complete_async() {}
600
605 virtual bool is_deferred_future() const {
606 return false;
607 }
608
614 struct make_ready final : at_thread_exit_elt {
615 weak_ptr<state_base> shared_state;
616
621 static void run(void* ptr) {
622 const auto self = static_cast<make_ready*>(ptr);
623 const auto state = self->shared_state.lock();
624 if (state) {
625 state->status.store_notify_all(status::ready, memory_order_release);
626 }
627 delete self;
628 }
629
633 void set() {
634 _MSTL at_thread_exit_register(this, &make_ready::run);
635 }
636 };
637 };
638
639private:
644 template <typename Ptr>
645 struct get_result_type {
646 using type = typename Ptr::element_type::result_type;
647 };
648
649 template <typename Ptr>
650 using result_res_t = typename get_result_type<Ptr>::type;
651
652public:
653 class async_state_common;
654
655 template <typename BoundFunc, typename Res = decltype(_MSTL declval<BoundFunc&>()())>
656 class deferred_state;
657
658 template <typename BoundFunc, typename Res = decltype(_MSTL declval<BoundFunc&>()())>
659 class async_state_impl;
660
661 template <typename Sign>
662 class task_state_base;
663
664 template <typename Func, typename Alloc, typename Sign>
665 class task_state;
666
667 template <typename ResPtrT, typename Func, typename ResT = result_res_t<ResPtrT>>
668 struct task_setter;
669
673 template <typename ResPtr, typename BoundFunc>
674 static task_setter<ResPtr, BoundFunc>
675 create_task_setter(ResPtr& ptr, BoundFunc& call) {
676 return { _MSTL addressof(ptr), _MSTL addressof(call) };
677 }
678};
679
684template <typename Res>
685struct __future_base::basic_result<Res&> : __future_base::result_base {
686private:
687 Res* value_ptr;
688
689 void destroy() override { delete this; }
690
691public:
692 using result_type = Res&;
693
697 basic_result() noexcept : value_ptr() {}
698
703 void set(Res& result) noexcept {
704 value_ptr = _MSTL addressof(result);
705 }
706
711 MSTL_NODISCARD Res& get() noexcept {
712 return *value_ptr;
713 }
714};
715
719template <>
720struct __future_base::basic_result<void> : __future_base::result_base {
721 using result_type = void;
722
723private:
724 void destroy() override { delete this; }
725};
726
727
734template <typename Res>
735class __basic_future : public __future_base {
736protected:
737 using state_type = shared_ptr<state_base>;
738 using result_type = __future_base::basic_result<Res>&;
739
740private:
741 state_type state_ptr;
742
743public:
744 __basic_future(const __basic_future&) = delete;
745 __basic_future& operator =(const __basic_future&) = delete;
746
751 bool valid() const noexcept {
752 return static_cast<bool>(state_ptr);
753 }
754
759 void wait() const {
760 state_base::check(state_ptr);
761 state_ptr->wait();
762 }
763
772 template <typename Rep, typename Period>
773 future_status wait_for(const duration<Rep, Period>& relative_time) const {
774 state_base::check(state_ptr);
775 return state_ptr->wait_for(relative_time);
776 }
777
786 template <typename Clock, typename Dur>
787 future_status wait_until(const time_point<Clock, Dur>& absolute_time) const {
788 state_base::check(state_ptr);
789 return state_ptr->wait_until(absolute_time);
790 }
791
792protected:
797 result_type get_result() const {
798 state_base::check(state_ptr);
799 result_base& result = state_ptr->wait();
800 if (result.error_ptr != nullptr) {
801 rethrow_exception(result.error_ptr);
802 }
803 return static_cast<result_type>(result);
804 }
805
810 void swap(__basic_future& other) noexcept {
811 state_ptr.swap(other.state_ptr);
812 }
813
818 explicit __basic_future(const state_type& state)
819 : state_ptr(state) {
820 state_base::check(state_ptr);
821 state_ptr->set_retrieved_flag();
822 }
823
824 explicit __basic_future(const shared_future<Res>&) noexcept;
825 explicit __basic_future(shared_future<Res>&&) noexcept;
826 explicit __basic_future(future<Res>&&) noexcept;
827
831 constexpr __basic_future() noexcept {}
832
838 struct reset {
839 __basic_future& future_ref;
840
845 explicit reset(__basic_future& future) noexcept
846 : future_ref(future) {}
847
851 ~reset() {
852 future_ref.state_ptr.reset();
853 }
854 };
855};
856
859
860
861template <typename Res, typename Arg>
862struct is_location_invariant<_INNER __future_base::state_base::setter<Res, Arg>> : true_type {};
863
864template <typename ResPtr, typename Func, typename Res>
865struct is_location_invariant<_INNER __future_base::task_setter<ResPtr, Func, Res>> : true_type {};
866
867
876template <typename Res>
877class future : public _INNER __basic_future<Res> {
878 static_assert(!is_array_v<Res>, "result type must not be an array");
879 static_assert(!is_function_v<Res>, "result type must not be a function");
880 static_assert(is_destructible_v<Res>, "result type must be destructible");
881
882 friend class promise<Res>;
883
884 template <typename Sign>
885 friend class packaged_task;
886
887 template <typename Function, typename... Args>
888 friend future<async_result_t<Function, Args...>>
889 async(launch, Function&&, Args&&...);
890
891 using base_type = _INNER __basic_future<Res>;
892 using state_type = typename base_type::state_type;
893
897 explicit future(const state_type& state) : base_type(state) {}
898
899public:
903 constexpr future() noexcept
904 : base_type() {}
905
910 future(future&& other) noexcept
911 : base_type(_MSTL move(other)) {}
912
913 future(const future&) = delete;
914 future& operator =(const future&) = delete;
915
921 future& operator =(future&& other) noexcept {
922 future(_MSTL move(other)).swap(*this);
923 return *this;
924 }
925
931 Res get() {
932 typename base_type::reset reset_(*this);
933 return _MSTL move(this->get_result().value());
934 }
935
941};
942
947template <typename Res>
948class future<Res&> : public _INNER __basic_future<Res&> {
949 friend class promise<Res&>;
950
951 template <typename Sign>
952 friend class packaged_task;
953
954 template <typename Function, typename... Args>
955 friend future<async_result_t<Function, Args...>>
956 async(launch, Function&&, Args&&...);
957
958 typedef _INNER __basic_future<Res&> base_type;
959 typedef typename base_type::state_type state_type;
960
961 explicit future(const state_type& state) : base_type(state) {}
962
963public:
964 constexpr future() noexcept : base_type() {}
965 future(future&& other) noexcept : base_type(_MSTL move(other)) {}
966
967 future(const future&) = delete;
968 future& operator =(const future&) = delete;
969
970 future& operator =(future&& other) noexcept {
971 future(_MSTL move(other)).swap(*this);
972 return *this;
973 }
974
980 Res& get() {
981 typename base_type::reset reset_(*this);
982 return this->get_result().get();
983 }
984
985 shared_future<Res&> share() noexcept;
986};
987
991template <>
992class future<void> : public _INNER __basic_future<void> {
993 friend class promise<void>;
994
995 template <typename> friend class packaged_task;
996
997 template <typename Function, typename... Args>
998 friend future<async_result_t<Function, Args...>>
999 async(launch, Function&&, Args&&...);
1000
1001 using base_type = _INNER __basic_future<void>;
1002 using state_type = base_type::state_type;
1003
1004 explicit future(const state_type& state) : base_type(state) {}
1005
1006public:
1007 constexpr future() noexcept {}
1008 future(future&& other) noexcept : base_type(_MSTL move(other)) {}
1009
1010 future(const future&) = delete;
1011 future& operator =(const future&) = delete;
1012
1013 future& operator =(future&& other) noexcept {
1014 future(_MSTL move(other)).swap(*this);
1015 return *this;
1016 }
1017
1022 void get() {
1023 base_type::reset reset(*this);
1024 MSTL_IGNORE this->get_result();
1025 }
1026
1027 shared_future<void> share() noexcept;
1028};
1029
1037template <typename Res>
1038class shared_future : public _INNER __basic_future<Res> {
1039 static_assert(!is_array_v<Res>, "result type must not be an array");
1040 static_assert(!is_function_v<Res>, "result type must not be a function");
1041 static_assert(is_destructible_v<Res>, "result type must be destructible");
1042
1043 using base_type = _INNER __basic_future<Res>;
1044
1045public:
1046 constexpr shared_future() noexcept : base_type() {}
1047 shared_future(const shared_future& other) noexcept : base_type(other) {}
1048 shared_future(future<Res>&& other) noexcept : base_type(_MSTL move(other)) {}
1049 shared_future(shared_future&& other) noexcept : base_type(_MSTL move(other)) {}
1050
1051 shared_future& operator =(const shared_future& other) noexcept {
1052 shared_future(other).swap(*this);
1053 return *this;
1054 }
1055
1056 shared_future& operator =(shared_future&& other) noexcept {
1057 shared_future(_MSTL move(other)).swap(*this);
1058 return *this;
1059 }
1060
1066 const Res& get() const {
1067 return this->get_result().value();
1068 }
1069};
1070
1075template <typename Res>
1076class shared_future<Res&> : public _INNER __basic_future<Res&> {
1077 using base_type = _INNER __basic_future<Res&>;
1078
1079public:
1080 constexpr shared_future() noexcept : base_type() {}
1081 shared_future(const shared_future& other) : base_type(other) {}
1082 shared_future(future<Res&>&& other) noexcept : base_type(_MSTL move(other)) {}
1083 shared_future(shared_future&& other) noexcept : base_type(_MSTL move(other)) {}
1084
1085 shared_future& operator =(const shared_future& other) {
1086 shared_future(other).swap(*this);
1087 return *this;
1088 }
1089
1090 shared_future& operator =(shared_future&& other) noexcept {
1091 shared_future(_MSTL move(other)).swap(*this);
1092 return *this;
1093 }
1094
1099 Res& get() const {
1100 return this->get_result().get();
1101 }
1102};
1103
1107template <>
1108class shared_future<void> : public _INNER __basic_future<void> {
1109 using base_type = _INNER __basic_future<void>;
1110
1111public:
1112 constexpr shared_future() noexcept {}
1113 shared_future(const shared_future& other) : base_type(other) {}
1114 shared_future(future<void>&& other) noexcept : base_type(_MSTL move(other)) {}
1115 shared_future(shared_future&& other) noexcept : base_type(_MSTL move(other)) {}
1116
1117 shared_future& operator =(const shared_future& other) {
1118 shared_future(other).swap(*this);
1119 return *this;
1120 }
1121
1122 shared_future& operator =(shared_future&& other) noexcept {
1123 shared_future(_MSTL move(other)).swap(*this);
1124 return *this;
1125 }
1126
1130 void get() const {
1131 MSTL_IGNORE this->get_result();
1132 }
1133};
1134
1136
1137template <typename Res>
1138_INNER __basic_future<Res>::__basic_future(const shared_future<Res>& other) noexcept
1139: state_ptr(other.state_ptr) {}
1140
1141template <typename Res>
1142_INNER __basic_future<Res>::__basic_future(shared_future<Res>&& other) noexcept
1143: state_ptr(_MSTL move(other.state_ptr)) {}
1144
1145template <typename Res>
1146_INNER __basic_future<Res>::__basic_future(future<Res>&& other) noexcept
1147: state_ptr(_MSTL move(other.state_ptr)) {}
1148
1149
1150template <typename Result>
1151shared_future<Result> future<Result>::share() noexcept {
1152 return shared_future<Result>(_MSTL move(*this));
1153}
1154
1155template <typename Res>
1156shared_future<Res&> future<Res&>::share() noexcept {
1157 return shared_future<Res&>(_MSTL move(*this));
1158}
1159
1160inline shared_future<void> future<void>::share() noexcept {
1161 return shared_future<void>(_MSTL move(*this));
1162}
1163
1165
1172template <typename T>
1174 using type = T;
1175};
1176
1180template <>
1181struct future_result<void> {
1182 using type = none_t;
1183};
1184
1189template <typename T>
1190using future_result_t = typename future_result<T>::type;
1191
1192
1199template <typename T>
1200MSTL_ALWAYS_INLINE
1203 f.get();
1204 return none;
1205}
1206
1213template <typename T>
1214MSTL_ALWAYS_INLINE
1217 return f.get();
1218}
1219 // Async
1221
1223#endif // MSTL_CORE_ASYNC_FUTURE_HPP__
MSTL对齐缓冲区实现
MSTL分配器指针包装器
MSTL线程退出回调支持
MSTL原子快速用户态互斥锁实现
MSTL单次调用
MSTL_ALWAYS_INLINE bool load_when_equal_for(const uint32_t value, const memory_order mo, const duration< Rep, Period > &rtime)
在指定时间内等待值等于指定值
MSTL_ALWAYS_INLINE void store_notify_all(const uint32_t value, const memory_order mo)
存储新值并通知所有等待线程
MSTL_ALWAYS_INLINE void load_when_equal(const uint32_t value, const memory_order mo)
等待直到值等于指定值
MSTL_ALWAYS_INLINE bool load_when_equal_until(const uint32_t value, const memory_order mo, const time_point< Clock, Dur > &atime)
在指定时间点前等待值等于指定值(通用时钟)
MSTL_NODISCARD MSTL_ALWAYS_INLINE uint32_t load(const memory_order mo) const
原子加载数据
函数包装器主模板声明
Res & get()
获取结果引用
void get()
获取结果
期望值操作异常
独占future类模板
Res get()
获取结果
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
获取常量引用结果
MSTL_CONSTEXPR20 pointer release() noexcept
释放所有权
MSTL_NODISCARD shared_ptr< T > lock() const noexcept
尝试获取共享智能指针
MSTL异常指针实现
MSTL通用函数包装器
allocated_ptr< Alloc > allocate_guarded(Alloc &alloc)
分配内存并创建allocated_ptr
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
MSTL_NODISCARD constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
future_errc
期值错误码枚举
MSTL_NODISCARD future< async_result_t< Func, Args... > > async(launch policy, Func &&function, Args &&... args)
异步执行函数(指定策略)
invoke_result_t< decay_t< Func >, decay_t< Args >... > async_result_t
异步调用结果类型推导
launch
异步启动策略枚举
future_status
期值状态枚举
typename future_result< T >::type future_result_t
future结果类型别名
MSTL_ALWAYS_INLINE enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
@ broken_promise
承诺被破坏
@ future_already_retrieved
future已经被获取
@ promise_already_satisfied
promise已经被满足
@ no_state
没有关联的状态对象
@ deferred
延迟执行
@ timeout
等待超时
@ ready
结果已就绪
void call_once(once_flag &flag, Callable &&func, Args &&... args)
单次调用函数
MSTL_INLINE17 constexpr bool is_clock_v
is_clock的便捷变量模板
milliseconds MSTL_API relative_time(int64_t sec, int64_t nsec, bool is_monotonic=false)
将绝对时间戳转换为相对延迟毫秒数
unsigned int uint32_t
32位无符号整数类型
#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
exception_ptr make_exception_ptr(Ex ex) noexcept
创建异常指针
void MSTL_API rethrow_exception(const exception_ptr &p)
重新抛出异常
@ wait
等待操作
MSTL_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
标准分配器别名
MSTL_INLINE17 constexpr auto memory_order_release
释放内存顺序常量
MSTL_INLINE17 constexpr auto memory_order_acquire
获取内存顺序常量
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
MSTL_INLINE17 constexpr none_t none
默认空表示
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result)
移动范围元素
void swap()=delete
删除无参数的swap重载
void MSTL_API at_thread_exit_register(at_thread_exit_elt *elt, void(*callback)(void *)) noexcept
注册线程退出回调
typename decay< T >::type decay_t
decay的便捷别名
bool_constant< true > true_type
表示true的类型
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
MSTL_CONSTEXPR20 unique_ptr< T > make_unique(Args &&... args)
创建unique_ptr
MSTL空状态类型
void * addr() noexcept
获取缓冲区的原始地址
T * ptr() noexcept
获取缓冲区的类型化指针
MSTL_ALWAYS_INLINE void clear(const memory_order mo=memory_order_seq_cst) noexcept
清除标志
MSTL_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)
构造函数
future结果类型转换
判断类型是否是位置不变的
空状态类型
MSTL弱智能指针实现