3#include "Impl/ContinuationFutureType.h"
4#include "Impl/RemoveFuture.h"
5#include "Impl/WithTracing.h"
6#include "Impl/cesium-async++.h"
8#include <CesiumAsync/Future.h>
9#include <CesiumAsync/Library.h>
10#include <CesiumAsync/Promise.h>
11#include <CesiumAsync/ThreadPool.h>
12#include <CesiumUtility/Tracing.h>
13#include <CesiumUtility/transformTuple.h>
45 AsyncSystem(
const std::shared_ptr<ITaskProcessor>& pTaskProcessor)
noexcept;
66 template <
typename T,
typename Func> Future<T>
createFuture(Func&& f)
const {
67 std::shared_ptr<async::event_task<T>> pEvent =
68 std::make_shared<async::event_task<T>>();
70 Promise<T> promise(this->_pSchedulers, pEvent);
75 promise.reject(std::current_exception());
78 return Future<T>(this->_pSchedulers, pEvent->get_task());
97 std::make_shared<async::event_task<T>>());
115 template <
typename Func>
116 CesiumImpl::ContinuationFutureType_t<Func, void>
118 static const char* tracingName =
"waiting for worker thread";
120 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
122 return CesiumImpl::ContinuationFutureType_t<Func, void>(
125 this->_pSchedulers->workerThread.immediate,
126 CesiumImpl::WithTracing<void>::end(
128 std::forward<Func>(f))));
145 template <
typename Func>
146 CesiumImpl::ContinuationFutureType_t<Func, void>
148 static const char* tracingName =
"waiting for main thread";
150 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
152 return CesiumImpl::ContinuationFutureType_t<Func, void>(
155 this->_pSchedulers->mainThread.immediate,
156 CesiumImpl::WithTracing<void>::end(
158 std::forward<Func>(f))));
170 template <
typename Func>
171 CesiumImpl::ContinuationFutureType_t<Func, void>
173 static const char* tracingName =
"waiting for thread pool";
175 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
177 return CesiumImpl::ContinuationFutureType_t<Func, void>(
180 threadPool._pScheduler->immediate,
181 CesiumImpl::WithTracing<void>::end(
183 std::forward<Func>(f))));
194 template <
typename T>
196 std::conditional_t<std::is_void_v<T>, void, std::vector<T>>;
220 template <
typename T>
221 Future<AllValueType<T>>
all(std::vector<Future<T>>&& futures)
const {
223 std::forward<std::vector<Future<T>>>(futures));
248 template <
typename T>
251 std::forward<std::vector<SharedFuture<T>>>(futures));
263 template <
typename... Futures>
265 std::tuple<typename CesiumImpl::RemoveFuture<Futures>::type...>;
290 template <
typename... Futures>
294 auto getTaskFromFuture = [](
auto&& future) {
295 return std::move(future._task);
298 using AggregateTaskType = std::tuple<
299 async::task<typename CesiumImpl::RemoveFuture<Futures>::type>...>;
302 async::when_all(getTaskFromFuture(std::forward<Futures>(futures))...)
303 .then(async::inline_scheduler(), [](AggregateTaskType&& tasks) {
306 [](
auto&& task) {
return task.get(); });
324 async::make_task<T>(std::forward<T>(value)));
333 return Future<void>(this->_pSchedulers, async::make_task());
362 CesiumImpl::QueuedScheduler>::SchedulerScope;
396 template <
typename T,
typename TFutureType>
397 Future<AllValueType<T>>
all(std::vector<TFutureType>&& futures)
const {
398 using TTaskType =
decltype(TFutureType::_task);
399 std::vector<TTaskType> tasks;
400 tasks.reserve(futures.size());
402 for (
auto it = futures.begin(); it != futures.end(); ++it) {
403 tasks.emplace_back(std::move(it->_task));
408 async::task<AllValueType<T>> task =
409 async::when_all(tasks.begin(), tasks.end())
411 async::inline_scheduler(),
412 [](std::vector<TTaskType>&& tasks) {
413 if constexpr (std::is_void_v<T>) {
416 for (
auto it = tasks.begin(); it != tasks.end(); ++it) {
422 std::vector<T> results;
423 results.reserve(tasks.size());
425 for (
auto it = tasks.begin(); it != tasks.end(); ++it) {
426 results.emplace_back(std::move(it->get()));
431 return Future<AllValueType<T>>(this->_pSchedulers, std::move(task));
434 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
436 template <
typename T>
friend class Future;
A system for managing asynchronous requests and tasks.
std::tuple< typename CesiumImpl::RemoveFuture< Futures >::type... > AllTupleType
The value type of the Future returned by AsyncSystem::all(Futures&&... futures) const.
Future< T > createResolvedFuture(T &&value) const
Creates a future that is already resolved.
bool dispatchOneMainThreadTask()
Runs a single waiting task that is currently queued for the main thread. If there are no tasks waitin...
CesiumImpl::ImmediateScheduler< CesiumImpl::QueuedScheduler >::SchedulerScope MainThreadScope
An object that denotes a scope for the current thread acting as the "main thread"....
Future< AllValueType< T > > all(std::vector< SharedFuture< T > > &&futures) const
Creates a Future that resolves when every Future in a vector resolves, and rejects when any Future in...
Future< AllTupleType< Futures... > > all(Futures &&... futures) const
Creates a Future that resolves when every Future in a variadic parameter pack resolves,...
bool operator==(const AsyncSystem &rhs) const noexcept
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > AllValueType
The value type of the Future returned by AsyncSystem::all(std::vector<Future<T>>&&) const.
void dispatchMainThreadTasks()
Runs all tasks that are currently queued for the main thread.
bool operator!=(const AsyncSystem &rhs) const noexcept
AsyncSystem(const std::shared_ptr< ITaskProcessor > &pTaskProcessor) noexcept
Constructs a new instance.
Future< void > createResolvedFuture() const
Creates a future that is already resolved and resolves to no value.
CesiumImpl::ContinuationFutureType_t< Func, void > runInThreadPool(const ThreadPool &threadPool, Func &&f) const
Runs a function in a thread pool, returning a Future that resolves when the function completes.
CesiumImpl::ContinuationFutureType_t< Func, void > runInWorkerThread(Func &&f) const
Runs a function in a worker thread, returning a Future that resolves when the function completes.
Promise< T > createPromise() const
Create a Promise that can be used at a later time to resolve or reject a Future.
Future< T > createFuture(Func &&f) const
Creates a new Future by immediately invoking a function and giving it the opportunity to resolve or r...
CesiumImpl::ContinuationFutureType_t< Func, void > runInMainThread(Func &&f) const
Runs a function in the main thread, returning a Future that resolves when the function completes.
MainThreadScope enterMainThread() const
Enters a scope in which the current thread is treated as the "mainthread". It is essential that no ot...
ThreadPool createThreadPool(int32_t numberOfThreads) const
Creates a new thread pool that can be used to run continuations.
Future< AllValueType< T > > all(std::vector< Future< T > > &&futures) const
Creates a Future that resolves when every Future in a vector resolves, and rejects when any Future in...
A value that will be available in the future, as produced by AsyncSystem.
When implemented by a rendering engine, allows tasks to be asynchronously executed in background thre...
A promise that can be resolved or rejected by an asynchronous task.
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.
auto transformTuple(Tuple &&tuple, Func &&f)
Transforms each element of a tuple by applying a function to it.