3 #include "Impl/AsyncSystemSchedulers.h"
4 #include "Impl/CatchFunction.h"
5 #include "Impl/ContinuationFutureType.h"
6 #include "Impl/WithTracing.h"
7 #include "SharedFuture.h"
8 #include "ThreadPool.h"
10 #include <CesiumUtility/Tracing.h>
16 namespace CesiumImpl {
29 template <
typename T>
class Future final {
35 : _pSchedulers(std::move(rhs._pSchedulers)),
36 _task(std::move(rhs._task)) {}
39 this->_pSchedulers = std::move(rhs._pSchedulers);
40 this->_task = std::move(rhs._task);
44 Future(
const Future<T>& rhs) =
delete;
45 Future<T>& operator=(
const Future<T>& rhs) =
delete;
65 template <
typename Func>
66 CesiumImpl::ContinuationFutureType_t<Func, T>
68 return std::move(*this).thenWithScheduler(
69 this->_pSchedulers->workerThread.immediate,
70 "waiting for worker thread",
71 std::forward<Func>(f));
92 template <
typename Func>
94 return std::move(*this).thenWithScheduler(
95 this->_pSchedulers->mainThread.immediate,
96 "waiting for main thread",
97 std::forward<Func>(f));
116 template <
typename Func>
118 return CesiumImpl::ContinuationFutureType_t<Func, T>(
121 async::inline_scheduler(),
122 CesiumImpl::WithTracing<T>::end(
nullptr, std::forward<Func>(f))));
143 template <
typename Func>
144 CesiumImpl::ContinuationFutureType_t<Func, T>
146 return std::move(*this).thenWithScheduler(
147 threadPool._pScheduler->immediate,
148 "waiting for thread pool thread",
149 std::forward<Func>(f));
174 return std::move(*this).catchWithScheduler(
175 this->_pSchedulers->mainThread.immediate,
176 std::forward<Func>(f));
199 return std::move(*this).catchWithScheduler(
200 async::inline_scheduler(),
201 std::forward<Func>(f));
216 template <
typename... TPassThrough>
217 Future<std::tuple<TPassThrough..., T>>
219 return std::move(*this).thenImmediately(
220 [values = std::tuple(std::forward<TPassThrough>(values)...)](
221 T&& result)
mutable {
222 return std::tuple_cat(
224 std::make_tuple(std::move(result)));
241 T
wait() {
return this->_task.get(); }
258 return this->_pSchedulers->mainThread.dispatchUntilTaskCompletes(
259 std::move(this->_task));
272 bool isReady()
const {
return this->_task.ready(); }
289 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
290 async::task<T>&& task) noexcept
291 : _pSchedulers(pSchedulers), _task(std::move(task)) {}
293 template <
typename Func,
typename Scheduler>
294 CesiumImpl::ContinuationFutureType_t<Func, T> thenWithScheduler(
295 Scheduler& scheduler,
296 const char* tracingName,
302 #if CESIUM_TRACING_ENABLED
305 auto task = this->_task.then(
306 async::inline_scheduler(),
307 CesiumImpl::WithTracing<T>::begin(tracingName, std::forward<Func>(f)));
309 auto& task = this->_task;
312 return CesiumImpl::ContinuationFutureType_t<Func, T>(
316 CesiumImpl::WithTracing<T>::end(
318 std::forward<Func>(f))));
321 template <
typename Func,
typename Scheduler>
322 CesiumImpl::ContinuationFutureType_t<Func, std::exception>
323 catchWithScheduler(Scheduler& scheduler, Func&& f) && {
324 return CesiumImpl::ContinuationFutureType_t<Func, std::exception>(
327 async::inline_scheduler(),
328 CesiumImpl::CatchFunction<Func, T, Scheduler>{
330 std::forward<Func>(f)}));
333 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
334 async::task<T> _task;
336 friend class AsyncSystem;
338 template <
typename R>
friend struct CesiumImpl::ParameterizedTaskUnwrapper;
340 friend struct CesiumImpl::TaskUnwrapper;
342 template <
typename R>
friend class Future;
343 template <
typename R>
friend class SharedFuture;
344 template <
typename R>
friend class Promise;
A value that will be available in the future, as produced by AsyncSystem.
CesiumImpl::ContinuationFutureType_t< Func, T > thenInWorkerThread(Func &&f) &&
Registers a continuation function to be invoked in a worker thread when this Future resolves,...
CesiumImpl::ContinuationFutureType_t< Func, T > thenInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future resolves,...
Future< T > catchInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future rejects,...
Future(Future< T > &&rhs) noexcept
Move constructor.
Future< T > catchImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately, and invalidates this Future.
Future< std::tuple< TPassThrough..., T > > thenPassThrough(TPassThrough &&... values) &&
Passes through one or more additional values to the next continuation.
T wait()
Waits for the future to resolve or reject and returns the result.
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,...
SharedFuture< T > share() &&
Creates a version of this future that can be shared, meaning that its value may be accessed multiple ...
T waitInMainThread()
Waits for this future to resolve or reject in the main thread while also processing main-thread tasks...
bool isReady() const
Determines if this future is already resolved or rejected.
CesiumImpl::ContinuationFutureType_t< Func, T > thenImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately in whichever thread causes the Future to ...
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.