1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Guest-to-host notification infrastructure, abstracting over [platform
//! events](pal_event::Event), and [task events](SlimEvent).

use crate::interrupt::Interrupt;
use crate::local_only::LocalOnly;
use crate::slim_event::SlimEvent;
use mesh::MeshPayload;
use pal_async::driver::Driver;
use pal_async::wait::PolledWait;
use pal_event::Event;
use parking_lot::Mutex;
use std::future::poll_fn;
use std::future::Future;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;

/// An object that can be signaled when the guest needs attention.
#[derive(Debug, Clone, MeshPayload)]
pub struct Notify(Inner);

impl Notify {
    /// Creates an object from an event.
    pub fn from_event(event: Event) -> Self {
        Self(Inner::Event(event))
    }

    /// Creates an object from a slim event.
    pub fn from_slim_event(event: Arc<SlimEvent>) -> Self {
        Self(Inner::SlimEvent(LocalOnly(event)))
    }

    /// Returns a pollable version, using `driver` to poll an underlying event
    /// if there is one.
    pub fn pollable(self, driver: &(impl Driver + ?Sized)) -> std::io::Result<PolledNotify> {
        Ok(PolledNotify(match self.0 {
            Inner::Event(e) => PolledInner::Event(Mutex::new(PolledWait::new(driver, e)?)),
            Inner::SlimEvent(LocalOnly(e)) => PolledInner::SlimEvent(e),
        }))
    }

    /// Gets the underlying OS event, if there is one.
    pub fn event(&self) -> Option<&Event> {
        match &self.0 {
            Inner::Event(e) => Some(e),
            Inner::SlimEvent(_) => None,
        }
    }

    /// Gets an interrupt object that can be used to signal the underlying
    /// event.
    pub fn interrupt(self) -> Interrupt {
        match self.0 {
            Inner::Event(e) => Interrupt::from_event(e),
            Inner::SlimEvent(LocalOnly(e)) => Interrupt::from_fn(move || e.signal()),
        }
    }
}

/// A [`Notify`] object that is ready to be polled.
pub struct PolledNotify(PolledInner);

#[derive(Debug, MeshPayload)]
enum Inner {
    Event(Event),
    SlimEvent(LocalOnly<Arc<SlimEvent>>),
}

impl Clone for Inner {
    fn clone(&self) -> Self {
        match self {
            Self::Event(event) => Self::Event(event.clone()),
            Self::SlimEvent(event) => Self::SlimEvent(event.clone()),
        }
    }
}

enum PolledInner {
    Event(Mutex<PolledWait<Event>>),
    SlimEvent(Arc<SlimEvent>),
}

impl PolledNotify {
    /// Polls for the notify object to be signaled.
    pub fn poll_wait(&self, cx: &mut Context<'_>) -> Poll<()> {
        match &self.0 {
            PolledInner::Event(e) => e
                .lock()
                .poll_wait(cx)
                .map(|r| r.expect("waits on Event cannot fail")),
            PolledInner::SlimEvent(e) => e.poll_wait(cx),
        }
    }

    /// Waits for the notify object to be signaled.
    pub fn wait(&mut self) -> impl '_ + Unpin + Future<Output = ()> {
        poll_fn(move |cx| match &mut self.0 {
            PolledInner::Event(e) => e
                .get_mut()
                .poll_wait(cx)
                .map(|r| r.expect("waits on Event cannot fail")),
            PolledInner::SlimEvent(e) => e.poll_wait(cx),
        })
    }

    /// Returns the inner notify object.
    pub fn into_inner(self) -> Notify {
        Notify(match self.0 {
            PolledInner::Event(e) => Inner::Event(e.into_inner().into_inner()),
            PolledInner::SlimEvent(e) => Inner::SlimEvent(LocalOnly(e)),
        })
    }
}