Manages a list of futures for a single asynchronous operation.
An asynchronous operation that vends futures to multiple callers can use a ListFutureProvider
to track them. This class can be used with any future that derives from a listable type like ListableFutureWithWaker
. The provider and its futures automatically handle list updates during moves.
All operations on the list are thread-safe, allowing futures to be modified from outside of an async context (for example, to complete a future on an external signal). The type of lock used is configurable, though it is important to understand that the lock will be acquired within the asynchronous dispatcher's thread. Therefore, it is strongly recommended to avoid long-blocking locks such as mutexes as they will stall other tasks.
The default lock is a pw::sync::InterruptSpinLock
, which is a safe choice for use within an async context. If it is certain that futures in the list will only be managed from within an async context (for example, between different async tasks), a no-op lock can be used for efficiency.
The future list is FIFO: Pop
returns futures in the order they were added.
When a future in the list is destroyed, it safely removes itself. The provider is not notified of this event.
Public Member Functions | |
ListFutureProvider (const ListFutureProvider &)=delete | |
ListFutureProvider & | operator= (const ListFutureProvider &)=delete |
void | Push (FutureType &future) |
Adds a future to the end of the list. | |
FutureType & | Pop () |
bool | empty () |
Returns true if there are no futures listed. | |
Lock & | lock () |
Provides access to the list's internal lock. | |
Friends | |
template<typename , typename > | |
class | ListableFutureWithWaker |
|
inline |
Removes and returns the first future from the list.
Triggers an assertion if the list is empty.