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 1 µs per send/receive operation on a slightly older i5-7300U CPU @ 2.60GHz CPU (clang 17) using the RunMode::Deferred
event loop mode. This includes the entire coroutine dance of suspending/resuming between the reader and writer. (RunMode::Immediate
is ~25% faster)
Caveat 1: the channel is obviously not thread safe. Only use within one loop. Caveat 2: As you can notice, the Channel is independent of a Loop
. This means that a 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_await
ing 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.
|
inline |
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.
NOTE: template argument restriction may not be entirely correct?
|
private |
|
private |
|
private |