Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion rs/moq-net/src/model/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
//!
//! Gates nest: a read through a gate that polls another gated reader passes it the outer
//! gate's waiter, whose waker stays the same across reads, so the inner gate keeps its arm
//! and its wake reaches the outer gate on the way to the task.
//! and its wake reaches the outer gate on the way to the task. A change made outside the
//! read, such as a drop poll hearing a fill's answer, reopens the gate instead of disarming
//! it, so that wake reaches the outer gates too.

pub(super) use crate::util::Idle;

Expand Down
17 changes: 17 additions & 0 deletions rs/moq-net/src/model/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8434,6 +8434,8 @@ impl Cursor {
/// The next run given up, judging the live edge first only when `probe`.
#[cfg_attr(not(feature = "async"), allow(dead_code, reason = "async-only"))]
fn poll_drop(&mut self, probe: bool, waiter: &kio::Waiter) -> Poll<(ops::Range<u64>, Hole)> {
// What the group read walks, which this poll may move outside that read.
let walks = (self.filter.revision, self.fill_view());
self.sync();
if self.drops.is_none() {
self.drops = Some(Box::new(DropCursor::new(&self.state.read())));
Expand All @@ -8453,6 +8455,12 @@ impl Cursor {
self.probe(from, &drift, waiter);
judged = Some(drift);
}
// The window settled, or a fill was asked or answered, on this poll's waiter rather
// than the one the group read parked on: that read, and every gate it nests in, reads
// again.
if (self.filter.revision, self.fill_view()) != walks {
idle::Idle::reopen(&mut self.idle);
}
// A cap at or below the floor leaves nothing to report, not a reversed span.
let floor = self.floor();
let within = Within {
Expand Down Expand Up @@ -9205,6 +9213,15 @@ impl Cursor {
group
}

/// What a group read consults of the fills: the lowest group still owed, whether a filled
/// one waits to be taken, and whether any is owed at all.
fn fill_view(&self) -> (Option<u64>, bool, bool) {
let Some(misses) = &self.misses else {
return (None, false, false);
};
(misses.lowest_wanted(), misses.has_filled(), misses.is_waiting())
}

/// Whether a fill still owes this cursor a group, so the end of the feed is not its end.
fn awaits_fill(&self) -> bool {
self.misses.as_ref().is_some_and(|misses| misses.is_waiting())
Expand Down
52 changes: 51 additions & 1 deletion rs/moq-net/src/util/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//! can answer `Pending` again without walking, and locking, everything it read before.
//!
//! Only what notifies can be trusted this way. A change the owner makes itself, which
//! notifies nothing, has to [`Idle::disarm`] the gate.
//! notifies nothing, has to [`Idle::disarm`] the gate, or [`Idle::reopen`] it when made
//! outside the read, so the gates it nests in and the task read again.
//!
//! An owner with more than one read keys each by `K`, since one parking says nothing
//! about another.
Expand Down Expand Up @@ -109,6 +110,17 @@ impl<K: Copy + Eq> Idle<K> {
idle.parked = None;
}
}

/// [`Self::disarm`] for a change made outside the read, which a gate the read nests in
Comment thread
kixelated marked this conversation as resolved.
Resolved
/// never hears: it parked on this one and answers `Pending` again until woken. So this
/// wakes through the gate's own waiter, and every gate it nests in reads again, as does
/// the task.
pub(crate) fn reopen(this: &mut Option<Box<Self>>) {
if let Some(idle) = this {
idle.parked = None;
idle.wake.wake_by_ref();
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -155,6 +167,44 @@ mod test {
assert!(!Idle::parked(&slot, (), &other), "another task reads anew");
}

/// A reopen reaches the gate the read nests in, which parked on this one and would
/// otherwise answer `Pending` again without reading through it. A disarm does not.
#[test]
fn a_reopen_reaches_the_gates_the_read_nests_in() {
for reopen in [false, true] {
let task = Arc::new(Woken {
woken: AtomicBool::new(false),
task: Waker::noop().clone(),
});
let waiter = kio::Waiter::new(Waker::from(task.clone()));
let mut outer_slot: Option<Box<Idle>> = None;
let mut inner_slot: Option<Box<Idle>> = None;

// The outer read parks through the inner one, as an ordered read parks on its cursor.
let mut outer = Idle::start(&mut outer_slot, &waiter);
let inner = Idle::start(&mut inner_slot, outer.waiter());
inner.finish(&mut inner_slot, (), true);
outer.finish(&mut outer_slot, (), true);
assert!(Idle::parked(&outer_slot, (), &waiter));

match reopen {
true => Idle::reopen(&mut inner_slot),
false => Idle::disarm(&mut inner_slot),
}
assert!(!Idle::parked(&inner_slot, (), &kio::Waiter::noop()));
assert_eq!(
Idle::parked(&outer_slot, (), &waiter),
!reopen,
"reopen {reopen}: the outer gate reads through"
);
assert_eq!(
task.woken.load(Ordering::Acquire),
reopen,
"reopen {reopen}: the task wakes"
);
}
}

/// A gate keyed by read answers only for the read that parked.
#[test]
fn a_parked_read_says_nothing_about_another() {
Expand Down