|
| | 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.
|
template<typename T, size_t N = 2>
class condy::Channel< T, N >
Thread-safe bounded channel for communication and synchronization.
- Template Parameters
-
| T | Type of the items transmitted through the channel. |
| N | When 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.
template<typename T, size_t N = 2>
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.
template<typename T, size_t N = 2>
template<typename U>
Push an item into the channel, awaiting if necessary.
- Template Parameters
-
| U | Type of the item to be pushed. |
- Parameters
-
| item | The 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.