add contention test

This commit is contained in:
Jorge Aparicio 2017-11-09 15:33:54 +01:00
parent 612bf44a78
commit 40994962e2
3 changed files with 52 additions and 1 deletions

View File

@ -11,8 +11,9 @@ main() {
cargo test --target $TARGET
cargo test --target $TARGET --release
export TSAN_OPTIONS="suppressions=$(pwd)/blacklist.txt"
export RUSTFLAGS="-Z sanitizer=thread"
export RUST_TEST_THREADS=1
export TSAN_OPTIONS="suppressions=$(pwd)/blacklist.txt"
cargo test --test tsan --target $TARGET
cargo test --test tsan --target $TARGET --release

View File

@ -129,6 +129,11 @@ where
}
}
/// Returns `true` if the ring buffer has a length of 0
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// Iterates from the front of the queue to the back
pub fn iter(&self) -> Iter<T, A> {
Iter {

View File

@ -73,3 +73,48 @@ fn scoped() {
rb.dequeue().unwrap();
}
#[test]
fn contention() {
const N: usize = 1024;
let mut rb: RingBuffer<u8, [u8; N]> = RingBuffer::new();
{
let (mut p, mut c) = rb.split();
Pool::new(2).scoped(move |scope| {
scope.execute(move || {
let mut sum: u32 = 0;
for i in 0..N {
let i = i as u8;
sum = sum.wrapping_add(i as u32);
while let Err(_) = p.enqueue(i) {}
}
println!("producer: {}", sum);
});
scope.execute(move || {
let mut sum: u32 = 0;
for _ in 0..N {
loop {
match c.dequeue() {
Some(v) => {
sum = sum.wrapping_add(v as u32);
break;
}
_ => {}
}
}
}
println!("consumer: {}", sum);
});
});
}
assert!(rb.is_empty());
}