4 #include "Impl/ContinuationFutureType.h"
5 #include "Impl/RemoveFuture.h"
6 #include "Impl/WithTracing.h"
7 #include "Impl/cesium-async++.h"
10 #include "ThreadPool.h"
12 #include <CesiumUtility/Tracing.h>
15 #include <type_traits>
44 AsyncSystem(
const std::shared_ptr<ITaskProcessor>& pTaskProcessor) noexcept;
66 std::shared_ptr<async::event_task<T>> pEvent =
67 std::make_shared<async::event_task<T>>();
69 Promise<T> promise(this->_pSchedulers, pEvent);
74 promise.reject(std::current_exception());
77 return Future<T>(this->_pSchedulers, pEvent->get_task());
96 std::make_shared<async::event_task<T>>());
114 template <
typename Func>
115 CesiumImpl::ContinuationFutureType_t<Func, void>
117 static const char* tracingName =
"waiting for worker thread";
119 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
121 return CesiumImpl::ContinuationFutureType_t<Func, void>(
124 this->_pSchedulers->workerThread.immediate,
125 CesiumImpl::WithTracing<void>::end(
127 std::forward<Func>(f))));
144 template <
typename Func>
145 CesiumImpl::ContinuationFutureType_t<Func, void>
147 static const char* tracingName =
"waiting for main thread";
149 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
151 return CesiumImpl::ContinuationFutureType_t<Func, void>(
154 this->_pSchedulers->mainThread.immediate,
155 CesiumImpl::WithTracing<void>::end(
157 std::forward<Func>(f))));
169 template <
typename Func>
170 CesiumImpl::ContinuationFutureType_t<Func, void>
172 static const char* tracingName =
"waiting for thread pool";
174 CESIUM_TRACE_BEGIN_IN_TRACK(tracingName);
176 return CesiumImpl::ContinuationFutureType_t<Func, void>(
179 threadPool._pScheduler->immediate,
180 CesiumImpl::WithTracing<void>::end(
182 std::forward<Func>(f))));
193 template <
typename T>
195 std::conditional_t<std::is_void_v<T>, void, std::vector<T>>;
219 template <
typename T>
221 return this->all<T, Future<T>>(
222 std::forward<std::vector<Future<T>>>(futures));
247 template <
typename T>
249 return this->all<T, SharedFuture<T>>(
250 std::forward<std::vector<SharedFuture<T>>>(futures));
263 async::make_task<T>(std::forward<T>(value)));
272 return Future<void>(this->_pSchedulers, async::make_task());
318 template <
typename T,
typename TFutureType>
320 using TTaskType = decltype(TFutureType::_task);
321 std::vector<TTaskType> tasks;
322 tasks.reserve(futures.size());
324 for (
auto it = futures.begin(); it != futures.end(); ++it) {
325 tasks.emplace_back(std::move(it->_task));
330 async::task<AllValueType<T>> task =
331 async::when_all(tasks.begin(), tasks.end())
333 async::inline_scheduler(),
334 [](std::vector<TTaskType>&& tasks) {
335 if constexpr (std::is_void_v<T>) {
338 for (
auto it = tasks.begin(); it != tasks.end(); ++it) {
344 std::vector<T> results;
345 results.reserve(tasks.size());
347 for (
auto it = tasks.begin(); it != tasks.end(); ++it) {
348 results.emplace_back(std::move(it->get()));
356 std::shared_ptr<CesiumImpl::AsyncSystemSchedulers> _pSchedulers;
358 template <
typename T>
friend class Future;
A system for managing asynchronous requests and tasks.
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 > runInMainThread(Func &&f) const
Runs a function in the main thread, returning a Future that resolves when the function completes.
bool dispatchOneMainThreadTask()
Runs a single waiting task that is currently queued for the main thread. If there are no tasks waitin...
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...
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 > runInWorkerThread(Func &&f) const
Runs a function in a worker thread, returning a Future that resolves when the function completes.
Future< void > createResolvedFuture() const
Creates a future that is already resolved and resolves to no value.
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...
bool operator==(const AsyncSystem &rhs) const noexcept
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.
Promise< T > createPromise() const
Create a Promise that can be used at a later time to resolve or reject a Future.
Future< T > createResolvedFuture(T &&value) const
Creates a future that is already resolved.
std::conditional_t< std::is_void_v< T >, void, std::vector< T > > AllValueType
The value type of the Future returned by all.
ThreadPool createThreadPool(int32_t numberOfThreads) const
Creates a new thread pool that can be used to run continuations.
A value that will be available in the future, as produced by AsyncSystem.
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.