Condy v1.6.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>
requires std::is_same_v<std::decay_t<U>, T>
int try_push (U &&item) noexcept
 Try to push an item into the channel.
std::pair< int, T > try_pop () noexcept
 Try to pop an item from the channel.
PushAwaiter push (T item) noexcept
 Push an item into the channel, awaiting if necessary.
PopAwaiter pop () noexcept
 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 () noexcept
 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.

Note
Legacy version with different error handling is available in condy::legacy::Channel, which is deprecated and will be removed in future versions.

Definition at line 40 of file channel.hpp.

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.

Definition at line 47 of file channel.hpp.

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.

Definition at line 165 of file channel.hpp.

◆ 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.

Definition at line 174 of file channel.hpp.

◆ pop()

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

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, the pop operation will return -EPIPE. If this operation is cancelled while waiting, it will return -ECANCELED.

Definition at line 145 of file channel.hpp.

◆ push()

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

Push an item into the channel, awaiting if necessary.

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, the push operation will return -EPIPE. If this operation is cancelled while waiting, it will return -ECANCELED.

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.

Definition at line 133 of file channel.hpp.

◆ push_close()

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

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 push operations will fail with -EPIPE. All pending pop operations will fail with -EPIPE if there are no more items to pop.

Note
This function is idempotent.

Definition at line 187 of file channel.hpp.

◆ 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.

Definition at line 156 of file channel.hpp.

◆ try_pop()

template<typename T, size_t N = 2>
std::pair< int, T > condy::Channel< T, N >::try_pop ( )
inlinenoexcept

Try to pop an item from the channel.

Returns
std::pair<int, T> 0 and the popped item if successful; -EPIPE if the channel is closed and no more items can be popped; -EAGAIN if the channel is empty.

Definition at line 86 of file channel.hpp.

◆ try_push()

template<typename T, size_t N = 2>
template<typename U>
requires std::is_same_v<std::decay_t<U>, T>
int condy::Channel< T, N >::try_push ( U && item)
inlinenoexcept

Try to push an item into the channel.

Parameters
itemThe item to be pushed into the channel.
Returns
int 0 if the item was successfully pushed; -EPIPE if the channel is closed; -EAGAIN if the channel is full.

Definition at line 69 of file channel.hpp.


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