|
cesium-native 0.52.0
|
A system for managing asynchronous requests and tasks. More...
#include <CesiumAsync/AsyncSystem.h>
Public Types | |
| template<typename T> | |
| using | AllValueType |
The value type of the Future returned by all. | |
| using | MainThreadScope |
| An object that denotes a scope for the current thread acting as the "main thread". When this object is constructed, the current thread becomes the main thread. When it's destroyed, the current thread is no longer treated as the main thread. | |
Public Member Functions | |
| AsyncSystem (const std::shared_ptr< ITaskProcessor > &pTaskProcessor) noexcept | |
| Constructs a new instance. | |
| template<typename T, typename Func> | |
| Future< T > | createFuture (Func &&f) const |
Creates a new Future by immediately invoking a function and giving it the opportunity to resolve or reject a Promise. | |
| template<typename T> | |
| Promise< T > | createPromise () const |
| Create a Promise that can be used at a later time to resolve or reject a Future. | |
| template<typename Func> | |
| 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. | |
| template<typename Func> | |
| 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. | |
| template<typename Func> | |
| 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. | |
| template<typename T> | |
| 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 the vector rejects. | |
| template<typename T> | |
| 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 the vector rejects. | |
| template<typename T> | |
| Future< T > | createResolvedFuture (T &&value) const |
| Creates a future that is already resolved. | |
| Future< void > | createResolvedFuture () const |
| Creates a future that is already resolved and resolves to no value. | |
| void | dispatchMainThreadTasks () |
| Runs all tasks that are currently queued for the main thread. | |
| bool | dispatchOneMainThreadTask () |
| Runs a single waiting task that is currently queued for the main thread. If there are no tasks waiting, it returns immediately without running any tasks. | |
| MainThreadScope | enterMainThread () const |
| Enters a scope in which the current thread is treated as the "main
thread". It is essential that no other thread be acting as the main thread at the same time, or undefined behavior will result. The scope continues until the returned object is destroyed. | |
| ThreadPool | createThreadPool (int32_t numberOfThreads) const |
| Creates a new thread pool that can be used to run continuations. | |
| bool | operator== (const AsyncSystem &rhs) const noexcept |
| bool | operator!= (const AsyncSystem &rhs) const noexcept |
Friends | |
| template<typename T> | |
| class | Future |
A system for managing asynchronous requests and tasks.
Instances of this class may be safely and efficiently stored and passed around by value. However, it is essential that the last AsyncSystem instance be destroyed only after all continuations have run to completion. Otherwise, continuations may be scheduled using invalid scheduler instances, leading to a crash. Broadly, there are two ways to achieve this:
Definition at line 36 of file AsyncSystem.h.
| using CesiumAsync::AsyncSystem::AllValueType |
The value type of the Future returned by all.
This will be either std::vector<T>, if the input Futures passed to the all function return values, or void if they do not.
| T | The value type of the input Futures passed to the function. |
Definition at line 194 of file AsyncSystem.h.
An object that denotes a scope for the current thread acting as the "main thread". When this object is constructed, the current thread becomes the main thread. When it's destroyed, the current thread is no longer treated as the main thread.
Definition at line 300 of file AsyncSystem.h.
|
noexcept |
Constructs a new instance.
| pTaskProcessor | The interface used to run tasks in background threads. |
|
inline |
Creates a Future that resolves when every Future in a vector resolves, and rejects when any Future in the vector rejects.
If the input Futures resolve to non-void values, the returned Future resolves to a vector of the values, in the same order as the input Futures. If the input Futures resolve to void, the returned Future resolves to void as well.
If any of the Futures rejects, the returned Future rejects as well. The exception included in the rejection will be from the first Future in the vector that rejects.
To get detailed rejection information from each of the Futures, attach a catchInMainThread continuation prior to passing the list into all.
| T | The type that each Future resolves to. |
| futures | The list of futures. |
Definition at line 220 of file AsyncSystem.h.
|
inline |
Creates a Future that resolves when every Future in a vector resolves, and rejects when any Future in the vector rejects.
If the input SharedFutures resolve to non-void values, the returned Future resolves to a vector of the values, in the same order as the input SharedFutures. If the input SharedFutures resolve to void, the returned Future resolves to void as well.
If any of the SharedFutures rejects, the returned Future rejects as well. The exception included in the rejection will be from the first SharedFuture in the vector that rejects.
To get detailed rejection information from each of the SharedFutures, attach a catchInMainThread continuation prior to passing the list into all.
| T | The type that each SharedFuture resolves to. |
| futures | The list of shared futures. |
Definition at line 248 of file AsyncSystem.h.
|
inline |
Creates a new Future by immediately invoking a function and giving it the opportunity to resolve or reject a Promise.
The Promise passed to the callback f may be resolved or rejected asynchronously, even after the function has returned.
This method is very similar to AsyncSystem::createPromise, except that that method returns the Promise directly. The advantage of using this method instead is that it is more exception-safe. If the callback f throws an exception, the Future will be rejected automatically and the exception will not escape the callback.
| T | The type that the Future resolves to. |
| Func | The type of the callback function. |
| f | The callback function to invoke immediately to create the Future. |
Definition at line 65 of file AsyncSystem.h.
|
inline |
Create a Promise that can be used at a later time to resolve or reject a Future.
Use Promise<T>::getFuture to get the Future that is resolved or rejected when this Promise is resolved or rejected.
Consider using AsyncSystem::createFuture instead of this method.
| T | The type that is provided when resolving the Promise and the type that the associated Future resolves to. Future. |
Definition at line 93 of file AsyncSystem.h.
|
inline |
Creates a future that is already resolved and resolves to no value.
Definition at line 271 of file AsyncSystem.h.
|
inline |
Creates a future that is already resolved.
| T | The type of the future. |
| value | The value for the future. |
Definition at line 260 of file AsyncSystem.h.
| ThreadPool CesiumAsync::AsyncSystem::createThreadPool | ( | int32_t | numberOfThreads | ) | const |
Creates a new thread pool that can be used to run continuations.
| numberOfThreads | The number of threads in the pool. |
| void CesiumAsync::AsyncSystem::dispatchMainThreadTasks | ( | ) |
Runs all tasks that are currently queued for the main thread.
The tasks are run in the calling thread.
| bool CesiumAsync::AsyncSystem::dispatchOneMainThreadTask | ( | ) |
Runs a single waiting task that is currently queued for the main thread. If there are no tasks waiting, it returns immediately without running any tasks.
The task is run in the calling thread.
|
noexcept |
Returns true if this instance and the right-hand side can not be used interchangeably because they schedule continuations differently. Otherwise, returns false.
|
noexcept |
Returns true if this instance and the right-hand side can be used interchangeably because they schedule continuations identically. Otherwise, returns false.
|
inline |
Runs a function in the main thread, returning a Future that resolves when the function completes.
If the function itself returns a Future, the function will not be considered complete until that returned Future also resolves.
If this method is called from the main thread, the callback will be invoked immediately and complete before this function returns.
| Func | The type of the function. |
| f | The function. |
Definition at line 146 of file AsyncSystem.h.
|
inline |
Runs a function in a thread pool, returning a Future that resolves when the function completes.
| Func | The type of the function. |
| threadPool | The thread pool in which to run the function. |
| f | The function to run. |
Definition at line 171 of file AsyncSystem.h.
|
inline |
Runs a function in a worker thread, returning a Future that resolves when the function completes.
If the function itself returns a Future, the function will not be considered complete until that returned Future also resolves.
If this method is called from a designated worker thread, the callback will be invoked immediately and complete before this function returns.
| Func | The type of the function. |
| f | The function. |
Definition at line 116 of file AsyncSystem.h.
|
friend |
Definition at line 375 of file AsyncSystem.h.