mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-28 12:10:37 +00:00

Introduce a tokio-sync crate containing useful synchronization primitives for programs written using Tokio. The initial release contains: * An mpsc channel * A oneshot channel * A semaphore implementation * An `AtomicTask` primitive. The `oneshot` and `mpsc` channels are new implementations providing improved performance characteristics. In some benchmarks, the new mpsc channel shows up to 7x improvement over the version provided by the `futures` crate. Unfortunately, the `oneshot` implementation only provides a slight performance improvement as it is mostly limited by the `futures` 0.1 task system. Once updated to the `std` version of `Future` (currently nightly only), much greater performance improvements should be achievable by `oneshot`. Additionally, he implementations provided here are checked using [Loom](http://github.com/carllerche/loom/), which provides greater confidence of correctness.
72 lines
1.4 KiB
Rust
72 lines
1.4 KiB
Rust
extern crate futures;
|
|
#[macro_use]
|
|
extern crate loom;
|
|
|
|
macro_rules! if_fuzz {
|
|
($($t:tt)*) => {
|
|
$($t)*
|
|
}
|
|
}
|
|
|
|
#[path = "../src/mpsc/list.rs"]
|
|
#[allow(warnings)]
|
|
mod list;
|
|
|
|
#[path = "../src/mpsc/block.rs"]
|
|
#[allow(warnings)]
|
|
mod block;
|
|
|
|
const BLOCK_CAP: usize = 2;
|
|
|
|
use loom::thread;
|
|
|
|
use std::sync::Arc;
|
|
|
|
#[test]
|
|
fn smoke() {
|
|
use block::Read::*;
|
|
|
|
const NUM_TX: usize = 2;
|
|
const NUM_MSG: usize = 2;
|
|
|
|
loom::fuzz(|| {
|
|
let (tx, mut rx) = list::channel();
|
|
let tx = Arc::new(tx);
|
|
|
|
for th in 0..NUM_TX {
|
|
let tx = tx.clone();
|
|
|
|
thread::spawn(move || {
|
|
for i in 0..NUM_MSG {
|
|
tx.push((th, i));
|
|
}
|
|
debug!(" + tx thread done");
|
|
});
|
|
}
|
|
|
|
let mut next = vec![0; NUM_TX];
|
|
|
|
loop {
|
|
debug!(" + rx.pop()");
|
|
match rx.pop(&tx) {
|
|
Some(Value((th, v))) => {
|
|
debug!(" + pop() -> Some(Value({}))", v);
|
|
assert_eq!(v, next[th]);
|
|
next[th] += 1;
|
|
|
|
if next.iter().all(|&i| i == NUM_MSG) {
|
|
break;
|
|
}
|
|
}
|
|
Some(Closed) => {
|
|
panic!();
|
|
}
|
|
None => {
|
|
debug!(" + pop() -> None");
|
|
loom::yield_now();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|