-
Notifications
You must be signed in to change notification settings - Fork 830
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move integrated timer queue into time-queue-driver
- Loading branch information
Showing
4 changed files
with
96 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,25 @@ | ||
//! Timer queue operations. | ||
use core::cmp::min; | ||
use super::util::SyncUnsafeCell; | ||
use core::cell::Cell; | ||
|
||
use super::TaskRef; | ||
|
||
/// An item in the timer queue. | ||
pub struct TimerQueueItem { | ||
next: SyncUnsafeCell<Option<TaskRef>>, | ||
expires_at: SyncUnsafeCell<u64>, | ||
} | ||
/// The next item in the queue. | ||
pub next: Cell<Option<TaskRef>>, | ||
|
||
impl TimerQueueItem { | ||
pub(crate) const fn new() -> Self { | ||
Self { | ||
next: SyncUnsafeCell::new(None), | ||
expires_at: SyncUnsafeCell::new(0), | ||
} | ||
} | ||
/// The time at which this item expires. | ||
pub expires_at: Cell<u64>, | ||
} | ||
|
||
/// A timer queue, with items integrated into tasks. | ||
pub struct TimerQueue { | ||
head: SyncUnsafeCell<Option<TaskRef>>, | ||
} | ||
unsafe impl Sync for TimerQueueItem {} | ||
|
||
impl TimerQueue { | ||
/// Creates a new timer queue. | ||
pub const fn new() -> Self { | ||
impl TimerQueueItem { | ||
pub(crate) const fn new() -> Self { | ||
Self { | ||
head: SyncUnsafeCell::new(None), | ||
} | ||
} | ||
|
||
/// Schedules a task to run at a specific time. | ||
/// | ||
/// If this function returns `true`, the called should find the next expiration time and set | ||
/// a new alarm for that time. | ||
pub fn schedule_wake(&mut self, at: u64, p: TaskRef) -> bool { | ||
unsafe { | ||
let item = p.timer_queue_item(); | ||
if item.next.get().is_none() { | ||
// If not in the queue, add it and update. | ||
let prev = self.head.replace(Some(p)); | ||
item.next.set(prev); | ||
} else if at <= item.expires_at.get() { | ||
// If expiration is sooner than previously set, update. | ||
} else { | ||
// Task does not need to be updated. | ||
return false; | ||
} | ||
|
||
item.expires_at.set(at); | ||
true | ||
} | ||
} | ||
|
||
/// Dequeues expired timers and returns the next alarm time. | ||
/// | ||
/// The provided callback will be called for each expired task. Tasks that never expire | ||
/// will be removed, but the callback will not be called. | ||
pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
let mut next_expiration = u64::MAX; | ||
|
||
self.retain(|p| { | ||
let item = p.timer_queue_item(); | ||
let expires = unsafe { item.expires_at.get() }; | ||
|
||
if expires <= now { | ||
// Timer expired, process task. | ||
super::wake_task(p); | ||
false | ||
} else { | ||
// Timer didn't yet expire, or never expires. | ||
next_expiration = min(next_expiration, expires); | ||
expires != u64::MAX | ||
} | ||
}); | ||
|
||
next_expiration | ||
} | ||
|
||
fn retain(&self, mut f: impl FnMut(TaskRef) -> bool) { | ||
unsafe { | ||
let mut prev = &self.head; | ||
while let Some(p) = prev.get() { | ||
let item = p.timer_queue_item(); | ||
if f(p) { | ||
// Skip to next | ||
prev = &item.next; | ||
} else { | ||
// Remove it | ||
prev.set(item.next.get()); | ||
item.next.set(None); | ||
} | ||
} | ||
next: Cell::new(None), | ||
expires_at: Cell::new(0), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Timer queue operations. | ||
use core::cell::Cell; | ||
use core::cmp::min; | ||
|
||
use embassy_executor::raw::TaskRef; | ||
|
||
/// A timer queue, with items integrated into tasks. | ||
pub struct TimerQueue { | ||
head: Cell<Option<TaskRef>>, | ||
} | ||
|
||
impl TimerQueue { | ||
/// Creates a new timer queue. | ||
pub const fn new() -> Self { | ||
Self { head: Cell::new(None) } | ||
} | ||
|
||
/// Schedules a task to run at a specific time. | ||
/// | ||
/// If this function returns `true`, the called should find the next expiration time and set | ||
/// a new alarm for that time. | ||
pub fn schedule_wake(&mut self, at: u64, p: TaskRef) -> bool { | ||
let item = p.timer_queue_item(); | ||
if item.next.get().is_none() { | ||
// If not in the queue, add it and update. | ||
let prev = self.head.replace(Some(p)); | ||
item.next.set(prev); | ||
} else if at <= item.expires_at.get() { | ||
// If expiration is sooner than previously set, update. | ||
} else { | ||
// Task does not need to be updated. | ||
return false; | ||
} | ||
|
||
item.expires_at.set(at); | ||
true | ||
} | ||
|
||
/// Dequeues expired timers and returns the next alarm time. | ||
/// | ||
/// The provided callback will be called for each expired task. Tasks that never expire | ||
/// will be removed, but the callback will not be called. | ||
pub fn next_expiration(&mut self, now: u64) -> u64 { | ||
let mut next_expiration = u64::MAX; | ||
|
||
self.retain(|p| { | ||
let item = p.timer_queue_item(); | ||
let expires = item.expires_at.get(); | ||
|
||
if expires <= now { | ||
// Timer expired, process task. | ||
embassy_executor::raw::wake_task(p); | ||
false | ||
} else { | ||
// Timer didn't yet expire, or never expires. | ||
next_expiration = min(next_expiration, expires); | ||
expires != u64::MAX | ||
} | ||
}); | ||
|
||
next_expiration | ||
} | ||
|
||
fn retain(&self, mut f: impl FnMut(TaskRef) -> bool) { | ||
let mut prev = &self.head; | ||
while let Some(p) = prev.get() { | ||
let item = p.timer_queue_item(); | ||
if f(p) { | ||
// Skip to next | ||
prev = &item.next; | ||
} else { | ||
// Remove it | ||
prev.set(item.next.get()); | ||
item.next.set(None); | ||
} | ||
} | ||
} | ||
} |