C/C++ API Reference
Loading...
Searching...
No Matches
pw::async2::Sender< T > Class Template Reference

Overview

template<typename T>
class pw::async2::Sender< T >

A sender which writes values to an asynchronous channel.

Public Member Functions

 Sender (const Sender &other)=delete
 
Senderoperator= (const Sender &other)=delete
 
 Sender (Sender &&other) noexcept
 
Senderoperator= (Sender &&other) noexcept
 
SendFuture< T > Send (const T &value)
 
SendFuture< T > Send (T &&value)
 
ReserveSendFuture< T > ReserveSend ()
 
std::optional< SendReservation< T > > TryReserveSend ()
 
bool TrySend (const T &value)
 
bool TrySend (T &&value)
 
Status BlockingSend (Dispatcher &dispatcher, const T &value, chrono::SystemClock::duration timeout=kWaitForever)
 
Status BlockingSend (Dispatcher &dispatcher, T &&value, chrono::SystemClock::duration timeout=kWaitForever)
 
void Disconnect ()
 
uint16_t remaining_capacity () const
 Returns the remaining capacity of the channel.
 
uint16_t capacity () const
 Returns the maximum capacity of the channel.
 
bool is_open () const
 Returns true if the channel is open.
 

Friends

template<typename U >
class internal::Channel
 
template<typename U >
std::optional< std::tuple< SpmcChannelHandle< U >, Sender< U > > > CreateSpmcChannel (Allocator &, uint16_t)
 
template<typename U , uint16_t kCapacity>
std::tuple< SpmcChannelHandle< U >, Sender< U > > CreateSpmcChannel (ChannelStorage< U, kCapacity > &storage)
 
template<typename U >
std::optional< std::tuple< SpscChannelHandle< U >, Sender< U >, Receiver< U > > > CreateSpscChannel (Allocator &, uint16_t)
 
template<typename U , uint16_t kCapacity>
std::tuple< SpscChannelHandle< U >, Sender< U >, Receiver< U > > CreateSpscChannel (ChannelStorage< U, kCapacity > &storage)
 

Member Function Documentation

◆ BlockingSend() [1/2]

template<typename T >
Status pw::async2::Sender< T >::BlockingSend ( Dispatcher dispatcher,
const T &  value,
chrono::SystemClock::duration  timeout = kWaitForever 
)
inline

Synchronously attempts to send value to the channel, blocking until space is available or the channel is closed.

Returns

This operation blocks the running thread until it is complete. It must not be called from an async context, or it will likely deadlock.

◆ BlockingSend() [2/2]

template<typename T >
Status pw::async2::Sender< T >::BlockingSend ( Dispatcher dispatcher,
T &&  value,
chrono::SystemClock::duration  timeout = kWaitForever 
)
inline

Synchronously attempts to send value to the channel, blocking until space is available or the channel is closed.

Returns

This operation blocks the running thread until it is complete. It must not be called from an async context, or it will likely deadlock.

◆ Disconnect()

template<typename T >
void pw::async2::Sender< T >::Disconnect ( )
inline

Removes this sender from its channel, preventing it from writing further values.

The channel may remain open if other senders and receivers exist.

◆ ReserveSend()

template<typename T >
ReserveSendFuture< T > pw::async2::Sender< T >::ReserveSend ( )
inline

Returns a Future<std::optional<SendReservation>> which resolves to a SendReservation which can be used to write count values directly into the channel when space is available.

If the channel is closed, the future resolves to nullopt.

◆ Send() [1/2]

template<typename T >
SendFuture< T > pw::async2::Sender< T >::Send ( const T &  value)
inline

Sends value through the channel, blocking until there is space.

Returns a Future<bool> which resolves to true if the value was successfully sent to the channel, or false if the channel is closed.

Note that a value being sent successfully does not guarantee that it will be read. If all corresponding receivers disconnect, any values still buffered in the channel are lost.

◆ Send() [2/2]

template<typename T >
SendFuture< T > pw::async2::Sender< T >::Send ( T &&  value)
inline

Sends value through the channel, blocking until there is space.

Returns a Future<bool> which resolves to true if the value was successfully sent to the channel, or false if the channel is closed.

Note that a value being sent successfully does not guarantee that it will be read. If all corresponding receivers disconnect, any values still buffered in the channel are lost.

◆ TryReserveSend()

template<typename T >
std::optional< SendReservation< T > > pw::async2::Sender< T >::TryReserveSend ( )
inline

Synchronously attempts to reserve a slot in the channel.

Returns a SendReservation if space is available. Returns std::nullopt if the channel is full or closed.

This operation is thread-safe and may be called from outside of an async context.

◆ TrySend() [1/2]

template<typename T >
bool pw::async2::Sender< T >::TrySend ( const T &  value)
inline

Synchronously attempts to send value if there is space in the channel. Returns true if successful. This operation is thread-safe and may be called from outside of an async context.

◆ TrySend() [2/2]

template<typename T >
bool pw::async2::Sender< T >::TrySend ( T &&  value)
inline

Synchronously attempts to send value if there is space in the channel. Returns true if successful. This operation is thread-safe and may be called from outside of an async context.

Friends And Related Function Documentation

◆ CreateSpmcChannel

template<typename T >
template<typename U >
std::optional< std::tuple< SpmcChannelHandle< U >, Sender< U > > > CreateSpmcChannel ( Allocator alloc,
uint16_t  capacity 
)
friend

Creates a dynamically allocated single-producer, multi-consumer channel with a fixed storage capacity.

Returns a handle to the channel which may be used to create receivers. After all desired receivers are created, the handle can be dropped without affecting the channel.

All allocation occurs during the creation of the channel. After this function returns, usage of the channel is guaranteed not to allocate. If allocation fails, returns std::nullopt.

The channel remains open as long as at least either a handle, or at least one sender and one receiver exist.

◆ CreateSpscChannel

template<typename T >
template<typename U >
std::optional< std::tuple< SpscChannelHandle< U >, Sender< U >, Receiver< U > > > CreateSpscChannel ( Allocator alloc,
uint16_t  capacity 
)
friend

Creates a dynamically allocated single-producer, single-consumer channel with a fixed storage capacity.

Returns a handle to the channel alongside the sender and receiver. The handle can be used to forcefully close the channel. If that is not required, it can be dropped without affecting the channel.

All allocation occurs during the creation of the channel. After this function returns, usage of the channel is guaranteed not to allocate. If allocation fails, returns std::nullopt.

The channel remains open as long as at least either a handle, or at least one sender and one receiver exist.


The documentation for this class was generated from the following file: