15#include <system_error>
19#include "coopsync_tbb/detail/macros.hpp"
20#include "coopsync_tbb/detail/wait_queue.hpp"
34 const std::error_code&
code() const noexcept {
return m_errc; }
38 : logic_error(errc.message()), m_errc(std::move(errc)) {}
39 std::error_code m_errc;
46enum class status :
unsigned char {
56struct shared_state_base {
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;
64 ~shared_state_base() { assert(waiters.empty()); }
66 detail::wait_queue waiters;
68 std::atomic<status> state{status::empty};
69 std::atomic<bool> future_obtained{
false};
70 std::exception_ptr exception;
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);
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);
86 exception = std::move(p);
87 state.store(status::exception, std::memory_order_release);
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)) {
102 waiters.wait_if([
this] {
return !ready(); });
108struct shared_state :
public shared_state_base {
110 class optional_storage {
112 optional_storage() noexcept = default;
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;
119 ~optional_storage() {
125 template <
typename... Args>
126 void emplace(Args&&... args) {
131 ::new (std::addressof(get())) T(std::forward<Args>(args)...);
135 bool has_value() const noexcept {
return m_has_value; }
139 return m_storage.value;
141 const T& get() const noexcept {
143 return m_storage.value;
146 T& operator*() noexcept {
return get(); }
147 const T& operator*() const noexcept {
return get(); }
150 void destroy() noexcept {
155 union storage_union {
156 storage_union() noexcept {}
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;
165 storage_union m_storage = {};
166 bool m_has_value =
false;
169 optional_storage value;
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);
181 value.emplace(std::forward<R>(v));
182 state.store(status::value, std::memory_order_release);
184 exception = std::current_exception();
185 state.store(status::exception, std::memory_order_release);
187 waiters.resume_all();
192struct shared_state<void> :
public shared_state_base {
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);
202 waiters.resume_all();
207struct shared_state<T&> :
public shared_state_base {
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);
219 value = std::addressof(v);
220 state.store(status::value, std::memory_order_release);
222 exception = std::current_exception();
223 state.store(status::exception, std::memory_order_release);
225 waiters.resume_all();
233 std::shared_ptr<shared_state<T>> m_state;
235 future_base() noexcept = default;
236 explicit future_base(std::shared_ptr<shared_state<T>> state)
237 : m_state(std::move(state)) {}
241 void ensure_valid()
const {
243 throw future_error(std::future_errc::no_state);
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);
257 if (st == status::broken) {
258 throw future_error(std::future_errc::broken_promise);
260 assert(st == status::value);
264 void reset() noexcept { m_state.reset(); }
268 COOPSYNC_TBB_NODISCARD std::shared_ptr<shared_state<T>>
269 release_state() noexcept {
271 return std::move(m_state);
278 bool valid() const noexcept {
return static_cast<bool>(m_state); }
284 bool try_wait()
const {
286 return m_state->ready();
304 std::shared_ptr<shared_state<T>> m_state;
307 promise_base() : m_state(std::make_shared<shared_state<T>>()) {}
311 template <
typename Alloc>
312 explicit promise_base(
const Alloc& alloc)
313 : m_state(std::allocate_shared<shared_state<T>>(alloc)) {}
317 void ensure_valid()
const {
319 throw future_error(std::future_errc::no_state);
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);
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;
346 m_state->break_promise();
352 void swap(promise_base& other)
noexcept { m_state.swap(other.m_state); }
358 void set_exception(std::exception_ptr p) {
360 m_state->set_exception(std::move(p));
384class COOPSYNC_TBB_EXPORT
future :
private detail::future::future_base<T> {
412 return detail::future::future_base<T>::valid();
419 bool try_wait()
const {
return detail::future::future_base<T>::try_wait(); }
425 void wait()
const { detail::future::future_base<T>::wait(); }
435 typename std::remove_reference<T>::type
get() {
436 this->ensure_valid();
437 this->m_state->wait();
438 this->throw_if_exception_or_broken();
440 assert(this->m_state->value.has_value());
441 auto out = std::move(*this->m_state->value);
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)) {}
463class COOPSYNC_TBB_EXPORT
464 future<void> :
private detail::future::future_base<void> {
492 return detail::future::future_base<void>::valid();
500 return detail::future::future_base<void>::try_wait();
508 void wait()
const { detail::future::future_base<void>::wait(); }
518 this->ensure_valid();
519 this->m_state->wait();
520 this->throw_if_exception_or_broken();
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)) {}
542class COOPSYNC_TBB_EXPORT
future<T&> :
private detail::future::future_base<T&> {
570 return detail::future::future_base<T&>::valid();
578 return detail::future::future_base<T&>::try_wait();
586 void wait()
const { detail::future::future_base<T&>::wait(); }
597 this->ensure_valid();
598 this->m_state->wait();
599 this->throw_if_exception_or_broken();
601 assert(this->m_state->value !=
nullptr);
602 T* out = this->m_state->value;
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)) {}
630 :
private detail::future::future_base<T> {
662 return detail::future::future_base<T>::valid();
668 bool try_wait()
const {
return detail::future::future_base<T>::try_wait(); }
672 void wait()
const { detail::future::future_base<T>::wait(); }
683 this->ensure_valid();
684 this->m_state->wait();
685 this->throw_if_exception_or_broken();
686 assert(this->m_state->value.has_value());
687 return *this->m_state->value;
691 template <
typename U>
694 std::shared_ptr<detail::future::shared_state<T>> state)
695 : detail::
future::future_base<T>(std::move(state)) {}
700class COOPSYNC_TBB_EXPORT
701 shared_future<void> :
private detail::future::future_base<void> {
736 return detail::future::future_base<void>::valid();
743 return detail::future::future_base<void>::try_wait();
750 void wait()
const { detail::future::future_base<void>::wait(); }
761 this->ensure_valid();
762 this->m_state->wait();
763 this->throw_if_exception_or_broken();
767 template <
typename U>
770 std::shared_ptr<detail::future::shared_state<void>> state)
771 : detail::
future::future_base<void>(std::move(state)) {}
776class COOPSYNC_TBB_EXPORT
777 shared_future<T&> :
private detail::future::future_base<T&> {
812 return detail::future::future_base<T&>::valid();
820 return detail::future::future_base<T&>::try_wait();
827 void wait()
const { detail::future::future_base<T&>::wait(); }
839 this->ensure_valid();
840 this->m_state->wait();
841 this->throw_if_exception_or_broken();
842 assert(this->m_state->value !=
nullptr);
843 return *this->m_state->value;
847 template <
typename U>
851 std::shared_ptr<detail::future::shared_state<T&>> state)
852 : detail::
future::future_base<T&>(std::move(state)) {}
857 this->ensure_valid();
862 this->ensure_valid();
868 this->ensure_valid();
877class COOPSYNC_TBB_EXPORT
promise :
private detail::future::promise_base<T> {
886 template <
typename Alloc>
888 : detail::
future::promise_base<T>(alloc) {}
913 detail::future::promise_base<T>::swap(other);
922 this->ensure_valid();
923 this->mark_future_obtained();
934 this->ensure_valid();
935 this->m_state->set_value(v);
945 this->ensure_valid();
946 this->m_state->set_value(std::move(v));
954 detail::future::promise_base<T>::set_exception(std::move(p));
961class COOPSYNC_TBB_EXPORT
962 promise<void> :
private detail::future::promise_base<void> {
969 template <
typename Alloc>
971 : detail::
future::promise_base<void>(alloc) {}
996 detail::future::promise_base<void>::swap(other);
1005 this->ensure_valid();
1006 this->mark_future_obtained();
1016 this->ensure_valid();
1017 this->m_state->set_value();
1025 detail::future::promise_base<void>::set_exception(std::move(p));
1031template <
typename T>
1032class COOPSYNC_TBB_EXPORT
1033 promise<T&> :
private detail::future::promise_base<T&> {
1040 template <
typename Alloc>
1042 : detail::
future::promise_base<T&>(alloc) {}
1054 promise&& other)
noexcept =
default;
1068 detail::future::promise_base<T&>::swap(other);
1077 this->ensure_valid();
1078 this->mark_future_obtained();
1091 this->ensure_valid();
1092 this->m_state->set_value(v);
1100 detail::future::promise_base<T&>::set_exception(std::move(p));
1110template <
typename R,
typename... Args>
1123 template <
typename F>
1125 : m_function(std::move(f)),
1126 m_state(std::make_shared<detail::
future::shared_state<R>>()) {}
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();
1148 if (
this != &other) {
1152 m_state->break_promise();
1156 m_function = std::move(other.m_function);
1157 m_state = std::move(other.m_state);
1159 other.m_function = {};
1160 other.m_state.reset();
1169 m_state->break_promise();
1176 bool valid() const noexcept {
return static_cast<bool>(m_state); }
1182 swap(m_function, other.m_function);
1183 swap(m_state, other.m_state);
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);
1220 invoke_and_set(std::forward<Args>(args)...);
1222 m_state->set_exception(std::current_exception());
1234 auto new_state = std::make_shared<detail::future::shared_state<R>>();
1237 m_state->break_promise();
1238 m_state = std::move(new_state);
1244 template <
typename U = R>
1245 typename std::enable_if<std::is_void<U>::value>::type invoke_and_set(
1247 m_function(std::forward<Args>(args)...);
1248 m_state->set_value();
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);
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)...));
1270 std::function<R(Args...)> m_function;
1271 std::shared_ptr<detail::future::shared_state<R>> m_state;
1277template <
typename R,
typename... Args>
1278COOPSYNC_TBB_EXPORT
void swap(packaged_task<R(Args...)>& lhs,
1279 packaged_task<R(Args...)>& rhs)
noexcept {
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