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 5b93bb9

Browse files
committedJun 22, 2024··
Add multi-producer, multi-consumer channel (mpmc)
1 parent 25c9f2c commit 5b93bb9

File tree

8 files changed

+1693
-46
lines changed

8 files changed

+1693
-46
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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
//! - [`Condvar`]: Condition Variable, providing the ability to block
126126
//! a thread while waiting for an event to occur.
127127
//!
128+
//! - [`mpmc`]: Multi-producer, multi-consumer queues, used for
129+
//! message-based communication. Can provide a lightweight
130+
//! inter-thread synchronisation mechanism, at the cost of some
131+
//! extra memory.
132+
//!
128133
//! - [`mpsc`]: Multi-producer, single-consumer queues, used for
129134
//! message-based communication. Can provide a lightweight
130135
//! inter-thread synchronisation mechanism, at the cost of some
@@ -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
@@ -191,12 +197,13 @@ pub use self::once_lock::OnceLock;
191197
#[unstable(feature = "reentrant_lock", issue = "121440")]
192198
pub use self::reentrant_lock::{ReentrantLock, ReentrantLockGuard};
193199

200+
#[unstable(feature = "mpmc_channel", issue = "125712")]
201+
pub mod mpmc;
194202
pub mod mpsc;
195203

196204
mod barrier;
197205
mod condvar;
198206
mod lazy_lock;
199-
mod mpmc;
200207
mod mutex;
201208
pub(crate) mod once;
202209
mod once_lock;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub use crate::sync::mpsc::{RecvError, RecvTimeoutError, SendError, TryRecvError
99
///
1010
/// [`send_timeout`]: super::Sender::send_timeout
1111
#[derive(PartialEq, Eq, Clone, Copy)]
12+
#[unstable(feature = "mpmc_channel", issue = "125712")]
1213
pub enum SendTimeoutError<T> {
1314
/// The message could not be sent because the channel is full and the operation timed out.
1415
///
@@ -20,12 +21,14 @@ pub enum SendTimeoutError<T> {
2021
Disconnected(T),
2122
}
2223

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

31+
#[unstable(feature = "mpmc_channel", issue = "125712")]
2932
impl<T> fmt::Display for SendTimeoutError<T> {
3033
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3134
match *self {
@@ -35,8 +38,10 @@ impl<T> fmt::Display for SendTimeoutError<T> {
3538
}
3639
}
3740

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

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

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

Lines changed: 922 additions & 41 deletions
Large diffs are not rendered by default.

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

Lines changed: 729 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.