starina_api/
mainloop.rs

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
//! The mainloop for applications.
use core::marker::PhantomData;

use hashbrown::HashMap;
use starina_types::error::FtlError;
use starina_types::handle::HandleId;
use starina_types::message::MessageBuffer;
use starina_types::message::MessageDeserialize;
use starina_types::poll::PollEvent;

use crate::channel::Channel;
use crate::channel::ChannelReceiver;
use crate::channel::ChannelSender;
use crate::channel::RecvError;
use crate::interrupt::Interrupt;
use crate::poll::Poll;

#[derive(Debug)]
pub enum Error {
    PollCreate(FtlError),
    PollAdd(FtlError),
    PollWait(FtlError),
    ChannelRecv(RecvError),
    ChannelRecvWouldBlock,
    ChannelAlreadyAdded(Channel),
    ChannelReceiverAlreadyAdded((ChannelReceiver, ChannelSender)),
    InterruptAlreadyAdded(Interrupt),
}

/// Events that applications need to handle.
#[derive(Debug)]
#[non_exhaustive]
pub enum Event<'a, Ctx, M: MessageDeserialize> {
    /// A received message.
    Message {
        /// The per-object state associated with the channel object.
        ctx: &'a mut Ctx,
        /// The received message.
        message: M::Reader<'a>,
        /// The channel where the message is received.
        sender: &'a mut ChannelSender,
        /// The handle ID of the channel.
        handle_id: HandleId,
    },
    /// A received hardware interrupts.
    Interrupt {
        /// The object which received the interrupt.
        interrupt: &'a mut Interrupt,
        /// The per-object state associated with the interrupt object.
        ctx: &'a mut Ctx,
    },
    /// An error occurred when processing events.
    Error(Error),
}

enum Object {
    Channel {
        receiver: ChannelReceiver,
        sender: ChannelSender,
    },
    Interrupt(Interrupt),
}

struct Entry<Ctx> {
    handle_id: HandleId,
    ctx: Ctx,
    object: Object,
}

/// The mainloop for applications.
///
/// This is a simple event loop to enable asynchronous programming without
/// Rust's `async fn`s. It is designed to be used in the `main` function of
/// applications.
///
/// # Per-object state
///
/// Each object in the mainloop has its own state. For example, a TCP socket
/// channel would have a state to track the connection state, timers, and
/// TX/RX buffers.
///
/// See the example below for how to define and use per-object state.
///
/// # Why not async Rust?
///
/// This API is very similar to `epoll` + non-blocking I/O in Linux. An event
/// loop API like this means that you need to write state machines manually, which
/// async Rust (`async fn`) does automatically.
///
/// However, explicit state machines make debugging easier because the
/// execution flow is crystal clear. Also we don't have to care about pitfalls like
/// *cancellation safety*. Moreover, my observation is that most of
/// OS components are very simple and manual state machines are sufficient.
///
/// # Future work
///
/// - Support multi-threaded mainloop.
///
/// # Example
///
/// ```
/// starina_api::autogen!(); // Include starina_autogen module
///
/// use starina_api::channel::Channel;
/// use starina_api::environ::Environ;
/// use starina_api::mainloop::Event;
/// use starina_api::mainloop::Mainloop;
/// use starina_api::prelude::*;
/// use starina_autogen::idl::ping::PingReply;
/// use starina_autogen::idl::Message;
///
/// // Per-object state.
/// #[derive(Debug)]
/// enum Context {
///     Startup,
///     Client { counter: i32 },
/// }
///
/// #[no_mangle]
/// pub fn main(mut env: Environ) {
///     let mut mainloop = Mainloop::<Context, Message>::new().unwrap();
///
///     // Take the startup channel, and start receiving messages through the
///     // mainloop.
///     let startup_ch = env.take_channel("dep:startup").unwrap();
///     mainloop.add_channel(startup_ch, Context::Startup).unwrap();
///
///     // Mainloop!
///     loop {
///        // Wait for the next event. Use `match` not to miss unexpected cases.
///         match mainloop.next() {
///             Event::Message { // The "message received" event.
///                 ctx: Context::Startup, // The message is from startup.
///                 message: Message::NewClient(m), // NewClient message.
///                 ..
///             } => {
///                 // Take the new client's channel and register it to the
///                 // mainloop.
///                 let new_ch = m.handle.take::<Channel>().unwrap();
///                 mainloop
///                     .add_channel(new_ch, Context::Client { counter: 0 })
///                     .unwrap();
///             }
///             Event::Message { // The "message received" event.
///                 ctx: Context::Client { counter }, // The message is from a client.
///                 message: Message::Ping(m), // Ping message.
///                 sender, // The channel which received the message.
///             } => {
///                 // Update the per-object state. It's mutable!
///                 *counter += 1;
///
///                 // Reply with the counter value.
///                 if let Err(err) = sender.send(PingReply { value: *counter }) {
///                     debug_warn!("failed to reply: {:?}", err);
///                 }
///             }
///             ev => {
///                 panic!("unexpected event: {:?}", ev);
///             }
///         }
///     }
/// }
/// ```
pub struct Mainloop<Ctx, AllM> {
    poll: Poll,
    objects: HashMap<HandleId, Entry<Ctx>>,
    msgbuffer: MessageBuffer,
    _pd: PhantomData<AllM>,
}

impl<Ctx, AllM: MessageDeserialize> Mainloop<Ctx, AllM> {
    /// Creates a new mainloop.
    pub fn new() -> Result<Self, Error> {
        let poll = Poll::new().map_err(Error::PollCreate)?;

        Ok(Self {
            poll,
            objects: HashMap::new(),
            msgbuffer: MessageBuffer::new(),
            _pd: PhantomData,
        })
    }

    /// Removes an object.
    pub fn remove(&mut self, handle_id: HandleId) -> Result<(), FtlError> {
        self.poll.remove(handle_id)?;
        self.objects.remove(&handle_id); // TODO: warn if not found
        Ok(())
    }

    /// Adds a channel to start receiving messages in the mainloop.
    pub fn add_channel<T: Into<(ChannelSender, ChannelReceiver)>>(
        &mut self,
        channel: T,
        state: Ctx,
    ) -> Result<(), Error> {
        let (sender, receiver) = channel.into();
        let handle_id = receiver.handle().id();
        if self.objects.contains_key(&handle_id) {
            return Err(Error::ChannelReceiverAlreadyAdded((receiver, sender)));
        }

        let entry = Entry {
            ctx: state,
            object: Object::Channel { receiver, sender },
            handle_id,
        };

        self.objects.insert(handle_id, entry);
        self.poll
            .add(handle_id, PollEvent::READABLE)
            .map_err(Error::PollAdd)?;

        Ok(())
    }

    /// Adds an interrupt to start receiving interrupts in the mainloop.
    pub fn add_interrupt(&mut self, interrupt: Interrupt, state: Ctx) -> Result<(), Error> {
        let handle_id = interrupt.handle().id();
        if self.objects.contains_key(&handle_id) {
            return Err(Error::InterruptAlreadyAdded(interrupt));
        }

        let entry = Entry {
            ctx: state,
            object: Object::Interrupt(interrupt),
            handle_id,
        };

        self.objects.insert(handle_id, entry);
        self.poll
            .add(handle_id, PollEvent::READABLE)
            .map_err(Error::PollAdd)?;

        Ok(())
    }

    /// Waits for the next event. Blocks until an event is available.
    pub fn next(&mut self) -> Event<'_, Ctx, AllM> {
        let (poll_ev, handle_id) = match self.poll.wait() {
            Ok(ev) => ev,
            Err(err) => return Event::Error(Error::PollWait(err)),
        };

        let entry = self.objects.get_mut(&handle_id).unwrap();
        if poll_ev.contains(PollEvent::READABLE) {
            match &mut entry.object {
                Object::Channel { sender, receiver } => {
                    let message = match receiver.try_recv::<AllM>(&mut self.msgbuffer) {
                        Ok(Some(m)) => m,
                        Ok(None) => return Event::Error(Error::ChannelRecvWouldBlock),
                        Err(err) => return Event::Error(Error::ChannelRecv(err)),
                    };

                    return Event::Message {
                        ctx: &mut entry.ctx,
                        handle_id: entry.handle_id,
                        message,
                        sender,
                    };
                }
                Object::Interrupt(interrupt) => {
                    return Event::Interrupt {
                        ctx: &mut entry.ctx,
                        interrupt,
                    };
                }
            }
        }

        todo!("unhandled poll event: {:?}", poll_ev);
    }
}