Set poll_fn in TaskStorage::new

This commit is contained in:
Grant Miller 2023-01-31 17:29:34 -06:00
parent 465e4c8b19
commit a697f1517a
2 changed files with 14 additions and 27 deletions

View File

@ -47,7 +47,7 @@ pub(crate) struct TaskHeader {
pub(crate) state: AtomicU32, pub(crate) state: AtomicU32,
pub(crate) run_queue_item: RunQueueItem, pub(crate) run_queue_item: RunQueueItem,
pub(crate) executor: Cell<*const Executor>, // Valid if state != 0 pub(crate) executor: Cell<*const Executor>, // Valid if state != 0
pub(crate) poll_fn: UninitCell<unsafe fn(TaskRef)>, // Valid if STATE_SPAWNED poll_fn: unsafe fn(TaskRef),
#[cfg(feature = "integrated-timers")] #[cfg(feature = "integrated-timers")]
pub(crate) expires_at: Cell<Instant>, pub(crate) expires_at: Cell<Instant>,
@ -55,22 +55,6 @@ pub(crate) struct TaskHeader {
pub(crate) timer_queue_item: timer_queue::TimerQueueItem, pub(crate) timer_queue_item: timer_queue::TimerQueueItem,
} }
impl TaskHeader {
const fn new() -> Self {
Self {
state: AtomicU32::new(0),
run_queue_item: RunQueueItem::new(),
executor: Cell::new(ptr::null()),
poll_fn: UninitCell::uninit(),
#[cfg(feature = "integrated-timers")]
expires_at: Cell::new(Instant::from_ticks(0)),
#[cfg(feature = "integrated-timers")]
timer_queue_item: timer_queue::TimerQueueItem::new(),
}
}
}
/// This is essentially a `&'static TaskStorage<F>` where the type of the future has been erased. /// This is essentially a `&'static TaskStorage<F>` where the type of the future has been erased.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct TaskRef { pub struct TaskRef {
@ -128,7 +112,17 @@ impl<F: Future + 'static> TaskStorage<F> {
/// Create a new TaskStorage, in not-spawned state. /// Create a new TaskStorage, in not-spawned state.
pub const fn new() -> Self { pub const fn new() -> Self {
Self { Self {
raw: TaskHeader::new(), raw: TaskHeader {
state: AtomicU32::new(0),
run_queue_item: RunQueueItem::new(),
executor: Cell::new(ptr::null()),
poll_fn: Self::poll,
#[cfg(feature = "integrated-timers")]
expires_at: Cell::new(Instant::from_ticks(0)),
#[cfg(feature = "integrated-timers")]
timer_queue_item: timer_queue::TimerQueueItem::new(),
},
future: UninitCell::uninit(), future: UninitCell::uninit(),
} }
} }
@ -164,7 +158,6 @@ impl<F: Future + 'static> TaskStorage<F> {
unsafe fn spawn_initialize(&'static self, future: impl FnOnce() -> F) -> TaskRef { unsafe fn spawn_initialize(&'static self, future: impl FnOnce() -> F) -> TaskRef {
// Initialize the task // Initialize the task
self.raw.poll_fn.write(Self::poll);
self.future.write(future()); self.future.write(future());
TaskRef::new(self) TaskRef::new(self)
} }
@ -405,7 +398,7 @@ impl Executor {
trace::task_exec_begin(p.as_ptr() as u32); trace::task_exec_begin(p.as_ptr() as u32);
// Run the task // Run the task
task.poll_fn.read()(p); (task.poll_fn)(p);
#[cfg(feature = "rtos-trace")] #[cfg(feature = "rtos-trace")]
trace::task_exec_end(); trace::task_exec_end();

View File

@ -25,9 +25,3 @@ impl<T> UninitCell<T> {
ptr::drop_in_place(self.as_mut_ptr()) ptr::drop_in_place(self.as_mut_ptr())
} }
} }
impl<T: Copy> UninitCell<T> {
pub unsafe fn read(&self) -> T {
ptr::read(self.as_mut_ptr())
}
}