3#include "Impl/AsyncSystemSchedulers.h"
4#include "Impl/CatchFunction.h"
5#include "Impl/ContinuationFutureType.h"
6#include "Impl/WithTracing.h"
7#include "SharedFuture.h"
10#include <CesiumUtility/Tracing.h>
18template <
typename R>
struct ParameterizedTaskUnwrapper;
29template <
typename T>
class Future final {
35 : _pSchedulers(std::move(rhs._pSchedulers)),
36 _task(std::move(rhs._task)) {}
42 this->_pSchedulers = std::move(rhs._pSchedulers);
43 this->_task = std::move(rhs._task);
68 template <
typename Func>
69 CesiumImpl::ContinuationFutureType_t<Func, T>
71 return std::move(*this).thenWithScheduler(
72 this->_pSchedulers->workerThread.immediate,
73 "waiting for worker thread",
74 std::forward<Func>(f));
95 template <
typename Func>
97 return std::move(*this).thenWithScheduler(
98 this->_pSchedulers->mainThread.immediate,
99 "waiting for main thread",
100 std::forward<Func>(f));
119 template <
typename Func>
121 return CesiumImpl::ContinuationFutureType_t<Func, T>(
124 async::inline_scheduler(),
125 CesiumImpl::WithTracing<T>::end(
nullptr, std::forward<Func>(f))));
147 template <
typename Func>
148 CesiumImpl::ContinuationFutureType_t<Func, T>
150 return std::move(*this).thenWithScheduler(
151 threadPool._pScheduler->immediate,
152 "waiting for thread pool thread",
153 std::forward<Func>(f));
178 return std::move(*this).catchWithScheduler(
179 this->_pSchedulers->mainThread.immediate,
180 std::forward<Func>(f));
203 return std::move(*this).catchWithScheduler(
204 async::inline_scheduler(),
205 std::forward<Func>(f));
220 template <
typename... TPassThrough>
223 return std::move(*this).thenImmediately(
224 [values = std::tuple(std::forward<TPassThrough>(values)...)](
225 T&& result)
mutable {
226 return std::tuple_cat(
228 std::make_tuple(std::move(result)));
245 T
wait() {
return this->_task.get(); }
262 return this->_pSchedulers->mainThread.dispatchUntilTaskCompletes(
263 std::move(this->_task));
276 bool isReady()
const {
return this->_task.ready(); }
293 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
294 async::task<T>&& task) noexcept
295 : _pSchedulers(pSchedulers), _task(std::move(task)) {}
297 template <
typename Func,
typename Scheduler>
298 CesiumImpl::ContinuationFutureType_t<Func, T> thenWithScheduler(
299 Scheduler& scheduler,
300 const char* tracingName,
306#if CESIUM_TRACING_ENABLED
309 auto task = this->_task.then(
310 async::inline_scheduler(),
311 CesiumImpl::WithTracing<T>::begin(tracingName, std::forward<Func>(f)));
313 auto& task = this->_task;
316 return CesiumImpl::ContinuationFutureType_t<Func, T>(
320 CesiumImpl::WithTracing<T>::end(
322 std::forward<Func>(f))));
325 template <
typename Func,
typename Scheduler>
326 CesiumImpl::ContinuationFutureType_t<Func, std::exception>
327 catchWithScheduler(Scheduler& scheduler, Func&& f) && {
328 return CesiumImpl::ContinuationFutureType_t<Func, std::exception>(
331 async::inline_scheduler(),
332 CesiumImpl::CatchFunction<Func, T, Scheduler>{
334 std::forward<Func>(f)}));
337 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
338 async::task<T> _task;
340 friend class AsyncSystem;
342 template <
typename R>
friend struct CesiumImpl::ParameterizedTaskUnwrapper;
344 friend struct CesiumImpl::TaskUnwrapper;
346 template <
typename R>
friend class Future;
347 template <
typename R>
friend class SharedFuture;
348 template <
typename R>
friend class Promise;
A value that will be available in the future, as produced by AsyncSystem.
CesiumImpl::ContinuationFutureType_t< Func, T > thenInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future resolves,...
SharedFuture< T > share() &&
Creates a version of this future that can be shared, meaning that its value may be accessed multiple ...
Future< T > & operator=(Future< T > &&rhs) noexcept
Move assignment operator.
CesiumImpl::ContinuationFutureType_t< Func, T > thenImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately in whichever thread causes the Future to ...
CesiumImpl::ContinuationFutureType_t< Func, T > thenInThreadPool(const ThreadPool &threadPool, Func &&f) &&
Registers a continuation function to be invoked in a thread pool when this Future resolves,...
Future(Future< T > &&rhs) noexcept
Move constructor.
CesiumImpl::ContinuationFutureType_t< Func, T > thenInWorkerThread(Func &&f) &&
Registers a continuation function to be invoked in a worker thread when this Future resolves,...
Future< T > catchImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately, and invalidates this Future.
T wait()
Waits for the future to resolve or reject and returns the result.
T waitInMainThread()
Waits for this future to resolve or reject in the main thread while also processing main-thread tasks...
Future< T > catchInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future rejects,...
bool isReady() const
Determines if this future is already resolved or rejected.
Future< std::tuple< std::remove_cvref_t< TPassThrough >..., T > > thenPassThrough(TPassThrough &&... values) &&
Passes through one or more additional values to the next continuation.
A value that will be available in the future, as produced by AsyncSystem. Unlike Future,...
A thread pool created by AsyncSystem::createThreadPool.
Classes that support asynchronous operations.