Skip to content

Commit

Permalink
Use expires_at to check if a task is enqueued
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Dec 10, 2024
1 parent 01bece5 commit 20cda81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion embassy-executor/src/raw/timer_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl TimerQueueItem {
pub(crate) const fn new() -> Self {
Self {
next: Cell::new(None),
expires_at: Cell::new(0),
expires_at: Cell::new(u64::MAX),
}
}
}
22 changes: 12 additions & 10 deletions embassy-time-queue-driver/src/queue_integrated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ impl TimerQueue {
/// 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 at <= item.expires_at.get() {
// If expiration is sooner than previously set, update.

if item.expires_at.get() == u64::MAX {
// If not in the queue, add it and update.
let prev = self.head.replace(Some(p));
item.next.set(prev);
}

item.expires_at.set(at);
true
} else {
// Task does not need to be updated.
return false;
false
}

item.expires_at.set(at);
true
}

/// Dequeues expired timers and returns the next alarm time.
Expand Down Expand Up @@ -71,7 +73,7 @@ impl TimerQueue {
} else {
// Remove it
prev.set(item.next.get());
item.next.set(None);
item.expires_at.set(u64::MAX);
}
}
}
Expand Down

0 comments on commit 20cda81

Please sign in to comment.