uefi-async 0.2.8

A lightweight asynchronous executor for UEFI environments.
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
use alloc::boxed::Box;
use alloc::collections::VecDeque;
use core::cell::RefCell;
use alloc::rc::Rc;
use alloc::sync::Arc;
use core::pin::Pin;
use core::task::{Context, Poll};
use crossbeam::queue::{ArrayQueue, SegQueue};
use spin::Mutex;
use crate::Yield;

pub mod single {
    use super::*;

    pub mod oneshot {
        use super::*;

        /// A synchronization primitive for sending a single value between asynchronous tasks.
        ///
        /// This channel is specifically designed for **single-core, multi-asynchronous** environments
        /// such as UEFI applications. It allows one task to provide a value (the Sender) and
        /// another task to asynchronously wait for that value (the Receiver).
        ///
        /// Since the environment is single-threaded, it utilizes `Rc<RefCell<Option<T>>>`
        /// instead of atomic primitives to maintain low overhead and `no_std` compatibility.
        ///
        /// # Examples
        ///
        /// ```rust
        /// async fn example() {
        ///     // 1. Create a channel pair
        ///     let (tx, rx) = oneshot::new::<u64>();
        ///
        ///     // 2. The Receiver waits for data without blocking the entire system
        ///     // In a real scenario, tx would be moved into a separate TaskNode
        ///     let data = rx.await;
        ///
        ///     // 3. Data is received and execution resumes
        /// }
        /// ```
        #[derive(Debug)]
        pub struct Oneshot<Data> (Rc<RefCell<Option<Data>>>);
        /// A future that resolves to the value sent by the corresponding [`Sender`].
        #[derive(Debug)]
        pub struct OneShotReceiver<Data> (Rc<RefCell<Option<Data>>>);

        impl<Data> Oneshot<Data> {
            /// Creates a new oneshot channel, returning the sender/receiver halves.
            ///
            /// All data sent via the [`Sender`] will become available to the [`Receiver`].
            pub fn new() -> (Self, OneShotReceiver<Data>) {
                let data = Rc::new(RefCell::new(None));
                (Self(data.clone()), OneShotReceiver(data))
            }
            /// Completes the channel by sending a value.
            ///
            /// This consumes the sender, as only one value can ever be sent.
            pub fn send(self, value: Data) { *self.0.borrow_mut() = Some(value) }
        }

        impl<Data> Future for OneShotReceiver<Data> {
            type Output = Data;
            /// Polls the receiver to check if the value has been sent.
            ///
            /// If the value is present, it returns [`Poll::Ready`].
            /// Otherwise, it returns [`Poll::Pending`], allowing the executor to
            /// switch to other tasks (cooperative multitasking).
            fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Data> {
                match self.0.borrow_mut().take() {
                    Some(val) => Poll::Ready(val),
                    None => Poll::Pending,
                }
            }
        }
    }

    pub mod unbounded_channel {
        use super::*;

        /// A Single-Core, Multi-Producer, Single-Consumer (MPSC) asynchronous channel.
        ///
        /// This channel is designed for single-core, multi-asynchronous environments
        /// typical in UEFI systems. It allows multiple senders to push data into a
        /// shared queue, while a single receiver waits for and consumes that data asynchronously.
        ///
        /// # Features
        /// * **Unbounded Capacity**: The queue grows dynamically (powered by `VecDeque`).
        /// * **Non-blocking Sending**: Sending is an immediate, synchronous operation.
        /// * **Asynchronous Receiving**: The receiver implements [`Future`], returning
        ///   `Poll::Pending` when empty and `Poll::Ready(Data)` when data arrives.
        ///
        /// # Examples
        ///
        /// ```rust,no_run
        /// // Main logic consuming events
        /// async fn main_logic(mut rx: ChannelReceiver<KeyEvent>) {
        ///     loop {
        ///         // If the queue is empty, .await will yield control back to the executor (Pending).
        ///         // Once data is pushed by a sender, the task is re-polled and returns Ready.
        ///         let key = rx.await;
        ///         match key {
        ///             KeyEvent::ScanCode(0x01) => break, // Exit on ESC key
        ///             _ => render_game(key),
        ///         }
        ///     }
        /// }
        ///
        /// // Input task producing events
        /// async fn input_poller(tx: ChannelSender<KeyEvent>) {
        ///     loop {
        ///         if let Some(key) = poll_uefi_key() {
        ///             // Synchronously push data into the channel
        ///             tx.send(key);
        ///         }
        ///         // Yield execution to allow the main_logic or other tasks to run
        ///         Yield.await;
        ///     }
        /// }
        /// ```
        pub fn channel<Data>() -> (ChannelSender<Data>, ChannelReceiver<Data>) {
            let queue = Rc::new(RefCell::new(VecDeque::new()));
            (ChannelSender(queue.clone()), ChannelReceiver(queue))
        }

        #[derive(Clone, Debug)]
        pub struct ChannelSender<Data> (Rc<RefCell<VecDeque<Data>>>);
        #[derive(Debug)]
        pub struct ChannelReceiver<Data> (Rc<RefCell<VecDeque<Data>>>);
        impl<Data> ChannelSender<Data> {
            /// Sends a value into the channel.
            ///
            /// Since this is an unbounded channel, this operation is synchronous and
            /// will not block the current task.
            pub fn send(&self, value: Data) { self.0.borrow_mut().push_back(value); }
        }
        impl<Data> Future for ChannelReceiver<Data> {
            type Output = Data;
            /// Polls the channel for the next available piece of data.
            ///
            /// Returns [`Poll::Ready(value)`] if the queue contains data.
            /// Returns [`Poll::Pending`] if the queue is empty, signaling the executor
            /// to switch to another task until the next polling cycle.
            fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
                match self.0.borrow_mut().pop_front() {
                    Some(value) => Poll::Ready(value),
                    None => Poll::Pending,
                }
            }
        }


    }

    pub mod bounded_channel {
        use super::*;

        /// A lightweight, single-core, multi-producer single-consumer (MPSC) asynchronous channel.
        ///
        /// This channel is specifically designed for single-threaded executors (like those in UEFI).
        /// It allows asynchronous tasks to communicate without the need for complex synchronization
        /// primitives like Mutex or Atomic types, leveraging the non-preemptive nature of the executor.
        ///
        /// # Safety
        /// This implementation uses raw pointers. It is safe **only** within a single-threaded
        /// executor where the `Receiver` is guaranteed to outlive all `Sender` instances,
        /// or where the `Sender` is not used after the `Receiver` is dropped.
        ///
        /// # Example
        /// ```rust,no_run
        /// // 1. Create a channel for keyboard events with a capacity of 32
        /// let (tx, mut rx) = bounded_channel::<Key>(32);
        ///
        /// add!(
        ///     executor => {
        ///         // Task A: Producer - Polls hardware at a high frequency (e.g., 100Hz)
        ///         100 -> async move {
        ///             loop {
        ///                 if let Some(key) = poll_keyboard() {
        ///                     tx.send(key); // Non-blocking send
        ///                 }
        ///                 Yield.await;
        ///             }
        ///         },
        ///
        ///         // Task B: Consumer - Processes game logic
        ///         0 -> async move {
        ///             loop {
        ///                 // The await point suspends the task if the queue is empty.
        ///                 // Execution resumes as soon as the producer sends data
        ///                 // and the executor polls this task again.
        ///                 let key = (&mut rx).await;
        ///                 process_game_logic(key);
        ///             }
        ///         }
        ///     }
        /// );
        /// ```
        pub fn channel<Data>(capacity: usize) -> (UnsafeChannelSender<Data>, UnsafeChannelReceiver<Data>) {
            let mut inner = Box::new(ChannelInner(VecDeque::with_capacity(capacity)));
            let sender_ptr = &mut *inner as *mut ChannelInner<Data>;

            (UnsafeChannelSender(sender_ptr), UnsafeChannelReceiver(inner))
        }

        #[derive(Debug)]
        struct ChannelInner<Data> (VecDeque<Data>);
        /// Handle to send data into the channel.
        ///
        /// Can be cloned to allow multiple producers to send data to a single receiver.
        #[derive(Clone, Debug)]
        pub struct UnsafeChannelSender<Data> (*mut ChannelInner<Data>);
        /// Handle to receive data from the channel.
        ///
        /// Implements [`Future`], yielding data when the internal queue is not empty.
        #[derive(Debug)]
        pub struct UnsafeChannelReceiver<Data> (Box<ChannelInner<Data>>);
        impl<Data> UnsafeChannelSender<Data> {
            /// Sends a value into the channel.
            ///
            /// This operation is non-blocking and assumes the internal buffer has
            /// enough capacity or can grow dynamically.
            pub fn send(&self, value: Data) {
                // Safety: In a single-core Executor, the Receiver owns the Box.
                // As long as the Executor holds the tasks, the inner pointer remains valid.
                unsafe { (*self.0).0.push_back(value) }
            }
        }
        impl<Data> Future for UnsafeChannelReceiver<Data> {
            type Output = Data;
            fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
                match self.0.0.pop_front() {
                    Some(value) => Poll::Ready(value),
                    None => Poll::Pending,
                }
            }
        }


    }
}

pub mod multiple {
    use super::*;

    pub mod oneshot {
        use super::*;

        /// A multi-core, synchronization-safe communication primitive for sending a single value
        /// between asynchronous tasks.
        ///
        /// This implementation uses an `Arc<Mutex<Option<T>>>` internally, making it suitable
        /// for cross-core communication in multi-processor UEFI environments where tasks
        /// in different executors need to synchronize data.
        ///
        /// # Usage
        ///
        /// ```rust
        /// // Example: Multi-core synchronization
        /// // Core 1 performs heavy physics while Core 0 handles rendering.
        /// let (tx, rx) = Oneshot::new();
        ///
        /// // Inside Core 1's executor:
        /// async move {
        ///     let result = heavy_physics_calculation();
        ///     tx.send(result); // Signal completion and send data
        /// };
        ///
        /// // Inside Core 0's executor:
        /// async move {
        ///     let data = rx.await; // Suspends execution until Core 1 sends the data
        ///     update_gpu_buffer(data);
        /// };
        /// ```
        #[derive(Debug)]
        pub struct Oneshot<Data>(Arc<Mutex<Option<Data>>>);

        /// The receiving half of the [`Oneshot`] channel.
        ///
        /// This struct implements [`Future`], allowing it to be `.await`ed until
        /// the sender provides a value.
        #[derive(Debug)]
        pub struct OneShotReceiver<Data>(Arc<Mutex<Option<Data>>>);

        impl<Data> Oneshot<Data> {
            /// Creates a new multi-core safe Oneshot channel pair.
            ///
            /// Returns a sender ([`Oneshot`]) and a receiver ([`OneShotReceiver`]).
            pub fn new() -> (Self, OneShotReceiver<Data>) {
                let data = Arc::new(Mutex::new(None));
                (Self(data.clone()), OneShotReceiver(data))
            }

            /// Consumes the sender and provides a value to the receiver.
            ///
            /// Since this involves an `Arc` and a `Mutex`, it can be safely called
            /// from a different CPU core than the receiver.
            pub fn send(self, value: Data) {
                let mut lock = self.0.lock();
                *lock = Some(value);
            }
        }

        impl<Data> Future for OneShotReceiver<Data> {
            type Output = Data;

            /// Polls the receiver to check if data has been sent.
            ///
            /// Returns [`Poll::Ready`] if the data is available, otherwise returns
            /// [`Poll::Pending`] and yields control back to the executor.
            fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
                // Attempt to acquire the lock and extract the data
                let mut lock = self.0.lock();
                match lock.take() {
                    Some(val) => Poll::Ready(val),
                    None => Poll::Pending,
                }
            }
        }

    }

    pub mod unbounded_channel {
        use super::*;

        /// A lock-free, multi-core safe asynchronous channel for inter-task communication.
        ///
        /// This channel is designed for high-performance scenarios where data needs to be
        /// transferred between different executor instances running on different CPU cores.
        /// It utilizes an atomic `SegQueue` to ensure that `send` operations are non-blocking
        /// and `poll` operations are thread-safe.
        ///
        /// # Architecture
        ///
        /// The channel consists of a [`ChannelSender`] and a [`ChannelReceiver`].
        /// The sender performs atomic push operations, while the receiver acts as a
        /// [`Future`], yielding control back to the executor if the queue is empty.
        ///
        /// # Usage
        ///
        /// ```rust,no_run
        /// // Example: Producer task on Core 1, Consumer task on Core 0
        /// let (tx, rx) = unbounded_channel::channel::<PhysicsResult>();
        ///
        /// // Task running on Core 1's executor
        /// async fn producer(tx: ChannelSender<PhysicsResult>) {
        ///     let result = heavy_physics_calculation();
        ///     tx.send(result); // Non-blocking atomic push
        /// }
        ///
        /// // Task running on Core 0's executor
        /// async fn consumer(rx: ChannelReceiver<PhysicsResult>) {
        ///     // This will return Poll::Pending and yield CPU if the queue is empty,
        ///     // allowing the executor to run other tasks (like UI rendering).
        ///     let data = rx.await;
        ///     update_gpu_buffer(data);
        /// }
        /// ```
        pub fn channel<Data>()
            -> (ChannelSender<Data>, ChannelReceiver<Data>) {
            let queue = Arc::new(SegQueue::new());
            (ChannelSender(queue.clone()), ChannelReceiver(queue))
        }

        #[derive(Clone, Debug)]
        pub struct ChannelSender<Data>(Arc<SegQueue<Data>>);
        #[derive(Debug)]
        pub struct ChannelReceiver<Data>(Arc<SegQueue<Data>>);

        impl<Data> ChannelSender<Data> {
            /// Sends a value into the channel.
            ///
            /// This operation is extremely efficient, typically requiring only a
            /// single Atomic CAS (Compare-And-Swap) without acquiring any locks.
            #[inline(always)]
            pub fn send(&self, value: Data) { self.0.push(value) }
        }

        impl<Data> Future for ChannelReceiver<Data> {
            type Output = Data;

            /// Polls the receiver for a value.
            ///
            /// Returns [`Poll::Ready(Data)`] if a value is available in the queue.
            /// Returns [`Poll::Pending`] if the queue is empty, suspending the
            /// current async task until the next executor tick.
            #[inline]
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
                // SegQueue's pop is thread-safe and lock-free.
                // In a multi-core scheduler, we return Pending to yield the CPU
                // if no data is currently available.
                match self.0.pop() {
                    Some(value) => Poll::Ready(value),
                    None => Poll::Pending,
                }
            }
        }
    }

    pub mod bounded_channel {
        use super::*;

        /// A multicore-safe, asynchronous, bounded MPMC (Multi-Producer, Multi-Consumer) channel.
        ///
        /// This channel is built on top of a lock-free `ArrayQueue`, making it ideal for
        /// communication between different CPU cores in a UEFI environment without the
        /// overhead of traditional mutexes.
        ///
        /// # Characteristics
        /// * **Lock-Free**: Uses Atomic CAS (Compare-And-Swap) operations for high-performance synchronization.
        /// * **Bounded**: Fixed capacity prevents memory exhaustion and provides backpressure.
        /// * **Async-Aware**: Composed of `send_async` and a `Receiver` that implements `Future`.
        ///
        /// # Usage Example
        ///
        /// ```rust
        /// // In UEFI Core 0 (Rendering Task)
        /// async fn render_task(rx: ChannelReceiver<FrameData>) {
        ///     loop {
        ///         // Suspends the task if the queue is empty, allowing other tasks to run.
        ///         let data = rx.await;
        ///         draw_frame(data);
        ///     }
        /// }
        ///
        /// // In UEFI Core 1 (Physics Task)
        /// async fn physics_task(tx: ChannelSender<FrameData>) {
        ///     loop {
        ///         let frame = calculate_physics();
        ///         // If the queue is full, it yields control back to the executor
        ///         // and retries on the next polling cycle.
        ///         tx.send_async(frame).await;
        ///     }
        /// }
        /// ```
        pub fn channel<Data>(cap: usize) -> (ChannelSender<Data>, ChannelReceiver<Data>) {
            let queue = Arc::new(ArrayQueue::new(cap));
            (ChannelSender(queue.clone()), ChannelReceiver(queue))
        }

        /// Multicore, multi-asynchronous, bounded
        #[derive(Clone,Debug)]
        pub struct ChannelSender<Data> (Arc<ArrayQueue<Data>>);
        #[derive(Debug)]
        pub struct ChannelReceiver<Data> (Arc<ArrayQueue<Data>>);

        impl<Data> ChannelSender<Data> {
            /// Attempts to send a value into the channel without blocking.
            ///
            /// Returns `Ok(())` if successful, or `Err(Data)` if the queue is full.
            #[inline(always)]
            pub fn try_send(&self, value: Data) -> Result<(), Data> { self.0.push(value) }

            /// Sends a value asynchronously.
            ///
            /// If the channel is full, this method will `Yield` control back to the executor,
            /// effectively suspending the current task until the next executor tick.
            pub async fn send_async(&self, mut data: Data) {
                loop {
                    match self.try_send(data) {
                        Ok(_) => return,
                        Err(d) => {
                            data = d;
                            // Queue is full; yield the current core's execution
                            // and wait for the next polling cycle to retry.
                            Yield.await;
                        }
                    }
                }
            }
        }

        impl<Data> Future for ChannelReceiver<Data> {
            type Output = Data;

            /// Polls the receiver for data.
            ///
            /// Internally uses `ArrayQueue::pop` which relies on atomic operations
            /// to maintain thread-safety across multiple cores.
            #[inline]
            fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
                // If no data is available, return Pending.
                // The executor will switch to other available tasks.
                match self.0.pop() {
                    Some(value) => Poll::Ready(value),
                    None => Poll::Pending,
                }
            }
        }
    }
}