cesium-native 0.43.0
Loading...
Searching...
No Matches
Future.h
1#pragma once
2
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"
9
10#include <CesiumUtility/Tracing.h>
11
12#include <variant>
13
14namespace CesiumAsync {
15
16namespace CesiumImpl {
17
18template <typename R> struct ParameterizedTaskUnwrapper;
19struct TaskUnwrapper;
20
21} // namespace CesiumImpl
22
29template <typename T> class Future final {
30public:
34 Future(Future<T>&& rhs) noexcept
35 : _pSchedulers(std::move(rhs._pSchedulers)),
36 _task(std::move(rhs._task)) {}
37
41 Future<T>& operator=(Future<T>&& rhs) noexcept {
42 this->_pSchedulers = std::move(rhs._pSchedulers);
43 this->_task = std::move(rhs._task);
44 return *this;
45 }
46
47 Future(const Future<T>& rhs) = delete;
48 Future<T>& operator=(const Future<T>& rhs) = delete;
49
68 template <typename Func>
69 CesiumImpl::ContinuationFutureType_t<Func, T>
70 thenInWorkerThread(Func&& f) && {
71 return std::move(*this).thenWithScheduler(
72 this->_pSchedulers->workerThread.immediate,
73 "waiting for worker thread",
74 std::forward<Func>(f));
75 }
76
95 template <typename Func>
96 CesiumImpl::ContinuationFutureType_t<Func, T> thenInMainThread(Func&& f) && {
97 return std::move(*this).thenWithScheduler(
98 this->_pSchedulers->mainThread.immediate,
99 "waiting for main thread",
100 std::forward<Func>(f));
101 }
102
119 template <typename Func>
120 CesiumImpl::ContinuationFutureType_t<Func, T> thenImmediately(Func&& f) && {
121 return CesiumImpl::ContinuationFutureType_t<Func, T>(
122 this->_pSchedulers,
123 _task.then(
124 async::inline_scheduler(),
125 CesiumImpl::WithTracing<T>::end(nullptr, std::forward<Func>(f))));
126 }
127
147 template <typename Func>
148 CesiumImpl::ContinuationFutureType_t<Func, T>
149 thenInThreadPool(const ThreadPool& threadPool, Func&& f) && {
150 return std::move(*this).thenWithScheduler(
151 threadPool._pScheduler->immediate,
152 "waiting for thread pool thread",
153 std::forward<Func>(f));
154 }
155
177 template <typename Func> Future<T> catchInMainThread(Func&& f) && {
178 return std::move(*this).catchWithScheduler(
179 this->_pSchedulers->mainThread.immediate,
180 std::forward<Func>(f));
181 }
182
202 template <typename Func> Future<T> catchImmediately(Func&& f) && {
203 return std::move(*this).catchWithScheduler(
204 async::inline_scheduler(),
205 std::forward<Func>(f));
206 }
207
220 template <typename... TPassThrough>
222 thenPassThrough(TPassThrough&&... values) && {
223 return std::move(*this).thenImmediately(
224 [values = std::tuple(std::forward<TPassThrough>(values)...)](
225 T&& result) mutable {
226 return std::tuple_cat(
227 std::move(values),
228 std::make_tuple(std::move(result)));
229 });
230 }
231
245 T wait() { return this->_task.get(); }
246
262 return this->_pSchedulers->mainThread.dispatchUntilTaskCompletes(
263 std::move(this->_task));
264 }
265
276 bool isReady() const { return this->_task.ready(); }
277
288 return SharedFuture<T>(this->_pSchedulers, this->_task.share());
289 }
290
291private:
292 Future(
293 const std::shared_ptr<CesiumImpl::AsyncSystemSchedulers>& pSchedulers,
294 async::task<T>&& task) noexcept
295 : _pSchedulers(pSchedulers), _task(std::move(task)) {}
296
297 template <typename Func, typename Scheduler>
298 CesiumImpl::ContinuationFutureType_t<Func, T> thenWithScheduler(
299 Scheduler& scheduler,
300 const char* tracingName,
301 Func&& f) && {
302 // It would be nice if tracingName were a template parameter instead of a
303 // function parameter, but that triggers a bug in VS2017. It was previously
304 // a bug in VS2019, too, but has been fixed there:
305 // https://developercommunity.visualstudio.com/t/internal-compiler-error-when-compiling-a-template-1/534210
306#if CESIUM_TRACING_ENABLED
307 // When tracing is enabled, we measure the time between scheduling and
308 // dispatching of the work.
309 auto task = this->_task.then(
310 async::inline_scheduler(),
311 CesiumImpl::WithTracing<T>::begin(tracingName, std::forward<Func>(f)));
312#else
313 auto& task = this->_task;
314#endif
315
316 return CesiumImpl::ContinuationFutureType_t<Func, T>(
317 this->_pSchedulers,
318 task.then(
319 scheduler,
320 CesiumImpl::WithTracing<T>::end(
321 tracingName,
322 std::forward<Func>(f))));
323 }
324
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>(
329 this->_pSchedulers,
330 this->_task.then(
331 async::inline_scheduler(),
332 CesiumImpl::CatchFunction<Func, T, Scheduler>{
333 scheduler,
334 std::forward<Func>(f)}));
335 }
336
337 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
338 async::task<T> _task;
339
340 friend class AsyncSystem;
341
342 template <typename R> friend struct CesiumImpl::ParameterizedTaskUnwrapper;
343
344 friend struct CesiumImpl::TaskUnwrapper;
345
346 template <typename R> friend class Future;
347 template <typename R> friend class SharedFuture;
348 template <typename R> friend class Promise;
349};
350
351} // namespace CesiumAsync
A value that will be available in the future, as produced by AsyncSystem.
Definition Promise.h:11
CesiumImpl::ContinuationFutureType_t< Func, T > thenInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future resolves,...
Definition Future.h:96
SharedFuture< T > share() &&
Creates a version of this future that can be shared, meaning that its value may be accessed multiple ...
Definition Future.h:287
Future< T > & operator=(Future< T > &&rhs) noexcept
Move assignment operator.
Definition Future.h:41
CesiumImpl::ContinuationFutureType_t< Func, T > thenImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately in whichever thread causes the Future to ...
Definition Future.h:120
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,...
Definition Future.h:149
Future(Future< T > &&rhs) noexcept
Move constructor.
Definition Future.h:34
CesiumImpl::ContinuationFutureType_t< Func, T > thenInWorkerThread(Func &&f) &&
Registers a continuation function to be invoked in a worker thread when this Future resolves,...
Definition Future.h:70
Future< T > catchImmediately(Func &&f) &&
Registers a continuation function to be invoked immediately, and invalidates this Future.
Definition Future.h:202
T wait()
Waits for the future to resolve or reject and returns the result.
Definition Future.h:245
T waitInMainThread()
Waits for this future to resolve or reject in the main thread while also processing main-thread tasks...
Definition Future.h:261
Future< T > catchInMainThread(Func &&f) &&
Registers a continuation function to be invoked in the main thread when this Future rejects,...
Definition Future.h:177
bool isReady() const
Determines if this future is already resolved or rejected.
Definition Future.h:276
Future< std::tuple< std::remove_cvref_t< TPassThrough >..., T > > thenPassThrough(TPassThrough &&... values) &&
Passes through one or more additional values to the next continuation.
Definition Future.h:222
A value that will be available in the future, as produced by AsyncSystem. Unlike Future,...
A thread pool created by AsyncSystem::createThreadPool.
Definition ThreadPool.h:18
Classes that support asynchronous operations.