Skip to content

Commit

Permalink
feat: Move EventListener back onto the heap
Browse files Browse the repository at this point in the history
Minimal amount of changes to make EventListener a heap-allocated type
again. The existence of the EventListener implies that it is already
listening; accordingly the new() and listen() methods on EventListener
have been removed.

cc #104

Signed-off-by: John Nunley <dev@notgull.net>
  • Loading branch information
notgull committed Feb 3, 2024
1 parent ac18bdf commit 86b7780
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 309 deletions.
19 changes: 5 additions & 14 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
use std::iter;
use std::pin::Pin;

use criterion::{criterion_group, criterion_main, Criterion};
use event_listener::{Event, EventListener};
use event_listener::Event;

const COUNT: usize = 8000;

fn bench_events(c: &mut Criterion) {
c.bench_function("notify_and_wait", |b| {
let ev = Event::new();
let mut handles = iter::repeat_with(EventListener::new)
.take(COUNT)
.collect::<Vec<_>>();
let mut handles = Vec::with_capacity(COUNT);

b.iter(|| {
for handle in &mut handles {
// SAFETY: The handle is not moved out.
let listener = unsafe { Pin::new_unchecked(handle) };
listener.listen(&ev);
}
handles.extend(iter::repeat_with(|| ev.listen()).take(COUNT));

ev.notify(COUNT);

for handle in &mut handles {
// SAFETY: The handle is not moved out.
let listener = unsafe { Pin::new_unchecked(handle) };
listener.wait();
for handle in handles.drain(..) {
handle.wait();
}
});
});
Expand Down
8 changes: 4 additions & 4 deletions examples/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ mod example {
// Start listening and then try locking again.
listener = Some(self.lock_ops.listen());
}
Some(mut l) => {
Some(l) => {
// Wait until a notification is received.
l.as_mut().wait();
l.wait();
}
}
}
Expand All @@ -90,9 +90,9 @@ mod example {
// Start listening and then try locking again.
listener = Some(self.lock_ops.listen());
}
Some(mut l) => {
Some(l) => {
// Wait until a notification is received.
l.as_mut().wait_deadline(deadline)?;
l.wait_deadline(deadline)?;
}
}
}
Expand Down

0 comments on commit 86b7780

Please sign in to comment.