Skip to content

Commit 0608dff

Browse files
committed
feat: Re-add the is_notified method
cc #48 Signed-off-by: John Nunley <[email protected]>
1 parent b2e8e5c commit 0608dff

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,25 @@ impl<T> Event<T> {
457457
0
458458
}
459459

460+
/// Tell whether any listeners are currently notified.
461+
///
462+
/// # Examples
463+
///
464+
/// ```
465+
/// use event_listener::Event;
466+
///
467+
/// let event = Event::new();
468+
/// let listener = event.listen();
469+
/// assert!(!event.is_notified());
470+
///
471+
/// event.notify(1);
472+
/// assert!(event.is_notified());
473+
/// ```
474+
#[inline]
475+
pub fn is_notified(&self) -> bool {
476+
self.try_inner().map_or(false, |inner| inner.is_notified())
477+
}
478+
460479
/// Returns a reference to the inner state if it was initialized.
461480
#[inline]
462481
fn try_inner(&self) -> Option<&Inner<T>> {

src/linked_list/lock_free.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,28 @@ impl<T> Inner<T> {
100100
true
101101
}
102102

103+
/// Tell whether any listeners are currently notified.
104+
#[inline]
105+
pub fn is_notified(&self) -> bool {
106+
let mut head = self.head.load(Ordering::Acquire);
107+
108+
loop {
109+
if head == 0 {
110+
// No entries left.
111+
return false;
112+
}
113+
let slot = self.slots.get(head);
114+
115+
// If this slot isn't occupied, use the next one.
116+
let state = slot.state.load(Ordering::Acquire);
117+
if state & OCCUPIED == 0 {
118+
head = slot.next.load(Ordering::Acquire);
119+
} else {
120+
return state & NOTIFIED != 0;
121+
}
122+
}
123+
}
124+
103125
/// Insert a listener into this linked list.
104126
#[cold]
105127
pub(crate) fn listen(&self) -> Listener<T> {

src/linked_list/mutex.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ impl<T> Inner<T> {
5555
self.notified.load(Ordering::Acquire) < limit
5656
}
5757

58+
/// Tell if any listeners are currently notified.
59+
#[inline]
60+
pub(crate) fn is_notified(&self) -> bool {
61+
self.notified.load(Ordering::Acquire) > 0
62+
}
63+
5864
/// Create a listener for this linked list.
5965
#[cold]
6066
pub(crate) fn listen(&self) -> Listener<T> {

tests/notify.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ fn notify() {
2222
let mut l2 = event.listen();
2323
let mut l3 = event.listen();
2424

25+
assert!(!event.is_notified());
2526
assert!(!is_notified(&mut l1));
2627
assert!(!is_notified(&mut l2));
2728
assert!(!is_notified(&mut l3));
2829

2930
assert_eq!(event.notify(2), 2);
31+
assert!(event.is_notified());
3032
assert_eq!(event.notify(1), 0);
33+
assert!(event.is_notified());
3134
assert!(is_notified(&mut l1));
3235
assert!(is_notified(&mut l2));
3336
assert!(!is_notified(&mut l3));
@@ -41,9 +44,11 @@ fn notify_additional() {
4144
let mut l2 = event.listen();
4245
let mut l3 = event.listen();
4346

47+
assert!(!event.is_notified());
4448
assert_eq!(event.notify_additional(1), 1);
4549
assert_eq!(event.notify(1), 0);
4650
assert_eq!(event.notify_additional(1), 1);
51+
assert!(event.is_notified());
4752

4853
assert!(is_notified(&mut l1));
4954
assert!(is_notified(&mut l2));
@@ -54,6 +59,7 @@ fn notify_additional() {
5459
fn notify_zero() {
5560
let event = Event::new();
5661
assert_eq!(event.notify(1), 0);
62+
assert!(!event.is_notified());
5763
}
5864

5965
#[test]
@@ -119,8 +125,10 @@ fn notify_all() {
119125

120126
assert!(!is_notified(&mut l1));
121127
assert!(!is_notified(&mut l2));
128+
assert!(!event.is_notified());
122129

123130
assert_eq!(event.notify(usize::MAX), 2);
131+
assert!(event.is_notified());
124132
assert!(is_notified(&mut l1));
125133
assert!(is_notified(&mut l2));
126134
}

0 commit comments

Comments
 (0)