|
uvco 0.1
|
#include <channel.h>

Classes | |
| struct | ChannelAwaiter_ |
Public Member Functions | |
| Channel (unsigned capacity, unsigned max_waiters=16) | |
| Create a channel for up to capacity items. | |
| template<typename U> | |
| Promise< void > | put (U value) |
| Promise< T > | get () |
| MultiPromise< T > | getAll () |
Private Member Functions | |
| void | awake_reader () |
| void | awake_writer () |
Private Attributes | |
| BoundedQueue< T > | queue_ |
| BoundedQueue< std::coroutine_handle<> > | read_waiting_ |
| BoundedQueue< std::coroutine_handle<> > | write_waiting_ |
A Channel is similar to a Go channel: buffered, and blocking for reading and writing if empty/full respectively.
A bounded-capacity channel for items of type T. A channel can be waited on by at most max_waiters coroutines. If more coroutines want to wait, an exception is thrown.
A reader waits while the channel is empty, and is awoken by the first writer. A writer waits while the channel is full, and is awoken by the first reader.
When only using a channel to communicate small objects between coroutines, it takes about 50 ns to send a small value (e.g. int) on clang-21 and AMD Ryzen 7 PRO 7840U.
Caveat 1: the channel is obviously not thread safe. Only use within one loop.
Caveat 2: Channel is independent of a Loop. This means that runMain() may return despite there being channels in use and awaited on. Ensure that at least one uv operation (socket read/write/listen, timer, etc.) is running to keep the loop alive.
|
inlineexplicit |
Create a channel for up to capacity items.
|
inlineprivate |
|
inlineprivate |
|
inline |
Take an item from the channel.
Suspends if no items are available. The suspended coroutine is resumed by the next writer adding an item.
|
inline |
Continuously read items from channel by repeatedly co_awaiting the returned MultiPromise. (getAll() is a generator)
Remember to call MultiPromise<T>::cancel() when you are done with the channel, although this should happen automatically when the last MultiPromise object is dropped.
Put an item into the channel.
Suspends if no space is available in the channel. The suspended coroutine is resumed by the next reader taking out an item.
Template method: implements perfect forwarding for both copy and move insertion.
The argument is only accepted by value, as it's safer to do so in a coroutine.
|
private |
|
private |
|
private |