Skip to content

Commit 6345794

Browse files
committed
Make the notify() function return notified count
Fix off-by-one error in implementation
1 parent fb90f08 commit 6345794

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl<T> Event<T> {
360360
/// event.notify(1.additional().relaxed());
361361
/// ```
362362
#[inline]
363-
pub fn notify(&self, notify: impl IntoNotification<Tag = T>) {
363+
pub fn notify(&self, notify: impl IntoNotification<Tag = T>) -> usize {
364364
let notify = notify.into_notification();
365365

366366
// Make sure the notification comes after whatever triggered it.
@@ -376,9 +376,11 @@ impl<T> Event<T> {
376376
// Notify if there is at least one unnotified listener and the number of notified
377377
// listeners is less than `limit`.
378378
if inner.notified.load(Ordering::Acquire) < limit {
379-
inner.notify(notify);
379+
return inner.notify(notify);
380380
}
381381
}
382+
383+
0
382384
}
383385

384386
/// Return a reference to the inner state if it has been initialized.
@@ -489,7 +491,7 @@ impl Event<()> {
489491
/// event.notify_relaxed(2);
490492
/// ```
491493
#[inline]
492-
pub fn notify_relaxed(&self, n: usize) {
494+
pub fn notify_relaxed(&self, n: usize) -> usize {
493495
self.notify(n.relaxed())
494496
}
495497

@@ -538,7 +540,7 @@ impl Event<()> {
538540
/// event.notify_additional(1);
539541
/// ```
540542
#[inline]
541-
pub fn notify_additional(&self, n: usize) {
543+
pub fn notify_additional(&self, n: usize) -> usize {
542544
self.notify(n.additional())
543545
}
544546

@@ -592,7 +594,7 @@ impl Event<()> {
592594
/// event.notify_additional_relaxed(1);
593595
/// ```
594596
#[inline]
595-
pub fn notify_additional_relaxed(&self, n: usize) {
597+
pub fn notify_additional_relaxed(&self, n: usize) -> usize {
596598
self.notify(n.additional().relaxed())
597599
}
598600
}

src/no_std.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ impl<T> crate::Inner<T> {
110110

111111
/// Notifies a number of entries.
112112
#[cold]
113-
pub(crate) fn notify(&self, mut notify: impl Notification<Tag = T>) {
113+
pub(crate) fn notify(&self, mut notify: impl Notification<Tag = T>) -> usize {
114114
match self.try_lock() {
115115
Some(mut guard) => {
116116
// Notify the listeners.
117-
guard.notify(notify);
117+
guard.notify(notify)
118118
}
119119

120120
None => {
@@ -140,6 +140,9 @@ impl<T> crate::Inner<T> {
140140
));
141141

142142
self.list.queue.push(node);
143+
144+
// We haven't notified anyone yet.
145+
0
143146
}
144147
}
145148
}
@@ -556,23 +559,24 @@ impl<T> ListenerSlab<T> {
556559

557560
/// Notifies a number of listeners.
558561
#[cold]
559-
pub(crate) fn notify(&mut self, mut notify: impl Notification<Tag = T>) {
562+
pub(crate) fn notify(&mut self, mut notify: impl Notification<Tag = T>) -> usize {
560563
let mut n = notify.count(Internal::new());
561564
let is_additional = notify.is_additional(Internal::new());
562565
if !is_additional {
563566
// Make sure we're not notifying more than we have.
564567
if n <= self.notified {
565-
return;
568+
return 0;
566569
}
567570
n -= self.notified;
568571
}
569572

573+
let original_count = n;
570574
while n > 0 {
571575
n -= 1;
572576

573577
// Notify the next entry.
574578
match self.start {
575-
None => break,
579+
None => return original_count - n - 1,
576580

577581
Some(e) => {
578582
// Get the entry and move the pointer forwards.
@@ -593,6 +597,8 @@ impl<T> ListenerSlab<T> {
593597
}
594598
}
595599
}
600+
601+
original_count - n
596602
}
597603

598604
/// Register a task to be notified when the event is triggered.

src/std.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<T> crate::Inner<T> {
110110

111111
/// Notifies a number of entries.
112112
#[cold]
113-
pub(crate) fn notify(&self, notify: impl Notification<Tag = T>) {
113+
pub(crate) fn notify(&self, notify: impl Notification<Tag = T>) -> usize {
114114
self.lock().notify(notify)
115115
}
116116

@@ -236,23 +236,24 @@ impl<T> Inner<T> {
236236
}
237237

238238
#[cold]
239-
fn notify(&mut self, mut notify: impl Notification<Tag = T>) {
239+
fn notify(&mut self, mut notify: impl Notification<Tag = T>) -> usize {
240240
let mut n = notify.count(Internal::new());
241241
let is_additional = notify.is_additional(Internal::new());
242242

243243
if !is_additional {
244244
if n < self.notified {
245-
return;
245+
return 0;
246246
}
247247
n -= self.notified;
248248
}
249249

250+
let original_count = n;
250251
while n > 0 {
251252
n -= 1;
252253

253254
// Notify the next entry.
254255
match self.next {
255-
None => break,
256+
None => return original_count - n - 1,
256257

257258
Some(e) => {
258259
// Get the entry and move the pointer forwards.
@@ -273,6 +274,8 @@ impl<T> Inner<T> {
273274
}
274275
}
275276
}
277+
278+
original_count - n
276279
}
277280
}
278281

tests/notify.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn notify() {
2424
assert!(!is_notified(l2.as_mut()));
2525
assert!(!is_notified(l3.as_mut()));
2626

27-
event.notify(2);
28-
event.notify(1);
27+
assert_eq!(event.notify(2), 2);
28+
assert_eq!(event.notify(1), 0);
2929

3030
assert!(is_notified(l1.as_mut()));
3131
assert!(is_notified(l2.as_mut()));
@@ -40,9 +40,9 @@ fn notify_additional() {
4040
let mut l2 = event.listen();
4141
let mut l3 = event.listen();
4242

43-
event.notify_additional(1);
44-
event.notify(1);
45-
event.notify_additional(1);
43+
assert_eq!(event.notify_additional(1), 1);
44+
assert_eq!(event.notify(1), 0);
45+
assert_eq!(event.notify_additional(1), 1);
4646

4747
assert!(is_notified(l1.as_mut()));
4848
assert!(is_notified(l2.as_mut()));
@@ -59,11 +59,11 @@ fn notify_one() {
5959
assert!(!is_notified(l1.as_mut()));
6060
assert!(!is_notified(l2.as_mut()));
6161

62-
event.notify(1);
62+
assert_eq!(event.notify(1), 1);
6363
assert!(is_notified(l1.as_mut()));
6464
assert!(!is_notified(l2.as_mut()));
6565

66-
event.notify(1);
66+
assert_eq!(event.notify(1), 1);
6767
assert!(is_notified(l2.as_mut()));
6868
}
6969

@@ -77,7 +77,7 @@ fn notify_all() {
7777
assert!(!is_notified(l1.as_mut()));
7878
assert!(!is_notified(l2.as_mut()));
7979

80-
event.notify(usize::MAX);
80+
assert_eq!(event.notify(usize::MAX), 2);
8181
assert!(is_notified(l1.as_mut()));
8282
assert!(is_notified(l2.as_mut()));
8383
}
@@ -90,7 +90,7 @@ fn drop_notified() {
9090
let mut l2 = event.listen();
9191
let mut l3 = event.listen();
9292

93-
event.notify(1);
93+
assert_eq!(event.notify(1), 1);
9494
drop(l1);
9595
assert!(is_notified(l2.as_mut()));
9696
assert!(!is_notified(l3.as_mut()));
@@ -104,7 +104,7 @@ fn drop_notified2() {
104104
let mut l2 = event.listen();
105105
let mut l3 = event.listen();
106106

107-
event.notify(2);
107+
assert_eq!(event.notify(2), 2);
108108
drop(l1);
109109
assert!(is_notified(l2.as_mut()));
110110
assert!(!is_notified(l3.as_mut()));
@@ -119,8 +119,8 @@ fn drop_notified_additional() {
119119
let mut l3 = event.listen();
120120
let mut l4 = event.listen();
121121

122-
event.notify_additional(1);
123-
event.notify(2);
122+
assert_eq!(event.notify_additional(1), 1);
123+
assert_eq!(event.notify(2), 1);
124124
drop(l1);
125125
assert!(is_notified(l2.as_mut()));
126126
assert!(is_notified(l3.as_mut()));
@@ -135,7 +135,7 @@ fn drop_non_notified() {
135135
let mut l2 = event.listen();
136136
let l3 = event.listen();
137137

138-
event.notify(1);
138+
assert_eq!(event.notify(1), 1);
139139
drop(l3);
140140
assert!(is_notified(l1.as_mut()));
141141
assert!(!is_notified(l2.as_mut()));
@@ -173,7 +173,7 @@ fn notify_all_fair() {
173173
.poll(&mut Context::from_waker(&waker3))
174174
.is_pending());
175175

176-
event.notify(usize::MAX);
176+
assert_eq!(event.notify(usize::MAX), 3);
177177
assert_eq!(&*v.lock().unwrap(), &[1, 2, 3]);
178178

179179
assert!(Pin::new(&mut l1)

0 commit comments

Comments
 (0)