mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-02 18:21:40 +08:00
ScheduledWorkItem::ScheduleClear: remove item from runnable queue
The existing behavior is unexpected: if the work item is already on the runnable queue, it will still be triggered after a call to ScheduleClear(). This can lead to race conditions.
This commit is contained in:
@@ -81,6 +81,10 @@ protected:
|
|||||||
|
|
||||||
virtual ~WorkItem();
|
virtual ~WorkItem();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove work item from the runnable queue, if it's there
|
||||||
|
*/
|
||||||
|
void ScheduleClear();
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
void RunPreamble() { _run_count++; }
|
void RunPreamble() { _run_count++; }
|
||||||
|
|||||||
@@ -66,7 +66,9 @@ ScheduledWorkItem::ScheduleOnInterval(uint32_t interval_us, uint32_t delay_us)
|
|||||||
void
|
void
|
||||||
ScheduledWorkItem::ScheduleClear()
|
ScheduledWorkItem::ScheduleClear()
|
||||||
{
|
{
|
||||||
|
// first clear any scheduled hrt call, then remove the item from the runnable queue
|
||||||
hrt_cancel(&_call);
|
hrt_cancel(&_call);
|
||||||
|
WorkItem::ScheduleClear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|||||||
@@ -89,6 +89,14 @@ WorkItem::Deinit()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
WorkItem::ScheduleClear()
|
||||||
|
{
|
||||||
|
if (_wq != nullptr) {
|
||||||
|
_wq->Remove(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
float
|
float
|
||||||
WorkItem::elapsed_time() const
|
WorkItem::elapsed_time() const
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -160,6 +160,7 @@ WorkQueue::Run()
|
|||||||
work_unlock(); // unlock work queue to run (item may requeue itself)
|
work_unlock(); // unlock work queue to run (item may requeue itself)
|
||||||
work->RunPreamble();
|
work->RunPreamble();
|
||||||
work->Run();
|
work->Run();
|
||||||
|
// Note: after Run() we cannot access work anymore, as it might have been deleted
|
||||||
work_lock(); // re-lock
|
work_lock(); // re-lock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user