18#ifndef INCLUDED_FUTURE
19#define INCLUDED_FUTURE
22#include <condition_variable>
30template<
typename Callback>
36 explicit StopToken(
const std::atomic<bool>& request) noexcept :
48template<
typename Callback>
49using CallbackResult =
typename std::conditional_t<std::is_invocable_v<Callback, StopToken>,
50 std::invoke_result<Callback, StopToken>, std::invoke_result<Callback>>::type;
55template<
typename ResultType>
61template<
typename ResultType>
85 std::unique_lock<std::mutex> lock(
m_Mutex);
107 std::get<std::exception_ptr>(
m_Outcome));
109 if (std::holds_alternative<std::exception_ptr>(
m_Outcome))
110 std::rethrow_exception(std::exchange(std::get<std::exception_ptr>(
m_Outcome), {}));
113 m_Outcome.template emplace<std::exception_ptr>();
115 if constexpr (std::is_void_v<ResultType>)
128 std::variant<std::exception_ptr, NonVoid<ResultType>>
m_Outcome;
134template<
typename Callback>
153template<
typename Callback>
170 if constexpr (std::is_void_v<ResultType>)
173 outcome.template emplace<FutureSharedStateDetail::VoidSubstitution>();
176 outcome.template emplace<ResultType>(
Invoke());
180 outcome.template emplace<std::exception_ptr>(std::current_exception());
189 std::lock_guard<std::mutex> lock(
m_SharedState->receiver.m_Mutex);
202 if constexpr (std::is_invocable_v<Callback, StopToken>)
208 std::shared_ptr<FutureSharedStateDetail::SharedState<Callback>>
m_SharedState;
222template<
typename ResultType>
242 template<
typename Callback,
typename... Args>
243 Future(
auto& taskManager, Callback&& callback, Args&&... args)
245 static_assert(std::is_same_v<CallbackResult<Callback>, ResultType>,
246 "The return type of the wrapped function is not the same as the type the Future expects.");
247 static_assert(std::is_invocable_v<Callback, StopToken> || !std::is_invocable_v<Callback, StopToken&>,
248 "Consider taking the `StopToken` by value");
250 auto temp = std::make_shared<FutureSharedStateDetail::SharedState<Callback>>(
251 std::forward<Callback>(callback));
273 return std::exchange(
m_Receiver,
nullptr)->GetResult();
308 std::shared_ptr<FutureSharedStateDetail::Receiver<ResultType>>
m_Receiver;
311template<
typename Callback,
typename... Args>
Future(auto &taskManager, Callback &&callback, Args &&... args) -> Future< CallbackResult< Callback > >
typename std::conditional_t< std::is_invocable_v< Callback, StopToken >, std::invoke_result< Callback, StopToken >, std::invoke_result< Callback > >::type CallbackResult
Definition: Future.h:50
Responsible for syncronization between the task and the receiving thread.
Definition: Future.h:63
std::condition_variable m_ConditionVariable
Definition: Future.h:126
std::atomic< bool > m_Done
Definition: Future.h:122
Receiver(Receiver &&)=delete
~Receiver()
Definition: Future.h:66
std::variant< std::exception_ptr, NonVoid< ResultType > > m_Outcome
Definition: Future.h:128
std::mutex m_Mutex
Definition: Future.h:125
Receiver(const Receiver &)=delete
void RequestStop() noexcept
Request the executing thread to stop as fast as possible.
Definition: Future.h:94
bool IsDone() const noexcept
Definition: Future.h:74
ResultType GetResult()
Move the result away from the shared state, mark the future invalid.
Definition: Future.h:102
std::atomic< bool > m_StopRequest
Definition: Future.h:124
void Wait()
Definition: Future.h:79
Corresponds to std::future.
Definition: Future.h:224
bool Valid() const
Definition: Future.h:287
ResultType Get()
Move the result out of the future, and invalidate the future.
Definition: Future.h:267
~Future()
Definition: Future.h:257
bool IsDone() const
Definition: Future.h:279
Future(Future &&)=default
Future & operator=(Future &&other)
Definition: Future.h:232
Future(auto &taskManager, Callback &&callback, Args &&... args)
Make the future wait for the result of callback.
Definition: Future.h:243
void Wait()
Definition: Future.h:292
void CancelOrWait()
Definition: Future.h:298
std::shared_ptr< FutureSharedStateDetail::Receiver< ResultType > > m_Receiver
Definition: Future.h:308
Future(const Future &o)=delete
Corresponds somewhat to std::packaged_task.
Definition: Future.h:155
CallbackResult< Callback > Invoke()
Definition: Future.h:200
std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > m_SharedState
Definition: Future.h:208
void operator()()
Definition: Future.h:162
PackagedTask(std::shared_ptr< FutureSharedStateDetail::SharedState< Callback > > ss)
Definition: Future.h:158
bool IsStopRequested() const noexcept
Definition: Future.h:40
const std::atomic< bool > & m_Request
Definition: Future.h:45
StopToken(const std::atomic< bool > &request) noexcept
Definition: Future.h:36
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
std::conditional_t< std::is_void_v< ResultType >, VoidSubstitution, ResultType > NonVoid
Definition: Future.h:56
Definition: ShaderDefines.cpp:43
The shared state between futures and packaged state.
Definition: Future.h:136
Callback callback
Definition: Future.h:141
SharedState(Callback &&callbackFunc)
Definition: Future.h:137
Receiver< CallbackResult< Callback > > receiver
Definition: Future.h:142