Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waking the other side on async consumer / producer close #30

Merged
merged 5 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions async/src/halves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ where
self.base.rb().is_closed()
}
fn close(&self) {
self.base.rb().close()
self.base.rb().close();
self.base.rb().wake_consumer();
}
}
impl<R: RbRef> AsyncProducer for AsyncProd<R>
Expand Down Expand Up @@ -181,7 +182,8 @@ where
self.base.rb().is_closed()
}
fn close(&self) {
self.base.rb().close()
self.base.rb().close();
self.base.rb().wake_producer();
}
}
impl<R: RbRef> AsyncConsumer for AsyncCons<R>
Expand Down
9 changes: 8 additions & 1 deletion async/src/rb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ impl<S: Storage> AsyncConsumer for AsyncRb<S> {
self.write.register(waker);
}
}
impl<S: Storage> AsyncRingBuffer for AsyncRb<S> {}
impl<S: Storage> AsyncRingBuffer for AsyncRb<S> {
fn wake_consumer(&self) {
self.write.wake()
}
fn wake_producer(&self) {
self.read.wake()
}
}

impl<S: Storage> SplitRef for AsyncRb<S> {
type RefProd<'a> = AsyncProd<&'a Self> where Self: 'a;
Expand Down
50 changes: 50 additions & 0 deletions async/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{async_transfer, traits::*, AsyncHeapRb};
use core::sync::atomic::{AtomicUsize, Ordering};
use futures::task::{noop_waker_ref, AtomicWaker};
use std::sync::Arc;
use std::{vec, vec::Vec};

#[test]
Expand Down Expand Up @@ -168,3 +169,52 @@ fn wait() {
},
);
}

#[test]
fn drop_close_prod() {
let (prod, mut cons) = AsyncHeapRb::<usize>::new(1).split();
let stage = Arc::new(AtomicUsize::new(0));
let stage_clone = stage.clone();
let t0 = std::thread::spawn(move || {
execute!(async {
drop(prod);
assert_eq!(stage.fetch_add(1, Ordering::SeqCst), 0);
});
});
let t1 = std::thread::spawn(move || {
execute!(async {
cons.wait_occupied(1).await;
assert_eq!(stage_clone.fetch_add(1, Ordering::SeqCst), 1);
assert!(cons.is_closed());
});
});
Comment on lines +178 to +190
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must separate as two polling, otherwise waking the wrong party also passes this test.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good note, join!-ing multiple futures can actually lead to falsely passing this test because of spurious wake-ups.

Also the usage of some async runtime (like Tokio or async-std) may be considered for testing. Each async block then can be ran in separate task.

t0.join().unwrap();
t1.join().unwrap();
}

#[test]
fn drop_close_cons() {
let (mut prod, mut cons) = AsyncHeapRb::<usize>::new(1).split();
let stage = Arc::new(AtomicUsize::new(0));
let stage_clone = stage.clone();
let t0 = std::thread::spawn(move || {
execute!(async {
prod.push(0).await.unwrap();
assert_eq!(stage.fetch_add(1, Ordering::SeqCst), 0);

prod.wait_vacant(1).await;
assert_eq!(stage.fetch_add(1, Ordering::SeqCst), 3);
assert!(prod.is_closed());
});
});
let t1 = std::thread::spawn(move || {
execute!(async {
cons.wait_occupied(1).await;
assert_eq!(stage_clone.fetch_add(1, Ordering::SeqCst), 1);
drop(cons);
assert_eq!(stage_clone.fetch_add(1, Ordering::SeqCst), 2);
});
});
t0.join().unwrap();
t1.join().unwrap();
}
4 changes: 2 additions & 2 deletions async/src/traits/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod consumer;
pub mod observer;
pub mod producer;
pub mod ring_buffer;

pub use consumer::AsyncConsumer;
pub use observer::AsyncObserver;
pub use producer::AsyncProducer;

pub trait AsyncRingBuffer: ringbuf::traits::RingBuffer + AsyncProducer + AsyncConsumer {}
pub use ring_buffer::AsyncRingBuffer;

pub use ringbuf::traits::*;
8 changes: 8 additions & 0 deletions async/src/traits/ring_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::consumer::AsyncConsumer;
use crate::producer::AsyncProducer;
use ringbuf::traits::RingBuffer;

pub trait AsyncRingBuffer: RingBuffer + AsyncProducer + AsyncConsumer {
fn wake_producer(&self);
fn wake_consumer(&self);
}
Comment on lines +5 to +8
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add two methods for waking producer / consumer for trait AsyncRingBuffer

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt that such low-level controls should be exposed to user but I can't suggest a better way to do this yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Just realized it's interesting that Rust does not actually have private trait fn.

6 changes: 1 addition & 5 deletions src/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use crate::{consumer::Consumer, producer::Producer};
/// `count` is the number of items being moved, if `None` - as much as possible items will be moved.
///
/// Returns number of items been moved.
pub fn transfer<T, C: Consumer<Item = T>, P: Producer<Item = T>>(
src: &mut C,
dst: &mut P,
count: Option<usize>,
) -> usize {
pub fn transfer<T, C: Consumer<Item = T>, P: Producer<Item = T>>(src: &mut C, dst: &mut P, count: Option<usize>) -> usize {
let (src_left, src_right) = src.occupied_slices();
let (dst_left, dst_right) = dst.vacant_slices_mut();
let src_iter = src_left.iter().chain(src_right.iter());
Expand Down
Loading