|
|
use std::{ |
|
|
fmt::{Debug, Formatter}, |
|
|
future::Future, |
|
|
mem::replace, |
|
|
pin::Pin, |
|
|
}; |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
use std::{ |
|
|
sync::Arc, |
|
|
task::{Poll, ready}, |
|
|
time::Duration, |
|
|
}; |
|
|
|
|
|
#[cfg(feature = "hanging_detection")] |
|
|
use tokio::time::{Timeout, timeout}; |
|
|
|
|
|
pub struct Event { |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
description: Arc<dyn Fn() -> String + Sync + Send>, |
|
|
event: event_listener::Event, |
|
|
} |
|
|
|
|
|
impl Event { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[inline(always)] |
|
|
pub fn new<InnerFn>(_description: impl FnOnce() -> InnerFn) -> Self |
|
|
where |
|
|
InnerFn: Fn() -> String + Sync + Send + 'static, |
|
|
{ |
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
return Self { |
|
|
event: event_listener::Event::new(), |
|
|
}; |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
return Self { |
|
|
description: Arc::new((_description)()), |
|
|
event: event_listener::Event::new(), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
pub fn listen(&self) -> EventListener { |
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
return EventListener { |
|
|
listener: self.event.listen(), |
|
|
}; |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
return EventListener { |
|
|
description: self.description.clone(), |
|
|
note: Arc::new(String::new), |
|
|
future: Some(Box::pin(timeout( |
|
|
Duration::from_secs(30), |
|
|
self.event.listen(), |
|
|
))), |
|
|
duration: Duration::from_secs(30), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn listen_with_note<InnerFn>(&self, _note: impl FnOnce() -> InnerFn) -> EventListener |
|
|
where |
|
|
InnerFn: Fn() -> String + Sync + Send + 'static, |
|
|
{ |
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
return EventListener { |
|
|
listener: self.event.listen(), |
|
|
}; |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
return EventListener { |
|
|
description: self.description.clone(), |
|
|
note: Arc::new((_note)()), |
|
|
future: Some(Box::pin(timeout( |
|
|
Duration::from_secs(30), |
|
|
self.event.listen(), |
|
|
))), |
|
|
duration: Duration::from_secs(30), |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
pub fn take(&mut self) -> Event { |
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
return Self { |
|
|
event: replace(&mut self.event, event_listener::Event::new()), |
|
|
}; |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
return Self { |
|
|
description: self.description.clone(), |
|
|
event: replace(&mut self.event, event_listener::Event::new()), |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
impl Event { |
|
|
|
|
|
pub fn notify(&self, n: usize) { |
|
|
self.event.notify(n); |
|
|
} |
|
|
} |
|
|
|
|
|
impl Debug for Event { |
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
|
|
let mut t = f.debug_tuple("Event"); |
|
|
#[cfg(feature = "hanging_detection")] |
|
|
t.field(&(self.description)()); |
|
|
t.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
pub struct EventListener { |
|
|
listener: event_listener::EventListener, |
|
|
} |
|
|
|
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
impl Debug for EventListener { |
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
|
|
f.debug_tuple("EventListener").finish() |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(not(feature = "hanging_detection"))] |
|
|
impl Future for EventListener { |
|
|
type Output = (); |
|
|
|
|
|
fn poll( |
|
|
self: Pin<&mut Self>, |
|
|
cx: &mut std::task::Context<'_>, |
|
|
) -> std::task::Poll<Self::Output> { |
|
|
let listener = unsafe { self.map_unchecked_mut(|s| &mut s.listener) }; |
|
|
listener.poll(cx) |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(feature = "hanging_detection")] |
|
|
pub struct EventListener { |
|
|
description: Arc<dyn Fn() -> String + Sync + Send>, |
|
|
note: Arc<dyn Fn() -> String + Sync + Send>, |
|
|
|
|
|
|
|
|
future: Option<std::pin::Pin<Box<Timeout<event_listener::EventListener>>>>, |
|
|
duration: Duration, |
|
|
} |
|
|
|
|
|
#[cfg(feature = "hanging_detection")] |
|
|
impl Debug for EventListener { |
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
|
|
let mut t = f.debug_tuple("EventListener"); |
|
|
t.field(&(self.description)()); |
|
|
let note = (self.note)(); |
|
|
if !note.is_empty() { |
|
|
t.field(¬e); |
|
|
} |
|
|
t.finish() |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(feature = "hanging_detection")] |
|
|
impl Future for EventListener { |
|
|
type Output = (); |
|
|
|
|
|
fn poll( |
|
|
mut self: Pin<&mut Self>, |
|
|
cx: &mut std::task::Context<'_>, |
|
|
) -> std::task::Poll<Self::Output> { |
|
|
while let Some(future) = self.future.as_mut() { |
|
|
match ready!(future.as_mut().poll(cx)) { |
|
|
Ok(_) => { |
|
|
self.future = None; |
|
|
return Poll::Ready(()); |
|
|
} |
|
|
Err(_) => { |
|
|
let note = (self.note)(); |
|
|
let description = (self.description)(); |
|
|
if note.is_empty() { |
|
|
eprintln!( |
|
|
"EventListener({}) is potentially hanging, waiting for {}s", |
|
|
description, |
|
|
self.duration.as_secs(), |
|
|
); |
|
|
} else { |
|
|
eprintln!( |
|
|
"EventListener({}) is potentially hanging, waiting for {}s from {}", |
|
|
description, |
|
|
self.duration.as_secs(), |
|
|
note |
|
|
); |
|
|
} |
|
|
self.duration *= 2; |
|
|
|
|
|
|
|
|
let future = self.future.take().unwrap(); |
|
|
self.future = Some(Box::pin(timeout( |
|
|
self.duration, |
|
|
|
|
|
|
|
|
unsafe { std::pin::Pin::into_inner_unchecked(future) }.into_inner(), |
|
|
))); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
Poll::Ready(()) |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(all(test, not(feature = "hanging_detection")))] |
|
|
mod tests { |
|
|
use std::hint::black_box; |
|
|
|
|
|
use tokio::time::{Duration, timeout}; |
|
|
|
|
|
use super::*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[tokio::test] |
|
|
async fn ensure_dead_code_elimination() { |
|
|
fn dead_fn() { |
|
|
|
|
|
unsafe { |
|
|
unsafe extern "C" { |
|
|
fn trigger_link_error() -> !; |
|
|
} |
|
|
trigger_link_error(); |
|
|
} |
|
|
} |
|
|
|
|
|
let event = black_box(Event::new(|| { |
|
|
dead_fn(); |
|
|
|| { |
|
|
dead_fn(); |
|
|
String::new() |
|
|
} |
|
|
})); |
|
|
let listener = black_box(event.listen_with_note(|| { |
|
|
dead_fn(); |
|
|
|| { |
|
|
dead_fn(); |
|
|
String::new() |
|
|
} |
|
|
})); |
|
|
|
|
|
let _ = black_box(timeout(Duration::from_millis(10), listener)).await; |
|
|
} |
|
|
} |
|
|
|