Fix a bug of channels

This bugfix ensures that when an object of Producer/Consumer for
channels is dropped, its shutdown method is called automatically. This ensures
that the peer of a Producer/Consumer gets notified and won't wait indefinitely.
This commit is contained in:
Tate, Hongliang Tian 2020-12-01 14:59:34 +00:00 committed by Zongmin.Gu
parent ea64939cac
commit 9809d81c4e

@ -90,9 +90,15 @@ impl<I: Copy> Channel<I> {
} }
} }
// A macro to implemennt the common part of the two end point types, Producer<I>
// and Consumer<I>.
macro_rules! impl_end_point_type {
($(#[$attr:meta])* $vis:vis struct $end_point:ident<$i:ident> {
inner: $inner:ident<$_:ident>,
}) => (
/// An endpoint is either the producer or consumer of a channel. /// An endpoint is either the producer or consumer of a channel.
pub struct EndPoint<T> { $(#[$attr])* $vis struct $end_point<$i> {
inner: SgxMutex<T>, inner: SgxMutex<$inner<$i>>,
state: Arc<State>, state: Arc<State>,
observer: Arc<WaiterQueueObserver<IoEvents>>, observer: Arc<WaiterQueueObserver<IoEvents>>,
notifier: Arc<IoNotifier>, notifier: Arc<IoNotifier>,
@ -100,8 +106,8 @@ pub struct EndPoint<T> {
is_nonblocking: AtomicBool, is_nonblocking: AtomicBool,
} }
impl<T> EndPoint<T> { impl<$i> $end_point<$i> {
fn new(inner: T, state: Arc<State>) -> Self { fn new(inner: $inner<$i>, state: Arc<State>) -> Self {
let inner = SgxMutex::new(inner); let inner = SgxMutex::new(inner);
let observer = WaiterQueueObserver::new(); let observer = WaiterQueueObserver::new();
let notifier = Arc::new(IoNotifier::new()); let notifier = Arc::new(IoNotifier::new());
@ -148,36 +154,7 @@ impl<T> EndPoint<T> {
} }
} }
} }
)
/// The state of a channel shared by the two endpoints of a channel.
struct State {
is_producer_shutdown: AtomicBool,
is_consumer_shutdown: AtomicBool,
}
impl State {
pub fn new() -> Self {
Self {
is_producer_shutdown: AtomicBool::new(false),
is_consumer_shutdown: AtomicBool::new(false),
}
}
pub fn is_producer_shutdown(&self) -> bool {
self.is_producer_shutdown.load(Ordering::Acquire)
}
pub fn is_consumer_shutdown(&self) -> bool {
self.is_consumer_shutdown.load(Ordering::Acquire)
}
pub fn set_producer_shutdown(&self) {
self.is_producer_shutdown.store(true, Ordering::Release)
}
pub fn set_consumer_shutdown(&self) {
self.is_consumer_shutdown.store(true, Ordering::Release)
}
} }
// Just like a normal loop, except that a waiter queue (as well as a waiter) // Just like a normal loop, except that a waiter queue (as well as a waiter)
@ -206,8 +183,12 @@ macro_rules! waiter_loop {
}; };
} }
impl_end_point_type! {
/// Producer is the writable endpoint of a channel. /// Producer is the writable endpoint of a channel.
pub type Producer<I> = EndPoint<RbProducer<I>>; pub struct Producer<I> {
inner: RbProducer<I>,
}
}
impl<I> Producer<I> { impl<I> Producer<I> {
pub fn push(&self, mut item: I) -> Result<()> { pub fn push(&self, mut item: I) -> Result<()> {
@ -260,6 +241,9 @@ impl<I> Producer<I> {
{ {
// It is important to hold this lock while updating the state // It is important to hold this lock while updating the state
let inner = self.inner.lock().unwrap(); let inner = self.inner.lock().unwrap();
if self.state.is_producer_shutdown() {
return;
}
self.state.set_producer_shutdown(); self.state.set_producer_shutdown();
} }
@ -307,8 +291,18 @@ impl<I: Copy> Producer<I> {
} }
} }
impl<I> Drop for Producer<I> {
fn drop(&mut self) {
self.shutdown();
}
}
impl_end_point_type! {
/// Consumer is the readable endpoint of a channel. /// Consumer is the readable endpoint of a channel.
pub type Consumer<I> = EndPoint<RbConsumer<I>>; pub struct Consumer<I> {
inner: RbConsumer<I>,
}
}
impl<I> Consumer<I> { impl<I> Consumer<I> {
pub fn pop(&self) -> Result<Option<I>> { pub fn pop(&self) -> Result<Option<I>> {
@ -361,6 +355,9 @@ impl<I> Consumer<I> {
{ {
// It is important to hold this lock while updating the state // It is important to hold this lock while updating the state
let inner = self.inner.lock().unwrap(); let inner = self.inner.lock().unwrap();
if self.state.is_consumer_shutdown() {
return;
}
self.state.set_consumer_shutdown(); self.state.set_consumer_shutdown();
} }
@ -410,3 +407,40 @@ impl<I: Copy> Consumer<I> {
); );
} }
} }
impl<I> Drop for Consumer<I> {
fn drop(&mut self) {
self.shutdown();
}
}
/// The state of a channel shared by the two endpoints of a channel.
struct State {
is_producer_shutdown: AtomicBool,
is_consumer_shutdown: AtomicBool,
}
impl State {
pub fn new() -> Self {
Self {
is_producer_shutdown: AtomicBool::new(false),
is_consumer_shutdown: AtomicBool::new(false),
}
}
pub fn is_producer_shutdown(&self) -> bool {
self.is_producer_shutdown.load(Ordering::Acquire)
}
pub fn is_consumer_shutdown(&self) -> bool {
self.is_consumer_shutdown.load(Ordering::Acquire)
}
pub fn set_producer_shutdown(&self) {
self.is_producer_shutdown.store(true, Ordering::Release)
}
pub fn set_consumer_shutdown(&self) {
self.is_consumer_shutdown.store(true, Ordering::Release)
}
}