Condy v1.1.0
C++ Asynchronous System Call Layer for Linux
Loading...
Searching...
No Matches
condy::Channel< T, N > Class Template Reference

Thread-safe bounded channel for communication and synchronization. More...

#include <channel.hpp>

Classes

struct  PushAwaiter
 Awaiter for pushing an item into the channel. More...
struct  PopAwaiter
 Awaiter for popping an item from the channel. More...

Public Member Functions

 Channel (size_t capacity)
 Construct a new Channel object.
template<typename U>
bool try_push (U &&item)
 Try to push an item into the channel.
std::optional< T > try_pop ()
 Try to pop an item from the channel.
template<typename U>
PushAwaiter push (U &&item)
 Push an item into the channel, awaiting if necessary.
PopAwaiter pop ()
 Pop an item from the channel, awaiting if necessary.
size_t capacity () const noexcept
 Get the capacity of the channel.
size_t size () const noexcept
 Get the current size of the channel.
bool empty () const noexcept
 Check if the channel is empty.
bool is_closed () const noexcept
 Check if the channel is closed.
void push_close ()
 Close the channel.

Detailed Description

template<typename T, size_t N = 2>
class condy::Channel< T, N >

Thread-safe bounded channel for communication and synchronization.

Template Parameters
TType of the items transmitted through the channel.
NWhen the capacity is less than or equal to N, the channel uses stack storage for buffering; otherwise, it uses heap storage.

This class provides a thread-safe channel for communication and synchronization for both intra-Runtime and inter-Runtime scenarios. It supports both buffered and unbuffered modes, as well as push and pop operations that can be awaited in coroutines. The internal implementation utilizes msg_ring operations of io_uring for efficient cross-runtime notifications.

Constructor & Destructor Documentation

◆ Channel()

template<typename T, size_t N = 2>
condy::Channel< T, N >::Channel ( size_t capacity)
inline

Construct a new Channel object.

Parameters
capacityCapacity of the channel. If capacity is zero, the channel operates in unbuffered mode.

Member Function Documentation

◆ empty()

template<typename T, size_t N = 2>
bool condy::Channel< T, N >::empty ( ) const
inlinenoexcept

Check if the channel is empty.

Warning
This function may not be accurate in multithreaded scenarios.

◆ is_closed()

template<typename T, size_t N = 2>
bool condy::Channel< T, N >::is_closed ( ) const
inlinenoexcept

Check if the channel is closed.

Warning
This function may not be accurate in multithreaded scenarios.

◆ pop()

template<typename T, size_t N = 2>
PopAwaiter condy::Channel< T, N >::pop ( )
inline

Pop an item from the channel, awaiting if necessary.

Returns
PopAwaiter Awaiter object for the pop operation.

This function attempts to pop an item from the channel. If the channel is empty, the coroutine will be suspended until an item becomes available. If the channel is closed, a default-constructed T will be returned.

◆ push()

template<typename T, size_t N = 2>
template<typename U>
PushAwaiter condy::Channel< T, N >::push ( U && item)
inline

Push an item into the channel, awaiting if necessary.

Template Parameters
UType of the item to be pushed.
Parameters
itemThe item to be pushed into the channel.
Returns
PushAwaiter Awaiter object for the push operation.

This function attempts to push the given item into the channel. If the channel is full, the coroutine will be suspended until space becomes available. If the channel is closed, a std::logic_error will be thrown.

Warning
The item will be moved during the push operation. If the push operation is cancelled, the moved item will be destroyed immediately and will not be pushed into the channel.

◆ push_close()

template<typename T, size_t N = 2>
void condy::Channel< T, N >::push_close ( )
inline

Close the channel.

This function closes the channel. After the channel is closed, no more items can be pushed into the channel. All pending and future pop operations will return default-constructed T values. All pending push operations will throw std::logic_error.

Note
This function is idempotent.

◆ size()

template<typename T, size_t N = 2>
size_t condy::Channel< T, N >::size ( ) const
inlinenoexcept

Get the current size of the channel.

Warning
This function may not be accurate in multithreaded scenarios.

◆ try_pop()

template<typename T, size_t N = 2>
std::optional< T > condy::Channel< T, N >::try_pop ( )
inline

Try to pop an item from the channel.

Returns
std::optional<T> The popped item if successful; std::nullopt if the channel is empty or closed.

◆ try_push()

template<typename T, size_t N = 2>
template<typename U>
bool condy::Channel< T, N >::try_push ( U && item)
inline

Try to push an item into the channel.

Template Parameters
UType of the item to be pushed.
Parameters
itemThe item to be pushed into the channel.
Returns
true If the item was successfully pushed.
false If the channel is full.
Exceptions
std::logic_errorIf the channel is closed.

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