Repository: tokio-rs/tokio
File: tokio/src/runtime/io/scheduled_io.rs (Rust)

Code before:
                                w.waker = Some(cx.waker().clone());
                            }

                            return Poll::Pending;
                        }

                        // Explicit drop of the lock to indicate the scope that the
                        // lock is held. Because holding the lock is required to
                        // ensure safe access to fields not held within the lock, it
                        // is helpful to visualize the scope of the critical
                        // section.
                        drop(waiters);
                    }
                    State::Done => {
                        // Safety: State::Done means it is no longer shared
                        let w = unsafe { &mut *waiter.get() };

                        // Note: the returned tick might be newer than the event
                        // which notified our waker. This is ok because the future
                        // still didn't return `Poll::Ready`.
                        let curr = scheduled_io.readiness.load(Acquire);
                        let tick = TICK.unpack(curr) as u8;

                        // Add more readiness which might have appeared in the meantime.
                        let curr_ready = Ready::from_usize(READINESS.unpack(curr));
                        let ready = w.ready | (curr_ready.intersection(w.interest));

                        return Poll::Ready(ReadyEvent {
                            tick,
                            ready,
                        });
                    }
                }
            }
        }
    }

    impl Drop for Readiness<'_> {
        fn drop(&mut self) {
            let mut waiters = self.scheduled_io.waiters.lock();

            // Safety: `waiter` is only ever stored in `waiters`
            unsafe {
                waiters
                    .list
                    .remove(NonNull::new_unchecked(self.waiter.get()))
            };
        }
    }

    unsafe impl Send for Readiness<'_> {}

Review comment (suggestion):
If a readiness bit is not included in `curr_ready` then something else already cleared it. Including `w.ready` here would result in additional false positives & unnecessary I/O ops.

Based on discord, it sounds like you want to avoid empty readiness events, but that seems better to me than false positives. The caller would loop and wait again.

Code after:
                            }

                            return Poll::Pending;
                        }

                        // Explicit drop of the lock to indicate the scope that the
                        // lock is held. Because holding the lock is required to
                        // ensure safe access to fields not held within the lock, it
                        // is helpful to visualize the scope of the critical
                        // section.
                        drop(waiters);
                    }
                    State::Done => {
                        // Safety: State::Done means it is no longer shared
                        let w = unsafe { &mut *waiter.get() };

                        let curr = scheduled_io.readiness.load(Acquire);

                        // The returned tick might be newer than the event
                        // which notified our waker. This is ok because the future
                        // still didn't return `Poll::Ready`.
                        let tick = TICK.unpack(curr) as u8;

                        // The readiness state could have been cleared in the meantime,
                        // but we allow the returned ready set to be empty.
                        let curr_ready = Ready::from_usize(READINESS.unpack(curr));
                        let ready = curr_ready.intersection(w.interest);

                        return Poll::Ready(ReadyEvent {
                            tick,
                            ready,
                        });
                    }
                }
            }
        }
    }

    impl Drop for Readiness<'_> {
        fn drop(&mut self) {
            let mut waiters = self.scheduled_io.waiters.lock();

            // Safety: `waiter` is only ever stored in `waiters`
            unsafe {
                waiters
                    .list
                    .remove(NonNull::new_unchecked(self.waiter.get()))
            };
        }
    }