mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-29 06:54:33 +01:00
library src
This commit is contained in:
parent
1e26a536cd
commit
b163e3ec27
2 changed files with 78 additions and 0 deletions
|
@ -117,6 +117,10 @@ pub fn codegen(
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if &task.is_async {
|
||||||
|
eprintln!("")
|
||||||
|
}
|
||||||
|
|
||||||
root.push(module::codegen(
|
root.push(module::codegen(
|
||||||
Context::SoftwareTask(name),
|
Context::SoftwareTask(name),
|
||||||
needs_lt,
|
needs_lt,
|
||||||
|
|
74
src/async_util.rs
Normal file
74
src/async_util.rs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
//! Async support for RTIC
|
||||||
|
|
||||||
|
use core::{
|
||||||
|
future::Future,
|
||||||
|
mem,
|
||||||
|
pin::Pin,
|
||||||
|
task::{Context, Poll, RawWaker, RawWakerVTable, Waker},
|
||||||
|
};
|
||||||
|
|
||||||
|
//=============
|
||||||
|
// Waker
|
||||||
|
|
||||||
|
///
|
||||||
|
pub static WAKER_VTABLE: RawWakerVTable =
|
||||||
|
RawWakerVTable::new(waker_clone, waker_wake, waker_wake, waker_drop);
|
||||||
|
|
||||||
|
unsafe fn waker_clone(p: *const ()) -> RawWaker {
|
||||||
|
RawWaker::new(p, &WAKER_VTABLE)
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn waker_wake(p: *const ()) {
|
||||||
|
let f: fn() = mem::transmute(p);
|
||||||
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe fn waker_drop(_: *const ()) {
|
||||||
|
// nop
|
||||||
|
}
|
||||||
|
|
||||||
|
//============
|
||||||
|
// Task
|
||||||
|
|
||||||
|
///
|
||||||
|
pub enum Task<F: Future + 'static> {
|
||||||
|
///
|
||||||
|
Idle,
|
||||||
|
|
||||||
|
///
|
||||||
|
Running(F),
|
||||||
|
|
||||||
|
///
|
||||||
|
Done(F::Output),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: Future + 'static> Task<F> {
|
||||||
|
///
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self::Idle
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn spawn(&mut self, future: impl FnOnce() -> F) {
|
||||||
|
*self = Task::Running(future());
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub unsafe fn poll(&mut self, wake: fn()) {
|
||||||
|
match self {
|
||||||
|
Task::Idle => {}
|
||||||
|
Task::Running(future) => {
|
||||||
|
let future = Pin::new_unchecked(future);
|
||||||
|
let waker_data: *const () = mem::transmute(wake);
|
||||||
|
let waker = Waker::from_raw(RawWaker::new(waker_data, &WAKER_VTABLE));
|
||||||
|
let mut cx = Context::from_waker(&waker);
|
||||||
|
|
||||||
|
match future.poll(&mut cx) {
|
||||||
|
Poll::Ready(r) => *self = Task::Done(r),
|
||||||
|
Poll::Pending => {}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Task::Done(_) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue