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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#![cfg_attr(not(feature = "std"), no_std)]

use core::fmt;
use core::future::Future;
use core::marker::PhantomData;
use core::task::{Context, Poll};

extern crate alloc;
use alloc::sync::Arc;

use async_task::Runnable;

pub use async_task::Task;

#[cfg(feature = "std")]
pub use crate::std::*;

#[cfg(all(feature = "alloc", target_has_atomic = "ptr", target_has_atomic = "8"))]
pub use crate::eventloop::*;

#[cfg(feature = "wasm")]
pub use crate::wasm::*;

#[derive(Debug)]
pub enum SpawnError {
    QueueFull,
    CollectorFull,
}

impl fmt::Display for SpawnError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::QueueFull => write!(f, "Queue Full Error"),
            Self::CollectorFull => write!(f, "Collector Full Error"),
        }
    }
}

#[cfg(feature = "std")]
impl ::std::error::Error for SpawnError {}

pub trait Notify {
    fn notify(&self);
}

pub trait Monitor {
    type Notify: Notify;

    fn notifier(&self) -> Self::Notify;
}

pub trait Wait {
    fn wait(&self);
}

pub trait Start {
    fn start<P>(&self, poll_fn: P)
    where
        P: FnMut() + 'static;
}

pub type Local = *const ();
pub type Sendable = ();

/// `Executor` is an async executor that is useful specfically for embedded environments.
///
/// The implementation is in fact a thin wrapper around [smol](::smol)'s [async-task](::async-task) crate.
///
/// Highlights:
/// - `no_std` (but does need `alloc`; for a `no_std` *and* "no_alloc" executor, look at [Embassy](::embassy), which statically pre-allocates all tasks);
///            (note also that usage of `alloc` is very limited - only when a new task is being spawn, as well as the executor itself);
/// - Does not assume an RTOS and can run completely bare-metal (or on top of an RTOS);
/// - Pluggable [Monitor] mechanism which makes it ISR-friendly. In particular:
///   - Tasks can be woken up (and thus re-scheduled) from within an ISR;
///   - The executor itself can run not just in the main "thread", but from within an ISR as well;
///     the latter is important for bare-metal non-RTOS use-cases, as it allows - by running multiple executors -
///     one in the main "thread" and the others - on ISR interrupts - to achieve RTOS-like pre-emptive execution by scheduling higher-priority
///     tasks in executors that run on (higher-level) ISRs and thus pre-empt the executors
///     scheduled on lower-level ISR and the "main thread" one; note that deploying an executor in an ISR requires the allocator to be usable
///     from an ISR (i.e. allocation/deallocation routines should be protected by critical sections that disable/enable interrupts);
///   - Out of the box implementation for [Monitor] based on condvar, compatible with Rust STD
///     (for cases where notifying from / running in ISRs is not important).
///   - Out of the box implementation for [Monitor] based on WASM event loop, compatible with WASM
pub struct Executor<'a, const C: usize, M, S = Local> {
    #[cfg(feature = "crossbeam-queue")]
    queue: Arc<crossbeam_queue::ArrayQueue<Runnable>>,
    #[cfg(not(feature = "crossbeam-queue"))]
    queue: Arc<heapless::mpmc::MpMcQueue<Runnable, C>>,
    monitor: M,
    _sendable: PhantomData<S>,
    _marker: PhantomData<core::cell::UnsafeCell<&'a ()>>,
}

#[allow(clippy::missing_safety_doc)]
impl<'a, const C: usize, M, S> Executor<'a, C, M, S>
where
    M: Monitor,
{
    pub fn new() -> Self
    where
        M: Default,
    {
        Self::wrap(Default::default())
    }

    pub fn wrap(monitor: M) -> Self {
        Self {
            #[cfg(feature = "crossbeam-queue")]
            queue: Arc::new(crossbeam_queue::ArrayQueue::new(C)),
            #[cfg(not(feature = "crossbeam-queue"))]
            queue: Arc::new(heapless::mpmc::MpMcQueue::<_, C>::new()),
            monitor,
            _sendable: PhantomData,
            _marker: PhantomData,
        }
    }

    pub fn spawn_detached<F, T>(&self, fut: F) -> Result<&Self, SpawnError>
    where
        F: Future<Output = T> + Send + 'a,
        T: 'a,
    {
        self.spawn(fut)?.detach();

        Ok(self)
    }

    pub fn spawn_collect<F, T>(
        &self,
        fut: F,
        collector: &mut heapless::Vec<Task<T>, C>,
    ) -> Result<&Self, SpawnError>
    where
        F: Future<Output = T> + Send + 'a,
        T: 'a,
    {
        let task = self.spawn(fut)?;

        collector
            .push(task)
            .map_err(|_| SpawnError::CollectorFull)?;

        Ok(self)
    }

    pub fn spawn<F, T>(&self, fut: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T> + Send + 'a,
        T: 'a,
    {
        unsafe { self.spawn_unchecked(fut) }
    }

    pub unsafe fn spawn_unchecked<F, T>(&self, fut: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T>,
    {
        let schedule = {
            let queue = self.queue.clone();
            let notify = self.monitor.notifier();

            move |runnable| {
                #[cfg(feature = "crossbeam-queue")]
                {
                    queue.push(runnable).unwrap();
                }

                #[cfg(not(feature = "crossbeam-queue"))]
                {
                    queue.enqueue(runnable).unwrap();
                }

                notify.notify();
            }
        };

        let (runnable, task) = async_task::spawn_unchecked(fut, schedule);

        runnable.schedule();

        Ok(task)
    }

    pub fn poll_one(&self) -> Poll<()> {
        let runnable;

        #[cfg(feature = "crossbeam-queue")]
        {
            runnable = self.queue.pop();
        }

        #[cfg(not(feature = "crossbeam-queue"))]
        {
            runnable = self.queue.dequeue();
        }

        if let Some(runnable) = runnable {
            runnable.run();

            Poll::Ready(())
        } else {
            Poll::Pending
        }
    }

    pub fn poll<B>(&self, condition: B) -> Poll<()>
    where
        B: Fn() -> bool,
    {
        while condition() {
            if self.poll_one().is_pending() {
                return Poll::Pending;
            }
        }

        Poll::Ready(())
    }

    pub fn drop_tasks<T>(&self, tasks: T) {
        drop(tasks);

        while !self.poll_one().is_pending() {}
    }

    // Method below only works when a `Start` implementation is provided
    // (i.e. the executor runs in the background, in an ISR context or in the WASM event loop)

    pub fn start<B, F>(&'static self, condition: B, finished: F)
    where
        M: Start,
        B: Fn() -> bool + 'static,
        F: FnOnce() + 'static,
    {
        let executor = self;
        let mut finished_opt = Some(finished);

        self.monitor.start(move || {
            if executor.poll(&condition).is_ready() {
                if let Some(finished) = finished_opt.take() {
                    finished();
                } else {
                    unreachable!("Should not happen - finished called twice");
                }
            }
        });
    }

    // Methods below only work when a `Wait` implementation is provided
    // (i.e. the executor runs in a thread)

    pub fn run<B>(&self, condition: B)
    where
        M: Wait,
        B: Fn() -> bool,
    {
        while self.poll(&condition).is_pending() {
            self.wait();
        }
    }

    pub fn run_tasks<B, T>(&self, condition: B, tasks: T)
    where
        M: Wait,
        B: Fn() -> bool,
    {
        self.run(condition);

        self.drop_tasks(tasks)
    }

    pub fn wait(&self)
    where
        M: Wait,
    {
        self.monitor.wait();
    }
}

impl<'a, const C: usize, M> Executor<'a, C, M, Local>
where
    M: Monitor,
{
    pub fn spawn_local_detached<F, T>(&self, fut: F) -> Result<&Self, SpawnError>
    where
        F: Future<Output = T> + 'a,
        T: 'a,
    {
        self.spawn_local(fut)?.detach();

        Ok(self)
    }

    pub fn spawn_local_collect<F, T>(
        &self,
        fut: F,
        collector: &mut heapless::Vec<Task<T>, C>,
    ) -> Result<&Self, SpawnError>
    where
        F: Future<Output = T> + 'a,
        T: 'a,
    {
        let task = self.spawn_local(fut)?;

        collector
            .push(task)
            .map_err(|_| SpawnError::CollectorFull)?;

        Ok(self)
    }

    pub fn spawn_local<F, T>(&self, fut: F) -> Result<Task<T>, SpawnError>
    where
        F: Future<Output = T> + 'a,
        T: 'a,
    {
        unsafe { self.spawn_unchecked(fut) }
    }
}

impl<'a, const C: usize, M, S> Default for Executor<'a, C, M, S>
where
    M: Monitor + Default,
{
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone)]
pub struct Blocker<M>(M);

impl<M> Blocker<M>
where
    M: Monitor + Wait,
    M::Notify: Send + Sync + 'static,
{
    pub fn new() -> Self
    where
        M: Default,
    {
        Self(Default::default())
    }

    pub const fn wrap(notify_factory: M) -> Self {
        Self(notify_factory)
    }

    pub fn block_on<F>(&self, mut f: F) -> F::Output
    where
        F: Future,
    {
        log::trace!("block_on(): started");

        let mut f = unsafe { core::pin::Pin::new_unchecked(&mut f) };

        let notify = self.0.notifier();

        let waker = waker_fn::waker_fn(move || {
            notify.notify();
        });

        let cx = &mut Context::from_waker(&waker);

        loop {
            if let Poll::Ready(t) = f.as_mut().poll(cx) {
                log::trace!("block_on(): completed");
                return t;
            }

            self.0.wait();
        }
    }
}

impl<M> Default for Blocker<M>
where
    M: Monitor + Wait + Default,
    M::Notify: Send + Sync + 'static,
{
    fn default() -> Self {
        Self::new()
    }
}

// #[cfg(feature = "embedded-svc")]
// impl<M> embedded_svc::executor::asynch::Blocker for Blocker<M>
// where
//     M: Monitor + Wait,
//     M::Notify: Send + Sync + 'static,
// {
//     fn block_on<F>(&self, f: F) -> F::Output
//     where
//         F: Future,
//     {
//         Blocker::block_on(self, f)
//     }
// }

#[cfg(feature = "std")]
mod std {
    use std::sync::{Condvar, Mutex};

    extern crate alloc;
    use alloc::sync::{Arc, Weak};

    use crate::{Monitor, Notify, Wait};

    pub struct StdMonitor(Mutex<()>, Arc<Condvar>);

    impl StdMonitor {
        pub fn new() -> Self {
            Self(Mutex::new(()), Arc::new(Condvar::new()))
        }
    }

    impl Default for StdMonitor {
        fn default() -> Self {
            Self::new()
        }
    }

    impl Monitor for StdMonitor {
        type Notify = StdNotify;

        fn notifier(&self) -> Self::Notify {
            StdNotify(Arc::downgrade(&self.1))
        }
    }

    impl Wait for StdMonitor {
        fn wait(&self) {
            let guard = self.0.lock().unwrap();

            drop(self.1.wait(guard).unwrap());
        }
    }

    pub struct StdNotify(Weak<Condvar>);

    impl Notify for StdNotify {
        fn notify(&self) {
            if let Some(notify) = Weak::upgrade(&self.0) {
                notify.notify_one();
            }
        }
    }
}

#[cfg(feature = "wasm")]
mod wasm {
    use core::cell::RefCell;

    extern crate alloc;
    use alloc::rc::{Rc, Weak};

    use js_sys::Promise;

    use wasm_bindgen::prelude::*;

    use crate::{Monitor, Notify, Start};

    struct WasmContext {
        promise: Promise,
        closure: Option<Closure<dyn FnMut(JsValue)>>,
    }

    pub struct WasmMonitor(Rc<RefCell<WasmContext>>);

    impl WasmMonitor {
        pub fn new() -> Self {
            Self(Rc::new(RefCell::new(WasmContext {
                promise: Promise::resolve(&JsValue::undefined()),
                closure: None,
            })))
        }
    }

    impl Default for WasmMonitor {
        fn default() -> Self {
            Self::new()
        }
    }

    impl Monitor for WasmMonitor {
        type Notify = WasmNotify;

        fn notifier(&self) -> Self::Notify {
            WasmNotify(Rc::downgrade(&self.0))
        }
    }

    impl Start for WasmMonitor {
        fn start<P>(&self, mut poll_fn: P)
        where
            P: FnMut() + 'static,
        {
            {
                self.0.borrow_mut().closure = Some(Closure::new(move |_| poll_fn()));
            }

            self.notifier().notify();
        }
    }

    pub struct WasmNotify(Weak<RefCell<WasmContext>>);

    impl Notify for WasmNotify {
        fn notify(&self) {
            if let Some(rc) = Weak::upgrade(&self.0) {
                let ctx = rc.borrow_mut();

                if let Some(closure) = ctx.closure.as_ref() {
                    let _ = ctx.promise.then(closure);
                }
            }
        }
    }
}

#[cfg(all(feature = "alloc", target_has_atomic = "ptr", target_has_atomic = "8"))]
mod eventloop {
    use core::cell::UnsafeCell;
    use core::marker::PhantomData;
    use core::sync::atomic::{AtomicBool, Ordering};

    extern crate alloc;
    use alloc::boxed::Box;
    use alloc::sync::{Arc, Weak};

    use crate::{Monitor, Notify, Start};

    struct EventLoopContext {
        scheduler: Box<dyn Fn(extern "C" fn(*mut ()), *mut ())>,
        scheduled: AtomicBool,
        poller: UnsafeCell<Option<Box<dyn FnMut()>>>,
    }

    pub struct EventLoopMonitor(Arc<EventLoopContext>, PhantomData<*const ()>);

    impl EventLoopMonitor {
        pub fn new<S>(scheduler: S) -> Self
        where
            S: Fn(extern "C" fn(*mut ()), *mut ()) + 'static,
        {
            Self(
                Arc::new(EventLoopContext {
                    scheduler: Box::new(scheduler),
                    scheduled: AtomicBool::new(false),
                    poller: UnsafeCell::new(None),
                }),
                PhantomData,
            )
        }
    }

    impl Monitor for EventLoopMonitor {
        type Notify = EventLoopNotify;

        fn notifier(&self) -> Self::Notify {
            EventLoopNotify(Arc::downgrade(&self.0))
        }
    }

    impl Start for EventLoopMonitor {
        fn start<P>(&self, poll_fn: P)
        where
            P: FnMut() + 'static,
        {
            {
                *unsafe { self.0.poller.get().as_mut() }.unwrap() = Some(Box::new(poll_fn));
            }

            self.notifier().notify();
        }
    }

    pub struct EventLoopNotify(Weak<EventLoopContext>);

    impl EventLoopNotify {
        extern "C" fn run(ctx: *mut ()) {
            let ctx = unsafe { Arc::from_raw(ctx as *const EventLoopContext) };

            ctx.scheduled.store(false, Ordering::SeqCst);

            if let Some(poll_fn) = unsafe { ctx.poller.get().as_mut() }.unwrap().as_mut() {
                poll_fn();
            }
        }
    }

    impl Notify for EventLoopNotify {
        fn notify(&self) {
            if let Some(ctx) = Weak::upgrade(&self.0) {
                if let Ok(false) =
                    ctx.scheduled
                        .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
                {
                    // TODO: Will leak the Arc if the scheduled event is not executed
                    (ctx.scheduler)(Self::run, Arc::into_raw(ctx.clone()) as *mut _);
                }
            }
        }
    }
}