From a9137270348022c765557cbe8c506a573304ad57 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 27 Jan 2015 06:17:02 -0600 Subject: Fix backward calculation in the work queue timing. From Liio Chen via the PX4 repository --- nuttx/libc/wqueue/work_usrthread.c | 18 +++++++++++++----- nuttx/sched/wqueue/kwork_process.c | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) (limited to 'nuttx') diff --git a/nuttx/libc/wqueue/work_usrthread.c b/nuttx/libc/wqueue/work_usrthread.c index f55aaa2f8..bc6797004 100644 --- a/nuttx/libc/wqueue/work_usrthread.c +++ b/nuttx/libc/wqueue/work_usrthread.c @@ -219,18 +219,26 @@ void work_process(FAR struct usr_wqueue_s *wqueue) work = (FAR struct work_s *)work->dq.flink; } } - else + else /* elapsed < work->delay */ { - /* This one is not ready.. will it be ready before the next - * scheduled wakeup interval? + /* This one is not ready. * * NOTE that elapsed is relative to the the current time, * not the time of beginning of this queue processing pass. * So it may need an adjustment. */ - elapsed += (ctick - stick); - remaining = elapsed - work->delay; + elapsed += (ctick - stick); + if (elapsed > work->delay) + { + /* The delay has expired while we are processing */ + + elapsed = work->delay; + } + + /* Will it be ready before the next scheduled wakeup interval? */ + + remaining = work->delay - elapsed; if (remaining < next) { /* Yes.. Then schedule to wake up when the work is ready */ diff --git a/nuttx/sched/wqueue/kwork_process.c b/nuttx/sched/wqueue/kwork_process.c index 87ed7900d..6333a6c21 100644 --- a/nuttx/sched/wqueue/kwork_process.c +++ b/nuttx/sched/wqueue/kwork_process.c @@ -198,18 +198,26 @@ void work_process(FAR struct kwork_wqueue_s *wqueue, uint32_t period, int wndx) work = (FAR struct work_s *)work->dq.flink; } } - else + else /* elapsed < work->delay */ { - /* This one is not ready.. will it be ready before the next - * scheduled wakeup interval? + /* This one is not ready. * * NOTE that elapsed is relative to the the current time, * not the time of beginning of this queue processing pass. * So it may need an adjustment. */ - elapsed += (ctick - stick); - remaining = elapsed - work->delay; + elapsed += (ctick - stick); + if (elapsed > work->delay) + { + /* The delay has expired while we are processing */ + + elapsed = work->delay; + } + + /* Will it be ready before the next scheduled wakeup interval? */ + + remaining = work->delay - elapsed; if (remaining < next) { /* Yes.. Then schedule to wake up when the work is ready */ -- cgit v1.2.3