Skip to main content

Module channel

Module channel 

Source
Expand description

Channel operations for CSP-style concurrency.

Channels are the primary communication mechanism between strands. They use May’s MPMC channels with cooperative blocking.

§Zero-Mutex Design

Channels are passed directly as Value::Channel on the stack. There is NO global registry and NO mutex contention. Send/receive operations work directly on the channel handles. The closed flag is a single atomic load on the send hot path; no locking.

§Non-Blocking Guarantee

All channel operations (send, receive) cooperatively block using May’s scheduler. They NEVER block OS threads — May handles scheduling other strands while waiting.

§Multi-Consumer Support

Channels support multiple producers AND multiple consumers (MPMC). Each message is delivered to exactly one receiver (work-stealing semantics).

§chan.close Semantics

Issue #499: chan.close is real, not “equivalent to drop”. The implementation uses the same typed-sentinel pattern as WeaveChannelData — see crates/core/src/value.rs::ChannelMsg and docs/design/CHAN_CLOSE_SEMANTICS.md.

  • chan.close atomically sets a shared closed flag (CAS) and, on the first close, enqueues a single ChannelMsg::Closed sentinel.
  • chan.send short-circuits to false when the flag is set.
  • chan.receive returns ( value true ) on ChannelMsg::Value. On ChannelMsg::Closed it re-broadcasts the sentinel (so the next blocked receiver also wakes — Go-style propagation across an unknown number of MPMC consumers) and returns ( default false ).

The user-facing API is unchanged: programs still write Channel, still call chan.make/chan.send/chan.receive/chan.close, still see the ( value Bool ) success-flag shape.

§Stack Effects

  • chan.make: ( – Channel )
  • chan.send: ( value Channel – Bool ) consumes the channel
  • chan.receive: ( Channel – value Bool ) consumes the channel
  • chan.close: ( Channel – ) consumes the channel

Re-exports§

pub use patch_seq_chan_receive as receive;
pub use patch_seq_chan_send as send;
pub use patch_seq_close_channel as close_channel;
pub use patch_seq_make_channel as make_channel;

Statics§

TOTAL_MESSAGES_RECEIVED
TOTAL_MESSAGES_SENT

Functions§

patch_seq_chan_receive
Receive a value from a channel.
patch_seq_chan_send
Send a value through a channel.
patch_seq_close_channel
Close a channel.
patch_seq_make_channel
Create a new channel.