broker_tokio/sync/mpsc/
mod.rs

1#![cfg_attr(not(feature = "sync"), allow(dead_code, unreachable_pub))]
2
3//! A multi-producer, single-consumer queue for sending values across
4//! asynchronous tasks.
5//!
6//! Similar to `std`, channel creation provides [`Receiver`] and [`Sender`]
7//! handles. [`Receiver`] implements `Stream` and allows a task to read values
8//! out of the channel. If there is no message to read, the current task will be
9//! notified when a new value is sent.  [`Sender`] implements the `Sink` trait
10//! and allows sending messages into the channel. If the channel is at capacity,
11//! the send is rejected and the task will be notified when additional capacity
12//! is available. In other words, the channel provides backpressure.
13//!
14//! Unbounded channels are also available using the `unbounded_channel`
15//! constructor.
16//!
17//! # Disconnection
18//!
19//! When all [`Sender`] handles have been dropped, it is no longer
20//! possible to send values into the channel. This is considered the termination
21//! event of the stream. As such, `Receiver::poll` returns `Ok(Ready(None))`.
22//!
23//! If the [`Receiver`] handle is dropped, then messages can no longer
24//! be read out of the channel. In this case, all further attempts to send will
25//! result in an error.
26//!
27//! # Clean Shutdown
28//!
29//! When the [`Receiver`] is dropped, it is possible for unprocessed messages to
30//! remain in the channel. Instead, it is usually desirable to perform a "clean"
31//! shutdown. To do this, the receiver first calls `close`, which will prevent
32//! any further messages to be sent into the channel. Then, the receiver
33//! consumes the channel to completion, at which point the receiver can be
34//! dropped.
35//!
36//! [`Sender`]: crate::sync::mpsc::Sender
37//! [`Receiver`]: crate::sync::mpsc::Receiver
38
39pub(super) mod block;
40
41mod bounded;
42pub use self::bounded::{channel, Receiver, Sender};
43
44mod chan;
45
46pub(super) mod list;
47
48mod unbounded;
49pub use self::unbounded::{unbounded_channel, UnboundedReceiver, UnboundedSender};
50
51pub mod error;
52
53/// The number of values a block can contain.
54///
55/// This value must be a power of 2. It also must be smaller than the number of
56/// bits in `usize`.
57#[cfg(all(target_pointer_width = "64", not(loom)))]
58const BLOCK_CAP: usize = 32;
59
60#[cfg(all(not(target_pointer_width = "64"), not(loom)))]
61const BLOCK_CAP: usize = 16;
62
63#[cfg(loom)]
64const BLOCK_CAP: usize = 2;