Skip to content

Commit

Permalink
chore: Fix up some minor bits before release
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull authored Jan 27, 2024
1 parent a68f5ee commit ac18bdf
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
26 changes: 17 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ impl<T> fmt::Debug for Event<T> {
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!("<locked>"))
Expand Down Expand Up @@ -480,12 +480,20 @@ impl<T> Event<T> {
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
///
Expand All @@ -510,7 +518,7 @@ impl<T> Event<T> {
#[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
}
Expand Down
9 changes: 4 additions & 5 deletions src/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,10 @@ impl<T> List<T> {
queue: concurrent_queue::ConcurrentQueue::unbounded(),
}
}
pub fn total_listeners(&self) -> Result<usize, &str> {
self.inner
.try_lock()
.map(|lock| Ok(lock.listeners.len()))
.unwrap_or(Err("<locked>"))

/// Try to get the total number of listeners without blocking.
pub(super) fn try_total_listeners(&self) -> Option<usize> {
self.inner.try_lock().map(|lock| lock.listeners.len())
}
}

Expand Down
22 changes: 7 additions & 15 deletions src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,15 @@ impl<T> List<T> {
notified: 0,
}))
}
// Accessor method because fields are private, not sure how to go around it.
pub fn total_listeners(&self) -> Result<usize, &str> {
match self.0.try_lock() {
Ok(mutex) => {
let len = mutex.len;
Ok(len)
}
Err(_) => Err("<locked>"),
}

/// Get the total number of listeners without blocking.
pub(crate) fn try_total_listeners(&self) -> Option<usize> {
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
}
}

Expand Down

0 comments on commit ac18bdf

Please sign in to comment.