Skip to main content

starnix_core/task/
waiter.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::task::{CurrentTask, RunState};
6use crate::vfs::{EpollEventHandler, FdNumber};
7use bitflags::bitflags;
8use futures::stream::AbortHandle;
9use slab::Slab;
10use smallvec::SmallVec;
11use starnix_lifecycle::AtomicCounter;
12use starnix_sync::{
13    EventHandlerReadyQueueLock, EventWaitGuard, FileOpsCore, InterruptibleEvent, LockDepMutex,
14    LockEqualOrBefore, Locked, NotifyKind, PortEvent, PortWaitResult, PortWaiterCallbacksLock,
15    PortWaiterWaitQueuesLock, WaitQueueImplLock, WaiterEventHandlerLock,
16};
17use starnix_types::ownership::debug_assert_no_local_temp_ref;
18use starnix_uapi::error;
19use starnix_uapi::errors::{EINTR, Errno};
20use starnix_uapi::signals::{SIGKILL, SigSet, Signal};
21use starnix_uapi::vfs::FdEvents;
22use std::collections::{HashMap, VecDeque};
23use std::sync::{Arc, Weak};
24use syncio::zxio::zxio_signals_t;
25use syncio::{ZxioSignals, ZxioWeak};
26
27#[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
28pub enum ReadyItemKey {
29    FdNumber(FdNumber),
30    Usize(usize),
31}
32
33impl From<FdNumber> for ReadyItemKey {
34    fn from(v: FdNumber) -> Self {
35        Self::FdNumber(v)
36    }
37}
38
39impl From<usize> for ReadyItemKey {
40    fn from(v: usize) -> Self {
41        Self::Usize(v)
42    }
43}
44
45#[derive(Debug, Copy, Clone)]
46pub struct ReadyItem {
47    pub key: ReadyItemKey,
48    pub events: FdEvents,
49}
50
51#[derive(Clone)]
52pub enum EventHandler {
53    /// Does nothing.
54    ///
55    /// It is up to the waiter to synchronize itself with the notifier if
56    /// synchronization is needed.
57    None,
58
59    /// Enqueues an event to a ready list.
60    ///
61    /// This event handler naturally synchronizes the notifier and notifee
62    /// because of the lock acquired/released when enqueuing the event.
63    Enqueue {
64        key: ReadyItemKey,
65        queue: Arc<LockDepMutex<VecDeque<ReadyItem>, EventHandlerReadyQueueLock>>,
66        sought_events: FdEvents,
67    },
68
69    /// Wraps another EventHandler and only triggers it once. Further .handle() calls are ignored.
70    ///
71    /// This is intended for cases like BinderFileObject which need to register
72    /// the same EventHandler on multiple wait queues.
73    HandleOnce(Arc<LockDepMutex<Option<EventHandler>, WaiterEventHandlerLock>>),
74
75    /// This handler is an epoll.
76    Epoll(EpollEventHandler),
77}
78
79impl EventHandler {
80    pub fn handle(self, events: FdEvents) {
81        match self {
82            Self::None => {}
83            Self::Enqueue { key, queue, sought_events } => {
84                let events = events & sought_events;
85                queue.lock().push_back(ReadyItem { key, events });
86            }
87            Self::HandleOnce(inner) => {
88                if let Some(inner) = inner.lock().take() {
89                    inner.handle(events);
90                }
91            }
92            Self::Epoll(e) => e.handle(events),
93        }
94    }
95}
96
97pub struct ZxioSignalHandler {
98    pub zxio: ZxioWeak,
99    pub get_events_from_zxio_signals: fn(zxio_signals_t) -> FdEvents,
100}
101
102// The counter is incremented as each handle is signaled; when the counter reaches the handle
103// count, the event handler is called with the given events.
104pub struct ManyZxHandleSignalHandler {
105    pub count: usize,
106    pub counter: Arc<AtomicCounter<usize>>,
107    pub expected_signals: zx::Signals,
108    pub events: FdEvents,
109}
110
111pub enum SignalHandlerInner {
112    None,
113    Zxio(ZxioSignalHandler),
114    ZxHandle(fn(zx::Signals) -> FdEvents),
115    ManyZxHandle(ManyZxHandleSignalHandler),
116}
117
118pub struct SignalHandler {
119    pub inner: SignalHandlerInner,
120    pub event_handler: EventHandler,
121    pub err_code: Option<Errno>,
122}
123
124impl SignalHandler {
125    fn handle(self, signals: zx::Signals) -> Option<Errno> {
126        let SignalHandler { inner, event_handler, err_code } = self;
127        let events = match inner {
128            SignalHandlerInner::None => None,
129            SignalHandlerInner::Zxio(ZxioSignalHandler { zxio, get_events_from_zxio_signals }) => {
130                if let Some(zxio) = zxio.upgrade() {
131                    Some(get_events_from_zxio_signals(zxio.wait_end(signals)))
132                } else {
133                    None
134                }
135            }
136            SignalHandlerInner::ZxHandle(get_events_from_zx_signals) => {
137                Some(get_events_from_zx_signals(signals))
138            }
139            SignalHandlerInner::ManyZxHandle(signal_handler) => {
140                if signals.contains(signal_handler.expected_signals) {
141                    let new_count = signal_handler.counter.next() + 1;
142                    assert!(new_count <= signal_handler.count);
143                    if new_count == signal_handler.count {
144                        Some(signal_handler.events)
145                    } else {
146                        None
147                    }
148                } else {
149                    None
150                }
151            }
152        };
153        if let Some(events) = events {
154            event_handler.handle(events)
155        }
156        err_code
157    }
158}
159
160pub enum WaitCallback {
161    SignalHandler(SignalHandler),
162    EventHandler(EventHandler),
163}
164
165struct WaitCancelerQueue {
166    wait_queue: Weak<LockDepMutex<WaitQueueImpl, WaitQueueImplLock>>,
167    waiter: WaiterRef,
168    wait_key: WaitKey,
169    waiter_id: WaitEntryId,
170}
171
172struct WaitCancelerZxio {
173    zxio: ZxioWeak,
174    inner: PortWaitCanceler,
175}
176
177struct WaitCancelerPort {
178    inner: PortWaitCanceler,
179}
180
181enum WaitCancelerInner {
182    Zxio(WaitCancelerZxio),
183    Queue(WaitCancelerQueue),
184    Port(WaitCancelerPort),
185}
186
187enum NotifiableRef {
188    Port(Arc<PortWaiter>),
189    Event(Arc<InterruptibleEvent>),
190    AbortHandle(Arc<AbortHandle>),
191}
192
193const WAIT_CANCELER_COMMON_SIZE: usize = 2;
194
195/// Return values for wait_async methods.
196///
197/// Calling `cancel` will cancel any running wait.
198///
199/// Does not implement `Clone` or `Copy` so that only a single canceler exists
200/// per wait.
201pub struct WaitCanceler {
202    cancellers: smallvec::SmallVec<[WaitCancelerInner; WAIT_CANCELER_COMMON_SIZE]>,
203}
204
205impl WaitCanceler {
206    fn new_inner(inner: WaitCancelerInner) -> Self {
207        Self { cancellers: smallvec::smallvec![inner] }
208    }
209
210    pub fn new_noop() -> Self {
211        Self { cancellers: Default::default() }
212    }
213
214    pub fn new_zxio(zxio: ZxioWeak, inner: PortWaitCanceler) -> Self {
215        Self::new_inner(WaitCancelerInner::Zxio(WaitCancelerZxio { zxio, inner }))
216    }
217
218    pub fn new_port(inner: PortWaitCanceler) -> Self {
219        Self::new_inner(WaitCancelerInner::Port(WaitCancelerPort { inner }))
220    }
221
222    /// Equivalent to `merge_unbounded`, except that it enforces that the resulting vector of
223    /// cancellers is small enough to avoid being separately allocated on the heap.
224    ///
225    /// If possible, use this function instead of `merge_unbounded`, because it gives us better
226    /// tools to keep this code path optimized.
227    pub fn merge(self, other: Self) -> Self {
228        // Increase `WAIT_CANCELER_COMMON_SIZE` if needed, or remove this assert and allow the
229        // smallvec to allocate.
230        assert!(
231            self.cancellers.len() + other.cancellers.len() <= WAIT_CANCELER_COMMON_SIZE,
232            "WaitCanceler::merge disallows more than {} cancellers, found {} + {}",
233            WAIT_CANCELER_COMMON_SIZE,
234            self.cancellers.len(),
235            other.cancellers.len()
236        );
237        WaitCanceler::merge_unbounded(self, other)
238    }
239
240    /// Creates a new `WaitCanceler` that is equivalent to canceling both its arguments.
241    pub fn merge_unbounded(
242        Self { mut cancellers }: Self,
243        Self { cancellers: mut other }: Self,
244    ) -> Self {
245        cancellers.append(&mut other);
246        WaitCanceler { cancellers }
247    }
248
249    /// Cancel the pending wait.
250    ///
251    /// Takes `self` by value since a wait can only be canceled once.
252    pub fn cancel(self) {
253        let Self { cancellers } = self;
254        for canceller in cancellers.into_iter().rev() {
255            match canceller {
256                WaitCancelerInner::Zxio(WaitCancelerZxio { zxio, inner }) => {
257                    let Some(zxio) = zxio.upgrade() else { return };
258                    let (_, signals) = zxio.wait_begin(ZxioSignals::NONE.bits());
259                    inner.cancel();
260                    zxio.wait_end(signals);
261                }
262                WaitCancelerInner::Queue(WaitCancelerQueue {
263                    wait_queue,
264                    waiter,
265                    wait_key,
266                    waiter_id: WaitEntryId { key, id },
267                }) => {
268                    let Some(wait_queue) = wait_queue.upgrade() else { return };
269                    waiter.remove_callback(&wait_key);
270                    waiter.will_remove_from_wait_queue(&wait_key);
271                    let mut wait_queue = wait_queue.lock();
272                    let waiters = &mut wait_queue.waiters;
273                    if let Some(entry) = waiters.get_mut(key) {
274                        // The map of waiters in a wait queue uses a `Slab` which
275                        // recycles keys. To make sure we are removing the right
276                        // entry, make sure the ID value matches what we expect
277                        // to remove.
278                        if entry.id == id {
279                            waiters.remove(key);
280                        }
281                    }
282                }
283                WaitCancelerInner::Port(WaitCancelerPort { inner }) => {
284                    inner.cancel();
285                }
286            }
287        }
288    }
289}
290
291/// Return values for wait_async methods that monitor the state of a handle.
292///
293/// Calling `cancel` will cancel any running wait.
294///
295/// Does not implement `Clone` or `Copy` so that only a single canceler exists
296/// per wait.
297pub struct PortWaitCanceler {
298    waiter: Weak<PortWaiter>,
299    key: WaitKey,
300}
301
302impl PortWaitCanceler {
303    /// Cancel the pending wait.
304    ///
305    /// Takes `self` by value since a wait can only be canceled once.
306    pub fn cancel(self) {
307        let Self { waiter, key } = self;
308        if let Some(waiter) = waiter.upgrade() {
309            let _ = waiter.port.cancel(key.raw);
310            waiter.remove_callback(&key);
311        }
312    }
313}
314
315#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
316struct WaitKey {
317    raw: u64,
318}
319
320/// The different type of event that can be waited on / triggered.
321#[derive(Clone, Copy, Debug)]
322enum WaitEvents {
323    /// All event: a wait on `All` will be woken up by all event, and a trigger on `All` will wake
324    /// every waiter.
325    All,
326    /// Wait on the set of FdEvents.
327    Fd(FdEvents),
328    /// Wait for the specified value.
329    Value(u64),
330    /// Wait for a signal in a specific mask to be received by the task.
331    SignalMask(SigSet),
332}
333
334impl WaitEvents {
335    /// Returns whether a wait on `self` should be woken up by `other`.
336    fn intercept(self: &WaitEvents, other: &WaitEvents) -> bool {
337        match (self, other) {
338            (Self::All, _) | (_, Self::All) => true,
339            (Self::Fd(m1), Self::Fd(m2)) => m1.bits() & m2.bits() != 0,
340            (Self::Value(v1), Self::Value(v2)) => v1 == v2,
341            // A SignalMask event can only be intercepted by another SignalMask event.
342            (Self::SignalMask(m1), Self::SignalMask(m2)) => m1.intersects(m2),
343            _ => false,
344        }
345    }
346}
347
348impl WaitCallback {
349    pub fn none() -> EventHandler {
350        EventHandler::None
351    }
352}
353
354bitflags! {
355    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
356    pub struct WaiterOptions: u8 {
357        /// The wait cannot be interrupted by signals.
358        const IGNORE_SIGNALS   = 1 << 0;
359
360        /// The wait is not taking place at a safe point.
361        ///
362        /// For example, the caller might be holding a lock, which could cause a deadlock if the
363        /// waiter triggers delayed releasers.
364        const UNSAFE_CALLSTACK = 1 << 1;
365    }
366}
367
368/// Implementation of Waiter. We put the Waiter data in an Arc so that WaitQueue can tell when the
369/// Waiter has been destroyed by keeping a Weak reference. But this is an implementation detail and
370/// a Waiter should have a single owner. So the Arc is hidden inside Waiter.
371struct PortWaiter {
372    port: PortEvent,
373    callbacks: LockDepMutex<HashMap<WaitKey, WaitCallback>, PortWaiterCallbacksLock>, // the key 0 is reserved for 'no handler'
374    next_key: AtomicCounter<u64>,
375    options: WaiterOptions,
376
377    /// Collection of wait queues this Waiter is waiting on, so that when the Waiter is Dropped it
378    /// can remove itself from the queues.
379    ///
380    /// This lock is nested inside the WaitQueue.waiters lock.
381    wait_queues: LockDepMutex<
382        HashMap<WaitKey, Weak<LockDepMutex<WaitQueueImpl, WaitQueueImplLock>>>,
383        PortWaiterWaitQueuesLock,
384    >,
385}
386
387impl PortWaiter {
388    /// Internal constructor.
389    fn new(options: WaiterOptions) -> Arc<Self> {
390        Arc::new(PortWaiter {
391            port: PortEvent::new(),
392            callbacks: Default::default(),
393            next_key: AtomicCounter::<u64>::new(1),
394            options,
395            wait_queues: Default::default(),
396        })
397    }
398
399    /// Waits until the given deadline has passed or the waiter is woken up. See wait_until().
400    fn wait_internal(&self, deadline: zx::MonotonicInstant) -> Result<(), Errno> {
401        // This method can block arbitrarily long, possibly waiting for another process. The
402        // current thread should not own any local ref that might delay the release of a resource
403        // while doing so.
404        debug_assert_no_local_temp_ref();
405
406        match self.port.wait(deadline) {
407            PortWaitResult::Notification { kind: NotifyKind::Regular } => Ok(()),
408            PortWaitResult::Notification { kind: NotifyKind::Interrupt } => error!(EINTR),
409            PortWaitResult::Signal { key, observed } => {
410                if let Some(callback) = self.remove_callback(&WaitKey { raw: key }) {
411                    match callback {
412                        WaitCallback::SignalHandler(handler) => {
413                            if let Some(errno) = handler.handle(observed) {
414                                return Err(errno);
415                            }
416                        }
417                        WaitCallback::EventHandler(_) => {
418                            panic!("wrong type of handler called")
419                        }
420                    }
421                }
422
423                Ok(())
424            }
425            PortWaitResult::TimedOut => error!(ETIMEDOUT),
426        }
427    }
428
429    fn wait_until<L>(
430        self: &Arc<Self>,
431        locked: &mut Locked<L>,
432        current_task: &CurrentTask,
433        run_state: RunState,
434        deadline: zx::MonotonicInstant,
435    ) -> Result<(), Errno>
436    where
437        L: LockEqualOrBefore<FileOpsCore>,
438    {
439        let is_waiting = deadline.into_nanos() > 0;
440
441        let callback = || {
442            // We are susceptible to spurious wakeups because interrupt() posts a message to the port
443            // queue. In addition to more subtle races, there could already be valid messages in the
444            // port queue that will immediately wake us up, leaving the interrupt message in the queue
445            // for subsequent waits (which by then may not have any signals pending) to read.
446            //
447            // It's impossible to non-racily guarantee that a signal is pending so there might always
448            // be an EINTR result here with no signal. But any signal we get when !is_waiting we know is
449            // leftover from before: the top of this function only sets ourself as the
450            // current_task.signals.run_state when there's a nonzero timeout, and that waiter reference
451            // is what is used to signal the interrupt().
452            loop {
453                let wait_result = self.wait_internal(deadline);
454                if let Err(errno) = &wait_result {
455                    if errno.code == EINTR && !is_waiting {
456                        continue; // Spurious wakeup.
457                    }
458                }
459                return wait_result;
460            }
461        };
462
463        // Trigger delayed releaser before blocking if we're at a safe point.
464        //
465        // For example, we cannot trigger delayed releaser if we are holding any locks.
466        if !self.options.contains(WaiterOptions::UNSAFE_CALLSTACK) {
467            current_task.trigger_delayed_releaser(locked);
468        }
469
470        if is_waiting { current_task.run_in_state(run_state, callback) } else { callback() }
471    }
472
473    fn next_key(&self) -> WaitKey {
474        let key = self.next_key.next();
475        // TODO - find a better reaction to wraparound
476        assert!(key != 0, "bad key from u64 wraparound");
477        WaitKey { raw: key }
478    }
479
480    fn register_callback(&self, callback: WaitCallback) -> WaitKey {
481        let key = self.next_key();
482        assert!(
483            self.callbacks.lock().insert(key, callback).is_none(),
484            "unexpected callback already present for key {key:?}"
485        );
486        key
487    }
488
489    fn remove_callback(&self, key: &WaitKey) -> Option<WaitCallback> {
490        self.callbacks.lock().remove(&key)
491    }
492
493    fn wake_immediately(&self, events: FdEvents, handler: EventHandler) {
494        let callback = WaitCallback::EventHandler(handler);
495        let key = self.register_callback(callback);
496        self.queue_events(&key, WaitEvents::Fd(events));
497    }
498
499    /// Establish an asynchronous wait for the signals on the given Zircon handle (not to be
500    /// confused with POSIX signals), optionally running a FnOnce. Wait operations will return
501    /// the error code present in the provided SignalHandler.
502    ///
503    /// Returns a `PortWaitCanceler` that can be used to cancel the wait.
504    fn wake_on_zircon_signals(
505        self: &Arc<Self>,
506        handle: &dyn zx::AsHandleRef,
507        zx_signals: zx::Signals,
508        handler: SignalHandler,
509    ) -> Result<PortWaitCanceler, zx::Status> {
510        let callback = WaitCallback::SignalHandler(handler);
511        let key = self.register_callback(callback);
512        self.port.object_wait_async(
513            handle,
514            key.raw,
515            zx_signals,
516            zx::WaitAsyncOpts::EDGE_TRIGGERED,
517        )?;
518        Ok(PortWaitCanceler { waiter: Arc::downgrade(self), key })
519    }
520
521    fn queue_events(&self, key: &WaitKey, events: WaitEvents) {
522        scopeguard::defer! {
523            self.port.notify(NotifyKind::Regular)
524        }
525
526        // Handling user events immediately when they are triggered breaks any
527        // ordering expectations on Linux by batching all starnix events with
528        // the first starnix event even if other events occur on the Fuchsia
529        // platform (and are enqueued to the `zx::Port`) between them. This
530        // ordering does not seem to be load-bearing for applications running on
531        // starnix so we take the divergence in ordering in favour of improved
532        // performance (by minimizing syscalls) when operating on FDs backed by
533        // starnix.
534        //
535        // TODO(https://fxbug.dev/42084319): If we can read a batch of packets
536        // from the `zx::Port`, maybe we can keep the ordering?
537        let Some(callback) = self.remove_callback(key) else {
538            return;
539        };
540
541        match callback {
542            WaitCallback::EventHandler(handler) => {
543                let events = match events {
544                    // If the event is All, signal on all possible fd
545                    // events.
546                    WaitEvents::All => FdEvents::all(),
547                    WaitEvents::Fd(events) => events,
548                    WaitEvents::SignalMask(_) => FdEvents::POLLIN,
549                    WaitEvents::Value(_) => FdEvents::POLLIN,
550                };
551                handler.handle(events)
552            }
553            WaitCallback::SignalHandler(_) => {
554                panic!("wrong type of handler called")
555            }
556        }
557    }
558
559    fn notify(&self) {
560        self.port.notify(NotifyKind::Regular);
561    }
562
563    fn interrupt(&self) {
564        if self.options.contains(WaiterOptions::IGNORE_SIGNALS) {
565            return;
566        }
567        self.port.notify(NotifyKind::Interrupt);
568    }
569}
570
571impl std::fmt::Debug for PortWaiter {
572    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
573        f.debug_struct("PortWaiter").field("port", &self.port).finish_non_exhaustive()
574    }
575}
576
577/// A type that can put a thread to sleep waiting for a condition.
578#[derive(Debug, Clone)]
579pub struct Waiter {
580    // TODO(https://g-issues.fuchsia.dev/issues/303068424): Avoid `PortWaiter`
581    // when operating purely over FDs backed by starnix.
582    inner: Arc<PortWaiter>,
583}
584
585impl Waiter {
586    /// Create a new waiter.
587    pub fn new() -> Self {
588        Self { inner: PortWaiter::new(WaiterOptions::empty()) }
589    }
590
591    /// Create a new waiter with the given options.
592    pub fn with_options(options: WaiterOptions) -> Self {
593        Self { inner: PortWaiter::new(options) }
594    }
595
596    /// Create a weak reference to this waiter.
597    fn weak(&self) -> WaiterRef {
598        WaiterRef::from_port(&self.inner)
599    }
600
601    /// Freeze the task until the waiter is woken up.
602    ///
603    /// No signal, e.g. EINTR (interrupt), should be received.
604    pub fn freeze<L>(&self, locked: &mut Locked<L>, current_task: &CurrentTask)
605    where
606        L: LockEqualOrBefore<FileOpsCore>,
607    {
608        while self
609            .inner
610            .wait_until(
611                locked,
612                current_task,
613                RunState::Frozen(self.clone()),
614                zx::MonotonicInstant::INFINITE,
615            )
616            .is_err()
617        {
618            // Avoid attempting to freeze the task if there is a pending SIGKILL.
619            if current_task.read().has_signal_pending(SIGKILL) {
620                break;
621            }
622            // Ignore spurious wakeups from the [`PortEvent.futex`]
623        }
624    }
625
626    /// Wait until the waiter is woken up.
627    ///
628    /// If the wait is interrupted (see [`Waiter::interrupt`]), this function returns EINTR.
629    pub fn wait<L>(&self, locked: &mut Locked<L>, current_task: &CurrentTask) -> Result<(), Errno>
630    where
631        L: LockEqualOrBefore<FileOpsCore>,
632    {
633        self.inner.wait_until(
634            locked,
635            current_task,
636            RunState::Waiter(WaiterRef::from_port(&self.inner)),
637            zx::MonotonicInstant::INFINITE,
638        )
639    }
640
641    /// Wait until the given deadline has passed or the waiter is woken up.
642    ///
643    /// If the wait deadline is nonzero and is interrupted (see [`Waiter::interrupt`]), this
644    /// function returns EINTR. Callers must take special care not to lose any accumulated data or
645    /// local state when EINTR is received as this is a normal and recoverable situation.
646    ///
647    /// Using a 0 deadline (no waiting, useful for draining pending events) will not wait and is
648    /// guaranteed not to issue EINTR.
649    ///
650    /// It the timeout elapses with no events, this function returns ETIMEDOUT.
651    ///
652    /// Processes at most one event. If the caller is interested in draining the events, it should
653    /// repeatedly call this function with a 0 deadline until it reports ETIMEDOUT. (This case is
654    /// why a 0 deadline must not return EINTR, as previous calls to wait_until() may have
655    /// accumulated state that would be lost when returning EINTR to userspace.)
656    ///
657    /// It is up to the caller (the "waiter") to make sure that it synchronizes with any object
658    /// that triggers an event (the "notifier"). This `Waiter` does not provide any synchronization
659    /// itself. Note that synchronization between the "waiter" the "notifier" may be provided by
660    /// the [`EventHandler`] used to handle an event iff the waiter observes the side-effects of
661    /// the handler (e.g. reading the ready list modified by [`EventHandler::Enqueue`] or
662    /// [`EventHandler::EnqueueOnce`]).
663    pub fn wait_until<L>(
664        &self,
665        locked: &mut Locked<L>,
666        current_task: &CurrentTask,
667        deadline: zx::MonotonicInstant,
668    ) -> Result<(), Errno>
669    where
670        L: LockEqualOrBefore<FileOpsCore>,
671    {
672        self.inner.wait_until(
673            locked,
674            current_task,
675            RunState::Waiter(WaiterRef::from_port(&self.inner)),
676            deadline,
677        )
678    }
679
680    fn create_wait_entry(&self, filter: WaitEvents) -> WaitEntry {
681        WaitEntry { waiter: self.weak(), filter, key: self.inner.next_key() }
682    }
683
684    fn create_wait_entry_with_handler(
685        &self,
686        filter: WaitEvents,
687        handler: EventHandler,
688    ) -> WaitEntry {
689        let key = self.inner.register_callback(WaitCallback::EventHandler(handler));
690        WaitEntry { waiter: self.weak(), filter, key }
691    }
692
693    pub fn wake_immediately(&self, events: FdEvents, handler: EventHandler) {
694        self.inner.wake_immediately(events, handler);
695    }
696
697    /// Establish an asynchronous wait for the signals on the given Zircon handle (not to be
698    /// confused with POSIX signals), optionally running a FnOnce.
699    ///
700    /// Returns a `PortWaitCanceler` that can be used to cancel the wait.
701    pub fn wake_on_zircon_signals(
702        &self,
703        handle: &dyn zx::AsHandleRef,
704        zx_signals: zx::Signals,
705        handler: SignalHandler,
706    ) -> Result<PortWaitCanceler, zx::Status> {
707        self.inner.wake_on_zircon_signals(handle, zx_signals, handler)
708    }
709
710    /// Return a WaitCanceler representing a wait that will never complete. Useful for stub
711    /// implementations that should block forever even though a real implementation would wake up
712    /// eventually.
713    pub fn fake_wait(&self) -> WaitCanceler {
714        WaitCanceler::new_noop()
715    }
716
717    // Notify the waiter to wake it up without signalling any events.
718    pub fn notify(&self) {
719        self.inner.notify();
720    }
721
722    /// Interrupt the waiter to deliver a signal. The wait operation will return EINTR, and a
723    /// typical caller should then unwind to the syscall dispatch loop to let the signal be
724    /// processed. See wait_until() for more details.
725    ///
726    /// Ignored if the waiter was created with new_ignoring_signals().
727    pub fn interrupt(&self) {
728        self.inner.interrupt();
729    }
730}
731
732impl Drop for Waiter {
733    fn drop(&mut self) {
734        // Delete ourselves from each wait queue we know we're on to prevent Weak references to
735        // ourself from sticking around forever.
736        let wait_queues = std::mem::take(&mut *self.inner.wait_queues.lock()).into_values();
737        for wait_queue in wait_queues {
738            if let Some(wait_queue) = wait_queue.upgrade() {
739                wait_queue.lock().waiters.retain(|_, entry| entry.entry.waiter != *self)
740            }
741        }
742    }
743}
744
745impl Default for Waiter {
746    fn default() -> Self {
747        Self::new()
748    }
749}
750
751impl PartialEq for Waiter {
752    fn eq(&self, other: &Self) -> bool {
753        Arc::ptr_eq(&self.inner, &other.inner)
754    }
755}
756
757pub struct SimpleWaiter {
758    event: Arc<InterruptibleEvent>,
759    wait_queues: Vec<Weak<LockDepMutex<WaitQueueImpl, WaitQueueImplLock>>>,
760}
761
762impl SimpleWaiter {
763    pub fn new(event: &Arc<InterruptibleEvent>) -> (SimpleWaiter, EventWaitGuard<'_>) {
764        (SimpleWaiter { event: event.clone(), wait_queues: Default::default() }, event.begin_wait())
765    }
766}
767
768impl Drop for SimpleWaiter {
769    fn drop(&mut self) {
770        for wait_queue in &self.wait_queues {
771            if let Some(wait_queue) = wait_queue.upgrade() {
772                wait_queue.lock().waiters.retain(|_, entry| entry.entry.waiter != self.event)
773            }
774        }
775    }
776}
777
778#[derive(Debug, Clone)]
779enum WaiterKind {
780    Port(Weak<PortWaiter>),
781    Event(Weak<InterruptibleEvent>),
782    AbortHandle(Weak<futures::stream::AbortHandle>),
783}
784
785impl Default for WaiterKind {
786    fn default() -> Self {
787        WaiterKind::Port(Default::default())
788    }
789}
790
791/// A weak reference to a Waiter. Intended for holding in wait queues or stashing elsewhere for
792/// calling queue_events later.
793#[derive(Debug, Default, Clone)]
794pub struct WaiterRef(WaiterKind);
795
796impl WaiterRef {
797    fn from_port(waiter: &Arc<PortWaiter>) -> WaiterRef {
798        WaiterRef(WaiterKind::Port(Arc::downgrade(waiter)))
799    }
800
801    fn from_event(event: &Arc<InterruptibleEvent>) -> WaiterRef {
802        WaiterRef(WaiterKind::Event(Arc::downgrade(event)))
803    }
804
805    pub fn from_abort_handle(handle: &Arc<futures::stream::AbortHandle>) -> WaiterRef {
806        WaiterRef(WaiterKind::AbortHandle(Arc::downgrade(handle)))
807    }
808
809    pub fn is_valid(&self) -> bool {
810        match &self.0 {
811            WaiterKind::Port(waiter) => waiter.strong_count() != 0,
812            WaiterKind::Event(event) => event.strong_count() != 0,
813            WaiterKind::AbortHandle(handle) => handle.strong_count() != 0,
814        }
815    }
816
817    pub fn interrupt(&self) {
818        match &self.0 {
819            WaiterKind::Port(waiter) => {
820                if let Some(waiter) = waiter.upgrade() {
821                    waiter.interrupt();
822                }
823            }
824            WaiterKind::Event(event) => {
825                if let Some(event) = event.upgrade() {
826                    event.interrupt();
827                }
828            }
829            WaiterKind::AbortHandle(handle) => {
830                if let Some(handle) = handle.upgrade() {
831                    handle.abort();
832                }
833            }
834        }
835    }
836
837    fn remove_callback(&self, key: &WaitKey) {
838        match &self.0 {
839            WaiterKind::Port(waiter) => {
840                if let Some(waiter) = waiter.upgrade() {
841                    waiter.remove_callback(key);
842                }
843            }
844            _ => (),
845        }
846    }
847
848    /// Attempts to upgrade a waiter ref to a notifiable ref. If the waiter ref is no
849    /// longer valid, returns None.
850    fn upgrade_notifiable(&self) -> Option<NotifiableRef> {
851        match &self.0 {
852            WaiterKind::Port(waiter) => {
853                if let Some(waiter) = waiter.upgrade() {
854                    return Some(NotifiableRef::Port(waiter));
855                }
856            }
857            WaiterKind::Event(event) => {
858                if let Some(event) = event.upgrade() {
859                    return Some(NotifiableRef::Event(event));
860                }
861            }
862            WaiterKind::AbortHandle(handle) => {
863                if let Some(handle) = handle.upgrade() {
864                    return Some(NotifiableRef::AbortHandle(handle));
865                }
866            }
867        }
868        None
869    }
870
871    /// Called by the WaitQueue when this waiter is about to be removed from the queue.
872    ///
873    /// TODO(abarth): This function does not appear to be called when the WaitQueue is dropped,
874    /// which appears to be a leak.
875    fn will_remove_from_wait_queue(&self, key: &WaitKey) {
876        match &self.0 {
877            WaiterKind::Port(waiter) => {
878                if let Some(waiter) = waiter.upgrade() {
879                    waiter.wait_queues.lock().remove(key);
880                }
881            }
882            _ => (),
883        }
884    }
885}
886
887impl PartialEq<Waiter> for WaiterRef {
888    fn eq(&self, other: &Waiter) -> bool {
889        match &self.0 {
890            WaiterKind::Port(waiter) => waiter.as_ptr() == Arc::as_ptr(&other.inner),
891            _ => false,
892        }
893    }
894}
895
896impl PartialEq<Arc<InterruptibleEvent>> for WaiterRef {
897    fn eq(&self, other: &Arc<InterruptibleEvent>) -> bool {
898        match &self.0 {
899            WaiterKind::Event(event) => event.as_ptr() == Arc::as_ptr(other),
900            _ => false,
901        }
902    }
903}
904
905impl PartialEq for WaiterRef {
906    fn eq(&self, other: &WaiterRef) -> bool {
907        match (&self.0, &other.0) {
908            (WaiterKind::Port(lhs), WaiterKind::Port(rhs)) => Weak::ptr_eq(lhs, rhs),
909            (WaiterKind::Event(lhs), WaiterKind::Event(rhs)) => Weak::ptr_eq(lhs, rhs),
910            (WaiterKind::AbortHandle(lhs), WaiterKind::AbortHandle(rhs)) => Weak::ptr_eq(lhs, rhs),
911            _ => false,
912        }
913    }
914}
915
916impl NotifiableRef {
917    fn notify(&self, key: &WaitKey, events: WaitEvents) {
918        match self {
919            NotifiableRef::Port(port_waiter) => port_waiter.queue_events(key, events),
920            NotifiableRef::Event(interruptible_event) => interruptible_event.notify(),
921            NotifiableRef::AbortHandle(handle) => handle.abort(),
922        }
923    }
924}
925
926/// A list of waiters waiting for some event.
927///
928/// For events that are generated inside Starnix, we walk the wait queue
929/// on the thread that triggered the event to notify the waiters that the event
930/// has occurred. The waiters will then wake up on their own thread to handle
931/// the event.
932#[derive(Default, Debug, Clone)]
933pub struct WaitQueue(Arc<LockDepMutex<WaitQueueImpl, WaitQueueImplLock>>);
934
935impl WaitQueue {
936    pub fn new() -> Self {
937        Self(Arc::new(WaitQueueImpl::default().into()))
938    }
939}
940
941#[derive(Debug)]
942struct WaitEntryWithId {
943    entry: WaitEntry,
944    /// The ID use to uniquely identify this wait entry even if it shares the
945    /// key used in the wait queue's [`Slab`] with another wait entry since a
946    /// slab's keys are recycled.
947    id: u64,
948}
949
950struct WaitEntryId {
951    key: usize,
952    id: u64,
953}
954
955#[derive(Default, Debug)]
956struct WaitQueueImpl {
957    /// Holds the next ID value to use when adding a new `WaitEntry` to the
958    /// waiters (dense) map.
959    ///
960    /// A [`Slab`]s keys are recycled so we use the ID to uniquely identify a
961    /// wait entry.
962    next_wait_entry_id: u64,
963    /// The list of waiters.
964    ///
965    /// The waiter's wait_queues lock is nested inside this lock.
966    waiters: Slab<WaitEntryWithId>,
967}
968
969/// An entry in a WaitQueue.
970#[derive(Debug)]
971struct WaitEntry {
972    /// The waiter that is waking for the FdEvent.
973    waiter: WaiterRef,
974
975    /// The events that the waiter is waiting for.
976    filter: WaitEvents,
977
978    /// key for cancelling and queueing events
979    key: WaitKey,
980}
981
982impl WaitQueue {
983    fn add_waiter(&self, entry: WaitEntry) -> WaitEntryId {
984        let mut wait_queue = self.0.lock();
985        let id = wait_queue
986            .next_wait_entry_id
987            .checked_add(1)
988            .expect("all possible wait entry ID values exhausted");
989        wait_queue.next_wait_entry_id = id;
990        WaitEntryId { key: wait_queue.waiters.insert(WaitEntryWithId { entry, id }), id }
991    }
992
993    /// Establish a wait for the given entry.
994    ///
995    /// The waiter will be notified when an event matching the entry occurs.
996    ///
997    /// This function does not actually block the waiter. To block the waiter,
998    /// call the [`Waiter::wait`] function on the waiter.
999    ///
1000    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1001    fn wait_async_entry(&self, waiter: &Waiter, entry: WaitEntry) -> WaitCanceler {
1002        let wait_key = entry.key;
1003        let waiter_id = self.add_waiter(entry);
1004        let wait_queue = Arc::downgrade(&self.0);
1005        waiter.inner.wait_queues.lock().insert(wait_key, wait_queue.clone());
1006        WaitCanceler::new_inner(WaitCancelerInner::Queue(WaitCancelerQueue {
1007            wait_queue,
1008            waiter: waiter.weak(),
1009            wait_key,
1010            waiter_id,
1011        }))
1012    }
1013
1014    /// Establish a wait for the given value event.
1015    ///
1016    /// The waiter will be notified when an event with the same value occurs.
1017    ///
1018    /// This function does not actually block the waiter. To block the waiter,
1019    /// call the [`Waiter::wait`] function on the waiter.
1020    ///
1021    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1022    pub fn wait_async_value(&self, waiter: &Waiter, value: u64) -> WaitCanceler {
1023        self.wait_async_entry(waiter, waiter.create_wait_entry(WaitEvents::Value(value)))
1024    }
1025
1026    /// Establish a wait for the given value event with an associated handler.
1027    ///
1028    /// The waiter will be notified when an event with the same value occurs, triggering the handler.
1029    ///
1030    /// This function does not actually block the waiter. To block the waiter,
1031    /// call the [`Waiter::wait`] function on the waiter.
1032    ///
1033    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1034    pub fn wait_async_value_with_handler(
1035        &self,
1036        waiter: &Waiter,
1037        value: u64,
1038        handler: EventHandler,
1039    ) -> WaitCanceler {
1040        let entry = waiter.create_wait_entry_with_handler(WaitEvents::Value(value), handler);
1041        self.wait_async_entry(waiter, entry)
1042    }
1043
1044    /// Establish a wait for the given FdEvents.
1045    ///
1046    /// The waiter will be notified when an event matching the `events` occurs.
1047    ///
1048    /// This function does not actually block the waiter. To block the waiter,
1049    /// call the [`Waiter::wait`] function on the waiter.
1050    ///
1051    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1052    pub fn wait_async_fd_events(
1053        &self,
1054        waiter: &Waiter,
1055        events: FdEvents,
1056        handler: EventHandler,
1057    ) -> WaitCanceler {
1058        let entry = waiter.create_wait_entry_with_handler(WaitEvents::Fd(events), handler);
1059        self.wait_async_entry(waiter, entry)
1060    }
1061
1062    /// Establish a wait for a particular signal mask.
1063    ///
1064    /// The waiter will be notified when a signal in the mask is received.
1065    ///
1066    /// This function does not actually block the waiter. To block the waiter,
1067    /// call the [`Waiter::wait`] function on the waiter.
1068    ///
1069    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1070    pub fn wait_async_signal_mask(
1071        &self,
1072        waiter: &Waiter,
1073        mask: SigSet,
1074        handler: EventHandler,
1075    ) -> WaitCanceler {
1076        let entry = waiter.create_wait_entry_with_handler(WaitEvents::SignalMask(mask), handler);
1077        self.wait_async_entry(waiter, entry)
1078    }
1079
1080    /// Establish a wait for any event.
1081    ///
1082    /// The waiter will be notified when any event occurs.
1083    ///
1084    /// This function does not actually block the waiter. To block the waiter,
1085    /// call the [`Waiter::wait`] function on the waiter.
1086    ///
1087    /// Returns a `WaitCanceler` that can be used to cancel the wait.
1088    pub fn wait_async(&self, waiter: &Waiter) -> WaitCanceler {
1089        self.wait_async_entry(waiter, waiter.create_wait_entry(WaitEvents::All))
1090    }
1091
1092    pub fn wait_async_simple(&self, waiter: &mut SimpleWaiter) {
1093        let entry = WaitEntry {
1094            waiter: WaiterRef::from_event(&waiter.event),
1095            filter: WaitEvents::All,
1096            key: Default::default(),
1097        };
1098        waiter.wait_queues.push(Arc::downgrade(&self.0));
1099        self.add_waiter(entry);
1100    }
1101
1102    fn notify_events_count(&self, mut events: WaitEvents, mut limit: usize) -> usize {
1103        if let WaitEvents::Fd(ref mut fd_events) = events {
1104            *fd_events = fd_events.add_equivalent_fd_events();
1105        }
1106        // Store references to waiters ready to be notified locally so that we can drop our waiters
1107        // lock before notifying the waiters. The waiters will need to acquire the waiters lock once
1108        // they wake up in order to remove themselves from the queue and so they might contend
1109        // with this thread for that lock.
1110        // Usually we expect to notify at most a single waiter.
1111        let mut notifiable_refs = SmallVec::<[(NotifiableRef, WaitKey); 1]>::new();
1112        let mut woken = 0;
1113        {
1114            let mut guard = self.0.lock();
1115            guard.waiters.retain(|_, WaitEntryWithId { entry, id: _ }| {
1116                if limit > 0 && entry.filter.intercept(&events) {
1117                    if let Some(notifiable_ref) = entry.waiter.upgrade_notifiable() {
1118                        limit -= 1;
1119                        woken += 1;
1120                        notifiable_refs.push((notifiable_ref, entry.key));
1121                    }
1122
1123                    entry.waiter.will_remove_from_wait_queue(&entry.key);
1124                    false
1125                } else {
1126                    true
1127                }
1128            });
1129        }
1130        for (notifiable_ref, key) in notifiable_refs {
1131            notifiable_ref.notify(&key, events);
1132        }
1133        woken
1134    }
1135
1136    pub fn notify_fd_events(&self, events: FdEvents) {
1137        self.notify_events_count(WaitEvents::Fd(events), usize::MAX);
1138    }
1139
1140    pub fn notify_fd_events_count(&self, events: FdEvents, limit: usize) {
1141        self.notify_events_count(WaitEvents::Fd(events), limit);
1142    }
1143
1144    pub fn notify_signal(&self, signal: &Signal) {
1145        let event = WaitEvents::SignalMask(SigSet::from(*signal));
1146        self.notify_events_count(event, usize::MAX);
1147    }
1148
1149    pub fn notify_value(&self, value: u64) {
1150        self.notify_events_count(WaitEvents::Value(value), usize::MAX);
1151    }
1152
1153    pub fn notify_unordered_count(&self, limit: usize) {
1154        self.notify_events_count(WaitEvents::All, limit);
1155    }
1156
1157    pub fn notify_all(&self) {
1158        self.notify_unordered_count(usize::MAX);
1159    }
1160
1161    /// Returns whether there is no active waiters waiting on this `WaitQueue`.
1162    pub fn is_empty(&self) -> bool {
1163        self.0.lock().waiters.is_empty()
1164    }
1165}
1166
1167/// A wait queue that dispatches events based on the value of an enum.
1168pub struct TypedWaitQueue<T: Into<u64>> {
1169    wait_queue: WaitQueue,
1170    value_type: std::marker::PhantomData<T>,
1171}
1172
1173// We can't #[derive(Default)] on [TypedWaitQueue<T>] as T may not implement the Default trait.
1174impl<T: Into<u64>> Default for TypedWaitQueue<T> {
1175    fn default() -> Self {
1176        Self { wait_queue: Default::default(), value_type: Default::default() }
1177    }
1178}
1179
1180impl<T: Into<u64>> TypedWaitQueue<T> {
1181    pub fn wait_async_value(&self, waiter: &Waiter, value: T) -> WaitCanceler {
1182        self.wait_queue.wait_async_value(waiter, value.into())
1183    }
1184
1185    pub fn wait_async_value_with_handler(
1186        &self,
1187        waiter: &Waiter,
1188        value: T,
1189        handler: EventHandler,
1190    ) -> WaitCanceler {
1191        self.wait_queue.wait_async_value_with_handler(waiter, value.into(), handler)
1192    }
1193
1194    pub fn notify_value(&self, value: T) {
1195        self.wait_queue.notify_value(value.into())
1196    }
1197}
1198
1199#[cfg(test)]
1200mod tests {
1201    use super::*;
1202    use crate::fs::fuchsia::create_fuchsia_pipe;
1203    use crate::signals::SignalInfo;
1204    use crate::task::TaskFlags;
1205    use crate::testing::{spawn_kernel_and_run, spawn_kernel_and_run_sync};
1206    use crate::vfs::buffers::{VecInputBuffer, VecOutputBuffer};
1207    use crate::vfs::eventfd::{EventFdType, new_eventfd};
1208    use assert_matches::assert_matches;
1209    use starnix_sync::Unlocked;
1210    use starnix_uapi::open_flags::OpenFlags;
1211    use starnix_uapi::signals::SIGUSR1;
1212
1213    const KEY: ReadyItemKey = ReadyItemKey::Usize(1234);
1214
1215    #[::fuchsia::test]
1216    async fn test_async_wait_exec() {
1217        spawn_kernel_and_run(async |locked, current_task| {
1218            let (local_socket, remote_socket) = zx::Socket::create_stream();
1219            let pipe =
1220                create_fuchsia_pipe(locked, &current_task, remote_socket, OpenFlags::RDWR).unwrap();
1221
1222            const MEM_SIZE: usize = 1024;
1223            let mut output_buffer = VecOutputBuffer::new(MEM_SIZE);
1224
1225            let test_string = "hello startnix".to_string();
1226            let queue: Arc<LockDepMutex<VecDeque<ReadyItem>, EventHandlerReadyQueueLock>> =
1227                Default::default();
1228            let handler = EventHandler::Enqueue {
1229                key: KEY,
1230                queue: queue.clone(),
1231                sought_events: FdEvents::all(),
1232            };
1233            let waiter = Waiter::new();
1234            pipe.wait_async(locked, &current_task, &waiter, FdEvents::POLLIN, handler)
1235                .expect("wait_async");
1236            let test_string_clone = test_string.clone();
1237
1238            let write_count = AtomicCounter::<usize>::default();
1239            std::thread::scope(|s| {
1240                let thread = s.spawn(|| {
1241                    let test_data = test_string_clone.as_bytes();
1242                    let no_written = local_socket.write(test_data).unwrap();
1243                    assert_eq!(0, write_count.add(no_written));
1244                    assert_eq!(no_written, test_data.len());
1245                });
1246
1247                // this code would block on failure
1248
1249                assert!(queue.lock().is_empty());
1250                waiter.wait(locked, &current_task).unwrap();
1251                thread.join().expect("join thread")
1252            });
1253            queue.lock().iter().for_each(|item| assert!(item.events.contains(FdEvents::POLLIN)));
1254
1255            let read_size = pipe.read(locked, &current_task, &mut output_buffer).unwrap();
1256
1257            let no_written = write_count.get();
1258            assert_eq!(no_written, read_size);
1259
1260            assert_eq!(output_buffer.data(), test_string.as_bytes());
1261        })
1262        .await;
1263    }
1264
1265    #[::fuchsia::test]
1266    async fn test_async_wait_cancel() {
1267        for do_cancel in [true, false] {
1268            spawn_kernel_and_run(async move |locked, current_task| {
1269                let event = new_eventfd(locked, &current_task, 0, EventFdType::Counter, true);
1270                let waiter = Waiter::new();
1271                let queue: Arc<LockDepMutex<VecDeque<ReadyItem>, EventHandlerReadyQueueLock>> =
1272                    Default::default();
1273                let handler = EventHandler::Enqueue {
1274                    key: KEY,
1275                    queue: queue.clone(),
1276                    sought_events: FdEvents::all(),
1277                };
1278                let wait_canceler = event
1279                    .wait_async(locked, &current_task, &waiter, FdEvents::POLLIN, handler)
1280                    .expect("wait_async");
1281                if do_cancel {
1282                    wait_canceler.cancel();
1283                }
1284                let add_val = 1u64;
1285                assert_eq!(
1286                    event
1287                        .write(
1288                            locked,
1289                            &current_task,
1290                            &mut VecInputBuffer::new(&add_val.to_ne_bytes())
1291                        )
1292                        .unwrap(),
1293                    std::mem::size_of::<u64>()
1294                );
1295
1296                let wait_result =
1297                    waiter.wait_until(locked, &current_task, zx::MonotonicInstant::ZERO);
1298                let final_count = queue.lock().len();
1299                if do_cancel {
1300                    assert_eq!(wait_result, error!(ETIMEDOUT));
1301                    assert_eq!(0, final_count);
1302                } else {
1303                    assert_eq!(wait_result, Ok(()));
1304                    assert_eq!(1, final_count);
1305                }
1306            })
1307            .await;
1308        }
1309    }
1310
1311    #[::fuchsia::test]
1312    async fn single_waiter_multiple_waits_cancel_one_waiter_still_notified() {
1313        spawn_kernel_and_run(async |locked, current_task| {
1314            let wait_queue = WaitQueue::default();
1315            let waiter = Waiter::new();
1316            let wk1 = wait_queue.wait_async(&waiter);
1317            let _wk2 = wait_queue.wait_async(&waiter);
1318            wk1.cancel();
1319            wait_queue.notify_all();
1320            assert!(waiter.wait_until(locked, &current_task, zx::MonotonicInstant::ZERO).is_ok());
1321        })
1322        .await;
1323    }
1324
1325    #[::fuchsia::test]
1326    async fn multiple_waiters_cancel_one_other_still_notified() {
1327        spawn_kernel_and_run(async |locked, current_task| {
1328            let wait_queue = WaitQueue::default();
1329            let waiter1 = Waiter::new();
1330            let waiter2 = Waiter::new();
1331            let wk1 = wait_queue.wait_async(&waiter1);
1332            let _wk2 = wait_queue.wait_async(&waiter2);
1333            wk1.cancel();
1334            wait_queue.notify_all();
1335            assert!(waiter1.wait_until(locked, &current_task, zx::MonotonicInstant::ZERO).is_err());
1336            assert!(waiter2.wait_until(locked, &current_task, zx::MonotonicInstant::ZERO).is_ok());
1337        })
1338        .await;
1339    }
1340
1341    #[::fuchsia::test]
1342    async fn test_wait_queue() {
1343        spawn_kernel_and_run(async |locked, current_task| {
1344            let queue = WaitQueue::default();
1345
1346            let waiters = <[Waiter; 3]>::default();
1347            waiters.iter().for_each(|w| {
1348                queue.wait_async(w);
1349            });
1350
1351            let woken = |locked: &mut Locked<Unlocked>| {
1352                waiters
1353                    .iter()
1354                    .filter(|w| {
1355                        w.wait_until(locked, &current_task, zx::MonotonicInstant::ZERO).is_ok()
1356                    })
1357                    .count()
1358            };
1359
1360            const INITIAL_NOTIFY_COUNT: usize = 2;
1361            let total_waiters = waiters.len();
1362            queue.notify_unordered_count(INITIAL_NOTIFY_COUNT);
1363            assert_eq!(INITIAL_NOTIFY_COUNT, woken(locked));
1364
1365            // Only the remaining (unnotified) waiters should be notified.
1366            queue.notify_all();
1367            assert_eq!(total_waiters - INITIAL_NOTIFY_COUNT, woken(locked));
1368        })
1369        .await;
1370    }
1371
1372    #[::fuchsia::test]
1373    async fn waiter_kind_abort_handle() {
1374        spawn_kernel_and_run_sync(|_locked, current_task| {
1375            let mut executor = fuchsia_async::TestExecutor::new();
1376            let (abort_handle, abort_registration) = futures::stream::AbortHandle::new_pair();
1377            let abort_handle = Arc::new(abort_handle);
1378            let waiter_ref = WaiterRef::from_abort_handle(&abort_handle);
1379
1380            let mut fut = futures::stream::Abortable::new(
1381                futures::future::pending::<()>(),
1382                abort_registration,
1383            );
1384
1385            assert_matches!(executor.run_until_stalled(&mut fut), std::task::Poll::Pending);
1386
1387            waiter_ref.interrupt();
1388            let output = current_task.run_in_state(RunState::Waiter(waiter_ref), move || {
1389                match executor.run_singlethreaded(&mut fut) {
1390                    Ok(()) => unreachable!("future never terminates normally"),
1391                    Err(futures::stream::Aborted) => Ok(()),
1392                }
1393            });
1394
1395            assert_eq!(output, Ok(()));
1396        })
1397        .await;
1398    }
1399
1400    #[::fuchsia::test]
1401    async fn freeze_with_pending_sigusr1() {
1402        spawn_kernel_and_run(async |_locked, current_task| {
1403            {
1404                let mut task_state = current_task.task.write();
1405                let siginfo = SignalInfo::kernel(SIGUSR1);
1406                task_state.enqueue_signal(siginfo);
1407                task_state.set_flags(TaskFlags::SIGNALS_AVAILABLE, true);
1408            }
1409
1410            let output: Result<(), Errno> = current_task
1411                .run_in_state(RunState::Event(InterruptibleEvent::new()), move || {
1412                    unreachable!("callback should not be called")
1413                });
1414            assert_eq!(output, error!(EINTR));
1415
1416            let output = current_task.run_in_state(RunState::Frozen(Waiter::new()), move || Ok(()));
1417            assert_eq!(output, Ok(()));
1418        })
1419        .await;
1420    }
1421
1422    #[::fuchsia::test]
1423    async fn freeze_with_pending_sigkill() {
1424        spawn_kernel_and_run(async |_locked, current_task| {
1425            {
1426                let mut task_state = current_task.task.write();
1427                let siginfo = SignalInfo::kernel(SIGKILL);
1428                task_state.enqueue_signal(siginfo);
1429                task_state.set_flags(TaskFlags::SIGNALS_AVAILABLE, true);
1430            }
1431
1432            let output: Result<(), _> = current_task
1433                .run_in_state(RunState::Frozen(Waiter::new()), move || {
1434                    unreachable!("callback should not be called")
1435                });
1436            assert_eq!(output, error!(EINTR));
1437        })
1438        .await;
1439    }
1440
1441    #[::fuchsia::test]
1442    async fn test_async_typed_wait_value_with_handler() {
1443        spawn_kernel_and_run(async |locked, current_task| {
1444            let queue: Arc<LockDepMutex<VecDeque<ReadyItem>, EventHandlerReadyQueueLock>> =
1445                Default::default();
1446            let handler = EventHandler::Enqueue {
1447                key: KEY,
1448                queue: queue.clone(),
1449                sought_events: FdEvents::all(),
1450            };
1451            let waiter = Waiter::new();
1452            let wait_queue = TypedWaitQueue::<u64>::default();
1453
1454            let test_value = 100u64;
1455            let _wait_canceler =
1456                wait_queue.wait_async_value_with_handler(&waiter, test_value, handler);
1457
1458            assert!(queue.lock().is_empty());
1459
1460            // Notify wrong value
1461            wait_queue.notify_value(test_value + 1);
1462            assert!(queue.lock().is_empty());
1463
1464            // Notify right value
1465            wait_queue.notify_value(test_value);
1466
1467            waiter.wait(locked, &current_task).expect("wait failed");
1468
1469            // Result delivered via POLLIN logic in central dispatcher
1470            let ready_items = queue.lock();
1471            assert_eq!(ready_items.len(), 1);
1472            assert!(ready_items[0].events.contains(FdEvents::POLLIN));
1473        })
1474        .await;
1475    }
1476}