Skip to content

Commit 09ded13

Browse files
committed
Replace Listener with Option<sys::Listener>
1 parent de1f13a commit 09ded13

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Event {
179179
// Register the listener.
180180
let mut listener = EventListener(Listener {
181181
event: unsafe { Arc::clone(&ManuallyDrop::new(Arc::from_raw(inner))) },
182-
listener: sys::Listener::Discarded,
182+
listener: None,
183183
});
184184

185185
listener.0.event.insert(&mut listener.0.listener);
@@ -510,10 +510,10 @@ impl EventListener {
510510
/// use event_listener::Event;
511511
///
512512
/// let event = Event::new();
513-
/// let mut listener = event.listen();
513+
/// let listener = event.listen();
514514
///
515515
/// // There are no notification so this times out.
516-
/// assert!(!listener.as_mut().wait_deadline(Instant::now() + Duration::from_secs(1)));
516+
/// assert!(!listener.wait_deadline(Instant::now() + Duration::from_secs(1)));
517517
/// ```
518518
#[cfg(feature = "std")]
519519
pub fn wait_deadline(self, deadline: Instant) -> bool {
@@ -601,7 +601,7 @@ struct Listener<B: Borrow<Inner> + Unpin> {
601601
event: B,
602602

603603
/// The inner state of the listener.
604-
listener: sys::Listener,
604+
listener: Option<sys::Listener>,
605605
}
606606

607607
unsafe impl<B: Borrow<Inner> + Unpin + Send> Send for Listener<B> {}

src/no_std.rs

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,43 @@ impl crate::Inner {
3333
/// Add a new listener to the list.
3434
///
3535
/// Does nothing if the list is already registered.
36-
pub(crate) fn insert(&self, listener: &mut Listener) {
37-
if let Listener::HasNode(_) | Listener::Queued(_) = *listener {
36+
pub(crate) fn insert(&self, listener: &mut Option<Listener>) {
37+
if listener.is_some() {
3838
// Already inserted.
3939
return;
4040
}
4141

4242
match self.try_lock() {
4343
Some(mut lock) => {
4444
let key = lock.insert(State::Created);
45-
*listener = Listener::HasNode(key);
45+
*listener = Some(Listener::HasNode(key));
4646
}
4747

4848
None => {
4949
// Push it to the queue.
5050
let (node, task_waiting) = Node::listener();
5151
self.list.queue.push(node);
52-
*listener = Listener::Queued(task_waiting);
52+
*listener = Some(Listener::Queued(task_waiting));
5353
}
5454
}
5555
}
5656

5757
/// Remove a listener from the list.
58-
pub(crate) fn remove(&self, listener: &mut Listener, propogate: bool) -> Option<State> {
59-
let state = match mem::replace(listener, Listener::Discarded) {
60-
Listener::HasNode(key) => {
58+
pub(crate) fn remove(&self, listener: &mut Option<Listener>, propogate: bool) -> Option<State> {
59+
let state = match listener.take() {
60+
Some(Listener::HasNode(key)) => {
6161
match self.try_lock() {
6262
Some(mut list) => {
6363
// Fast path removal.
64-
list.remove(Listener::HasNode(key), propogate)
64+
list.remove(key, propogate)
6565
}
6666

6767
None => {
6868
// Slow path removal.
6969
// This is why intrusive lists don't work on no_std.
7070
let node = Node::RemoveListener {
71+
key,
7172
propagate: propogate,
72-
listener: Listener::HasNode(key),
7373
};
7474

7575
self.list.queue.push(node);
@@ -79,12 +79,12 @@ impl crate::Inner {
7979
}
8080
}
8181

82-
Listener::Queued(_) => {
82+
Some(Listener::Queued(_)) => {
8383
// This won't be added after we drop the lock.
8484
None
8585
}
8686

87-
_ => None,
87+
None => None,
8888
};
8989

9090
state
@@ -115,11 +115,15 @@ impl crate::Inner {
115115
///
116116
/// Returns `true` if the listener was already notified, and `false` otherwise. If the listener
117117
/// isn't inserted, returns `None`.
118-
pub(crate) fn register(&self, listener: &mut Listener, task: TaskRef<'_>) -> Option<bool> {
118+
pub(crate) fn register(
119+
&self,
120+
listener: &mut Option<Listener>,
121+
task: TaskRef<'_>,
122+
) -> Option<bool> {
119123
loop {
120-
match mem::replace(listener, Listener::Discarded) {
121-
Listener::HasNode(key) => {
122-
*listener = Listener::HasNode(key);
124+
match listener.take() {
125+
Some(Listener::HasNode(key)) => {
126+
*listener = Some(Listener::HasNode(key));
123127
match self.try_lock() {
124128
Some(mut guard) => {
125129
// Fast path registration.
@@ -135,18 +139,18 @@ impl crate::Inner {
135139
}
136140
}
137141

138-
Listener::Queued(task_waiting) => {
142+
Some(Listener::Queued(task_waiting)) => {
139143
// Are we done yet?
140144
match task_waiting.status() {
141145
Some(key) => {
142146
// We're inserted now, adjust state.
143-
*listener = Listener::HasNode(key);
147+
*listener = Some(Listener::HasNode(key));
144148
}
145149

146150
None => {
147151
// We're still queued, so register the task.
148152
task_waiting.register(task.into_task());
149-
*listener = Listener::Queued(task_waiting);
153+
*listener = Some(Listener::Queued(task_waiting));
150154
return None;
151155
}
152156
}
@@ -476,12 +480,7 @@ impl ListenerSlab {
476480
}
477481

478482
/// Removes an entry from the list and returns its state.
479-
pub(crate) fn remove(&mut self, listener: Listener, propogate: bool) -> Option<State> {
480-
let key = match listener {
481-
Listener::HasNode(key) => key,
482-
_ => return None,
483-
};
484-
483+
pub(crate) fn remove(&mut self, key: NonZeroUsize, propogate: bool) -> Option<State> {
485484
let entry = &self.listeners[key.get()];
486485
let prev = entry.prev().get();
487486
let next = entry.next().get();
@@ -569,9 +568,13 @@ impl ListenerSlab {
569568
///
570569
/// Returns `true` if the listener was already notified, and `false` otherwise. If the listener
571570
/// isn't inserted, returns `None`.
572-
pub(crate) fn register(&mut self, listener: &mut Listener, task: TaskRef<'_>) -> Option<bool> {
571+
pub(crate) fn register(
572+
&mut self,
573+
listener: &mut Option<Listener>,
574+
task: TaskRef<'_>,
575+
) -> Option<bool> {
573576
let key = match *listener {
574-
Listener::HasNode(key) => key,
577+
Some(Listener::HasNode(key)) => key,
575578
_ => return None,
576579
};
577580

@@ -581,7 +584,8 @@ impl ListenerSlab {
581584
match entry.state().replace(State::NotifiedTaken) {
582585
State::Notified(_) | State::NotifiedTaken => {
583586
// The listener was already notified, so we don't need to do anything.
584-
self.remove(mem::replace(listener, Listener::Discarded), false)?;
587+
self.remove(key, false)?;
588+
*listener = None;
585589
Some(true)
586590
}
587591

@@ -609,9 +613,6 @@ pub(crate) enum Listener {
609613
/// The listener has a node inside of the linked list.
610614
HasNode(NonZeroUsize),
611615

612-
/// The listener has already been notified and has discarded its entry.
613-
Discarded,
614-
615616
/// The listener has an entry in the queue that may or may not have a task waiting.
616617
Queued(Arc<TaskWaiting>),
617618
}

src/no_std/node.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The node that makes up queues.
22
3-
use super::{Listener, ListenerSlab};
3+
use super::ListenerSlab;
44
use crate::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
55
use crate::sync::Arc;
66
use crate::{State, Task};
@@ -32,7 +32,7 @@ pub(crate) enum Node {
3232
/// This node is removing a listener.
3333
RemoveListener {
3434
/// The ID of the listener to remove.
35-
listener: Listener,
35+
key: NonZeroUsize,
3636

3737
/// Whether to propagate notifications to the next listener.
3838
propagate: bool,
@@ -83,12 +83,9 @@ impl Node {
8383
// Notify the listener.
8484
list.notify(count, additional);
8585
}
86-
Node::RemoveListener {
87-
listener,
88-
propagate,
89-
} => {
86+
Node::RemoveListener { key, propagate } => {
9087
// Remove the listener from the list.
91-
list.remove(listener, propagate);
88+
list.remove(key, propagate);
9289
}
9390
Node::Waiting(task) => {
9491
return Some(task);

0 commit comments

Comments
 (0)