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>

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>
int32_t try_push (U &&item) noexcept
 Try to push an item into the channel.
std::pair< int32_t, T > try_pop () noexcept
 Try to pop an item from the channel.
MovePushSender push (T &&item) noexcept
 Push an item into the channel, awaiting if necessary.
CopyPushSender push (const T &item) noexcept
 Push an item into the channel, awaiting if necessary.
PopSender 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.

Definition at line 38 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 45 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 173 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 182 of file channel.hpp.

◆ pop()

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

Pop an item from the channel, awaiting if necessary.

Returns
std::pair<int32_t, T> 0 and the popped item if successful; -EPIPE if the channel is closed and no more items can be popped; -ECANCELED if the operation was cancelled while waiting.

Definition at line 153 of file channel.hpp.

◆ push() [1/2]

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

Push an item into the channel, awaiting if necessary.

Parameters
itemThe item to be pushed into the channel.
Returns
int32_t 0 if the item was successfully pushed; -EPIPE if the channel is closed; -ECANCELED if the operation was cancelled while waiting.

Definition at line 140 of file channel.hpp.

◆ push() [2/2]

template<typename T, size_t N = 2>
MovePushSender 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
int32_t 0 if the item was successfully pushed; -EPIPE if the channel is closed; -ECANCELED if the operation was cancelled while waiting.
Note
If the operation is cancelled while waiting, the item will not be moved.
This operation holds a reference to the item, so the caller must ensure that the lifetime of the item must not be shorter than this asynchronous operation.

Definition at line 130 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 195 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 164 of file channel.hpp.

◆ try_pop()

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

Try to pop an item from the channel.

Returns
std::pair<int32_t, 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 84 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>
int32_t 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
int32_t 0 if the item was successfully pushed; -EPIPE if the channel is closed; -EAGAIN if the channel is full.

Definition at line 67 of file channel.hpp.


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