CoopSync for TBB 0.1.0
Cooperative synchronization primitives for TBB resumable tasks
Loading...
Searching...
No Matches
future.hpp
1// SPDX-FileCopyrightText: 2026 CERN
2//
3// SPDX-License-Identifier: Apache-2.0
4
5#pragma once
6
7#include <atomic>
8#include <cassert>
9#include <exception>
10#include <functional>
11#include <future>
12#include <memory>
13#include <new>
14#include <stdexcept>
15#include <system_error>
16#include <type_traits>
17#include <utility>
18
19#include "coopsync_tbb/detail/macros.hpp"
20#include "coopsync_tbb/detail/wait_queue.hpp"
21
22namespace coopsync_tbb {
23
25class COOPSYNC_TBB_EXPORT future_error : public std::logic_error {
26 public:
29 explicit future_error(std::future_errc errc)
30 : future_error(make_error_code(errc)) {}
31
34 const std::error_code& code() const noexcept { return m_errc; }
35
36 private:
37 explicit future_error(std::error_code errc)
38 : logic_error(errc.message()), m_errc(std::move(errc)) {}
39 std::error_code m_errc;
40};
41
42namespace detail {
43namespace future {
44
46enum class status : unsigned char {
47 empty = 0,
48 value = 1,
49 exception = 2,
50 broken = 3,
52 transition = 4
54};
55
56struct shared_state_base {
57
58 shared_state_base() = default;
59 shared_state_base(const shared_state_base&) = delete;
60 shared_state_base& operator=(const shared_state_base&) = delete;
61 shared_state_base(shared_state_base&&) = delete;
62 shared_state_base& operator=(shared_state_base&&) = delete;
63
64 ~shared_state_base() { assert(waiters.empty()); } // LCOV_EXCL_LINE
65
66 detail::wait_queue waiters;
67
68 std::atomic<status> state{status::empty};
69 std::atomic<bool> future_obtained{false};
70 std::exception_ptr exception;
71
72 bool ready() const noexcept {
73 auto status = state.load(std::memory_order_acquire);
74 return (status == status::value) || (status == status::exception) ||
75 (status == status::broken);
76 }
77
78 void set_exception(std::exception_ptr p) {
79 auto expected = status::empty;
80 const auto desired = status::transition;
81 if (!state.compare_exchange_strong(expected, desired,
82 std::memory_order_release,
83 std::memory_order_relaxed)) {
84 throw future_error(std::future_errc::promise_already_satisfied);
85 }
86 exception = std::move(p);
87 state.store(status::exception, std::memory_order_release);
88 waiters.resume_all();
89 }
90
91 void break_promise() noexcept {
92 auto expected = status::empty;
93 const auto desired = status::broken;
94 if (state.compare_exchange_strong(expected, desired,
95 std::memory_order_release,
96 std::memory_order_relaxed)) {
97 waiters.resume_all();
98 }
99 }
100
101 void wait() {
102 waiters.wait_if([this] { return !ready(); });
103 assert(ready()); // LCOV_EXCL_LINE
104 }
105};
106
107template <typename T>
108struct shared_state : public shared_state_base {
109
110 class optional_storage {
111 public:
112 optional_storage() noexcept = default;
113
114 optional_storage(const optional_storage&) = delete;
115 optional_storage& operator=(const optional_storage&) = delete;
116 optional_storage(optional_storage&&) = delete;
117 optional_storage& operator=(optional_storage&&) = delete;
118
119 ~optional_storage() {
120 if (m_has_value) {
121 destroy();
122 }
123 }
124
125 template <typename... Args>
126 void emplace(Args&&... args) {
127 if (m_has_value) {
128 destroy();
129 }
130
131 ::new (std::addressof(get())) T(std::forward<Args>(args)...);
132 m_has_value = true;
133 }
134
135 bool has_value() const noexcept { return m_has_value; }
136
137 T& get() noexcept {
138 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
139 return m_storage.value;
140 }
141 const T& get() const noexcept {
142 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access)
143 return m_storage.value;
144 }
145
146 T& operator*() noexcept { return get(); }
147 const T& operator*() const noexcept { return get(); }
148
149 private:
150 void destroy() noexcept {
151 get().~T();
152 m_has_value = false;
153 }
154
155 union storage_union {
156 storage_union() noexcept {}
157 ~storage_union() {}
158 storage_union(const storage_union&) = delete;
159 storage_union& operator=(const storage_union&) = delete;
160 storage_union(storage_union&&) = delete;
161 storage_union& operator=(storage_union&&) = delete;
162 T value;
163 };
164
165 storage_union m_storage = {};
166 bool m_has_value = false;
167 };
168
169 optional_storage value;
170
171 template <typename R>
172 void set_value(R&& v) {
173 auto expected = status::empty;
174 const auto desired = status::transition;
175 if (!state.compare_exchange_strong(expected, desired,
176 std::memory_order_release,
177 std::memory_order_relaxed)) {
178 throw future_error(std::future_errc::promise_already_satisfied);
179 }
180 try {
181 value.emplace(std::forward<R>(v));
182 state.store(status::value, std::memory_order_release);
183 } catch (...) {
184 exception = std::current_exception();
185 state.store(status::exception, std::memory_order_release);
186 }
187 waiters.resume_all();
188 }
189};
190
191template <>
192struct shared_state<void> : public shared_state_base {
193 void set_value() {
194
195 auto expected = status::empty;
196 const auto desired = status::value;
197 if (!state.compare_exchange_strong(expected, desired,
198 std::memory_order_release,
199 std::memory_order_relaxed)) {
200 throw future_error(std::future_errc::promise_already_satisfied);
201 }
202 waiters.resume_all();
203 }
204};
205
206template <typename T>
207struct shared_state<T&> : public shared_state_base {
208 T* value = nullptr;
209
210 void set_value(T& v) {
211 auto expected = status::empty;
212 const auto desired = status::transition;
213 if (!state.compare_exchange_strong(expected, desired,
214 std::memory_order_release,
215 std::memory_order_relaxed)) {
216 throw future_error(std::future_errc::promise_already_satisfied);
217 }
218 try {
219 value = std::addressof(v);
220 state.store(status::value, std::memory_order_release);
221 } catch (...) {
222 exception = std::current_exception();
223 state.store(status::exception, std::memory_order_release);
224 }
225 waiters.resume_all();
226 }
227};
228
229template <typename T>
230class future_base {
231 protected:
232 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
233 std::shared_ptr<shared_state<T>> m_state;
234
235 future_base() noexcept = default;
236 explicit future_base(std::shared_ptr<shared_state<T>> state)
237 : m_state(std::move(state)) {}
238
241 void ensure_valid() const {
242 if (!m_state) {
243 throw future_error(std::future_errc::no_state);
244 }
245 }
246
252 void throw_if_exception_or_broken() const {
253 const auto st = m_state->state.load(std::memory_order_acquire);
254 if (st == status::exception) {
255 std::rethrow_exception(m_state->exception);
256 }
257 if (st == status::broken) {
258 throw future_error(std::future_errc::broken_promise);
259 }
260 assert(st == status::value); // LCOV_EXCL_LINE
261 }
262
264 void reset() noexcept { m_state.reset(); }
265
268 COOPSYNC_TBB_NODISCARD std::shared_ptr<shared_state<T>>
269 release_state() noexcept {
270 // Moved from m_state is left empty
271 return std::move(m_state);
272 }
273
274 public:
278 bool valid() const noexcept { return static_cast<bool>(m_state); }
279
284 bool try_wait() const {
285 ensure_valid();
286 return m_state->ready();
287 }
288
294 void wait() const {
295 ensure_valid();
296 m_state->wait();
297 }
298};
299
300template <typename T>
301class promise_base {
302 protected:
303 // NOLINTNEXTLINE(cppcoreguidelines-non-private-member-variables-in-classes)
304 std::shared_ptr<shared_state<T>> m_state;
305
307 promise_base() : m_state(std::make_shared<shared_state<T>>()) {}
308
311 template <typename Alloc>
312 explicit promise_base(const Alloc& alloc)
313 : m_state(std::allocate_shared<shared_state<T>>(alloc)) {}
314
317 void ensure_valid() const {
318 if (!m_state) {
319 throw future_error(std::future_errc::no_state);
320 }
321 }
322
327 void mark_future_obtained() {
328 bool expected = false;
329 if (!m_state->future_obtained.compare_exchange_strong(
330 expected, true, std::memory_order_acq_rel,
331 std::memory_order_relaxed)) {
332 throw future_error(std::future_errc::future_already_retrieved);
333 }
334 }
335
336 public:
337 promise_base(const promise_base&) = delete;
338 promise_base& operator=(const promise_base&) = delete;
339 promise_base(promise_base&&) noexcept = default;
340 promise_base& operator=(promise_base&&) noexcept = default;
341
344 ~promise_base() {
345 if (m_state) {
346 m_state->break_promise();
347 }
348 }
349
352 void swap(promise_base& other) noexcept { m_state.swap(other.m_state); }
353
358 void set_exception(std::exception_ptr p) {
359 ensure_valid();
360 m_state->set_exception(std::move(p));
361 }
362};
363} // namespace future
364} // namespace detail
365
366template <typename T>
367class future;
368
369template <typename T>
370class promise;
371
372template <typename>
373class packaged_task;
374
375template <typename T>
376class shared_future;
377
383template <typename T>
384class COOPSYNC_TBB_EXPORT future : private detail::future::future_base<T> {
385 public:
388 future() noexcept = default;
389
391 future(const future&) = delete;
392
394 future& operator=(const future&) = delete;
395
399 future(future&& other) noexcept = default;
400
404 future& operator=(future&& other) noexcept = default;
405
407 ~future() = default;
408
411 bool valid() const noexcept {
412 return detail::future::future_base<T>::valid();
413 }
414
419 bool try_wait() const { return detail::future::future_base<T>::try_wait(); }
420
425 void wait() const { detail::future::future_base<T>::wait(); }
426
435 typename std::remove_reference<T>::type get() {
436 this->ensure_valid();
437 this->m_state->wait();
438 this->throw_if_exception_or_broken();
439
440 assert(this->m_state->value.has_value()); // LCOV_EXCL_LINE
441 auto out = std::move(*this->m_state->value);
442 this->reset();
443 return out;
444 }
445
451 COOPSYNC_TBB_NODISCARD shared_future<T> share();
452
453 private:
454 friend class promise<T>;
455 template <typename>
456 friend class packaged_task;
457 explicit future(std::shared_ptr<detail::future::shared_state<T>> state)
458 : detail::future::future_base<T>(std::move(state)) {}
459};
460
462template <>
463class COOPSYNC_TBB_EXPORT
464 future<void> : private detail::future::future_base<void> {
465 public:
468 future() noexcept = default;
469
471 future(const future&) = delete;
472
474 future& operator=(const future&) = delete;
475
479 future(future&& other) noexcept = default;
480
484 future& operator=(future&& other) noexcept = default;
485
487 ~future() = default;
488
491 bool valid() const noexcept {
492 return detail::future::future_base<void>::valid();
493 }
494
499 bool try_wait() const {
500 return detail::future::future_base<void>::try_wait();
501 }
502
508 void wait() const { detail::future::future_base<void>::wait(); }
509
517 void get() {
518 this->ensure_valid();
519 this->m_state->wait();
520 this->throw_if_exception_or_broken();
521 this->reset();
522 }
523
529 COOPSYNC_TBB_NODISCARD shared_future<void> share();
530
531 private:
532 friend class promise<void>;
533 template <typename>
534 friend class packaged_task;
535 explicit future(std::shared_ptr<detail::future::shared_state<void>> state)
536 : detail::future::future_base<void>(std::move(state)) {}
537};
538
541template <typename T>
542class COOPSYNC_TBB_EXPORT future<T&> : private detail::future::future_base<T&> {
543 public:
546 future() noexcept = default;
547
549 future(const future&) = delete;
550
552 future& operator=(const future&) = delete;
553
557 future(future&& other) noexcept = default;
558
562 future& operator=(future&& other) noexcept = default;
563
565 ~future() = default;
566
569 bool valid() const noexcept {
570 return detail::future::future_base<T&>::valid();
571 }
572
577 bool try_wait() const {
578 return detail::future::future_base<T&>::try_wait();
579 }
580
586 void wait() const { detail::future::future_base<T&>::wait(); }
587
596 T& get() {
597 this->ensure_valid();
598 this->m_state->wait();
599 this->throw_if_exception_or_broken();
600
601 assert(this->m_state->value != nullptr); // LCOV_EXCL_LINE
602 T* out = this->m_state->value;
603 this->reset();
604 return *out;
605 }
606
612 COOPSYNC_TBB_NODISCARD shared_future<T&> share();
613
614 private:
615 friend class promise<T&>;
616 template <typename>
617 friend class packaged_task;
618 explicit future(std::shared_ptr<detail::future::shared_state<T&>> state)
619 : detail::future::future_base<T&>(std::move(state)) {}
620};
621
628template <typename T>
629class COOPSYNC_TBB_EXPORT shared_future
630 : private detail::future::future_base<T> {
631 public:
633 shared_future() noexcept = default;
634
638 shared_future(const shared_future& other) noexcept = default;
639
643 shared_future& operator=(const shared_future& other) noexcept = default;
644
648 shared_future(shared_future&& other) noexcept = default;
649
653 shared_future& operator=(shared_future&& other) noexcept = default;
654
656 ~shared_future() = default;
657
661 bool valid() const noexcept {
662 return detail::future::future_base<T>::valid();
663 }
664
668 bool try_wait() const { return detail::future::future_base<T>::try_wait(); }
669
672 void wait() const { detail::future::future_base<T>::wait(); }
673
682 const T& get() const {
683 this->ensure_valid();
684 this->m_state->wait();
685 this->throw_if_exception_or_broken();
686 assert(this->m_state->value.has_value()); // LCOV_EXCL_LINE
687 return *this->m_state->value;
688 }
689
690 private:
691 template <typename U>
692 friend class future;
693 explicit shared_future(
694 std::shared_ptr<detail::future::shared_state<T>> state)
695 : detail::future::future_base<T>(std::move(state)) {}
696};
697
699template <>
700class COOPSYNC_TBB_EXPORT
701 shared_future<void> : private detail::future::future_base<void> {
702 public:
704 shared_future() noexcept = default;
705
709 shared_future(const shared_future& other) noexcept = default;
710
714 shared_future& operator=(const shared_future& other) noexcept = default;
715
720 shared_future(shared_future&& other) noexcept = default;
721
726 shared_future& operator=(shared_future&& other) noexcept = default;
727
730 ~shared_future() = default;
731
735 bool valid() const noexcept {
736 return detail::future::future_base<void>::valid();
737 }
738
742 bool try_wait() const {
743 return detail::future::future_base<void>::try_wait();
744 }
745
750 void wait() const { detail::future::future_base<void>::wait(); }
751
760 void get() const {
761 this->ensure_valid();
762 this->m_state->wait();
763 this->throw_if_exception_or_broken();
764 }
765
766 private:
767 template <typename U>
768 friend class future;
769 explicit shared_future(
770 std::shared_ptr<detail::future::shared_state<void>> state)
771 : detail::future::future_base<void>(std::move(state)) {}
772};
773
775template <typename T>
776class COOPSYNC_TBB_EXPORT
777 shared_future<T&> : private detail::future::future_base<T&> {
778 public:
780 shared_future() noexcept = default;
781
785 shared_future(const shared_future& other) noexcept = default;
786
790 shared_future& operator=(const shared_future& other) noexcept = default;
791
796 shared_future(shared_future&& other) noexcept = default;
797
802 shared_future& operator=(shared_future&& other) noexcept = default;
803
806 ~shared_future() = default;
807
811 bool valid() const noexcept {
812 return detail::future::future_base<T&>::valid();
813 }
814
819 bool try_wait() const {
820 return detail::future::future_base<T&>::try_wait();
821 }
822
827 void wait() const { detail::future::future_base<T&>::wait(); }
828
838 T& get() const {
839 this->ensure_valid();
840 this->m_state->wait();
841 this->throw_if_exception_or_broken();
842 assert(this->m_state->value != nullptr); // LCOV_EXCL_LINE
843 return *this->m_state->value;
844 }
845
846 private:
847 template <typename U>
848 friend class future;
849 // cppcheck-suppress functionStatic
850 explicit shared_future(
851 std::shared_ptr<detail::future::shared_state<T&>> state)
852 : detail::future::future_base<T&>(std::move(state)) {}
853};
854
855template <typename T>
857 this->ensure_valid();
858 return shared_future<T>(this->release_state());
859}
860
862 this->ensure_valid();
863 return shared_future<void>(this->release_state());
864}
865
866template <typename T>
868 this->ensure_valid();
869 return shared_future<T&>(this->release_state());
870}
871
876template <typename T>
877class COOPSYNC_TBB_EXPORT promise : private detail::future::promise_base<T> {
878 public:
880 promise() = default;
881
886 template <typename Alloc>
887 explicit promise(const Alloc& alloc)
888 : detail::future::promise_base<T>(alloc) {}
889
891 promise(const promise&) = delete;
892
894 promise& operator=(const promise&) = delete;
895
899 promise(promise&& other) noexcept = default;
900
904 promise& operator=(promise&& other) noexcept = default;
905
908 ~promise() = default;
909
912 void swap(promise& other) noexcept {
913 detail::future::promise_base<T>::swap(other);
914 }
915
921 COOPSYNC_TBB_NODISCARD future<T> get_future() {
922 this->ensure_valid();
923 this->mark_future_obtained();
924 return future<T>(this->m_state);
925 }
926
933 void set_value(const T& v) {
934 this->ensure_valid();
935 this->m_state->set_value(v);
936 }
937
944 void set_value(T&& v) {
945 this->ensure_valid();
946 this->m_state->set_value(std::move(v));
947 }
948
953 void set_exception(std::exception_ptr p) {
954 detail::future::promise_base<T>::set_exception(std::move(p));
955 }
956};
957
960template <>
961class COOPSYNC_TBB_EXPORT
962 promise<void> : private detail::future::promise_base<void> {
963 public:
965 promise() = default;
966
969 template <typename Alloc>
970 explicit promise(const Alloc& alloc)
971 : detail::future::promise_base<void>(alloc) {}
972
974 promise(const promise&) = delete;
975
977 promise& operator=(const promise&) = delete;
978
982 promise(promise&& other) noexcept = default;
983
987 promise& operator=(promise&& other) noexcept = default;
988
991 ~promise() = default;
992
995 void swap(promise& other) noexcept {
996 detail::future::promise_base<void>::swap(other);
997 }
998
1004 COOPSYNC_TBB_NODISCARD future<void> get_future() {
1005 this->ensure_valid();
1006 this->mark_future_obtained();
1007 return future<void>(this->m_state);
1008 }
1009
1015 void set_value() {
1016 this->ensure_valid();
1017 this->m_state->set_value();
1018 }
1019
1024 void set_exception(std::exception_ptr p) {
1025 detail::future::promise_base<void>::set_exception(std::move(p));
1026 }
1027};
1028
1031template <typename T>
1032class COOPSYNC_TBB_EXPORT
1033 promise<T&> : private detail::future::promise_base<T&> {
1034 public:
1036 promise() = default;
1037
1040 template <typename Alloc>
1041 explicit promise(const Alloc& alloc)
1042 : detail::future::promise_base<T&>(alloc) {}
1043
1045 promise(const promise&) = delete;
1046
1048 promise& operator=(const promise&) = delete;
1049
1053 promise( // cppcheck-suppress noExplicitConstructor
1054 promise&& other) noexcept = default;
1055
1059 promise& operator=(promise&& other) noexcept = default;
1060
1063 ~promise() = default;
1064
1067 void swap(promise& other) noexcept {
1068 detail::future::promise_base<T&>::swap(other);
1069 }
1070
1076 COOPSYNC_TBB_NODISCARD future<T&> get_future() {
1077 this->ensure_valid();
1078 this->mark_future_obtained();
1079 return future<T&>(this->m_state);
1080 }
1081
1090 void set_value(T& v) {
1091 this->ensure_valid();
1092 this->m_state->set_value(v);
1093 }
1094
1099 void set_exception(std::exception_ptr p) {
1100 detail::future::promise_base<T&>::set_exception(std::move(p));
1101 }
1102};
1103
1110template <typename R, typename... Args>
1111class COOPSYNC_TBB_EXPORT packaged_task<R(Args...)> {
1112 public:
1115 packaged_task() = default;
1116
1123 template <typename F>
1124 explicit packaged_task(F f)
1125 : m_function(std::move(f)),
1126 m_state(std::make_shared<detail::future::shared_state<R>>()) {}
1127
1130
1133
1138 : m_function(std::move(other.m_function)),
1139 m_state(std::move(other.m_state)) {
1140 other.m_function = {};
1141 other.m_state.reset();
1142 }
1143
1148 if (this != &other) {
1149 // Ensure any previously held shared state is released and marked as
1150 // broken if it wasn't made ready.
1151 if (m_state) {
1152 m_state->break_promise();
1153 }
1154 m_state.reset();
1155
1156 m_function = std::move(other.m_function);
1157 m_state = std::move(other.m_state);
1158
1159 other.m_function = {};
1160 other.m_state.reset();
1161 }
1162 return *this;
1163 }
1164
1168 if (m_state) {
1169 m_state->break_promise();
1170 }
1171 }
1172
1176 bool valid() const noexcept { return static_cast<bool>(m_state); }
1177
1180 void swap(packaged_task& other) noexcept {
1181 using std::swap;
1182 swap(m_function, other.m_function);
1183 swap(m_state, other.m_state);
1184 }
1185
1191 COOPSYNC_TBB_NODISCARD future<R> get_future() {
1192 if (!valid()) {
1193 throw future_error(std::future_errc::no_state);
1194 }
1195
1196 bool expected = false;
1197 if (!m_state->future_obtained.compare_exchange_strong(
1198 expected, true, std::memory_order_acq_rel,
1199 std::memory_order_relaxed)) {
1200 throw future_error(std::future_errc::future_already_retrieved);
1201 }
1202 return future<R>(m_state);
1203 }
1204
1214 void operator()(Args... args) {
1215 if (!valid()) {
1216 throw future_error(std::future_errc::no_state);
1217 }
1218
1219 try {
1220 invoke_and_set(std::forward<Args>(args)...);
1221 } catch (...) {
1222 m_state->set_exception(std::current_exception());
1223 }
1224 }
1225
1229 void reset() {
1230 if (!valid()) {
1231 throw future_error(std::future_errc::no_state);
1232 }
1233
1234 auto new_state = std::make_shared<detail::future::shared_state<R>>();
1235
1236 // Mark the old shared state as broken if it wasn't made ready yet.
1237 m_state->break_promise();
1238 m_state = std::move(new_state);
1239 }
1240
1241 private:
1244 template <typename U = R>
1245 typename std::enable_if<std::is_void<U>::value>::type invoke_and_set(
1246 Args&&... args) {
1247 m_function(std::forward<Args>(args)...);
1248 m_state->set_value();
1249 }
1250
1253 template <typename U = R>
1254 typename std::enable_if<!std::is_void<U>::value &&
1255 std::is_reference<U>::value>::type
1256 invoke_and_set(Args&&... args) {
1257 auto& r = m_function(std::forward<Args>(args)...);
1258 m_state->set_value(r);
1259 }
1260
1263 template <typename U = R>
1264 typename std::enable_if<!std::is_void<U>::value &&
1265 !std::is_reference<U>::value>::type
1266 invoke_and_set(Args&&... args) {
1267 m_state->set_value(m_function(std::forward<Args>(args)...));
1268 }
1269
1270 std::function<R(Args...)> m_function;
1271 std::shared_ptr<detail::future::shared_state<R>> m_state;
1272};
1273
1277template <typename R, typename... Args>
1278COOPSYNC_TBB_EXPORT void swap(packaged_task<R(Args...)>& lhs,
1279 packaged_task<R(Args...)>& rhs) noexcept {
1280 lhs.swap(rhs);
1281}
1282
1283} // namespace coopsync_tbb
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:577
T & get()
Retrieves the reference stored in the shared state. If the shared state is ready, this function retur...
Definition future.hpp:596
bool valid() const noexcept
Tests whether the future is valid and refers to a shared state.
Definition future.hpp:569
future() noexcept=default
Constructs a new future. After construction the future has no shared state and is not valid.
void wait() const
Suspends the current task until the shared state is ready.
Definition future.hpp:586
future() noexcept=default
Constructs a new future. After construction the future has no shared state and is not valid.
void get()
Waits for readiness and consumes the shared state. If the shared state is ready, this function return...
Definition future.hpp:517
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:499
bool valid() const noexcept
Tests whether the future is valid and refers to a shared state.
Definition future.hpp:491
void wait() const
Suspends the current task until the shared state is ready.
Definition future.hpp:508
Exception type thrown by future and promise operations.
Definition future.hpp:25
future_error(std::future_errc errc)
Constructs a future_error with the given error code.
Definition future.hpp:29
const std::error_code & code() const noexcept
Obtains the error code associated with this future_error.
Definition future.hpp:34
Future holding a shared state with a value type. Waiting or getting the value from a not ready state ...
Definition future.hpp:384
future() noexcept=default
Constructs a new future. After construction the future has no shared state and is not valid.
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:419
void wait() const
Suspends the current task until the shared state is ready. If the shared state is ready this function...
Definition future.hpp:425
std::remove_reference< T >::type get()
Retrieves the value stored in the shared state. If the shared state is ready, this function returns i...
Definition future.hpp:435
bool valid() const noexcept
Tests whether the future is valid and refers to a shared state.
Definition future.hpp:411
shared_future< T > share()
Converts this future into a shared_future. After this call, the current future is left in not valid s...
Definition future.hpp:856
bool valid() const noexcept
Tests whether the packaged_task has an associated shared state.
Definition future.hpp:1176
packaged_task & operator=(const packaged_task &)=delete
The packaged_task is not copy-assignable.
packaged_task(const packaged_task &)=delete
The packaged_task is not copy-constructible.
future< R > get_future()
Returns a future associated with the shared state. Can be called only once for a given packaged_task.
Definition future.hpp:1191
packaged_task()=default
Constructs a new packaged_task with no callable and no shared state. After construction the packaged_...
packaged_task & operator=(packaged_task &&other) noexcept
The packaged_task is move-assignable.
Definition future.hpp:1147
void operator()(Args... args)
Invokes the wrapped callable and in the shared state either obtained result or encountered exception....
Definition future.hpp:1214
void reset()
Resets the packaged_task to a valid state with an empty shared state. After this call,...
Definition future.hpp:1229
packaged_task(F f)
Constructs a new packaged_task with the given callable and an allocated shared state....
Definition future.hpp:1124
~packaged_task()
Destroys the packaged_task. If it has a shared state that is not ready, the shared state is marked as...
Definition future.hpp:1167
packaged_task(packaged_task &&other) noexcept
The packaged_task is move-constructible.
Definition future.hpp:1137
void swap(packaged_task &other) noexcept
Exchanges the shared states with another packaged_task.
Definition future.hpp:1180
void set_exception(std::exception_ptr p)
Stores an exception into the shared state and makes it ready.
Definition future.hpp:1099
void swap(promise &other) noexcept
Exchanges the shared states between two promises.
Definition future.hpp:1067
void set_value(T &v)
Stores a reference into the shared state and makes it ready.
Definition future.hpp:1090
promise()=default
Constructs a new promise with an empty shared state.
promise(const promise &)=delete
The promise is not copy-constructible.
promise & operator=(const promise &)=delete
The promise is not copy-assignable.
~promise()=default
Destroys the promise. If the shared state is not ready, it is marked as broken.
future< T & > get_future()
Returns a future associated with the shared state.
Definition future.hpp:1076
promise(const Alloc &alloc)
Constructs a new promise using the given allocator.
Definition future.hpp:1041
promise(promise &&other) noexcept=default
The promise is move-constructible.
promise & operator=(promise &&other) noexcept=default
The promise is move-assignable.
promise(promise &&other) noexcept=default
The promise is move-constructible.
future< void > get_future()
Returns a future associated with the shared state.
Definition future.hpp:1004
void set_exception(std::exception_ptr p)
Stores an exception into the shared state and makes it ready.
Definition future.hpp:1024
void set_value()
Marks the shared state as ready without storing a value.
Definition future.hpp:1015
promise()=default
Constructs a new promise with an empty shared state.
~promise()=default
Destroys the promise. If the shared state is not ready, it is marked as broken.
void swap(promise &other) noexcept
Exchanges the shared states between two promises.
Definition future.hpp:995
promise(const promise &)=delete
The promise is not copy-constructible.
promise & operator=(promise &&other) noexcept=default
The promise is move-assignable.
promise & operator=(const promise &)=delete
The promise is not copy-assignable.
promise(const Alloc &alloc)
Constructs a new promise using the given allocator.
Definition future.hpp:970
Promise holding a shared state with a value type. The promise is used to set the value in the shared ...
Definition future.hpp:877
~promise()=default
Destroys the promise. If the shared state is not ready, it is marked as broken.
void set_value(T &&v)
Stores a value into the shared state and makes it ready.
Definition future.hpp:944
promise & operator=(promise &&other) noexcept=default
The promise is move-assignable.
void swap(promise &other) noexcept
Exchanges the shared states between two promises.
Definition future.hpp:912
promise(promise &&other) noexcept=default
The promise is move-constructible.
promise(const promise &)=delete
The promise is not copy-constructible.
promise()=default
Constructs a new promise with an empty shared state.
promise(const Alloc &alloc)
Constructs a new promise with a shared state allocated using the given allocator.
Definition future.hpp:887
future< T > get_future()
Returns a future associated with the shared state.
Definition future.hpp:921
void set_exception(std::exception_ptr p)
Stores an exception into the shared state and makes it ready.
Definition future.hpp:953
promise & operator=(const promise &)=delete
The promise is not copy-assignable.
void set_value(const T &v)
Stores a value into the shared state and makes it ready.
Definition future.hpp:933
bool valid() const noexcept
Tests whether the shared_future is valid and refers to a shared state.
Definition future.hpp:811
shared_future() noexcept=default
Constructs the shared_future in a not valid state.
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:819
void wait() const
Suspends the current task until the shared state is ready. If the shared state is ready this function...
Definition future.hpp:827
T & get() const
Obtains a reference stored in the shared state. If the shared state is ready, this function returns i...
Definition future.hpp:838
void wait() const
Suspends the current task until the shared state is ready. If the shared state is ready this function...
Definition future.hpp:750
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:742
void get() const
Waits for readiness and checks for exception/broken promise. If the shared state is ready,...
Definition future.hpp:760
bool valid() const noexcept
Tests whether the shared_future is valid and refers to a shared state.
Definition future.hpp:735
shared_future() noexcept=default
Constructs the shared_future in a not valid state.
Copyable future that allows retrieving the result multiple times.
Definition future.hpp:630
const T & get() const
Obtains a reference to the value stored in the shared state. If the shared state is ready,...
Definition future.hpp:682
bool try_wait() const
Checks whether the shared state is ready without suspending.
Definition future.hpp:668
shared_future() noexcept=default
Constructs an invalid shared_future.
void wait() const
Suspends the current task until the shared state is ready.
Definition future.hpp:672
bool valid() const noexcept
Tests whether the shared_future is valid and refers to a shared state.
Definition future.hpp:661
Main namespace of the library, containing all public APIs.
Definition wait_for.hpp:11
COOPSYNC_TBB_EXPORT void swap(packaged_task< R(Args...)> &lhs, packaged_task< R(Args...)> &rhs) noexcept
Exchanges the states between two packaged_tasks.
Definition future.hpp:1278