Skip to content

Commit

Permalink
[refactor] remove in_timer_list flag in axtask
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Sep 30, 2024
1 parent 75b7756 commit 347f3fa
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
17 changes: 0 additions & 17 deletions modules/axtask/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ pub struct TaskInner {

/// Mark whether the task is in the wait queue.
in_wait_queue: AtomicBool,
/// Mark whether the task is in the timer list.
#[cfg(feature = "irq")]
in_timer_list: AtomicBool,
/// A ticket ID used to identify the timer event.
/// Incremented by 1 each time the timer event is triggered or expired.
#[cfg(feature = "irq")]
Expand Down Expand Up @@ -200,8 +197,6 @@ impl TaskInner {
cpumask: SpinNoIrq::new(CpuMask::full()),
in_wait_queue: AtomicBool::new(false),
#[cfg(feature = "irq")]
in_timer_list: AtomicBool::new(false),
#[cfg(feature = "irq")]
timer_ticket_id: AtomicU64::new(0),
on_cpu: AtomicBool::new(false),
prev_task_on_cpu_ptr: AtomicPtr::new(core::ptr::null_mut()),
Expand Down Expand Up @@ -297,18 +292,6 @@ impl TaskInner {
self.in_wait_queue.store(in_wait_queue, Ordering::Release);
}

#[inline]
#[cfg(feature = "irq")]
pub(crate) fn in_timer_list(&self) -> bool {
self.in_timer_list.load(Ordering::Acquire)
}

#[inline]
#[cfg(feature = "irq")]
pub(crate) fn set_in_timer_list(&self, in_timer_list: bool) {
self.in_timer_list.store(in_timer_list, Ordering::Release);
}

/// Returns current available timer ticket ID.
#[inline]
#[cfg(feature = "irq")]
Expand Down
9 changes: 3 additions & 6 deletions modules/axtask/src/timers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ struct TaskWakeupEvent {

impl TimerEvent for TaskWakeupEvent {
fn callback(self, _now: TimeValue) {
// Ignore the timer event if the task is not in the timer list.
// timeout was set but not triggered (wake up by `WaitQueue::notify()`).
// Ignore the timer event if timeout was set but not triggered
// (wake up by `WaitQueue::notify()`).
// Judge if this timer event is still valid by checking the ticket ID.
if !self.task.in_timer_list() || self.task.timer_ticket() != self.ticket_id {
if self.task.timer_ticket() != self.ticket_id {
// The task is not in the timer list or the ticket ID is not matched.
// Just ignore this timer event and return.
return;
}

// Timer ticket match.
// Mark the task as not in the timer list.
self.task.set_in_timer_list(false);
// Timer event is triggered, expire the ticket ID.
self.task.timer_ticket_expire_one();
select_run_queue::<NoOp>(self.task.clone()).unblock_task(self.task, true)
Expand All @@ -37,7 +35,6 @@ impl TimerEvent for TaskWakeupEvent {

pub fn set_alarm_wakeup(deadline: TimeValue, task: AxTaskRef) {
TIMER_LIST.with_current(|timer_list| {
task.set_in_timer_list(true);
timer_list.set(
deadline,
TaskWakeupEvent {
Expand Down
20 changes: 13 additions & 7 deletions modules/axtask/src/wait_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ impl WaitQueue {
}
}

fn cancel_events(&self, curr: CurrentTask) {
/// Cancel events by removing the task from the wait queue.
/// If `from_timer_list` is true, the task should be removed from the timer list.
fn cancel_events(&self, curr: CurrentTask, from_timer_list: bool) {
// A task can be wake up only one events (timer or `notify()`), remove
// the event from another queue.
if curr.in_wait_queue() {
Expand All @@ -56,9 +58,10 @@ impl WaitQueue {
wq_locked.retain(|t| !curr.ptr_eq(t));
curr.set_in_wait_queue(false);
}
// Try to cancel a timer event from timer lists.
// Just mark task's current timer ticket ID as expired.
#[cfg(feature = "irq")]
if curr.in_timer_list() {
curr.set_in_timer_list(false);
if from_timer_list {
curr.timer_ticket_expire_one();
// TODO:
// this task is still not removed from timer list of target CPU,
Expand Down Expand Up @@ -91,7 +94,7 @@ impl WaitQueue {
let mut rq = current_run_queue::<NoPreemptIrqSave>();
self.push_to_wait_queue();
rq.blocked_resched();
self.cancel_events(crate::current());
self.cancel_events(crate::current(), false);
}

/// Blocks the current task and put it into the wait queue, until the given
Expand Down Expand Up @@ -128,7 +131,7 @@ impl WaitQueue {

rq.blocked_resched();
}
self.cancel_events(crate::current());
self.cancel_events(crate::current(), false);
}

/// Blocks the current task and put it into the wait queue, until other tasks
Expand All @@ -149,7 +152,10 @@ impl WaitQueue {
rq.blocked_resched();

let timeout = curr.in_wait_queue(); // still in the wait queue, must have timed out
self.cancel_events(curr);

// If `timeout` is true, the task is still in the wait queue,
// which means timer event is triggered and the task has been removed from timer list.
self.cancel_events(curr, !timeout);
timeout
}

Expand Down Expand Up @@ -196,7 +202,7 @@ impl WaitQueue {

rq.blocked_resched()
}
self.cancel_events(curr);
self.cancel_events(curr, !timeout);
timeout
}

Expand Down

0 comments on commit 347f3fa

Please sign in to comment.