3 #include "Impl/AsyncSystemSchedulers.h"
4 #include "Impl/CatchFunction.h"
5 #include "Impl/ContinuationFutureType.h"
6 #include "Impl/WithTracing.h"
7 #include "ThreadPool.h"
9 #include <CesiumUtility/Tracing.h>
11 #include <type_traits>
16 namespace CesiumImpl {
18 template <
typename R>
struct ParameterizedTaskUnwrapper;
51 template <
typename Func>
53 return this->thenWithScheduler(
54 this->_pSchedulers->workerThread.immediate,
55 "waiting for worker thread",
56 std::forward<Func>(f));
77 template <
typename Func>
79 return this->thenWithScheduler(
80 this->_pSchedulers->mainThread.immediate,
81 "waiting for main thread",
82 std::forward<Func>(f));
100 template <
typename Func>
102 return CesiumImpl::ContinuationFutureType_t<Func, T>(
105 async::inline_scheduler(),
106 CesiumImpl::WithTracingShared<T>::end(
108 std::forward<Func>(f))));
129 template <
typename Func>
130 CesiumImpl::ContinuationFutureType_t<Func, T>
132 return this->thenWithScheduler(
133 threadPool._pScheduler->immediate,
134 "waiting for thread pool thread",
135 std::forward<Func>(f));
160 return this->catchWithScheduler(
161 this->_pSchedulers->mainThread.immediate,
162 std::forward<Func>(f));
185 return this->catchWithScheduler(
186 async::inline_scheduler(),
187 std::forward<Func>(f));
202 template <
typename... TPassThrough>
203 Future<std::tuple<TPassThrough..., T>>
206 [values = std::tuple(std::forward<TPassThrough>(values)...)](
207 const T& result)
mutable {
208 return std::tuple_cat(std::move(values), std::make_tuple(result));
227 std::enable_if_t<std::is_same_v<U, T>,
int> = 0,
228 std::enable_if_t<!std::is_same_v<U, void>,
int> = 0>
230 return this->_task.get();
235 std::enable_if_t<std::is_same_v<U, T>,
int> = 0,
236 std::enable_if_t<std::is_same_v<U, void>,
int> = 0>
256 return this->_pSchedulers->mainThread.dispatchUntilTaskCompletes(
257 std::move(this->_task));
270 bool isReady()
const {
return this->_task.ready(); }
274 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
275 async::shared_task<T>&& task) noexcept
276 : _pSchedulers(pSchedulers), _task(std::move(task)) {}
278 template <
typename Func,
typename Scheduler>
279 CesiumImpl::ContinuationFutureType_t<Func, T>
280 thenWithScheduler(Scheduler& scheduler,
const char* tracingName, Func&& f) {
285 #if CESIUM_TRACING_ENABLED
288 auto task = this->_task.then(
289 async::inline_scheduler(),
290 CesiumImpl::WithTracingShared<T>::begin(
292 std::forward<Func>(f)));
294 auto& task = this->_task;
297 return CesiumImpl::ContinuationFutureType_t<Func, T>(
301 CesiumImpl::WithTracingShared<T>::end(
303 std::forward<Func>(f))));
306 template <
typename Func,
typename Scheduler>
307 CesiumImpl::ContinuationFutureType_t<Func, std::exception>
308 catchWithScheduler(Scheduler& scheduler, Func&& f) {
309 return CesiumImpl::ContinuationFutureType_t<Func, std::exception>(
312 async::inline_scheduler(),
314 CatchFunction<Func, T, Scheduler,
const async::shared_task<T>&>{
316 std::forward<Func>(f)}));
319 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
320 async::shared_task<T> _task;
322 friend class AsyncSystem;
324 template <
typename R>
friend struct CesiumImpl::ParameterizedTaskUnwrapper;
326 friend struct CesiumImpl::TaskUnwrapper;
328 template <
typename R>
friend class Future;
329 template <
typename R>
friend class SharedFuture;
A value that will be available in the future, as produced by AsyncSystem.
A value that will be available in the future, as produced by AsyncSystem. Unlike Future,...
CesiumImpl::ContinuationFutureType_t< Func, T > thenImmediately(Func &&f)
Registers a continuation function to be invoked immediately in whichever thread causes the Future to ...
bool isReady() const
Determines if this future is already resolved or rejected.
T waitInMainThread()
Waits for this future to resolve or reject in the main thread while also processing main-thread tasks...
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 > catchInMainThread(Func &&f)
Registers a continuation function to be invoked in the main thread when this Future rejects.
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.
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.
CesiumImpl::ContinuationFutureType_t< Func, T > thenInMainThread(Func &&f)
Registers a continuation function to be invoked in the main thread when this Future resolves.
const U & wait() const
Waits for the future to resolve or reject and returns the result.
A thread pool created by AsyncSystem::createThreadPool.
Classes that support asynchronous operations.