Skip to content

Commit

Permalink
feat: Re-add the is_notified method
Browse files Browse the repository at this point in the history
cc #48

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Apr 14, 2024
1 parent b2e8e5c commit 0608dff
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,25 @@ impl<T> Event<T> {
0
}

/// Tell whether any listeners are currently notified.
///
/// # Examples
///
/// ```
/// use event_listener::Event;
///
/// let event = Event::new();
/// let listener = event.listen();
/// assert!(!event.is_notified());
///
/// event.notify(1);
/// assert!(event.is_notified());
/// ```
#[inline]
pub fn is_notified(&self) -> bool {
self.try_inner().map_or(false, |inner| inner.is_notified())
}

/// Returns a reference to the inner state if it was initialized.
#[inline]
fn try_inner(&self) -> Option<&Inner<T>> {
Expand Down
22 changes: 22 additions & 0 deletions src/linked_list/lock_free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ impl<T> Inner<T> {
true
}

/// Tell whether any listeners are currently notified.
#[inline]
pub fn is_notified(&self) -> bool {
let mut head = self.head.load(Ordering::Acquire);

loop {
if head == 0 {
// No entries left.
return false;
}
let slot = self.slots.get(head);

// If this slot isn't occupied, use the next one.
let state = slot.state.load(Ordering::Acquire);
if state & OCCUPIED == 0 {
head = slot.next.load(Ordering::Acquire);
} else {
return state & NOTIFIED != 0;
}
}
}

/// Insert a listener into this linked list.
#[cold]
pub(crate) fn listen(&self) -> Listener<T> {
Expand Down
6 changes: 6 additions & 0 deletions src/linked_list/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ impl<T> Inner<T> {
self.notified.load(Ordering::Acquire) < limit
}

/// Tell if any listeners are currently notified.
#[inline]
pub(crate) fn is_notified(&self) -> bool {
self.notified.load(Ordering::Acquire) > 0
}

/// Create a listener for this linked list.
#[cold]
pub(crate) fn listen(&self) -> Listener<T> {
Expand Down
8 changes: 8 additions & 0 deletions tests/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ fn notify() {
let mut l2 = event.listen();
let mut l3 = event.listen();

assert!(!event.is_notified());
assert!(!is_notified(&mut l1));
assert!(!is_notified(&mut l2));
assert!(!is_notified(&mut l3));

assert_eq!(event.notify(2), 2);
assert!(event.is_notified());
assert_eq!(event.notify(1), 0);
assert!(event.is_notified());
assert!(is_notified(&mut l1));
assert!(is_notified(&mut l2));
assert!(!is_notified(&mut l3));
Expand All @@ -41,9 +44,11 @@ fn notify_additional() {
let mut l2 = event.listen();
let mut l3 = event.listen();

assert!(!event.is_notified());
assert_eq!(event.notify_additional(1), 1);
assert_eq!(event.notify(1), 0);
assert_eq!(event.notify_additional(1), 1);
assert!(event.is_notified());

assert!(is_notified(&mut l1));
assert!(is_notified(&mut l2));
Expand All @@ -54,6 +59,7 @@ fn notify_additional() {
fn notify_zero() {
let event = Event::new();
assert_eq!(event.notify(1), 0);
assert!(!event.is_notified());
}

#[test]
Expand Down Expand Up @@ -119,8 +125,10 @@ fn notify_all() {

assert!(!is_notified(&mut l1));
assert!(!is_notified(&mut l2));
assert!(!event.is_notified());

assert_eq!(event.notify(usize::MAX), 2);
assert!(event.is_notified());
assert!(is_notified(&mut l1));
assert!(is_notified(&mut l2));
}
Expand Down

0 comments on commit 0608dff

Please sign in to comment.