mirror of
https://github.com/rust-embedded/heapless.git
synced 2025-09-27 12:30:35 +00:00
add contention test
This commit is contained in:
parent
612bf44a78
commit
40994962e2
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user