Skip to content

Commit

Permalink
auto merge of #12991 : alexcrichton/rust/sync-chan, r=brson
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Mar 25, 2014
2 parents 6bf3fca + 56cae9b commit 1e6e98c
Show file tree
Hide file tree
Showing 11 changed files with 1,229 additions and 115 deletions.
698 changes: 696 additions & 2 deletions src/libstd/comm/mod.rs

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions src/libstd/comm/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,4 +648,40 @@ mod test {
tx1.send(());
rx2.recv();
})

test!(fn sync1() {
let (tx, rx) = sync_channel(1);
tx.send(1);
select! {
n = rx.recv() => { assert_eq!(n, 1); }
}
})

test!(fn sync2() {
let (tx, rx) = sync_channel(0);
spawn(proc() {
for _ in range(0, 100) { task::deschedule() }
tx.send(1);
});
select! {
n = rx.recv() => { assert_eq!(n, 1); }
}
})

test!(fn sync3() {
let (tx1, rx1) = sync_channel(0);
let (tx2, rx2) = channel();
spawn(proc() { tx1.send(1); });
spawn(proc() { tx2.send(2); });
select! {
n = rx1.recv() => {
assert_eq!(n, 1);
assert_eq!(rx2.recv(), 2);
},
n = rx2.recv() => {
assert_eq!(n, 2);
assert_eq!(rx1.recv(), 1);
}
}
})
}
Loading

0 comments on commit 1e6e98c

Please sign in to comment.