Skip to content

Commit

Permalink
Deduplicate RefCell+Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Dec 6, 2024
1 parent 4434326 commit f849279
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 98 deletions.
21 changes: 1 addition & 20 deletions embassy-nrf/src/time_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<embassy_time_queue_driver::queue_generic::Queue>,
}

#[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<RawQueue>,
Expand Down
21 changes: 1 addition & 20 deletions embassy-rp/src/time_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<embassy_time_queue_driver::queue_generic::Queue>,
}

#[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<CriticalSectionRawMutex, RawQueue>,
Expand Down
21 changes: 1 addition & 20 deletions embassy-stm32/src/time_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<embassy_time_queue_driver::queue_generic::Queue>,
}

#[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<CriticalSectionRawMutex, RawQueue>,
Expand Down
50 changes: 50 additions & 0 deletions embassy-time-queue-driver/src/queue_generic.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<Queue>,
}

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")]
Expand Down Expand Up @@ -168,4 +194,28 @@ mod implem {
self.queue.next_expiration(now)
}
}

/// A simple wrapper around a `Queue` to provide interior mutability.
pub struct RefCellQueue<const QUEUE_SIZE: usize> {
inner: RefCell<Queue<QUEUE_SIZE>>,
}

impl<const QUEUE_SIZE: usize> RefCellQueue<QUEUE_SIZE> {
/// 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)
}
}
}
20 changes: 1 addition & 19 deletions embassy-time/src/driver_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,25 +164,7 @@ impl<T: Copy> UninitCell<T> {
}
}

struct RawQueue {
inner: core::cell::RefCell<embassy_time_queue_driver::queue_generic::Queue>,
}

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<RawQueue>,
Expand Down
20 changes: 1 addition & 19 deletions embassy-time/src/driver_wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,7 @@ impl<T: Copy> UninitCell<T> {
}
}

struct RawQueue {
inner: core::cell::RefCell<embassy_time_queue_driver::queue_generic::Queue>,
}

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<RawQueue>,
Expand Down

0 comments on commit f849279

Please sign in to comment.