eve.rt.channel
Cross-thread and cross-fiber communication channels.
This module provides typed channels for safe message passing between concurrent execution contexts. Channels support both bounded (with a maximum capacity) and unbounded operation modes.
Warning: This module contains blocking operations (blockingSend, blockingReceive) that use mutex-based waiting. Never call blocking operations from an event-loop thread — they will block the entire loop and cause deadlocks or latency spikes.
For event-loop-safe usage, use the non-blocking variants:
trySend— fails immediately withWOULD_BLOCKif fulltryReceive— fails immediately withWOULD_BLOCKif empty
Example (correct usage from worker thread):
// Worker thread — blocking is OK here
auto ch = channel!int(10);
ch.blockingSend(42); // Blocks until space available
// Event loop thread — use non-blocking only!
auto outcome = ch.trySend(value);
if (!outcome.ok() && outcome.error().reason == ChannelErrorReason.WOULD_BLOCK) {
// Handle backpressure without blocking the loop
}Types 5
Reason a channel operation failed.
Recoverable failure of a channel operation.
ChannelErrorReason reasonWhy the operation failed.int errnoReserved; OS errors do not apply to channel operations.A bounded channel for cross-thread message passing.
Important: Channels have both blocking and non-blocking operations:
blockingSend/blockingReceive— block the thread, unsafe for event loopstrySend/tryReceive— return immediately, safe for event loops
Example (non-blocking, event-loop safe):
auto ch = channel!int(10); // bounded channel with capacity 10
// Non-blocking send (safe from event loop)
if (ch.trySend(42).ok()) {
writeln("Sent!");
}
// Non-blocking receive (safe from event loop)
auto received = ch.tryReceive();
if (received.ok()) {
writeln("Received: ", received.value());
}Example (blocking, worker thread only):
// Only use from worker threads, never from event loop!
ch.blockingSend(42); // Blocks until space available
auto value = ch.blockingReceive(); // Blocks until value availableprivate T[] bufferprivate size_t headprivate size_t tailprivate size_t countprivate size_t capacity_private bool closed_private Mutex mutexprivate Condition notEmptyprivate Condition notFullChannelStatus blockingSend(T value) @trustedSend a value into the channel, blocking if the channel is full.ChannelStatus trySend(T value, CancelToken token) @trustedChannelResult!T tryReceive(CancelToken token) @trustedChannelStatus blockingSend(T value, CancelToken token) @trustedChannelResult!T blockingReceive(CancelToken token) @trusted