From f8492796fb8edf8432943706d7bdf2dd572ceb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Sat, 7 Dec 2024 00:31:55 +0100 Subject: [PATCH] Deduplicate RefCell+Queue --- embassy-nrf/src/time_driver.rs | 21 +------- embassy-rp/src/time_driver.rs | 21 +------- embassy-stm32/src/time_driver.rs | 21 +------- .../src/queue_generic.rs | 50 +++++++++++++++++++ embassy-time/src/driver_std.rs | 20 +------- embassy-time/src/driver_wasm.rs | 20 +------- 6 files changed, 55 insertions(+), 98 deletions(-) diff --git a/embassy-nrf/src/time_driver.rs b/embassy-nrf/src/time_driver.rs index 8731cba85f..a4c02062b8 100644 --- a/embassy-nrf/src/time_driver.rs +++ b/embassy-nrf/src/time_driver.rs @@ -281,26 +281,7 @@ pub(crate) fn init(irq_prio: crate::interrupt::Priority) { type RawQueue = embassy_executor::raw::timer_queue::TimerQueue; #[cfg(not(feature = "integrated-timers"))] -struct RawQueue { - inner: core::cell::RefCell, -} - -#[cfg(not(feature = "integrated-timers"))] -impl RawQueue { - const fn new() -> Self { - Self { - inner: core::cell::RefCell::new(embassy_time_queue_driver::queue_generic::Queue::new()), - } - } - - fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { - self.inner.borrow_mut().schedule_wake(at, waker) - } - - fn next_expiration(&self, now: u64) -> u64 { - self.inner.borrow_mut().next_expiration(now) - } -} +type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue; struct TimerQueueDriver { inner: Mutex, diff --git a/embassy-rp/src/time_driver.rs b/embassy-rp/src/time_driver.rs index a6fec98fe2..ad71e1fea6 100644 --- a/embassy-rp/src/time_driver.rs +++ b/embassy-rp/src/time_driver.rs @@ -129,26 +129,7 @@ fn TIMER0_IRQ_0() { type RawQueue = embassy_executor::raw::timer_queue::TimerQueue; #[cfg(not(feature = "integrated-timers"))] -struct RawQueue { - inner: core::cell::RefCell, -} - -#[cfg(not(feature = "integrated-timers"))] -impl RawQueue { - const fn new() -> Self { - Self { - inner: core::cell::RefCell::new(embassy_time_queue_driver::queue_generic::Queue::new()), - } - } - - fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { - self.inner.borrow_mut().schedule_wake(at, waker) - } - - fn next_expiration(&self, now: u64) -> u64 { - self.inner.borrow_mut().next_expiration(now) - } -} +type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue; struct TimerQueueDriver { inner: Mutex, diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index b40d58de4b..c8874d49f0 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs @@ -522,26 +522,7 @@ pub(crate) fn init(cs: CriticalSection) { type RawQueue = embassy_executor::raw::timer_queue::TimerQueue; #[cfg(not(feature = "integrated-timers"))] -struct RawQueue { - inner: core::cell::RefCell, -} - -#[cfg(not(feature = "integrated-timers"))] -impl RawQueue { - const fn new() -> Self { - Self { - inner: core::cell::RefCell::new(embassy_time_queue_driver::queue_generic::Queue::new()), - } - } - - fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { - self.inner.borrow_mut().schedule_wake(at, waker) - } - - fn next_expiration(&self, now: u64) -> u64 { - self.inner.borrow_mut().next_expiration(now) - } -} +type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue; struct TimerQueueDriver { inner: Mutex, diff --git a/embassy-time-queue-driver/src/queue_generic.rs b/embassy-time-queue-driver/src/queue_generic.rs index b86f70cbfb..a40ac36550 100644 --- a/embassy-time-queue-driver/src/queue_generic.rs +++ b/embassy-time-queue-driver/src/queue_generic.rs @@ -1,10 +1,12 @@ //! A generic timer queue. Time queue drivers may use this to simplify their implementation. +use core::cell::RefCell; use core::cmp::{min, Ordering}; use core::task::Waker; use heapless::Vec; pub use implem::Queue; +pub use implem::RefCellQueue; #[derive(Debug)] struct Timer { @@ -139,6 +141,30 @@ mod implem { self.queue.next_expiration(now) } } + + /// A simple wrapper around a `Queue` to provide interior mutability. + pub struct RefCellQueue { + inner: RefCell, + } + + impl RefCellQueue { + /// Creates a new timer queue. + pub const fn new() -> Self { + Self { + inner: RefCell::new(Queue::new()), + } + } + + /// Schedules a task to run at a specific time, and returns whether any changes were made. + pub fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { + self.inner.borrow_mut().schedule_wake(at, waker) + } + + /// Dequeues expired timers and returns the next alarm time. + pub fn next_expiration(&self, now: u64) -> u64 { + self.inner.borrow_mut().next_expiration(now) + } + } } #[cfg(feature = "generic-queue-const-generic")] @@ -168,4 +194,28 @@ mod implem { self.queue.next_expiration(now) } } + + /// A simple wrapper around a `Queue` to provide interior mutability. + pub struct RefCellQueue { + inner: RefCell>, + } + + impl RefCellQueue { + /// Creates a new timer queue. + pub const fn new() -> Self { + Self { + inner: RefCell::new(Queue::new()), + } + } + + /// Schedules a task to run at a specific time, and returns whether any changes were made. + pub fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { + self.inner.borrow_mut().schedule_wake(at, waker) + } + + /// Dequeues expired timers and returns the next alarm time. + pub fn next_expiration(&self, now: u64) -> u64 { + self.inner.borrow_mut().next_expiration(now) + } + } } diff --git a/embassy-time/src/driver_std.rs b/embassy-time/src/driver_std.rs index e2f22bd8b9..6f9328cd1a 100644 --- a/embassy-time/src/driver_std.rs +++ b/embassy-time/src/driver_std.rs @@ -164,25 +164,7 @@ impl UninitCell { } } -struct RawQueue { - inner: core::cell::RefCell, -} - -impl RawQueue { - const fn new() -> Self { - Self { - inner: core::cell::RefCell::new(embassy_time_queue_driver::queue_generic::Queue::new()), - } - } - - fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { - self.inner.borrow_mut().schedule_wake(at, waker) - } - - fn next_expiration(&self, now: u64) -> u64 { - self.inner.borrow_mut().next_expiration(now) - } -} +type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue; struct TimerQueueDriver { inner: Mutex, diff --git a/embassy-time/src/driver_wasm.rs b/embassy-time/src/driver_wasm.rs index 12c62ac294..cda89580cb 100644 --- a/embassy-time/src/driver_wasm.rs +++ b/embassy-time/src/driver_wasm.rs @@ -108,25 +108,7 @@ impl UninitCell { } } -struct RawQueue { - inner: core::cell::RefCell, -} - -impl RawQueue { - const fn new() -> Self { - Self { - inner: core::cell::RefCell::new(embassy_time_queue_driver::queue_generic::Queue::new()), - } - } - - fn schedule_wake(&self, waker: &core::task::Waker, at: u64) -> bool { - self.inner.borrow_mut().schedule_wake(at, waker) - } - - fn next_expiration(&self, now: u64) -> u64 { - self.inner.borrow_mut().next_expiration(now) - } -} +type RawQueue = embassy_time_queue_driver::queue_generic::RefCellQueue; struct TimerQueueDriver { inner: Mutex,