From ac18bdf61736aeea64896b12bf655a3e6141f44f Mon Sep 17 00:00:00 2001 From: John Nunley Date: Sat, 27 Jan 2024 12:21:00 -0800 Subject: [PATCH] chore: Fix up some minor bits before release Signed-off-by: John Nunley --- src/lib.rs | 26 +++++++++++++++++--------- src/no_std.rs | 9 ++++----- src/std.rs | 22 +++++++--------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index c5d338a..969f807 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -174,9 +174,9 @@ impl fmt::Debug for Event { match self.try_inner() { Some(inner) => { let notified_count = inner.notified.load(Ordering::Relaxed); - let total_count = match inner.list.total_listeners() { - Ok(total_count) => total_count, - Err(_) => { + let total_count = match inner.list.try_total_listeners() { + Some(total_count) => total_count, + None => { return f .debug_tuple("Event") .field(&format_args!("")) @@ -480,12 +480,20 @@ impl Event { inner } - /// Return the listener count by acquiring a lock. + /// Get the number of listeners currently listening to this [`Event`]. /// - /// This is just a snapshot of the number of listeners at this point in time. - /// It is possible for the actual number to change at any point. - /// The number should only ever be used as a hint. - /// This is only available when `std` feature is enabled. + /// This call returns the number of [`EventListener`]s that are currently listening to + /// this event. It does this by acquiring the internal event lock and reading the listener + /// count. Therefore it is only available for `std`-enabled platforms. + /// + /// # Caveats + /// + /// This function returns just a snapshot of the number of listeners at this point in time. + /// Due to the nature of multi-threaded CPUs, it is possible that this number will be + /// inaccurate by the time that this function returns. + /// + /// It is possible for the actual number to change at any point. Therefore, the number should + /// only ever be used as a hint. /// /// # Examples /// @@ -510,7 +518,7 @@ impl Event { #[inline] pub fn total_listeners(&self) -> usize { if let Some(inner) = self.try_inner() { - inner.list.total_listeners_wait() + inner.list.total_listeners() } else { 0 } diff --git a/src/no_std.rs b/src/no_std.rs index d37b1f2..1341103 100644 --- a/src/no_std.rs +++ b/src/no_std.rs @@ -240,11 +240,10 @@ impl List { queue: concurrent_queue::ConcurrentQueue::unbounded(), } } - pub fn total_listeners(&self) -> Result { - self.inner - .try_lock() - .map(|lock| Ok(lock.listeners.len())) - .unwrap_or(Err("")) + + /// Try to get the total number of listeners without blocking. + pub(super) fn try_total_listeners(&self) -> Option { + self.inner.try_lock().map(|lock| lock.listeners.len()) } } diff --git a/src/std.rs b/src/std.rs index fb06819..239dc74 100644 --- a/src/std.rs +++ b/src/std.rs @@ -44,23 +44,15 @@ impl List { notified: 0, })) } - // Accessor method because fields are private, not sure how to go around it. - pub fn total_listeners(&self) -> Result { - match self.0.try_lock() { - Ok(mutex) => { - let len = mutex.len; - Ok(len) - } - Err(_) => Err(""), - } + + /// Get the total number of listeners without blocking. + pub(crate) fn try_total_listeners(&self) -> Option { + self.0.try_lock().ok().map(|list| list.len) } - // Get the listener count by blocking. - pub(crate) fn total_listeners_wait(&self) -> usize { - match self.0.lock() { - Ok(mutex) => mutex.len, - Err(err) => panic!("{err}"), - } + /// Get the total number of listeners with blocking. + pub(crate) fn total_listeners(&self) -> usize { + self.0.lock().unwrap_or_else(|e| e.into_inner()).len } }