Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 04721ba

Browse files
committedSep 30, 2024··
Add multi-producer, multi-consumer channel (mpmc)
1 parent 0245b0c commit 04721ba

File tree

8 files changed

+1759
-53
lines changed

8 files changed

+1759
-53
lines changed
 

‎library/std/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@
153153
//! the [`io`], [`fs`], and [`net`] modules.
154154
//!
155155
//! The [`thread`] module contains Rust's threading abstractions. [`sync`]
156-
//! contains further primitive shared memory types, including [`atomic`] and
156+
//! contains further primitive shared memory types, including [`atomic`], [`mpmc`] and
157157
//! [`mpsc`], which contains the channel types for message passing.
158158
//!
159159
//! # Use before and after `main()`
@@ -177,6 +177,7 @@
177177
//! - after-main use of thread-locals, which also affects additional features:
178178
//! - [`thread::current()`]
179179
//! - [`thread::scope()`]
180+
//! - [`sync::mpmc`]
180181
//! - [`sync::mpsc`]
181182
//! - before-main stdio file descriptors are not guaranteed to be open on unix platforms
182183
//!
@@ -202,6 +203,7 @@
202203
//! [`atomic`]: sync::atomic
203204
//! [`for`]: ../book/ch03-05-control-flow.html#looping-through-a-collection-with-for
204205
//! [`str`]: prim@str
206+
//! [`mpmc`]: sync::mpmc
205207
//! [`mpsc`]: sync::mpsc
206208
//! [`std::cmp`]: cmp
207209
//! [`std::slice`]: mod@slice

‎library/std/src/sync/mod.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@
133133
//! inter-thread synchronisation mechanism, at the cost of some
134134
//! extra memory.
135135
//!
136+
//! - [`mpmc`]: Multi-producer, multi-consumer queues, used for
137+
//! message-based communication. Can provide a lightweight
138+
//! inter-thread synchronisation mechanism, at the cost of some
139+
//! extra memory.
140+
//!
136141
//! - [`Mutex`]: Mutual Exclusion mechanism, which ensures that at
137142
//! most one thread at a time is able to access some data.
138143
//!
@@ -153,6 +158,7 @@
153158
//! [`Arc`]: crate::sync::Arc
154159
//! [`Barrier`]: crate::sync::Barrier
155160
//! [`Condvar`]: crate::sync::Condvar
161+
//! [`mpmc`]: crate::sync::mpmc
156162
//! [`mpsc`]: crate::sync::mpsc
157163
//! [`Mutex`]: crate::sync::Mutex
158164
//! [`Once`]: crate::sync::Once
@@ -184,7 +190,7 @@ pub use self::mutex::{Mutex, MutexGuard};
184190
pub use self::once::{ONCE_INIT, Once, OnceState};
185191
#[stable(feature = "once_cell", since = "1.70.0")]
186192
pub use self::once_lock::OnceLock;
187-
#[stable(feature = "rust1", since = "1.0.0")]
193+
#[stable(feature = "once_cell", since = "1.70.0")]
188194
pub use self::poison::{LockResult, PoisonError, TryLockError, TryLockResult};
189195
#[unstable(feature = "reentrant_lock", issue = "121440")]
190196
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
@@ -193,12 +199,13 @@ pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
193199
#[stable(feature = "rust1", since = "1.0.0")]
194200
pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
195201

202+
#[unstable(feature = "mpmc_channel", issue = "126840")]
203+
pub mod mpmc;
196204
pub mod mpsc;
197205

198206
mod barrier;
199207
mod condvar;
200208
mod lazy_lock;
201-
mod mpmc;
202209
mod mutex;
203210
pub(crate) mod once;
204211
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 = "126840")]
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 = "126840")]
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 = "126840")]
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 = "126840")]
3640
impl<T> error::Error for SendTimeoutError<T> {}
3741

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

‎library/std/src/sync/mpmc/mod.rs

Lines changed: 988 additions & 47 deletions
Large diffs are not rendered by default.

‎library/std/src/sync/mpmc/tests.rs

Lines changed: 728 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/const-generics/issues/issue-82956.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL + use std::collections::btree_map::IntoIter;
1414
|
1515
LL + use std::collections::btree_set::IntoIter;
1616
|
17-
and 8 other candidates
17+
and 9 other candidates
1818

1919
error: aborting due to 1 previous error
2020

‎tests/ui/issues/issue-12041.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0382]: use of moved value: `tx`
44
LL | let tx = tx;
55
| ^^ value moved here, in previous iteration of loop
66
|
7-
= note: move occurs because `tx` has type `Sender<i32>`, which does not implement the `Copy` trait
7+
= note: move occurs because `tx` has type `std::sync::mpsc::Sender<i32>`, which does not implement the `Copy` trait
88

99
error: aborting due to 1 previous error
1010

‎tests/ui/lint/use_suggestion_json.stderr

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,29 @@ mod foo {
348348
"label": null,
349349
"suggested_replacement": "use std::slice::Iter;
350350

351+
",
352+
"suggestion_applicability": "MaybeIncorrect",
353+
"expansion": null
354+
},
355+
{
356+
"file_name": "$DIR/use_suggestion_json.rs",
357+
"byte_start": 541,
358+
"byte_end": 541,
359+
"line_start": 11,
360+
"line_end": 11,
361+
"column_start": 1,
362+
"column_end": 1,
363+
"is_primary": true,
364+
"text": [
365+
{
366+
"text": "fn main() {",
367+
"highlight_start": 1,
368+
"highlight_end": 1
369+
}
370+
],
371+
"label": null,
372+
"suggested_replacement": "use std::sync::mpmc::Iter;
373+
351374
",
352375
"suggestion_applicability": "MaybeIncorrect",
353376
"expansion": null
@@ -396,7 +419,7 @@ mod foo {
396419
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m
397420
\u001b[0m\u001b[1m\u001b[38;5;12mLL\u001b[0m\u001b[0m \u001b[0m\u001b[0m\u001b[38;5;10m+ use std::collections::hash_map::Iter;\u001b[0m
398421
\u001b[0m \u001b[0m\u001b[0m\u001b[1m\u001b[38;5;12m|\u001b[0m
399-
\u001b[0m and 8 other candidates\u001b[0m
422+
\u001b[0m and 9 other candidates\u001b[0m
400423

401424
"
402425
}

0 commit comments

Comments
 (0)
Please sign in to comment.