Skip to content

Commit 1e6e98c

Browse files
committed
auto merge of #12991 : alexcrichton/rust/sync-chan, r=brson
This commit contains an implementation of synchronous, bounded channels for Rust. This is an implementation of the proposal made last January [1]. These channels are built on mutexes, and currently focus on a working implementation rather than speed. Receivers for sync channels have select() implemented for them, but there is currently no implementation of select() for sync senders. Rust will continue to provide both synchronous and asynchronous channels as part of the standard distribution, there is no intent to remove asynchronous channels. This flavor of channels is meant to provide an alternative to asynchronous channels because like green tasks, asynchronous channels are not appropriate for all situations. [1] - https://mail.mozilla.org/pipermail/rust-dev/2014-January/007924.html
2 parents 6bf3fca + 56cae9b commit 1e6e98c

File tree

11 files changed

+1229
-115
lines changed

11 files changed

+1229
-115
lines changed

src/libstd/comm/mod.rs

+696-2
Large diffs are not rendered by default.

src/libstd/comm/select.rs

+36
Original file line numberDiff line numberDiff line change
@@ -648,4 +648,40 @@ mod test {
648648
tx1.send(());
649649
rx2.recv();
650650
})
651+
652+
test!(fn sync1() {
653+
let (tx, rx) = sync_channel(1);
654+
tx.send(1);
655+
select! {
656+
n = rx.recv() => { assert_eq!(n, 1); }
657+
}
658+
})
659+
660+
test!(fn sync2() {
661+
let (tx, rx) = sync_channel(0);
662+
spawn(proc() {
663+
for _ in range(0, 100) { task::deschedule() }
664+
tx.send(1);
665+
});
666+
select! {
667+
n = rx.recv() => { assert_eq!(n, 1); }
668+
}
669+
})
670+
671+
test!(fn sync3() {
672+
let (tx1, rx1) = sync_channel(0);
673+
let (tx2, rx2) = channel();
674+
spawn(proc() { tx1.send(1); });
675+
spawn(proc() { tx2.send(2); });
676+
select! {
677+
n = rx1.recv() => {
678+
assert_eq!(n, 1);
679+
assert_eq!(rx2.recv(), 2);
680+
},
681+
n = rx2.recv() => {
682+
assert_eq!(n, 2);
683+
assert_eq!(rx1.recv(), 1);
684+
}
685+
}
686+
})
651687
}

0 commit comments

Comments
 (0)