Skip to content

Commit 9c31ae6

Browse files
committed
refactor: Move drop glue of AddListener into separate struct
1 parent 7707b73 commit 9c31ae6

File tree

2 files changed

+37
-68
lines changed

2 files changed

+37
-68
lines changed

src/lib.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl Event {
282282
if let Some(mut lock) = inner.lock() {
283283
lock.notify_unnotified(n);
284284
} else {
285-
inner.push(Node::notify(Notify {
285+
inner.push(Node::Notify(Notify {
286286
count: n,
287287
kind: NotifyKind::Notify,
288288
}));
@@ -333,7 +333,7 @@ impl Event {
333333
if let Some(mut lock) = inner.lock() {
334334
lock.notify_unnotified(n);
335335
} else {
336-
inner.push(Node::notify(Notify {
336+
inner.push(Node::Notify(Notify {
337337
count: n,
338338
kind: NotifyKind::Notify,
339339
}));
@@ -383,7 +383,7 @@ impl Event {
383383
if let Some(mut lock) = inner.lock() {
384384
lock.notify_additional(n);
385385
} else {
386-
inner.push(Node::notify(Notify {
386+
inner.push(Node::Notify(Notify {
387387
count: n,
388388
kind: NotifyKind::NotifyAdditional,
389389
}));
@@ -435,7 +435,7 @@ impl Event {
435435
if let Some(mut lock) = inner.lock() {
436436
lock.notify_additional(n);
437437
} else {
438-
inner.push(Node::notify(Notify {
438+
inner.push(Node::Notify(Notify {
439439
count: n,
440440
kind: NotifyKind::NotifyAdditional,
441441
}));
@@ -618,7 +618,7 @@ impl EventListener {
618618
None => {
619619
// Wake us up when the lock is free.
620620
let unparker = parker.unparker();
621-
self.inner.push(Node::waiting(Task::Thread(unparker)));
621+
self.inner.push(Node::Waiting(Task::Thread(unparker)));
622622
parker.park()
623623
}
624624
}
@@ -715,7 +715,7 @@ impl EventListener {
715715
}
716716
} else {
717717
// Let someone else do it for us.
718-
self.inner.push(Node::remove_listener(entry, false));
718+
self.inner.push(Node::RemoveListener { listener: entry, propagate: false });
719719
}
720720
}
721721

@@ -773,7 +773,7 @@ impl Future for EventListener {
773773
None => {
774774
// Wait for the lock to be available.
775775
self.inner
776-
.push(Node::waiting(Task::Waker(cx.waker().clone())));
776+
.push(Node::Waiting(Task::Waker(cx.waker().clone())));
777777

778778
// If the lock is suddenly available, we need to poll again.
779779
if let Some(list) = self.inner.lock() {
@@ -835,7 +835,7 @@ impl Drop for EventListener {
835835
}
836836
None => {
837837
// Request that someone else do it.
838-
self.inner.push(Node::remove_listener(entry, true));
838+
self.inner.push(Node::RemoveListener { listener: entry, propagate: true });
839839
}
840840
}
841841
}

src/node.rs

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,11 @@ use alloc::boxed::Box;
88
use core::ptr::NonNull;
99

1010
/// A node in the backup queue.
11-
pub(crate) struct Node {
12-
/// The data associated with the node.
13-
data: Option<NodeData>,
14-
}
15-
16-
impl From<NodeData> for Node {
17-
fn from(data: NodeData) -> Self {
18-
Self { data: Some(data) }
19-
}
20-
}
21-
22-
enum NodeData {
11+
pub(crate) enum Node {
2312
/// This node is requesting to add a listener.
2413
AddListener {
2514
/// The pointer to the listener to add.
26-
listener: NonNull<Entry>,
15+
listener: Option<DistOwnedListener>,
2716
},
2817

2918
/// This node is notifying a listener.
@@ -42,65 +31,55 @@ enum NodeData {
4231
Waiting(Task),
4332
}
4433

45-
impl Node {
46-
/// Create a new listener submission entry.
47-
pub(crate) fn listener() -> (Self, NonNull<Entry>) {
48-
// Allocate an entry on the heap.
49-
let entry = unsafe { NonNull::new_unchecked(Box::into_raw(Box::new(Entry::new()))) };
50-
51-
(NodeData::AddListener { listener: entry }.into(), entry)
52-
}
34+
pub(crate) struct DistOwnedListener(NonNull<Entry>);
5335

54-
/// Create a new notification entry.
55-
pub(crate) fn notify(notify: Notify) -> Self {
56-
NodeData::Notify(notify).into()
36+
impl DistOwnedListener {
37+
/// extracts the contained entry pointer from the DOL,
38+
/// without calling the DOL Drop handler (such that the returned pointer stays valid)
39+
fn take(self) -> NonNull<Entry> {
40+
(&*core::mem::ManuallyDrop::new(self)).0
5741
}
42+
}
5843

59-
/// Create a new listener removal entry.
60-
pub(crate) fn remove_listener(listener: NonNull<Entry>, propagate: bool) -> Self {
61-
NodeData::RemoveListener {
62-
listener,
63-
propagate,
64-
}
65-
.into()
44+
impl Drop for DistOwnedListener {
45+
fn drop(&mut self) {
46+
drop(unsafe { Box::from_raw(self.0.as_ptr()) });
6647
}
48+
}
6749

68-
/// Create a new waiting entry.
69-
pub(crate) fn waiting(task: Task) -> Self {
70-
NodeData::Waiting(task).into()
50+
impl Node {
51+
pub(crate) fn listener() -> (Self, NonNull<Entry>) {
52+
let entry = Box::into_raw(Box::new(Entry::new()));
53+
let entry = unsafe { NonNull::new_unchecked(entry) };
54+
(Self::AddListener { listener: Some(DistOwnedListener(entry)) }, entry)
7155
}
7256

7357
/// Indicate that this node has been enqueued.
7458
pub(crate) fn enqueue(&self) {
75-
if let Some(NodeData::AddListener { listener }) = &self.data {
76-
unsafe {
77-
listener.as_ref().enqueue();
78-
}
59+
if let Node::AddListener { listener: Some(entry) } = self {
60+
unsafe { entry.0.as_ref() }.enqueue();
7961
}
8062
}
8163

8264
/// Apply the node to the list.
83-
pub(crate) fn apply(mut self, list: &mut List, inner: &Inner) -> Option<Task> {
84-
let data = self.data.take().unwrap();
85-
86-
match data {
87-
NodeData::AddListener { listener } => {
65+
pub(crate) fn apply(self, list: &mut List, inner: &Inner) -> Option<Task> {
66+
match self {
67+
Node::AddListener { mut listener } => {
8868
// Add the listener to the list.
89-
list.insert(listener);
69+
let entry = listener.take().unwrap().take();
70+
list.insert(entry);
9071

9172
// Dequeue the listener.
92-
return unsafe { listener.as_ref().dequeue() };
73+
return unsafe { entry.as_ref().dequeue() };
9374
}
94-
NodeData::Notify(notify) => {
75+
Node::Notify(Notify { count, kind }) => {
9576
// Notify the listener.
96-
let Notify { count, kind } = notify;
97-
9877
match kind {
9978
NotifyKind::Notify => list.notify_unnotified(count),
10079
NotifyKind::NotifyAdditional => list.notify_additional(count),
10180
}
10281
}
103-
NodeData::RemoveListener {
82+
Node::RemoveListener {
10483
listener,
10584
propagate,
10685
} => {
@@ -112,21 +91,11 @@ impl Node {
11291
list.notify(1, additional);
11392
}
11493
}
115-
NodeData::Waiting(task) => {
94+
Node::Waiting(task) => {
11695
return Some(task);
11796
}
11897
}
11998

12099
None
121100
}
122101
}
123-
124-
impl Drop for Node {
125-
fn drop(&mut self) {
126-
if let Some(NodeData::AddListener { listener }) = self.data.take() {
127-
unsafe {
128-
drop(Box::from_raw(listener.as_ptr()));
129-
}
130-
}
131-
}
132-
}

0 commit comments

Comments
 (0)