Skip to content

Commit 3652450

Browse files
committed
Add multi-producer, multi-consumer channel (mpmc)
1 parent a5ee5cb commit 3652450

File tree

8 files changed

+1764
-53
lines changed

8 files changed

+1764
-53
lines changed

library/std/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
//! the [`io`], [`fs`], and [`net`] modules.
150150
//!
151151
//! The [`thread`] module contains Rust's threading abstractions. [`sync`]
152-
//! contains further primitive shared memory types, including [`atomic`] and
152+
//! contains further primitive shared memory types, including [`atomic`], [`mpmc`] and
153153
//! [`mpsc`], which contains the channel types for message passing.
154154
//!
155155
//! # Use before and after `main()`
@@ -173,6 +173,7 @@
173173
//! - after-main use of thread-locals, which also affects additional features:
174174
//! - [`thread::current()`]
175175
//! - [`thread::scope()`]
176+
//! - [`sync::mpmc`]
176177
//! - [`sync::mpsc`]
177178
//! - before-main stdio file descriptors are not guaranteed to be open on unix platforms
178179
//!
@@ -198,6 +199,7 @@
198199
//! [`atomic`]: sync::atomic
199200
//! [`for`]: ../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
200201
//! [`str`]: prim@str
202+
//! [`mpmc`]: sync::mpmc
201203
//! [`mpsc`]: sync::mpsc
202204
//! [`std::cmp`]: cmp
203205
//! [`std::slice`]: mod@slice

library/std/src/sync/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
//! inter-thread synchronisation mechanism, at the cost of some
131131
//! extra memory.
132132
//!
133+
//! - [`mpmc`]: Multi-producer, multi-consumer queues, used for
134+
//! message-based communication. Can provide a lightweight
135+
//! inter-thread synchronisation mechanism, at the cost of some
136+
//! extra memory.
137+
//!
133138
//! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at
134139
//! most one thread at a time is able to access some data.
135140
//!
@@ -150,6 +155,7 @@
150155
//! [`Arc`]: crate::sync::Arc
151156
//! [`Barrier`]: crate::sync::Barrier
152157
//! [`Condvar`]: crate::sync::Condvar
158+
//! [`mpmc`]: crate::sync::mpmc
153159
//! [`mpsc`]: crate::sync::mpsc
154160
//! [`Mutex`]: crate::sync::Mutex
155161
//! [`Once`]: crate::sync::Once
@@ -172,6 +178,8 @@ pub use self::barrier::{Barrier, BarrierWaitResult};
172178
pub use self::condvar::{Condvar, WaitTimeoutResult};
173179
#[stable(feature = "lazy_cell", since = "1.80.0")]
174180
pub use self::lazy_lock::LazyLock;
181+
#[stable(feature = "lazy_cell", since = "1.80.0")]
182+
pub use self::lazy_lock::LazyLock;
175183
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
176184
pub use self::mutex::MappedMutexGuard;
177185
#[stable(feature = "rust1", since = "1.0.0")]
@@ -181,21 +189,25 @@ pub use self::mutex::{Mutex, MutexGuard};
181189
pub use self::once::{Once, OnceState, ONCE_INIT};
182190
#[stable(feature = "once_cell", since = "1.70.0")]
183191
pub use self::once_lock::OnceLock;
192+
#[stable(feature = "once_cell", since = "1.70.0")]
193+
pub use self::once_lock::OnceLock;
184194
#[stable(feature = "rust1", since = "1.0.0")]
185195
pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
186196
#[unstable(feature = "reentrant_lock", issue = "121440")]
187197
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
198+
#[unstable(feature = "reentrant_lock", issue = "121440")]
199+
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
188200
#[unstable(feature = "mapped_lock_guards", issue = "117108")]
189201
pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
190202
#[stable(feature = "rust1", since = "1.0.0")]
191203
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
192204

193-
pub mod mpsc;
205+
#[unstable(feature = "mpmc_channel", issue = "125712")]
206+
pub mod mpmc;
194207

195208
mod barrier;
196209
mod condvar;
197210
mod lazy_lock;
198-
mod mpmc;
199211
mod mutex;
200212
pub(crate) mod once;
201213
mod once_lock;

library/std/src/sync/mpmc/error.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{error, fmt};
77
///
88
/// [`send_timeout`]: super::Sender::send_timeout
99
#[derive(PartialEq, Eq, Clone, Copy)]
10+
#[unstable(feature = "mpmc_channel", issue = "125712")]
1011
pub enum SendTimeoutError<T> {
1112
/// The message could not be sent because the channel is full and the operation timed out.
1213
///
@@ -18,12 +19,14 @@ pub enum SendTimeoutError<T> {
1819
Disconnected(T),
1920
}
2021

22+
#[unstable(feature = "mpmc_channel", issue = "125712")]
2123
impl<T> fmt::Debug for SendTimeoutError<T> {
2224
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2325
"SendTimeoutError(..)".fmt(f)
2426
}
2527
}
2628

29+
#[unstable(feature = "mpmc_channel", issue = "125712")]
2730
impl<T> fmt::Display for SendTimeoutError<T> {
2831
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2932
match *self {
@@ -33,8 +36,10 @@ impl<T> fmt::Display for SendTimeoutError<T> {
3336
}
3437
}
3538

39+
#[unstable(feature = "mpmc_channel", issue = "125712")]
3640
impl<T> error::Error for SendTimeoutError<T> {}
3741

42+
#[unstable(feature = "mpmc_channel", issue = "125712")]
3843
impl<T> From<SendError<T>> for SendTimeoutError<T> {
3944
fn from(err: SendError<T>) -> SendTimeoutError<T> {
4045
match err {

0 commit comments

Comments
 (0)